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) { 115163Smax.romanov@nginx.com nxt_process_port_remove(port); 116163Smax.romanov@nginx.com } 117163Smax.romanov@nginx.com 118197Smax.romanov@nginx.com nxt_mp_release(port->mem_pool, NULL); 119163Smax.romanov@nginx.com } 120163Smax.romanov@nginx.com 121141Smax.romanov@nginx.com 122141Smax.romanov@nginx.com nxt_port_id_t 123141Smax.romanov@nginx.com nxt_port_get_next_id() 124141Smax.romanov@nginx.com { 125141Smax.romanov@nginx.com return nxt_atomic_fetch_add(&nxt_port_last_id, 1); 126141Smax.romanov@nginx.com } 127141Smax.romanov@nginx.com 12811Sigor@sysoev.ru 12911Sigor@sysoev.ru void 130141Smax.romanov@nginx.com nxt_port_reset_next_id() 131141Smax.romanov@nginx.com { 132141Smax.romanov@nginx.com nxt_port_last_id = 1; 133141Smax.romanov@nginx.com } 134141Smax.romanov@nginx.com 135141Smax.romanov@nginx.com 136141Smax.romanov@nginx.com void 137141Smax.romanov@nginx.com nxt_port_enable(nxt_task_t *task, nxt_port_t *port, 138320Smax.romanov@nginx.com nxt_port_handlers_t *handlers) 13911Sigor@sysoev.ru { 14014Sigor@sysoev.ru port->pid = nxt_pid; 14114Sigor@sysoev.ru port->handler = nxt_port_handler; 142320Smax.romanov@nginx.com port->data = (nxt_port_handler_t *) (handlers); 14311Sigor@sysoev.ru 14477Smax.romanov@nginx.com nxt_port_read_enable(task, port); 14511Sigor@sysoev.ru } 14611Sigor@sysoev.ru 14711Sigor@sysoev.ru 14811Sigor@sysoev.ru static void 14914Sigor@sysoev.ru nxt_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) 15011Sigor@sysoev.ru { 15114Sigor@sysoev.ru nxt_port_handler_t *handlers; 15211Sigor@sysoev.ru 153125Smax.romanov@nginx.com if (nxt_fast_path(msg->port_msg.type < NXT_PORT_MSG_MAX)) { 15411Sigor@sysoev.ru 15511Sigor@sysoev.ru nxt_debug(task, "port %d: message type:%uD", 15642Smax.romanov@nginx.com msg->port->socket.fd, msg->port_msg.type); 15711Sigor@sysoev.ru 15811Sigor@sysoev.ru handlers = msg->port->data; 15942Smax.romanov@nginx.com handlers[msg->port_msg.type](task, msg); 16011Sigor@sysoev.ru 16111Sigor@sysoev.ru return; 16211Sigor@sysoev.ru } 16311Sigor@sysoev.ru 16411Sigor@sysoev.ru nxt_log(task, NXT_LOG_CRIT, "port %d: unknown message type:%uD", 16542Smax.romanov@nginx.com msg->port->socket.fd, msg->port_msg.type); 16611Sigor@sysoev.ru } 16711Sigor@sysoev.ru 16811Sigor@sysoev.ru 16911Sigor@sysoev.ru void 17014Sigor@sysoev.ru nxt_port_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) 17111Sigor@sysoev.ru { 17220Sigor@sysoev.ru nxt_runtime_quit(task); 17311Sigor@sysoev.ru } 17411Sigor@sysoev.ru 17511Sigor@sysoev.ru 17611Sigor@sysoev.ru void 17720Sigor@sysoev.ru nxt_port_send_new_port(nxt_task_t *task, nxt_runtime_t *rt, 178141Smax.romanov@nginx.com nxt_port_t *new_port, uint32_t stream) 17911Sigor@sysoev.ru { 180141Smax.romanov@nginx.com nxt_port_t *port; 18181Smax.romanov@nginx.com nxt_process_t *process; 18211Sigor@sysoev.ru 183141Smax.romanov@nginx.com nxt_debug(task, "new port %d for process %PI", 184141Smax.romanov@nginx.com new_port->pair[1], new_port->pid); 18511Sigor@sysoev.ru 186277Sigor@sysoev.ru nxt_runtime_process_each(rt, process) { 187277Sigor@sysoev.ru 18842Smax.romanov@nginx.com if (process->pid == new_port->pid || process->pid == nxt_pid) { 18911Sigor@sysoev.ru continue; 19011Sigor@sysoev.ru } 19111Sigor@sysoev.ru 192141Smax.romanov@nginx.com port = nxt_process_port_first(process); 193141Smax.romanov@nginx.com 194277Sigor@sysoev.ru if (port->type == NXT_PROCESS_MAIN 195277Sigor@sysoev.ru || port->type == NXT_PROCESS_CONTROLLER 196277Sigor@sysoev.ru || port->type == NXT_PROCESS_ROUTER) 197277Sigor@sysoev.ru { 198141Smax.romanov@nginx.com (void) nxt_port_send_port(task, port, new_port, stream); 199141Smax.romanov@nginx.com } 200277Sigor@sysoev.ru 201277Sigor@sysoev.ru } nxt_runtime_process_loop; 20211Sigor@sysoev.ru } 20311Sigor@sysoev.ru 20411Sigor@sysoev.ru 20581Smax.romanov@nginx.com nxt_int_t 206141Smax.romanov@nginx.com nxt_port_send_port(nxt_task_t *task, nxt_port_t *port, nxt_port_t *new_port, 207141Smax.romanov@nginx.com uint32_t stream) 20881Smax.romanov@nginx.com { 20981Smax.romanov@nginx.com nxt_buf_t *b; 21081Smax.romanov@nginx.com nxt_port_msg_new_port_t *msg; 21181Smax.romanov@nginx.com 212342Smax.romanov@nginx.com b = nxt_buf_mem_ts_alloc(task, task->thread->engine->mem_pool, 213342Smax.romanov@nginx.com sizeof(nxt_port_data_t)); 21481Smax.romanov@nginx.com if (nxt_slow_path(b == NULL)) { 21581Smax.romanov@nginx.com return NXT_ERROR; 21681Smax.romanov@nginx.com } 21781Smax.romanov@nginx.com 21881Smax.romanov@nginx.com nxt_debug(task, "send port %FD to process %PI", 21981Smax.romanov@nginx.com new_port->pair[1], port->pid); 22081Smax.romanov@nginx.com 22181Smax.romanov@nginx.com b->mem.free += sizeof(nxt_port_msg_new_port_t); 22281Smax.romanov@nginx.com msg = (nxt_port_msg_new_port_t *) b->mem.pos; 22381Smax.romanov@nginx.com 22481Smax.romanov@nginx.com msg->id = new_port->id; 22581Smax.romanov@nginx.com msg->pid = new_port->pid; 22681Smax.romanov@nginx.com msg->max_size = port->max_size; 22781Smax.romanov@nginx.com msg->max_share = port->max_share; 22881Smax.romanov@nginx.com msg->type = new_port->type; 22981Smax.romanov@nginx.com 23081Smax.romanov@nginx.com return nxt_port_socket_write(task, port, NXT_PORT_MSG_NEW_PORT, 231141Smax.romanov@nginx.com new_port->pair[1], stream, 0, b); 23281Smax.romanov@nginx.com } 23381Smax.romanov@nginx.com 23481Smax.romanov@nginx.com 23511Sigor@sysoev.ru void 23614Sigor@sysoev.ru nxt_port_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) 23711Sigor@sysoev.ru { 23811Sigor@sysoev.ru nxt_port_t *port; 23920Sigor@sysoev.ru nxt_process_t *process; 24020Sigor@sysoev.ru nxt_runtime_t *rt; 24114Sigor@sysoev.ru nxt_port_msg_new_port_t *new_port_msg; 24211Sigor@sysoev.ru 24320Sigor@sysoev.ru rt = task->thread->runtime; 24411Sigor@sysoev.ru 24542Smax.romanov@nginx.com new_port_msg = (nxt_port_msg_new_port_t *) msg->buf->mem.pos; 24642Smax.romanov@nginx.com 247141Smax.romanov@nginx.com nxt_debug(task, "new port %d received for process %PI:%d", 248141Smax.romanov@nginx.com msg->fd, new_port_msg->pid, new_port_msg->id); 249141Smax.romanov@nginx.com 250141Smax.romanov@nginx.com port = nxt_runtime_port_find(rt, new_port_msg->pid, new_port_msg->id); 251141Smax.romanov@nginx.com if (port != NULL) { 252141Smax.romanov@nginx.com nxt_debug(task, "port %PI:%d already exists", new_port_msg->pid, 253141Smax.romanov@nginx.com new_port_msg->id); 254141Smax.romanov@nginx.com 255141Smax.romanov@nginx.com nxt_fd_close(msg->fd); 256141Smax.romanov@nginx.com msg->fd = -1; 257141Smax.romanov@nginx.com return; 258141Smax.romanov@nginx.com } 259141Smax.romanov@nginx.com 26042Smax.romanov@nginx.com process = nxt_runtime_process_get(rt, new_port_msg->pid); 26120Sigor@sysoev.ru if (nxt_slow_path(process == NULL)) { 26220Sigor@sysoev.ru return; 26320Sigor@sysoev.ru } 26420Sigor@sysoev.ru 265197Smax.romanov@nginx.com port = nxt_port_new(task, new_port_msg->id, new_port_msg->pid, 266163Smax.romanov@nginx.com new_port_msg->type); 26711Sigor@sysoev.ru if (nxt_slow_path(port == NULL)) { 26811Sigor@sysoev.ru return; 26911Sigor@sysoev.ru } 27011Sigor@sysoev.ru 271164Smax.romanov@nginx.com nxt_process_port_add(task, process, port); 27214Sigor@sysoev.ru 27314Sigor@sysoev.ru port->pair[0] = -1; 27411Sigor@sysoev.ru port->pair[1] = msg->fd; 27514Sigor@sysoev.ru port->max_size = new_port_msg->max_size; 27614Sigor@sysoev.ru port->max_share = new_port_msg->max_share; 27711Sigor@sysoev.ru 27814Sigor@sysoev.ru port->socket.task = task; 27914Sigor@sysoev.ru 280343Smax.romanov@nginx.com nxt_runtime_port_add(task, port); 281343Smax.romanov@nginx.com 282343Smax.romanov@nginx.com nxt_port_use(task, port, -1); 28342Smax.romanov@nginx.com 28411Sigor@sysoev.ru nxt_port_write_enable(task, port); 285141Smax.romanov@nginx.com 286*347Smax.romanov@nginx.com msg->u.new_port = port; 287141Smax.romanov@nginx.com } 288141Smax.romanov@nginx.com 289141Smax.romanov@nginx.com 290141Smax.romanov@nginx.com void 291320Smax.romanov@nginx.com nxt_port_process_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) 292141Smax.romanov@nginx.com { 293141Smax.romanov@nginx.com nxt_port_t *port; 294141Smax.romanov@nginx.com nxt_process_t *process; 295141Smax.romanov@nginx.com nxt_runtime_t *rt; 296141Smax.romanov@nginx.com 297141Smax.romanov@nginx.com rt = task->thread->runtime; 298141Smax.romanov@nginx.com 299240Sigor@sysoev.ru nxt_assert(nxt_runtime_is_main(rt)); 300196Smax.romanov@nginx.com 301141Smax.romanov@nginx.com process = nxt_runtime_process_get(rt, msg->port_msg.pid); 302141Smax.romanov@nginx.com if (nxt_slow_path(process == NULL)) { 303141Smax.romanov@nginx.com return; 304141Smax.romanov@nginx.com } 305141Smax.romanov@nginx.com 306141Smax.romanov@nginx.com process->ready = 1; 307141Smax.romanov@nginx.com 308141Smax.romanov@nginx.com port = nxt_process_port_first(process); 309141Smax.romanov@nginx.com if (nxt_slow_path(port == NULL)) { 310141Smax.romanov@nginx.com return; 311141Smax.romanov@nginx.com } 312141Smax.romanov@nginx.com 313141Smax.romanov@nginx.com nxt_debug(task, "process %PI ready", msg->port_msg.pid); 314141Smax.romanov@nginx.com 315196Smax.romanov@nginx.com nxt_port_send_new_port(task, rt, port, msg->port_msg.stream); 31611Sigor@sysoev.ru } 31711Sigor@sysoev.ru 31811Sigor@sysoev.ru 31911Sigor@sysoev.ru void 32042Smax.romanov@nginx.com nxt_port_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) 32142Smax.romanov@nginx.com { 32242Smax.romanov@nginx.com nxt_runtime_t *rt; 32342Smax.romanov@nginx.com nxt_process_t *process; 32442Smax.romanov@nginx.com 32542Smax.romanov@nginx.com rt = task->thread->runtime; 32642Smax.romanov@nginx.com 32742Smax.romanov@nginx.com if (nxt_slow_path(msg->fd == -1)) { 32842Smax.romanov@nginx.com nxt_log(task, NXT_LOG_WARN, "invalid fd passed with mmap message"); 32942Smax.romanov@nginx.com 33042Smax.romanov@nginx.com return; 33142Smax.romanov@nginx.com } 33242Smax.romanov@nginx.com 333196Smax.romanov@nginx.com process = nxt_runtime_process_find(rt, msg->port_msg.pid); 33442Smax.romanov@nginx.com if (nxt_slow_path(process == NULL)) { 33542Smax.romanov@nginx.com nxt_log(task, NXT_LOG_WARN, "failed to get process #%PI", 33642Smax.romanov@nginx.com msg->port_msg.pid); 33742Smax.romanov@nginx.com 33842Smax.romanov@nginx.com goto fail_close; 33942Smax.romanov@nginx.com } 34042Smax.romanov@nginx.com 34142Smax.romanov@nginx.com nxt_port_incoming_port_mmap(task, process, msg->fd); 34242Smax.romanov@nginx.com 34342Smax.romanov@nginx.com fail_close: 34442Smax.romanov@nginx.com 34542Smax.romanov@nginx.com close(msg->fd); 34642Smax.romanov@nginx.com } 34742Smax.romanov@nginx.com 34842Smax.romanov@nginx.com 34942Smax.romanov@nginx.com void 35020Sigor@sysoev.ru nxt_port_change_log_file(nxt_task_t *task, nxt_runtime_t *rt, nxt_uint_t slot, 35120Sigor@sysoev.ru nxt_fd_t fd) 35211Sigor@sysoev.ru { 35320Sigor@sysoev.ru nxt_buf_t *b; 35420Sigor@sysoev.ru nxt_port_t *port; 35520Sigor@sysoev.ru nxt_process_t *process; 35611Sigor@sysoev.ru 35714Sigor@sysoev.ru nxt_debug(task, "change log file #%ui fd:%FD", slot, fd); 35811Sigor@sysoev.ru 359277Sigor@sysoev.ru nxt_runtime_process_each(rt, process) { 360277Sigor@sysoev.ru 36142Smax.romanov@nginx.com if (nxt_pid == process->pid) { 36242Smax.romanov@nginx.com continue; 36342Smax.romanov@nginx.com } 36411Sigor@sysoev.ru 36542Smax.romanov@nginx.com port = nxt_process_port_first(process); 36611Sigor@sysoev.ru 367342Smax.romanov@nginx.com b = nxt_buf_mem_ts_alloc(task, task->thread->engine->mem_pool, 368342Smax.romanov@nginx.com sizeof(nxt_port_data_t)); 36911Sigor@sysoev.ru if (nxt_slow_path(b == NULL)) { 37011Sigor@sysoev.ru continue; 37111Sigor@sysoev.ru } 37211Sigor@sysoev.ru 37311Sigor@sysoev.ru *(nxt_uint_t *) b->mem.pos = slot; 37411Sigor@sysoev.ru b->mem.free += sizeof(nxt_uint_t); 37511Sigor@sysoev.ru 37620Sigor@sysoev.ru (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_CHANGE_FILE, 37742Smax.romanov@nginx.com fd, 0, 0, b); 378277Sigor@sysoev.ru 379277Sigor@sysoev.ru } nxt_runtime_process_loop; 38011Sigor@sysoev.ru } 38111Sigor@sysoev.ru 38211Sigor@sysoev.ru 38311Sigor@sysoev.ru void 38414Sigor@sysoev.ru nxt_port_change_log_file_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) 38511Sigor@sysoev.ru { 38620Sigor@sysoev.ru nxt_buf_t *b; 38720Sigor@sysoev.ru nxt_uint_t slot; 38820Sigor@sysoev.ru nxt_file_t *log_file; 38920Sigor@sysoev.ru nxt_runtime_t *rt; 39011Sigor@sysoev.ru 39120Sigor@sysoev.ru rt = task->thread->runtime; 39211Sigor@sysoev.ru 39311Sigor@sysoev.ru b = msg->buf; 39411Sigor@sysoev.ru slot = *(nxt_uint_t *) b->mem.pos; 39511Sigor@sysoev.ru 39620Sigor@sysoev.ru log_file = nxt_list_elt(rt->log_files, slot); 39711Sigor@sysoev.ru 39811Sigor@sysoev.ru nxt_debug(task, "change log file %FD:%FD", msg->fd, log_file->fd); 39911Sigor@sysoev.ru 40011Sigor@sysoev.ru /* 40111Sigor@sysoev.ru * The old log file descriptor must be closed at the moment when no 40211Sigor@sysoev.ru * other threads use it. dup2() allows to use the old file descriptor 40311Sigor@sysoev.ru * for new log file. This change is performed atomically in the kernel. 40411Sigor@sysoev.ru */ 40511Sigor@sysoev.ru if (nxt_file_redirect(log_file, msg->fd) == NXT_OK) { 40611Sigor@sysoev.ru if (slot == 0) { 40711Sigor@sysoev.ru (void) nxt_file_stderr(log_file); 40811Sigor@sysoev.ru } 40911Sigor@sysoev.ru } 41011Sigor@sysoev.ru } 41111Sigor@sysoev.ru 41211Sigor@sysoev.ru 41311Sigor@sysoev.ru void 41414Sigor@sysoev.ru nxt_port_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) 41511Sigor@sysoev.ru { 41642Smax.romanov@nginx.com size_t dump_size; 41711Sigor@sysoev.ru nxt_buf_t *b; 41811Sigor@sysoev.ru 41911Sigor@sysoev.ru b = msg->buf; 42042Smax.romanov@nginx.com dump_size = b->mem.free - b->mem.pos; 42111Sigor@sysoev.ru 42242Smax.romanov@nginx.com if (dump_size > 300) { 42342Smax.romanov@nginx.com dump_size = 300; 42442Smax.romanov@nginx.com } 42542Smax.romanov@nginx.com 42642Smax.romanov@nginx.com nxt_debug(task, "data: %*s", dump_size, b->mem.pos); 42711Sigor@sysoev.ru } 42811Sigor@sysoev.ru 42911Sigor@sysoev.ru 43011Sigor@sysoev.ru void 431125Smax.romanov@nginx.com nxt_port_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) 432125Smax.romanov@nginx.com { 433192Smax.romanov@nginx.com nxt_buf_t *buf; 434125Smax.romanov@nginx.com nxt_pid_t pid; 435125Smax.romanov@nginx.com nxt_runtime_t *rt; 436125Smax.romanov@nginx.com nxt_process_t *process; 437125Smax.romanov@nginx.com 438192Smax.romanov@nginx.com buf = msg->buf; 439192Smax.romanov@nginx.com 440192Smax.romanov@nginx.com nxt_assert(nxt_buf_used_size(buf) == sizeof(pid)); 441192Smax.romanov@nginx.com 442192Smax.romanov@nginx.com nxt_memcpy(&pid, buf->mem.pos, sizeof(pid)); 443192Smax.romanov@nginx.com 444*347Smax.romanov@nginx.com msg->u.removed_pid = pid; 445*347Smax.romanov@nginx.com 446196Smax.romanov@nginx.com nxt_debug(task, "port remove pid %PI handler", pid); 447196Smax.romanov@nginx.com 448125Smax.romanov@nginx.com rt = task->thread->runtime; 449125Smax.romanov@nginx.com 450190Smax.romanov@nginx.com nxt_port_rpc_remove_peer(task, msg->port, pid); 451190Smax.romanov@nginx.com 452125Smax.romanov@nginx.com process = nxt_runtime_process_find(rt, pid); 453125Smax.romanov@nginx.com 454125Smax.romanov@nginx.com if (process) { 455343Smax.romanov@nginx.com nxt_runtime_process_remove(task, process); 456125Smax.romanov@nginx.com } 457125Smax.romanov@nginx.com } 458125Smax.romanov@nginx.com 459125Smax.romanov@nginx.com 460125Smax.romanov@nginx.com void 46114Sigor@sysoev.ru nxt_port_empty_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) 46211Sigor@sysoev.ru { 46311Sigor@sysoev.ru nxt_debug(task, "port empty handler"); 46411Sigor@sysoev.ru } 465343Smax.romanov@nginx.com 466343Smax.romanov@nginx.com 467343Smax.romanov@nginx.com typedef struct { 468343Smax.romanov@nginx.com nxt_work_t work; 469343Smax.romanov@nginx.com nxt_port_t *port; 470343Smax.romanov@nginx.com nxt_port_post_handler_t handler; 471343Smax.romanov@nginx.com } nxt_port_work_t; 472343Smax.romanov@nginx.com 473343Smax.romanov@nginx.com 474343Smax.romanov@nginx.com static void 475343Smax.romanov@nginx.com nxt_port_post_handler(nxt_task_t *task, void *obj, void *data) 476343Smax.romanov@nginx.com { 477343Smax.romanov@nginx.com nxt_port_t *port; 478343Smax.romanov@nginx.com nxt_port_work_t *pw; 479343Smax.romanov@nginx.com nxt_port_post_handler_t handler; 480343Smax.romanov@nginx.com 481343Smax.romanov@nginx.com pw = obj; 482343Smax.romanov@nginx.com port = pw->port; 483343Smax.romanov@nginx.com handler = pw->handler; 484343Smax.romanov@nginx.com 485343Smax.romanov@nginx.com nxt_free(pw); 486343Smax.romanov@nginx.com 487343Smax.romanov@nginx.com handler(task, port, data); 488343Smax.romanov@nginx.com 489343Smax.romanov@nginx.com nxt_port_use(task, port, -1); 490343Smax.romanov@nginx.com } 491343Smax.romanov@nginx.com 492343Smax.romanov@nginx.com 493343Smax.romanov@nginx.com nxt_int_t 494343Smax.romanov@nginx.com nxt_port_post(nxt_task_t *task, nxt_port_t *port, 495343Smax.romanov@nginx.com nxt_port_post_handler_t handler, void *data) 496343Smax.romanov@nginx.com { 497343Smax.romanov@nginx.com nxt_port_work_t *pw; 498343Smax.romanov@nginx.com 499343Smax.romanov@nginx.com if (task->thread->engine == port->engine) { 500343Smax.romanov@nginx.com handler(task, port, data); 501343Smax.romanov@nginx.com 502343Smax.romanov@nginx.com return NXT_OK; 503343Smax.romanov@nginx.com } 504343Smax.romanov@nginx.com 505343Smax.romanov@nginx.com pw = nxt_zalloc(sizeof(nxt_port_work_t)); 506343Smax.romanov@nginx.com 507343Smax.romanov@nginx.com if (nxt_slow_path(pw == NULL)) { 508343Smax.romanov@nginx.com return NXT_ERROR; 509343Smax.romanov@nginx.com } 510343Smax.romanov@nginx.com 511343Smax.romanov@nginx.com nxt_atomic_fetch_add(&port->use_count, 1); 512343Smax.romanov@nginx.com 513343Smax.romanov@nginx.com pw->work.handler = nxt_port_post_handler; 514343Smax.romanov@nginx.com pw->work.task = &port->engine->task; 515343Smax.romanov@nginx.com pw->work.obj = pw; 516343Smax.romanov@nginx.com pw->work.data = data; 517343Smax.romanov@nginx.com 518343Smax.romanov@nginx.com pw->port = port; 519343Smax.romanov@nginx.com pw->handler = handler; 520343Smax.romanov@nginx.com 521343Smax.romanov@nginx.com nxt_event_engine_post(port->engine, &pw->work); 522343Smax.romanov@nginx.com 523343Smax.romanov@nginx.com return NXT_OK; 524343Smax.romanov@nginx.com } 525343Smax.romanov@nginx.com 526343Smax.romanov@nginx.com 527343Smax.romanov@nginx.com static void 528343Smax.romanov@nginx.com nxt_port_release_handler(nxt_task_t *task, nxt_port_t *port, void *data) 529343Smax.romanov@nginx.com { 530343Smax.romanov@nginx.com /* no op */ 531343Smax.romanov@nginx.com } 532343Smax.romanov@nginx.com 533343Smax.romanov@nginx.com 534343Smax.romanov@nginx.com void 535343Smax.romanov@nginx.com nxt_port_use(nxt_task_t *task, nxt_port_t *port, int i) 536343Smax.romanov@nginx.com { 537343Smax.romanov@nginx.com int c; 538343Smax.romanov@nginx.com 539343Smax.romanov@nginx.com c = nxt_atomic_fetch_add(&port->use_count, i); 540343Smax.romanov@nginx.com 541343Smax.romanov@nginx.com if (i < 0 && c == -i) { 542343Smax.romanov@nginx.com 543343Smax.romanov@nginx.com if (task->thread->engine == port->engine) { 544343Smax.romanov@nginx.com nxt_port_release(task, port); 545343Smax.romanov@nginx.com 546343Smax.romanov@nginx.com return; 547343Smax.romanov@nginx.com } 548343Smax.romanov@nginx.com 549343Smax.romanov@nginx.com nxt_port_post(task, port, nxt_port_release_handler, NULL); 550343Smax.romanov@nginx.com } 551343Smax.romanov@nginx.com } 552