111Sigor@sysoev.ru 211Sigor@sysoev.ru /* 311Sigor@sysoev.ru * Copyright (C) Igor Sysoev 411Sigor@sysoev.ru * Copyright (C) NGINX, Inc. 511Sigor@sysoev.ru */ 611Sigor@sysoev.ru 711Sigor@sysoev.ru #include <nxt_main.h> 820Sigor@sysoev.ru #include <nxt_runtime.h> 911Sigor@sysoev.ru #include <nxt_port.h> 10163Smax.romanov@nginx.com #include <nxt_router.h> 1111Sigor@sysoev.ru 1211Sigor@sysoev.ru 1314Sigor@sysoev.ru static void nxt_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 1411Sigor@sysoev.ru 15163Smax.romanov@nginx.com static nxt_atomic_uint_t nxt_port_last_id = 1; 16163Smax.romanov@nginx.com 17163Smax.romanov@nginx.com 18197Smax.romanov@nginx.com static void 19197Smax.romanov@nginx.com nxt_port_mp_cleanup(nxt_task_t *task, void *obj, void *data) 20197Smax.romanov@nginx.com { 21197Smax.romanov@nginx.com nxt_mp_t *mp; 22197Smax.romanov@nginx.com nxt_port_t *port; 23197Smax.romanov@nginx.com 24197Smax.romanov@nginx.com port = obj; 25197Smax.romanov@nginx.com mp = data; 26197Smax.romanov@nginx.com 27197Smax.romanov@nginx.com nxt_assert(port->pair[0] == -1); 28197Smax.romanov@nginx.com nxt_assert(port->pair[1] == -1); 29197Smax.romanov@nginx.com 30343Smax.romanov@nginx.com nxt_assert(port->use_count == 0); 31197Smax.romanov@nginx.com nxt_assert(port->app_link.next == NULL); 32197Smax.romanov@nginx.com 33197Smax.romanov@nginx.com nxt_assert(nxt_queue_is_empty(&port->messages)); 34197Smax.romanov@nginx.com nxt_assert(nxt_lvlhsh_is_empty(&port->rpc_streams)); 35197Smax.romanov@nginx.com nxt_assert(nxt_lvlhsh_is_empty(&port->rpc_peers)); 36197Smax.romanov@nginx.com 37343Smax.romanov@nginx.com nxt_thread_mutex_destroy(&port->write_mutex); 38343Smax.romanov@nginx.com 39197Smax.romanov@nginx.com nxt_mp_free(mp, port); 40197Smax.romanov@nginx.com } 41197Smax.romanov@nginx.com 42197Smax.romanov@nginx.com 43163Smax.romanov@nginx.com nxt_port_t * 44197Smax.romanov@nginx.com nxt_port_new(nxt_task_t *task, nxt_port_id_t id, nxt_pid_t pid, 45197Smax.romanov@nginx.com nxt_process_type_t type) 46163Smax.romanov@nginx.com { 47163Smax.romanov@nginx.com nxt_mp_t *mp; 48163Smax.romanov@nginx.com nxt_port_t *port; 49163Smax.romanov@nginx.com 50163Smax.romanov@nginx.com mp = nxt_mp_create(1024, 128, 256, 32); 51163Smax.romanov@nginx.com 52163Smax.romanov@nginx.com if (nxt_slow_path(mp == NULL)) { 53163Smax.romanov@nginx.com return NULL; 54163Smax.romanov@nginx.com } 55163Smax.romanov@nginx.com 56163Smax.romanov@nginx.com port = nxt_mp_zalloc(mp, sizeof(nxt_port_t)); 57163Smax.romanov@nginx.com 58163Smax.romanov@nginx.com if (nxt_fast_path(port != NULL)) { 59163Smax.romanov@nginx.com port->id = id; 60163Smax.romanov@nginx.com port->pid = pid; 61163Smax.romanov@nginx.com port->type = type; 62163Smax.romanov@nginx.com port->mem_pool = mp; 63343Smax.romanov@nginx.com port->use_count = 1; 64163Smax.romanov@nginx.com 65197Smax.romanov@nginx.com nxt_mp_cleanup(mp, nxt_port_mp_cleanup, task, port, mp); 66197Smax.romanov@nginx.com 67163Smax.romanov@nginx.com nxt_queue_init(&port->messages); 68343Smax.romanov@nginx.com nxt_thread_mutex_create(&port->write_mutex); 69163Smax.romanov@nginx.com 70163Smax.romanov@nginx.com } else { 71163Smax.romanov@nginx.com nxt_mp_destroy(mp); 72163Smax.romanov@nginx.com } 73163Smax.romanov@nginx.com 74167Smax.romanov@nginx.com nxt_thread_log_debug("port %p %d:%d new, type %d", port, pid, id, type); 75167Smax.romanov@nginx.com 76163Smax.romanov@nginx.com return port; 77163Smax.romanov@nginx.com } 78163Smax.romanov@nginx.com 79163Smax.romanov@nginx.com 80343Smax.romanov@nginx.com void 81343Smax.romanov@nginx.com nxt_port_close(nxt_task_t *task, nxt_port_t *port) 82163Smax.romanov@nginx.com { 83343Smax.romanov@nginx.com nxt_debug(task, "port %p %d:%d close, type %d", port, port->pid, 84343Smax.romanov@nginx.com port->id, port->type); 85167Smax.romanov@nginx.com 86163Smax.romanov@nginx.com if (port->pair[0] != -1) { 87163Smax.romanov@nginx.com nxt_fd_close(port->pair[0]); 88163Smax.romanov@nginx.com port->pair[0] = -1; 89163Smax.romanov@nginx.com } 90163Smax.romanov@nginx.com 91163Smax.romanov@nginx.com if (port->pair[1] != -1) { 92163Smax.romanov@nginx.com nxt_fd_close(port->pair[1]); 93163Smax.romanov@nginx.com port->pair[1] = -1; 94343Smax.romanov@nginx.com 95343Smax.romanov@nginx.com if (port->app != NULL) { 96343Smax.romanov@nginx.com nxt_router_app_port_close(task, port); 97343Smax.romanov@nginx.com } 98163Smax.romanov@nginx.com } 99343Smax.romanov@nginx.com } 100343Smax.romanov@nginx.com 101163Smax.romanov@nginx.com 102343Smax.romanov@nginx.com static void 103343Smax.romanov@nginx.com nxt_port_release(nxt_task_t *task, nxt_port_t *port) 104343Smax.romanov@nginx.com { 105343Smax.romanov@nginx.com nxt_debug(task, "port %p %d:%d release, type %d", port, port->pid, 106343Smax.romanov@nginx.com port->id, port->type); 107343Smax.romanov@nginx.com 108343Smax.romanov@nginx.com if (port->app != NULL) { 109343Smax.romanov@nginx.com nxt_router_app_use(task, port->app, -1); 110343Smax.romanov@nginx.com 111343Smax.romanov@nginx.com port->app = NULL; 112163Smax.romanov@nginx.com } 113163Smax.romanov@nginx.com 114163Smax.romanov@nginx.com if (port->link.next != NULL) { 115349Smax.romanov@nginx.com nxt_assert(port->process != NULL); 116349Smax.romanov@nginx.com 117163Smax.romanov@nginx.com nxt_process_port_remove(port); 118349Smax.romanov@nginx.com 119349Smax.romanov@nginx.com nxt_process_use(task, port->process, -1); 120163Smax.romanov@nginx.com } 121163Smax.romanov@nginx.com 122197Smax.romanov@nginx.com nxt_mp_release(port->mem_pool, NULL); 123163Smax.romanov@nginx.com } 124163Smax.romanov@nginx.com 125141Smax.romanov@nginx.com 126141Smax.romanov@nginx.com nxt_port_id_t 127141Smax.romanov@nginx.com nxt_port_get_next_id() 128141Smax.romanov@nginx.com { 129141Smax.romanov@nginx.com return nxt_atomic_fetch_add(&nxt_port_last_id, 1); 130141Smax.romanov@nginx.com } 131141Smax.romanov@nginx.com 13211Sigor@sysoev.ru 13311Sigor@sysoev.ru void 134141Smax.romanov@nginx.com nxt_port_reset_next_id() 135141Smax.romanov@nginx.com { 136141Smax.romanov@nginx.com nxt_port_last_id = 1; 137141Smax.romanov@nginx.com } 138141Smax.romanov@nginx.com 139141Smax.romanov@nginx.com 140141Smax.romanov@nginx.com void 141141Smax.romanov@nginx.com nxt_port_enable(nxt_task_t *task, nxt_port_t *port, 142320Smax.romanov@nginx.com nxt_port_handlers_t *handlers) 14311Sigor@sysoev.ru { 14414Sigor@sysoev.ru port->pid = nxt_pid; 14514Sigor@sysoev.ru port->handler = nxt_port_handler; 146320Smax.romanov@nginx.com port->data = (nxt_port_handler_t *) (handlers); 14711Sigor@sysoev.ru 14877Smax.romanov@nginx.com nxt_port_read_enable(task, port); 14911Sigor@sysoev.ru } 15011Sigor@sysoev.ru 15111Sigor@sysoev.ru 15211Sigor@sysoev.ru static void 15314Sigor@sysoev.ru nxt_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) 15411Sigor@sysoev.ru { 15514Sigor@sysoev.ru nxt_port_handler_t *handlers; 15611Sigor@sysoev.ru 157125Smax.romanov@nginx.com if (nxt_fast_path(msg->port_msg.type < NXT_PORT_MSG_MAX)) { 15811Sigor@sysoev.ru 15911Sigor@sysoev.ru nxt_debug(task, "port %d: message type:%uD", 16042Smax.romanov@nginx.com msg->port->socket.fd, msg->port_msg.type); 16111Sigor@sysoev.ru 16211Sigor@sysoev.ru handlers = msg->port->data; 16342Smax.romanov@nginx.com handlers[msg->port_msg.type](task, msg); 16411Sigor@sysoev.ru 16511Sigor@sysoev.ru return; 16611Sigor@sysoev.ru } 16711Sigor@sysoev.ru 16811Sigor@sysoev.ru nxt_log(task, NXT_LOG_CRIT, "port %d: unknown message type:%uD", 16942Smax.romanov@nginx.com msg->port->socket.fd, msg->port_msg.type); 17011Sigor@sysoev.ru } 17111Sigor@sysoev.ru 17211Sigor@sysoev.ru 17311Sigor@sysoev.ru void 17414Sigor@sysoev.ru nxt_port_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) 17511Sigor@sysoev.ru { 17620Sigor@sysoev.ru nxt_runtime_quit(task); 17711Sigor@sysoev.ru } 17811Sigor@sysoev.ru 17911Sigor@sysoev.ru 180*366Smax.romanov@nginx.com nxt_inline void 18120Sigor@sysoev.ru nxt_port_send_new_port(nxt_task_t *task, nxt_runtime_t *rt, 182141Smax.romanov@nginx.com nxt_port_t *new_port, uint32_t stream) 18311Sigor@sysoev.ru { 184141Smax.romanov@nginx.com nxt_port_t *port; 18581Smax.romanov@nginx.com nxt_process_t *process; 18611Sigor@sysoev.ru 187141Smax.romanov@nginx.com nxt_debug(task, "new port %d for process %PI", 188141Smax.romanov@nginx.com new_port->pair[1], new_port->pid); 18911Sigor@sysoev.ru 190277Sigor@sysoev.ru nxt_runtime_process_each(rt, process) { 191277Sigor@sysoev.ru 19242Smax.romanov@nginx.com if (process->pid == new_port->pid || process->pid == nxt_pid) { 19311Sigor@sysoev.ru continue; 19411Sigor@sysoev.ru } 19511Sigor@sysoev.ru 196141Smax.romanov@nginx.com port = nxt_process_port_first(process); 197141Smax.romanov@nginx.com 198*366Smax.romanov@nginx.com if (nxt_proc_conn_martix[port->type][new_port->type]) { 199141Smax.romanov@nginx.com (void) nxt_port_send_port(task, port, new_port, stream); 200141Smax.romanov@nginx.com } 201277Sigor@sysoev.ru 202277Sigor@sysoev.ru } nxt_runtime_process_loop; 20311Sigor@sysoev.ru } 20411Sigor@sysoev.ru 20511Sigor@sysoev.ru 20681Smax.romanov@nginx.com nxt_int_t 207141Smax.romanov@nginx.com nxt_port_send_port(nxt_task_t *task, nxt_port_t *port, nxt_port_t *new_port, 208141Smax.romanov@nginx.com uint32_t stream) 20981Smax.romanov@nginx.com { 21081Smax.romanov@nginx.com nxt_buf_t *b; 21181Smax.romanov@nginx.com nxt_port_msg_new_port_t *msg; 21281Smax.romanov@nginx.com 213342Smax.romanov@nginx.com b = nxt_buf_mem_ts_alloc(task, task->thread->engine->mem_pool, 214342Smax.romanov@nginx.com sizeof(nxt_port_data_t)); 21581Smax.romanov@nginx.com if (nxt_slow_path(b == NULL)) { 21681Smax.romanov@nginx.com return NXT_ERROR; 21781Smax.romanov@nginx.com } 21881Smax.romanov@nginx.com 21981Smax.romanov@nginx.com nxt_debug(task, "send port %FD to process %PI", 22081Smax.romanov@nginx.com new_port->pair[1], port->pid); 22181Smax.romanov@nginx.com 22281Smax.romanov@nginx.com b->mem.free += sizeof(nxt_port_msg_new_port_t); 22381Smax.romanov@nginx.com msg = (nxt_port_msg_new_port_t *) b->mem.pos; 22481Smax.romanov@nginx.com 22581Smax.romanov@nginx.com msg->id = new_port->id; 22681Smax.romanov@nginx.com msg->pid = new_port->pid; 22781Smax.romanov@nginx.com msg->max_size = port->max_size; 22881Smax.romanov@nginx.com msg->max_share = port->max_share; 22981Smax.romanov@nginx.com msg->type = new_port->type; 23081Smax.romanov@nginx.com 23181Smax.romanov@nginx.com return nxt_port_socket_write(task, port, NXT_PORT_MSG_NEW_PORT, 232141Smax.romanov@nginx.com new_port->pair[1], stream, 0, b); 23381Smax.romanov@nginx.com } 23481Smax.romanov@nginx.com 23581Smax.romanov@nginx.com 23611Sigor@sysoev.ru void 23714Sigor@sysoev.ru nxt_port_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) 23811Sigor@sysoev.ru { 23911Sigor@sysoev.ru nxt_port_t *port; 24020Sigor@sysoev.ru nxt_process_t *process; 24120Sigor@sysoev.ru nxt_runtime_t *rt; 24214Sigor@sysoev.ru nxt_port_msg_new_port_t *new_port_msg; 24311Sigor@sysoev.ru 24420Sigor@sysoev.ru rt = task->thread->runtime; 24511Sigor@sysoev.ru 24642Smax.romanov@nginx.com new_port_msg = (nxt_port_msg_new_port_t *) msg->buf->mem.pos; 24742Smax.romanov@nginx.com 248352Smax.romanov@nginx.com /* TODO check b size and make plain */ 249352Smax.romanov@nginx.com 250141Smax.romanov@nginx.com nxt_debug(task, "new port %d received for process %PI:%d", 251141Smax.romanov@nginx.com msg->fd, new_port_msg->pid, new_port_msg->id); 252141Smax.romanov@nginx.com 253141Smax.romanov@nginx.com port = nxt_runtime_port_find(rt, new_port_msg->pid, new_port_msg->id); 254141Smax.romanov@nginx.com if (port != NULL) { 255141Smax.romanov@nginx.com nxt_debug(task, "port %PI:%d already exists", new_port_msg->pid, 256141Smax.romanov@nginx.com new_port_msg->id); 257141Smax.romanov@nginx.com 258141Smax.romanov@nginx.com nxt_fd_close(msg->fd); 259141Smax.romanov@nginx.com msg->fd = -1; 260141Smax.romanov@nginx.com return; 261141Smax.romanov@nginx.com } 262141Smax.romanov@nginx.com 26342Smax.romanov@nginx.com process = nxt_runtime_process_get(rt, new_port_msg->pid); 26420Sigor@sysoev.ru if (nxt_slow_path(process == NULL)) { 26520Sigor@sysoev.ru return; 26620Sigor@sysoev.ru } 26720Sigor@sysoev.ru 268197Smax.romanov@nginx.com port = nxt_port_new(task, new_port_msg->id, new_port_msg->pid, 269163Smax.romanov@nginx.com new_port_msg->type); 27011Sigor@sysoev.ru if (nxt_slow_path(port == NULL)) { 271349Smax.romanov@nginx.com nxt_process_use(task, process, -1); 27211Sigor@sysoev.ru return; 27311Sigor@sysoev.ru } 27411Sigor@sysoev.ru 275164Smax.romanov@nginx.com nxt_process_port_add(task, process, port); 27614Sigor@sysoev.ru 277349Smax.romanov@nginx.com nxt_process_use(task, process, -1); 278349Smax.romanov@nginx.com 27914Sigor@sysoev.ru port->pair[0] = -1; 28011Sigor@sysoev.ru port->pair[1] = msg->fd; 28114Sigor@sysoev.ru port->max_size = new_port_msg->max_size; 28214Sigor@sysoev.ru port->max_share = new_port_msg->max_share; 28311Sigor@sysoev.ru 28414Sigor@sysoev.ru port->socket.task = task; 28514Sigor@sysoev.ru 286343Smax.romanov@nginx.com nxt_runtime_port_add(task, port); 287343Smax.romanov@nginx.com 288343Smax.romanov@nginx.com nxt_port_use(task, port, -1); 28942Smax.romanov@nginx.com 29011Sigor@sysoev.ru nxt_port_write_enable(task, port); 291141Smax.romanov@nginx.com 292347Smax.romanov@nginx.com msg->u.new_port = port; 293141Smax.romanov@nginx.com } 294141Smax.romanov@nginx.com 295141Smax.romanov@nginx.com 296141Smax.romanov@nginx.com void 297320Smax.romanov@nginx.com nxt_port_process_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) 298141Smax.romanov@nginx.com { 299141Smax.romanov@nginx.com nxt_port_t *port; 300141Smax.romanov@nginx.com nxt_process_t *process; 301141Smax.romanov@nginx.com nxt_runtime_t *rt; 302141Smax.romanov@nginx.com 303141Smax.romanov@nginx.com rt = task->thread->runtime; 304141Smax.romanov@nginx.com 305240Sigor@sysoev.ru nxt_assert(nxt_runtime_is_main(rt)); 306196Smax.romanov@nginx.com 307349Smax.romanov@nginx.com process = nxt_runtime_process_find(rt, msg->port_msg.pid); 308141Smax.romanov@nginx.com if (nxt_slow_path(process == NULL)) { 309141Smax.romanov@nginx.com return; 310141Smax.romanov@nginx.com } 311141Smax.romanov@nginx.com 312141Smax.romanov@nginx.com process->ready = 1; 313141Smax.romanov@nginx.com 314141Smax.romanov@nginx.com port = nxt_process_port_first(process); 315141Smax.romanov@nginx.com if (nxt_slow_path(port == NULL)) { 316141Smax.romanov@nginx.com return; 317141Smax.romanov@nginx.com } 318141Smax.romanov@nginx.com 319141Smax.romanov@nginx.com nxt_debug(task, "process %PI ready", msg->port_msg.pid); 320141Smax.romanov@nginx.com 321196Smax.romanov@nginx.com nxt_port_send_new_port(task, rt, port, msg->port_msg.stream); 32211Sigor@sysoev.ru } 32311Sigor@sysoev.ru 32411Sigor@sysoev.ru 32511Sigor@sysoev.ru void 32642Smax.romanov@nginx.com nxt_port_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) 32742Smax.romanov@nginx.com { 32842Smax.romanov@nginx.com nxt_runtime_t *rt; 32942Smax.romanov@nginx.com nxt_process_t *process; 33042Smax.romanov@nginx.com 33142Smax.romanov@nginx.com rt = task->thread->runtime; 33242Smax.romanov@nginx.com 33342Smax.romanov@nginx.com if (nxt_slow_path(msg->fd == -1)) { 33442Smax.romanov@nginx.com nxt_log(task, NXT_LOG_WARN, "invalid fd passed with mmap message"); 33542Smax.romanov@nginx.com 33642Smax.romanov@nginx.com return; 33742Smax.romanov@nginx.com } 33842Smax.romanov@nginx.com 339196Smax.romanov@nginx.com process = nxt_runtime_process_find(rt, msg->port_msg.pid); 34042Smax.romanov@nginx.com if (nxt_slow_path(process == NULL)) { 34142Smax.romanov@nginx.com nxt_log(task, NXT_LOG_WARN, "failed to get process #%PI", 34242Smax.romanov@nginx.com msg->port_msg.pid); 34342Smax.romanov@nginx.com 34442Smax.romanov@nginx.com goto fail_close; 34542Smax.romanov@nginx.com } 34642Smax.romanov@nginx.com 34742Smax.romanov@nginx.com nxt_port_incoming_port_mmap(task, process, msg->fd); 34842Smax.romanov@nginx.com 34942Smax.romanov@nginx.com fail_close: 35042Smax.romanov@nginx.com 35142Smax.romanov@nginx.com close(msg->fd); 35242Smax.romanov@nginx.com } 35342Smax.romanov@nginx.com 35442Smax.romanov@nginx.com 35542Smax.romanov@nginx.com void 35620Sigor@sysoev.ru nxt_port_change_log_file(nxt_task_t *task, nxt_runtime_t *rt, nxt_uint_t slot, 35720Sigor@sysoev.ru nxt_fd_t fd) 35811Sigor@sysoev.ru { 35920Sigor@sysoev.ru nxt_buf_t *b; 36020Sigor@sysoev.ru nxt_port_t *port; 36120Sigor@sysoev.ru nxt_process_t *process; 36211Sigor@sysoev.ru 36314Sigor@sysoev.ru nxt_debug(task, "change log file #%ui fd:%FD", slot, fd); 36411Sigor@sysoev.ru 365277Sigor@sysoev.ru nxt_runtime_process_each(rt, process) { 366277Sigor@sysoev.ru 36742Smax.romanov@nginx.com if (nxt_pid == process->pid) { 36842Smax.romanov@nginx.com continue; 36942Smax.romanov@nginx.com } 37011Sigor@sysoev.ru 37142Smax.romanov@nginx.com port = nxt_process_port_first(process); 37211Sigor@sysoev.ru 373342Smax.romanov@nginx.com b = nxt_buf_mem_ts_alloc(task, task->thread->engine->mem_pool, 374342Smax.romanov@nginx.com sizeof(nxt_port_data_t)); 37511Sigor@sysoev.ru if (nxt_slow_path(b == NULL)) { 37611Sigor@sysoev.ru continue; 37711Sigor@sysoev.ru } 37811Sigor@sysoev.ru 37911Sigor@sysoev.ru *(nxt_uint_t *) b->mem.pos = slot; 38011Sigor@sysoev.ru b->mem.free += sizeof(nxt_uint_t); 38111Sigor@sysoev.ru 38220Sigor@sysoev.ru (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_CHANGE_FILE, 38342Smax.romanov@nginx.com fd, 0, 0, b); 384277Sigor@sysoev.ru 385277Sigor@sysoev.ru } nxt_runtime_process_loop; 38611Sigor@sysoev.ru } 38711Sigor@sysoev.ru 38811Sigor@sysoev.ru 38911Sigor@sysoev.ru void 39014Sigor@sysoev.ru nxt_port_change_log_file_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) 39111Sigor@sysoev.ru { 39220Sigor@sysoev.ru nxt_buf_t *b; 39320Sigor@sysoev.ru nxt_uint_t slot; 39420Sigor@sysoev.ru nxt_file_t *log_file; 39520Sigor@sysoev.ru nxt_runtime_t *rt; 39611Sigor@sysoev.ru 39720Sigor@sysoev.ru rt = task->thread->runtime; 39811Sigor@sysoev.ru 39911Sigor@sysoev.ru b = msg->buf; 40011Sigor@sysoev.ru slot = *(nxt_uint_t *) b->mem.pos; 40111Sigor@sysoev.ru 40220Sigor@sysoev.ru log_file = nxt_list_elt(rt->log_files, slot); 40311Sigor@sysoev.ru 40411Sigor@sysoev.ru nxt_debug(task, "change log file %FD:%FD", msg->fd, log_file->fd); 40511Sigor@sysoev.ru 40611Sigor@sysoev.ru /* 40711Sigor@sysoev.ru * The old log file descriptor must be closed at the moment when no 40811Sigor@sysoev.ru * other threads use it. dup2() allows to use the old file descriptor 40911Sigor@sysoev.ru * for new log file. This change is performed atomically in the kernel. 41011Sigor@sysoev.ru */ 41111Sigor@sysoev.ru if (nxt_file_redirect(log_file, msg->fd) == NXT_OK) { 41211Sigor@sysoev.ru if (slot == 0) { 41311Sigor@sysoev.ru (void) nxt_file_stderr(log_file); 41411Sigor@sysoev.ru } 41511Sigor@sysoev.ru } 41611Sigor@sysoev.ru } 41711Sigor@sysoev.ru 41811Sigor@sysoev.ru 41911Sigor@sysoev.ru void 42014Sigor@sysoev.ru nxt_port_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) 42111Sigor@sysoev.ru { 42242Smax.romanov@nginx.com size_t dump_size; 42311Sigor@sysoev.ru nxt_buf_t *b; 42411Sigor@sysoev.ru 42511Sigor@sysoev.ru b = msg->buf; 42642Smax.romanov@nginx.com dump_size = b->mem.free - b->mem.pos; 42711Sigor@sysoev.ru 42842Smax.romanov@nginx.com if (dump_size > 300) { 42942Smax.romanov@nginx.com dump_size = 300; 43042Smax.romanov@nginx.com } 43142Smax.romanov@nginx.com 43242Smax.romanov@nginx.com nxt_debug(task, "data: %*s", dump_size, b->mem.pos); 43311Sigor@sysoev.ru } 43411Sigor@sysoev.ru 43511Sigor@sysoev.ru 43611Sigor@sysoev.ru void 437125Smax.romanov@nginx.com nxt_port_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) 438125Smax.romanov@nginx.com { 439192Smax.romanov@nginx.com nxt_buf_t *buf; 440125Smax.romanov@nginx.com nxt_pid_t pid; 441125Smax.romanov@nginx.com nxt_runtime_t *rt; 442125Smax.romanov@nginx.com nxt_process_t *process; 443125Smax.romanov@nginx.com 444192Smax.romanov@nginx.com buf = msg->buf; 445192Smax.romanov@nginx.com 446192Smax.romanov@nginx.com nxt_assert(nxt_buf_used_size(buf) == sizeof(pid)); 447192Smax.romanov@nginx.com 448192Smax.romanov@nginx.com nxt_memcpy(&pid, buf->mem.pos, sizeof(pid)); 449192Smax.romanov@nginx.com 450347Smax.romanov@nginx.com msg->u.removed_pid = pid; 451347Smax.romanov@nginx.com 452196Smax.romanov@nginx.com nxt_debug(task, "port remove pid %PI handler", pid); 453196Smax.romanov@nginx.com 454125Smax.romanov@nginx.com rt = task->thread->runtime; 455125Smax.romanov@nginx.com 456190Smax.romanov@nginx.com nxt_port_rpc_remove_peer(task, msg->port, pid); 457190Smax.romanov@nginx.com 458125Smax.romanov@nginx.com process = nxt_runtime_process_find(rt, pid); 459125Smax.romanov@nginx.com 460125Smax.romanov@nginx.com if (process) { 461349Smax.romanov@nginx.com nxt_process_close_ports(task, process); 462125Smax.romanov@nginx.com } 463125Smax.romanov@nginx.com } 464125Smax.romanov@nginx.com 465125Smax.romanov@nginx.com 466125Smax.romanov@nginx.com void 46714Sigor@sysoev.ru nxt_port_empty_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) 46811Sigor@sysoev.ru { 46911Sigor@sysoev.ru nxt_debug(task, "port empty handler"); 47011Sigor@sysoev.ru } 471343Smax.romanov@nginx.com 472343Smax.romanov@nginx.com 473343Smax.romanov@nginx.com typedef struct { 474343Smax.romanov@nginx.com nxt_work_t work; 475343Smax.romanov@nginx.com nxt_port_t *port; 476343Smax.romanov@nginx.com nxt_port_post_handler_t handler; 477343Smax.romanov@nginx.com } nxt_port_work_t; 478343Smax.romanov@nginx.com 479343Smax.romanov@nginx.com 480343Smax.romanov@nginx.com static void 481343Smax.romanov@nginx.com nxt_port_post_handler(nxt_task_t *task, void *obj, void *data) 482343Smax.romanov@nginx.com { 483343Smax.romanov@nginx.com nxt_port_t *port; 484343Smax.romanov@nginx.com nxt_port_work_t *pw; 485343Smax.romanov@nginx.com nxt_port_post_handler_t handler; 486343Smax.romanov@nginx.com 487343Smax.romanov@nginx.com pw = obj; 488343Smax.romanov@nginx.com port = pw->port; 489343Smax.romanov@nginx.com handler = pw->handler; 490343Smax.romanov@nginx.com 491343Smax.romanov@nginx.com nxt_free(pw); 492343Smax.romanov@nginx.com 493343Smax.romanov@nginx.com handler(task, port, data); 494343Smax.romanov@nginx.com 495343Smax.romanov@nginx.com nxt_port_use(task, port, -1); 496343Smax.romanov@nginx.com } 497343Smax.romanov@nginx.com 498343Smax.romanov@nginx.com 499343Smax.romanov@nginx.com nxt_int_t 500343Smax.romanov@nginx.com nxt_port_post(nxt_task_t *task, nxt_port_t *port, 501343Smax.romanov@nginx.com nxt_port_post_handler_t handler, void *data) 502343Smax.romanov@nginx.com { 503343Smax.romanov@nginx.com nxt_port_work_t *pw; 504343Smax.romanov@nginx.com 505343Smax.romanov@nginx.com if (task->thread->engine == port->engine) { 506343Smax.romanov@nginx.com handler(task, port, data); 507343Smax.romanov@nginx.com 508343Smax.romanov@nginx.com return NXT_OK; 509343Smax.romanov@nginx.com } 510343Smax.romanov@nginx.com 511343Smax.romanov@nginx.com pw = nxt_zalloc(sizeof(nxt_port_work_t)); 512343Smax.romanov@nginx.com 513343Smax.romanov@nginx.com if (nxt_slow_path(pw == NULL)) { 514343Smax.romanov@nginx.com return NXT_ERROR; 515343Smax.romanov@nginx.com } 516343Smax.romanov@nginx.com 517343Smax.romanov@nginx.com nxt_atomic_fetch_add(&port->use_count, 1); 518343Smax.romanov@nginx.com 519343Smax.romanov@nginx.com pw->work.handler = nxt_port_post_handler; 520343Smax.romanov@nginx.com pw->work.task = &port->engine->task; 521343Smax.romanov@nginx.com pw->work.obj = pw; 522343Smax.romanov@nginx.com pw->work.data = data; 523343Smax.romanov@nginx.com 524343Smax.romanov@nginx.com pw->port = port; 525343Smax.romanov@nginx.com pw->handler = handler; 526343Smax.romanov@nginx.com 527343Smax.romanov@nginx.com nxt_event_engine_post(port->engine, &pw->work); 528343Smax.romanov@nginx.com 529343Smax.romanov@nginx.com return NXT_OK; 530343Smax.romanov@nginx.com } 531343Smax.romanov@nginx.com 532343Smax.romanov@nginx.com 533343Smax.romanov@nginx.com static void 534343Smax.romanov@nginx.com nxt_port_release_handler(nxt_task_t *task, nxt_port_t *port, void *data) 535343Smax.romanov@nginx.com { 536343Smax.romanov@nginx.com /* no op */ 537343Smax.romanov@nginx.com } 538343Smax.romanov@nginx.com 539343Smax.romanov@nginx.com 540343Smax.romanov@nginx.com void 541343Smax.romanov@nginx.com nxt_port_use(nxt_task_t *task, nxt_port_t *port, int i) 542343Smax.romanov@nginx.com { 543343Smax.romanov@nginx.com int c; 544343Smax.romanov@nginx.com 545343Smax.romanov@nginx.com c = nxt_atomic_fetch_add(&port->use_count, i); 546343Smax.romanov@nginx.com 547343Smax.romanov@nginx.com if (i < 0 && c == -i) { 548343Smax.romanov@nginx.com 549343Smax.romanov@nginx.com if (task->thread->engine == port->engine) { 550343Smax.romanov@nginx.com nxt_port_release(task, port); 551343Smax.romanov@nginx.com 552343Smax.romanov@nginx.com return; 553343Smax.romanov@nginx.com } 554343Smax.romanov@nginx.com 555343Smax.romanov@nginx.com nxt_port_post(task, port, nxt_port_release_handler, NULL); 556343Smax.romanov@nginx.com } 557343Smax.romanov@nginx.com } 558