120Sigor@sysoev.ru 220Sigor@sysoev.ru /* 320Sigor@sysoev.ru * Copyright (C) Igor Sysoev 420Sigor@sysoev.ru * Copyright (C) Valentin V. Bartenev 520Sigor@sysoev.ru * Copyright (C) NGINX, Inc. 620Sigor@sysoev.ru */ 720Sigor@sysoev.ru 820Sigor@sysoev.ru #include <nxt_main.h> 920Sigor@sysoev.ru #include <nxt_runtime.h> 1020Sigor@sysoev.ru #include <nxt_port.h> 11240Sigor@sysoev.ru #include <nxt_main_process.h> 12141Smax.romanov@nginx.com #include <nxt_router.h> 1320Sigor@sysoev.ru 1420Sigor@sysoev.ru 1520Sigor@sysoev.ru static nxt_int_t nxt_runtime_inherited_listen_sockets(nxt_task_t *task, 1620Sigor@sysoev.ru nxt_runtime_t *rt); 1720Sigor@sysoev.ru static nxt_int_t nxt_runtime_systemd_listen_sockets(nxt_task_t *task, 1820Sigor@sysoev.ru nxt_runtime_t *rt); 1920Sigor@sysoev.ru static nxt_int_t nxt_runtime_event_engines(nxt_task_t *task, nxt_runtime_t *rt); 2020Sigor@sysoev.ru static nxt_int_t nxt_runtime_thread_pools(nxt_thread_t *thr, nxt_runtime_t *rt); 2120Sigor@sysoev.ru static void nxt_runtime_start(nxt_task_t *task, void *obj, void *data); 2220Sigor@sysoev.ru static void nxt_runtime_initial_start(nxt_task_t *task); 2320Sigor@sysoev.ru static void nxt_runtime_close_idle_connections(nxt_event_engine_t *engine); 2420Sigor@sysoev.ru static void nxt_runtime_exit(nxt_task_t *task, void *obj, void *data); 2520Sigor@sysoev.ru static nxt_int_t nxt_runtime_event_engine_change(nxt_task_t *task, 2620Sigor@sysoev.ru nxt_runtime_t *rt); 2720Sigor@sysoev.ru static nxt_int_t nxt_runtime_conf_init(nxt_task_t *task, nxt_runtime_t *rt); 2820Sigor@sysoev.ru static nxt_int_t nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt); 2920Sigor@sysoev.ru static nxt_int_t nxt_runtime_hostname(nxt_task_t *task, nxt_runtime_t *rt); 3020Sigor@sysoev.ru static nxt_int_t nxt_runtime_log_files_init(nxt_runtime_t *rt); 3120Sigor@sysoev.ru static nxt_int_t nxt_runtime_log_files_create(nxt_task_t *task, 3220Sigor@sysoev.ru nxt_runtime_t *rt); 3320Sigor@sysoev.ru static nxt_int_t nxt_runtime_pid_file_create(nxt_task_t *task, 3420Sigor@sysoev.ru nxt_file_name_t *pid_file); 3520Sigor@sysoev.ru static void nxt_runtime_thread_pool_destroy(nxt_task_t *task, nxt_runtime_t *rt, 3620Sigor@sysoev.ru nxt_runtime_cont_t cont); 37223Sigor@sysoev.ru static void nxt_runtime_thread_pool_init(void); 38223Sigor@sysoev.ru static void nxt_runtime_thread_pool_exit(nxt_task_t *task, void *obj, 39223Sigor@sysoev.ru void *data); 40196Smax.romanov@nginx.com static void nxt_runtime_process_destroy(nxt_runtime_t *rt, 41196Smax.romanov@nginx.com nxt_process_t *process); 42196Smax.romanov@nginx.com static nxt_process_t *nxt_runtime_process_remove_pid(nxt_runtime_t *rt, 43196Smax.romanov@nginx.com nxt_pid_t pid); 44196Smax.romanov@nginx.com 4520Sigor@sysoev.ru 4620Sigor@sysoev.ru nxt_int_t 4720Sigor@sysoev.ru nxt_runtime_create(nxt_task_t *task) 4820Sigor@sysoev.ru { 49216Sigor@sysoev.ru nxt_mp_t *mp; 50216Sigor@sysoev.ru nxt_int_t ret; 51216Sigor@sysoev.ru nxt_array_t *listen_sockets; 52216Sigor@sysoev.ru nxt_runtime_t *rt; 53216Sigor@sysoev.ru nxt_app_lang_module_t *lang; 5420Sigor@sysoev.ru 5565Sigor@sysoev.ru mp = nxt_mp_create(1024, 128, 256, 32); 5620Sigor@sysoev.ru 5720Sigor@sysoev.ru if (nxt_slow_path(mp == NULL)) { 5820Sigor@sysoev.ru return NXT_ERROR; 5920Sigor@sysoev.ru } 6020Sigor@sysoev.ru 6165Sigor@sysoev.ru rt = nxt_mp_zget(mp, sizeof(nxt_runtime_t)); 6265Sigor@sysoev.ru if (nxt_slow_path(rt == NULL)) { 6365Sigor@sysoev.ru return NXT_ERROR; 6465Sigor@sysoev.ru } 6520Sigor@sysoev.ru 6620Sigor@sysoev.ru task->thread->runtime = rt; 6720Sigor@sysoev.ru rt->mem_pool = mp; 6820Sigor@sysoev.ru 69196Smax.romanov@nginx.com nxt_thread_mutex_create(&rt->processes_mutex); 70196Smax.romanov@nginx.com 7120Sigor@sysoev.ru rt->services = nxt_services_init(mp); 7220Sigor@sysoev.ru if (nxt_slow_path(rt->services == NULL)) { 7320Sigor@sysoev.ru goto fail; 7420Sigor@sysoev.ru } 7520Sigor@sysoev.ru 76216Sigor@sysoev.ru rt->languages = nxt_array_create(mp, 1, sizeof(nxt_app_lang_module_t)); 77216Sigor@sysoev.ru if (nxt_slow_path(rt->languages == NULL)) { 78216Sigor@sysoev.ru goto fail; 79216Sigor@sysoev.ru } 80216Sigor@sysoev.ru 81216Sigor@sysoev.ru /* Should not fail. */ 82216Sigor@sysoev.ru lang = nxt_array_add(rt->languages); 83356Svbart@nginx.com lang->type = NXT_APP_GO; 84354Svbart@nginx.com lang->version = (u_char *) ""; 85216Sigor@sysoev.ru lang->file = NULL; 86216Sigor@sysoev.ru lang->module = &nxt_go_module; 87216Sigor@sysoev.ru 8820Sigor@sysoev.ru listen_sockets = nxt_array_create(mp, 1, sizeof(nxt_listen_socket_t)); 8920Sigor@sysoev.ru if (nxt_slow_path(listen_sockets == NULL)) { 9020Sigor@sysoev.ru goto fail; 9120Sigor@sysoev.ru } 9220Sigor@sysoev.ru 9320Sigor@sysoev.ru rt->listen_sockets = listen_sockets; 9420Sigor@sysoev.ru 9520Sigor@sysoev.ru ret = nxt_runtime_inherited_listen_sockets(task, rt); 9620Sigor@sysoev.ru if (nxt_slow_path(ret != NXT_OK)) { 9720Sigor@sysoev.ru goto fail; 9820Sigor@sysoev.ru } 9920Sigor@sysoev.ru 10020Sigor@sysoev.ru if (nxt_runtime_hostname(task, rt) != NXT_OK) { 10120Sigor@sysoev.ru goto fail; 10220Sigor@sysoev.ru } 10320Sigor@sysoev.ru 10420Sigor@sysoev.ru if (nxt_slow_path(nxt_runtime_log_files_init(rt) != NXT_OK)) { 10520Sigor@sysoev.ru goto fail; 10620Sigor@sysoev.ru } 10720Sigor@sysoev.ru 10820Sigor@sysoev.ru if (nxt_runtime_event_engines(task, rt) != NXT_OK) { 10920Sigor@sysoev.ru goto fail; 11020Sigor@sysoev.ru } 11120Sigor@sysoev.ru 11220Sigor@sysoev.ru if (nxt_slow_path(nxt_runtime_thread_pools(task->thread, rt) != NXT_OK)) { 11320Sigor@sysoev.ru goto fail; 11420Sigor@sysoev.ru } 11520Sigor@sysoev.ru 11620Sigor@sysoev.ru rt->start = nxt_runtime_initial_start; 11720Sigor@sysoev.ru 118221Sigor@sysoev.ru if (nxt_runtime_conf_init(task, rt) != NXT_OK) { 119221Sigor@sysoev.ru goto fail; 120221Sigor@sysoev.ru } 121221Sigor@sysoev.ru 12220Sigor@sysoev.ru nxt_work_queue_add(&task->thread->engine->fast_work_queue, 12320Sigor@sysoev.ru nxt_runtime_start, task, rt, NULL); 12420Sigor@sysoev.ru 12520Sigor@sysoev.ru return NXT_OK; 12620Sigor@sysoev.ru 12720Sigor@sysoev.ru fail: 12820Sigor@sysoev.ru 12965Sigor@sysoev.ru nxt_mp_destroy(mp); 13020Sigor@sysoev.ru 13120Sigor@sysoev.ru return NXT_ERROR; 13220Sigor@sysoev.ru } 13320Sigor@sysoev.ru 13420Sigor@sysoev.ru 13520Sigor@sysoev.ru static nxt_int_t 13620Sigor@sysoev.ru nxt_runtime_inherited_listen_sockets(nxt_task_t *task, nxt_runtime_t *rt) 13720Sigor@sysoev.ru { 13820Sigor@sysoev.ru u_char *v, *p; 13920Sigor@sysoev.ru nxt_int_t type; 14020Sigor@sysoev.ru nxt_array_t *inherited_sockets; 14120Sigor@sysoev.ru nxt_socket_t s; 14220Sigor@sysoev.ru nxt_listen_socket_t *ls; 14320Sigor@sysoev.ru 14420Sigor@sysoev.ru v = (u_char *) getenv("NGINX"); 14520Sigor@sysoev.ru 14620Sigor@sysoev.ru if (v == NULL) { 14720Sigor@sysoev.ru return nxt_runtime_systemd_listen_sockets(task, rt); 14820Sigor@sysoev.ru } 14920Sigor@sysoev.ru 150564Svbart@nginx.com nxt_alert(task, "using inherited listen sockets: %s", v); 15120Sigor@sysoev.ru 15220Sigor@sysoev.ru inherited_sockets = nxt_array_create(rt->mem_pool, 15320Sigor@sysoev.ru 1, sizeof(nxt_listen_socket_t)); 15420Sigor@sysoev.ru if (inherited_sockets == NULL) { 15520Sigor@sysoev.ru return NXT_ERROR; 15620Sigor@sysoev.ru } 15720Sigor@sysoev.ru 15820Sigor@sysoev.ru rt->inherited_sockets = inherited_sockets; 15920Sigor@sysoev.ru 16020Sigor@sysoev.ru for (p = v; *p != '\0'; p++) { 16120Sigor@sysoev.ru 16220Sigor@sysoev.ru if (*p == ';') { 16320Sigor@sysoev.ru s = nxt_int_parse(v, p - v); 16420Sigor@sysoev.ru 16520Sigor@sysoev.ru if (nxt_slow_path(s < 0)) { 166564Svbart@nginx.com nxt_alert(task, "invalid socket number \"%s\" " 167564Svbart@nginx.com "in NGINX environment variable, " 168564Svbart@nginx.com "ignoring the rest of the variable", v); 16920Sigor@sysoev.ru return NXT_ERROR; 17020Sigor@sysoev.ru } 17120Sigor@sysoev.ru 17220Sigor@sysoev.ru v = p + 1; 17320Sigor@sysoev.ru 17420Sigor@sysoev.ru ls = nxt_array_zero_add(inherited_sockets); 17520Sigor@sysoev.ru if (nxt_slow_path(ls == NULL)) { 17620Sigor@sysoev.ru return NXT_ERROR; 17720Sigor@sysoev.ru } 17820Sigor@sysoev.ru 17920Sigor@sysoev.ru ls->socket = s; 18020Sigor@sysoev.ru 18120Sigor@sysoev.ru ls->sockaddr = nxt_getsockname(task, rt->mem_pool, s); 18220Sigor@sysoev.ru if (nxt_slow_path(ls->sockaddr == NULL)) { 18320Sigor@sysoev.ru return NXT_ERROR; 18420Sigor@sysoev.ru } 18520Sigor@sysoev.ru 18620Sigor@sysoev.ru type = nxt_socket_getsockopt(task, s, SOL_SOCKET, SO_TYPE); 18720Sigor@sysoev.ru if (nxt_slow_path(type == -1)) { 18820Sigor@sysoev.ru return NXT_ERROR; 18920Sigor@sysoev.ru } 19020Sigor@sysoev.ru 19120Sigor@sysoev.ru ls->sockaddr->type = (uint16_t) type; 19220Sigor@sysoev.ru } 19320Sigor@sysoev.ru } 19420Sigor@sysoev.ru 19520Sigor@sysoev.ru return NXT_OK; 19620Sigor@sysoev.ru } 19720Sigor@sysoev.ru 19820Sigor@sysoev.ru 19920Sigor@sysoev.ru static nxt_int_t 20020Sigor@sysoev.ru nxt_runtime_systemd_listen_sockets(nxt_task_t *task, nxt_runtime_t *rt) 20120Sigor@sysoev.ru { 20220Sigor@sysoev.ru u_char *nfd, *pid; 20320Sigor@sysoev.ru nxt_int_t n; 20420Sigor@sysoev.ru nxt_array_t *inherited_sockets; 20520Sigor@sysoev.ru nxt_socket_t s; 20620Sigor@sysoev.ru nxt_listen_socket_t *ls; 20720Sigor@sysoev.ru 20820Sigor@sysoev.ru /* 20920Sigor@sysoev.ru * Number of listening sockets passed. The socket 21020Sigor@sysoev.ru * descriptors start from number 3 and are sequential. 21120Sigor@sysoev.ru */ 21220Sigor@sysoev.ru nfd = (u_char *) getenv("LISTEN_FDS"); 21320Sigor@sysoev.ru if (nfd == NULL) { 21420Sigor@sysoev.ru return NXT_OK; 21520Sigor@sysoev.ru } 21620Sigor@sysoev.ru 21720Sigor@sysoev.ru /* The pid of the service process. */ 21820Sigor@sysoev.ru pid = (u_char *) getenv("LISTEN_PID"); 21920Sigor@sysoev.ru if (pid == NULL) { 22020Sigor@sysoev.ru return NXT_OK; 22120Sigor@sysoev.ru } 22220Sigor@sysoev.ru 22320Sigor@sysoev.ru n = nxt_int_parse(nfd, nxt_strlen(nfd)); 22420Sigor@sysoev.ru if (n < 0) { 22520Sigor@sysoev.ru return NXT_OK; 22620Sigor@sysoev.ru } 22720Sigor@sysoev.ru 22820Sigor@sysoev.ru if (nxt_pid != nxt_int_parse(pid, nxt_strlen(pid))) { 22920Sigor@sysoev.ru return NXT_OK; 23020Sigor@sysoev.ru } 23120Sigor@sysoev.ru 232494Spluknet@nginx.com nxt_log(task, NXT_LOG_INFO, "using %i systemd listen sockets", n); 23320Sigor@sysoev.ru 23420Sigor@sysoev.ru inherited_sockets = nxt_array_create(rt->mem_pool, 23520Sigor@sysoev.ru n, sizeof(nxt_listen_socket_t)); 23620Sigor@sysoev.ru if (inherited_sockets == NULL) { 23720Sigor@sysoev.ru return NXT_ERROR; 23820Sigor@sysoev.ru } 23920Sigor@sysoev.ru 24020Sigor@sysoev.ru rt->inherited_sockets = inherited_sockets; 24120Sigor@sysoev.ru 24220Sigor@sysoev.ru for (s = 3; s < n; s++) { 24320Sigor@sysoev.ru ls = nxt_array_zero_add(inherited_sockets); 24420Sigor@sysoev.ru if (nxt_slow_path(ls == NULL)) { 24520Sigor@sysoev.ru return NXT_ERROR; 24620Sigor@sysoev.ru } 24720Sigor@sysoev.ru 24820Sigor@sysoev.ru ls->socket = s; 24920Sigor@sysoev.ru 25020Sigor@sysoev.ru ls->sockaddr = nxt_getsockname(task, rt->mem_pool, s); 25120Sigor@sysoev.ru if (nxt_slow_path(ls->sockaddr == NULL)) { 25220Sigor@sysoev.ru return NXT_ERROR; 25320Sigor@sysoev.ru } 25420Sigor@sysoev.ru 25520Sigor@sysoev.ru ls->sockaddr->type = SOCK_STREAM; 25620Sigor@sysoev.ru } 25720Sigor@sysoev.ru 25820Sigor@sysoev.ru return NXT_OK; 25920Sigor@sysoev.ru } 26020Sigor@sysoev.ru 26120Sigor@sysoev.ru 26220Sigor@sysoev.ru static nxt_int_t 26320Sigor@sysoev.ru nxt_runtime_event_engines(nxt_task_t *task, nxt_runtime_t *rt) 26420Sigor@sysoev.ru { 26553Sigor@sysoev.ru nxt_thread_t *thread; 26653Sigor@sysoev.ru nxt_event_engine_t *engine; 26720Sigor@sysoev.ru const nxt_event_interface_t *interface; 26820Sigor@sysoev.ru 26920Sigor@sysoev.ru interface = nxt_service_get(rt->services, "engine", NULL); 27020Sigor@sysoev.ru 27120Sigor@sysoev.ru if (nxt_slow_path(interface == NULL)) { 27220Sigor@sysoev.ru /* TODO: log */ 27320Sigor@sysoev.ru return NXT_ERROR; 27420Sigor@sysoev.ru } 27520Sigor@sysoev.ru 27620Sigor@sysoev.ru engine = nxt_event_engine_create(task, interface, 277240Sigor@sysoev.ru nxt_main_process_signals, 0, 0); 27820Sigor@sysoev.ru 27920Sigor@sysoev.ru if (nxt_slow_path(engine == NULL)) { 28020Sigor@sysoev.ru return NXT_ERROR; 28120Sigor@sysoev.ru } 28220Sigor@sysoev.ru 28353Sigor@sysoev.ru thread = task->thread; 28453Sigor@sysoev.ru thread->engine = engine; 285326Svbart@nginx.com #if 0 28653Sigor@sysoev.ru thread->fiber = &engine->fibers->fiber; 287326Svbart@nginx.com #endif 28853Sigor@sysoev.ru 28920Sigor@sysoev.ru engine->id = rt->last_engine_id++; 290342Smax.romanov@nginx.com engine->mem_pool = nxt_mp_create(1024, 128, 256, 32); 29153Sigor@sysoev.ru 29253Sigor@sysoev.ru nxt_queue_init(&rt->engines); 29353Sigor@sysoev.ru nxt_queue_insert_tail(&rt->engines, &engine->link); 29420Sigor@sysoev.ru 29520Sigor@sysoev.ru return NXT_OK; 29620Sigor@sysoev.ru } 29720Sigor@sysoev.ru 29820Sigor@sysoev.ru 29920Sigor@sysoev.ru static nxt_int_t 30020Sigor@sysoev.ru nxt_runtime_thread_pools(nxt_thread_t *thr, nxt_runtime_t *rt) 30120Sigor@sysoev.ru { 30220Sigor@sysoev.ru nxt_int_t ret; 30320Sigor@sysoev.ru nxt_array_t *thread_pools; 30420Sigor@sysoev.ru 30520Sigor@sysoev.ru thread_pools = nxt_array_create(rt->mem_pool, 1, 30620Sigor@sysoev.ru sizeof(nxt_thread_pool_t *)); 30720Sigor@sysoev.ru 30820Sigor@sysoev.ru if (nxt_slow_path(thread_pools == NULL)) { 30920Sigor@sysoev.ru return NXT_ERROR; 31020Sigor@sysoev.ru } 31120Sigor@sysoev.ru 31220Sigor@sysoev.ru rt->thread_pools = thread_pools; 31320Sigor@sysoev.ru ret = nxt_runtime_thread_pool_create(thr, rt, 2, 60000 * 1000000LL); 31420Sigor@sysoev.ru 31520Sigor@sysoev.ru if (nxt_slow_path(ret != NXT_OK)) { 31620Sigor@sysoev.ru return NXT_ERROR; 31720Sigor@sysoev.ru } 31820Sigor@sysoev.ru 31920Sigor@sysoev.ru return NXT_OK; 32020Sigor@sysoev.ru } 32120Sigor@sysoev.ru 32220Sigor@sysoev.ru 32320Sigor@sysoev.ru static void 32420Sigor@sysoev.ru nxt_runtime_start(nxt_task_t *task, void *obj, void *data) 32520Sigor@sysoev.ru { 32620Sigor@sysoev.ru nxt_runtime_t *rt; 32720Sigor@sysoev.ru 32820Sigor@sysoev.ru rt = obj; 32920Sigor@sysoev.ru 33020Sigor@sysoev.ru nxt_debug(task, "rt conf done"); 33120Sigor@sysoev.ru 33220Sigor@sysoev.ru task->thread->log->ctx_handler = NULL; 33320Sigor@sysoev.ru task->thread->log->ctx = NULL; 33420Sigor@sysoev.ru 33520Sigor@sysoev.ru if (nxt_runtime_log_files_create(task, rt) != NXT_OK) { 33620Sigor@sysoev.ru goto fail; 33720Sigor@sysoev.ru } 33820Sigor@sysoev.ru 33920Sigor@sysoev.ru if (nxt_runtime_event_engine_change(task, rt) != NXT_OK) { 34020Sigor@sysoev.ru goto fail; 34120Sigor@sysoev.ru } 34220Sigor@sysoev.ru 34320Sigor@sysoev.ru /* 34420Sigor@sysoev.ru * Thread pools should be destroyed before starting worker 34520Sigor@sysoev.ru * processes, because thread pool semaphores will stick in 34620Sigor@sysoev.ru * locked state in new processes after fork(). 34720Sigor@sysoev.ru */ 34820Sigor@sysoev.ru nxt_runtime_thread_pool_destroy(task, rt, rt->start); 34920Sigor@sysoev.ru 35020Sigor@sysoev.ru return; 35120Sigor@sysoev.ru 35220Sigor@sysoev.ru fail: 35320Sigor@sysoev.ru 35420Sigor@sysoev.ru nxt_runtime_quit(task); 35520Sigor@sysoev.ru } 35620Sigor@sysoev.ru 35720Sigor@sysoev.ru 35820Sigor@sysoev.ru static void 35920Sigor@sysoev.ru nxt_runtime_initial_start(nxt_task_t *task) 36020Sigor@sysoev.ru { 36120Sigor@sysoev.ru nxt_int_t ret; 36220Sigor@sysoev.ru nxt_thread_t *thr; 36320Sigor@sysoev.ru nxt_runtime_t *rt; 36420Sigor@sysoev.ru const nxt_event_interface_t *interface; 36520Sigor@sysoev.ru 36620Sigor@sysoev.ru thr = task->thread; 36720Sigor@sysoev.ru rt = thr->runtime; 36820Sigor@sysoev.ru 36920Sigor@sysoev.ru if (rt->inherited_sockets == NULL && rt->daemon) { 37020Sigor@sysoev.ru 37120Sigor@sysoev.ru if (nxt_process_daemon(task) != NXT_OK) { 37220Sigor@sysoev.ru goto fail; 37320Sigor@sysoev.ru } 37420Sigor@sysoev.ru 37520Sigor@sysoev.ru /* 37620Sigor@sysoev.ru * An event engine should be updated after fork() 37720Sigor@sysoev.ru * even if an event facility was not changed because: 37820Sigor@sysoev.ru * 1) inherited kqueue descriptor is invalid, 37920Sigor@sysoev.ru * 2) the signal thread is not inherited. 38020Sigor@sysoev.ru */ 38120Sigor@sysoev.ru interface = nxt_service_get(rt->services, "engine", rt->engine); 38220Sigor@sysoev.ru if (interface == NULL) { 38320Sigor@sysoev.ru goto fail; 38420Sigor@sysoev.ru } 38520Sigor@sysoev.ru 38620Sigor@sysoev.ru ret = nxt_event_engine_change(task->thread->engine, interface, 38720Sigor@sysoev.ru rt->batch); 38820Sigor@sysoev.ru if (ret != NXT_OK) { 38920Sigor@sysoev.ru goto fail; 39020Sigor@sysoev.ru } 39120Sigor@sysoev.ru } 39220Sigor@sysoev.ru 39320Sigor@sysoev.ru ret = nxt_runtime_pid_file_create(task, rt->pid_file); 39420Sigor@sysoev.ru if (ret != NXT_OK) { 39520Sigor@sysoev.ru goto fail; 39620Sigor@sysoev.ru } 39720Sigor@sysoev.ru 39820Sigor@sysoev.ru if (nxt_runtime_event_engine_change(task, rt) != NXT_OK) { 39920Sigor@sysoev.ru goto fail; 40020Sigor@sysoev.ru } 40120Sigor@sysoev.ru 40220Sigor@sysoev.ru thr->engine->max_connections = rt->engine_connections; 40320Sigor@sysoev.ru 404695Sigor@sysoev.ru if (nxt_main_process_start(thr, task, rt) != NXT_ERROR) { 40520Sigor@sysoev.ru return; 40620Sigor@sysoev.ru } 40720Sigor@sysoev.ru 40820Sigor@sysoev.ru fail: 40920Sigor@sysoev.ru 41020Sigor@sysoev.ru nxt_runtime_quit(task); 41120Sigor@sysoev.ru } 41220Sigor@sysoev.ru 41320Sigor@sysoev.ru 41420Sigor@sysoev.ru void 41520Sigor@sysoev.ru nxt_runtime_quit(nxt_task_t *task) 41620Sigor@sysoev.ru { 41720Sigor@sysoev.ru nxt_bool_t done; 41820Sigor@sysoev.ru nxt_runtime_t *rt; 41920Sigor@sysoev.ru nxt_event_engine_t *engine; 42020Sigor@sysoev.ru 42120Sigor@sysoev.ru rt = task->thread->runtime; 42220Sigor@sysoev.ru engine = task->thread->engine; 42320Sigor@sysoev.ru 42420Sigor@sysoev.ru nxt_debug(task, "exiting"); 42520Sigor@sysoev.ru 42620Sigor@sysoev.ru done = 1; 42720Sigor@sysoev.ru 42820Sigor@sysoev.ru if (!engine->shutdown) { 42920Sigor@sysoev.ru engine->shutdown = 1; 43020Sigor@sysoev.ru 43120Sigor@sysoev.ru if (!nxt_array_is_empty(rt->thread_pools)) { 43220Sigor@sysoev.ru nxt_runtime_thread_pool_destroy(task, rt, nxt_runtime_quit); 43320Sigor@sysoev.ru done = 0; 43420Sigor@sysoev.ru } 43520Sigor@sysoev.ru 436*696Sigor@sysoev.ru if (rt->type == NXT_PROCESS_MAIN) { 437240Sigor@sysoev.ru nxt_main_stop_worker_processes(task, rt); 43820Sigor@sysoev.ru done = 0; 43920Sigor@sysoev.ru } 44020Sigor@sysoev.ru } 44120Sigor@sysoev.ru 44220Sigor@sysoev.ru nxt_runtime_close_idle_connections(engine); 44320Sigor@sysoev.ru 44420Sigor@sysoev.ru if (done) { 44520Sigor@sysoev.ru nxt_work_queue_add(&engine->fast_work_queue, nxt_runtime_exit, 44620Sigor@sysoev.ru task, rt, engine); 44720Sigor@sysoev.ru } 44820Sigor@sysoev.ru } 44920Sigor@sysoev.ru 45020Sigor@sysoev.ru 45120Sigor@sysoev.ru static void 45220Sigor@sysoev.ru nxt_runtime_close_idle_connections(nxt_event_engine_t *engine) 45320Sigor@sysoev.ru { 45462Sigor@sysoev.ru nxt_conn_t *c; 45520Sigor@sysoev.ru nxt_queue_t *idle; 45620Sigor@sysoev.ru nxt_queue_link_t *link, *next; 45720Sigor@sysoev.ru 45820Sigor@sysoev.ru nxt_debug(&engine->task, "close idle connections"); 45920Sigor@sysoev.ru 46020Sigor@sysoev.ru idle = &engine->idle_connections; 46120Sigor@sysoev.ru 46220Sigor@sysoev.ru for (link = nxt_queue_head(idle); 46320Sigor@sysoev.ru link != nxt_queue_tail(idle); 46420Sigor@sysoev.ru link = next) 46520Sigor@sysoev.ru { 46620Sigor@sysoev.ru next = nxt_queue_next(link); 46762Sigor@sysoev.ru c = nxt_queue_link_data(link, nxt_conn_t, link); 46820Sigor@sysoev.ru 46920Sigor@sysoev.ru if (!c->socket.read_ready) { 47020Sigor@sysoev.ru nxt_queue_remove(link); 47162Sigor@sysoev.ru nxt_conn_close(engine, c); 47220Sigor@sysoev.ru } 47320Sigor@sysoev.ru } 47420Sigor@sysoev.ru } 47520Sigor@sysoev.ru 47620Sigor@sysoev.ru 47720Sigor@sysoev.ru static void 47820Sigor@sysoev.ru nxt_runtime_exit(nxt_task_t *task, void *obj, void *data) 47920Sigor@sysoev.ru { 480125Smax.romanov@nginx.com nxt_runtime_t *rt; 481125Smax.romanov@nginx.com nxt_process_t *process; 48220Sigor@sysoev.ru nxt_event_engine_t *engine; 48320Sigor@sysoev.ru 48420Sigor@sysoev.ru rt = obj; 48520Sigor@sysoev.ru engine = data; 48620Sigor@sysoev.ru 48720Sigor@sysoev.ru nxt_debug(task, "thread pools: %d", rt->thread_pools->nelts); 48820Sigor@sysoev.ru 48920Sigor@sysoev.ru if (!nxt_array_is_empty(rt->thread_pools)) { 49020Sigor@sysoev.ru return; 49120Sigor@sysoev.ru } 49220Sigor@sysoev.ru 493*696Sigor@sysoev.ru if (rt->type == NXT_PROCESS_MAIN) { 49420Sigor@sysoev.ru if (rt->pid_file != NULL) { 49520Sigor@sysoev.ru nxt_file_delete(rt->pid_file); 49620Sigor@sysoev.ru } 497234Sigor@sysoev.ru 498234Sigor@sysoev.ru #if (NXT_HAVE_UNIX_DOMAIN) 499234Sigor@sysoev.ru { 500234Sigor@sysoev.ru nxt_sockaddr_t *sa; 501234Sigor@sysoev.ru nxt_file_name_t *name; 502234Sigor@sysoev.ru 503234Sigor@sysoev.ru sa = rt->controller_listen; 504234Sigor@sysoev.ru 505234Sigor@sysoev.ru if (sa->u.sockaddr.sa_family == AF_UNIX) { 506234Sigor@sysoev.ru name = (nxt_file_name_t *) sa->u.sockaddr_un.sun_path; 507234Sigor@sysoev.ru (void) nxt_file_delete(name); 508234Sigor@sysoev.ru } 509234Sigor@sysoev.ru } 510234Sigor@sysoev.ru #endif 51120Sigor@sysoev.ru } 51220Sigor@sysoev.ru 51320Sigor@sysoev.ru if (!engine->event.signal_support) { 51420Sigor@sysoev.ru nxt_event_engine_signals_stop(engine); 51520Sigor@sysoev.ru } 51620Sigor@sysoev.ru 517125Smax.romanov@nginx.com nxt_runtime_process_each(rt, process) { 518125Smax.romanov@nginx.com 519349Smax.romanov@nginx.com nxt_process_close_ports(task, process); 520125Smax.romanov@nginx.com 521125Smax.romanov@nginx.com } nxt_runtime_process_loop; 522125Smax.romanov@nginx.com 523196Smax.romanov@nginx.com nxt_thread_mutex_destroy(&rt->processes_mutex); 524196Smax.romanov@nginx.com 525125Smax.romanov@nginx.com nxt_mp_destroy(rt->mem_pool); 526125Smax.romanov@nginx.com 52720Sigor@sysoev.ru nxt_debug(task, "exit"); 52820Sigor@sysoev.ru 52920Sigor@sysoev.ru exit(0); 53020Sigor@sysoev.ru nxt_unreachable(); 53120Sigor@sysoev.ru } 53220Sigor@sysoev.ru 53320Sigor@sysoev.ru 53420Sigor@sysoev.ru static nxt_int_t 53520Sigor@sysoev.ru nxt_runtime_event_engine_change(nxt_task_t *task, nxt_runtime_t *rt) 53620Sigor@sysoev.ru { 53720Sigor@sysoev.ru nxt_event_engine_t *engine; 53820Sigor@sysoev.ru const nxt_event_interface_t *interface; 53920Sigor@sysoev.ru 54020Sigor@sysoev.ru engine = task->thread->engine; 54120Sigor@sysoev.ru 54220Sigor@sysoev.ru if (engine->batch == rt->batch 54320Sigor@sysoev.ru && nxt_strcmp(engine->event.name, rt->engine) == 0) 54420Sigor@sysoev.ru { 54520Sigor@sysoev.ru return NXT_OK; 54620Sigor@sysoev.ru } 54720Sigor@sysoev.ru 54820Sigor@sysoev.ru interface = nxt_service_get(rt->services, "engine", rt->engine); 54920Sigor@sysoev.ru 55020Sigor@sysoev.ru if (interface != NULL) { 55120Sigor@sysoev.ru return nxt_event_engine_change(engine, interface, rt->batch); 55220Sigor@sysoev.ru } 55320Sigor@sysoev.ru 55420Sigor@sysoev.ru return NXT_ERROR; 55520Sigor@sysoev.ru } 55620Sigor@sysoev.ru 55720Sigor@sysoev.ru 55820Sigor@sysoev.ru void 55920Sigor@sysoev.ru nxt_runtime_event_engine_free(nxt_runtime_t *rt) 56020Sigor@sysoev.ru { 56153Sigor@sysoev.ru nxt_queue_link_t *link; 56253Sigor@sysoev.ru nxt_event_engine_t *engine; 56320Sigor@sysoev.ru 56453Sigor@sysoev.ru link = nxt_queue_first(&rt->engines); 56553Sigor@sysoev.ru nxt_queue_remove(link); 56620Sigor@sysoev.ru 56753Sigor@sysoev.ru engine = nxt_queue_link_data(link, nxt_event_engine_t, link); 56820Sigor@sysoev.ru nxt_event_engine_free(engine); 56920Sigor@sysoev.ru } 57020Sigor@sysoev.ru 57120Sigor@sysoev.ru 57220Sigor@sysoev.ru nxt_int_t 57320Sigor@sysoev.ru nxt_runtime_thread_pool_create(nxt_thread_t *thr, nxt_runtime_t *rt, 57420Sigor@sysoev.ru nxt_uint_t max_threads, nxt_nsec_t timeout) 57520Sigor@sysoev.ru { 57620Sigor@sysoev.ru nxt_thread_pool_t *thread_pool, **tp; 57720Sigor@sysoev.ru 57820Sigor@sysoev.ru tp = nxt_array_add(rt->thread_pools); 57920Sigor@sysoev.ru if (tp == NULL) { 58020Sigor@sysoev.ru return NXT_ERROR; 58120Sigor@sysoev.ru } 58220Sigor@sysoev.ru 58320Sigor@sysoev.ru thread_pool = nxt_thread_pool_create(max_threads, timeout, 58420Sigor@sysoev.ru nxt_runtime_thread_pool_init, 58520Sigor@sysoev.ru thr->engine, 58620Sigor@sysoev.ru nxt_runtime_thread_pool_exit); 58720Sigor@sysoev.ru 58820Sigor@sysoev.ru if (nxt_fast_path(thread_pool != NULL)) { 58920Sigor@sysoev.ru *tp = thread_pool; 59020Sigor@sysoev.ru } 59120Sigor@sysoev.ru 59220Sigor@sysoev.ru return NXT_OK; 59320Sigor@sysoev.ru } 59420Sigor@sysoev.ru 59520Sigor@sysoev.ru 59620Sigor@sysoev.ru static void 59720Sigor@sysoev.ru nxt_runtime_thread_pool_destroy(nxt_task_t *task, nxt_runtime_t *rt, 59820Sigor@sysoev.ru nxt_runtime_cont_t cont) 59920Sigor@sysoev.ru { 60020Sigor@sysoev.ru nxt_uint_t n; 60120Sigor@sysoev.ru nxt_thread_pool_t **tp; 60220Sigor@sysoev.ru 60320Sigor@sysoev.ru rt->continuation = cont; 60420Sigor@sysoev.ru 60520Sigor@sysoev.ru n = rt->thread_pools->nelts; 60620Sigor@sysoev.ru 60720Sigor@sysoev.ru if (n == 0) { 60820Sigor@sysoev.ru cont(task); 60920Sigor@sysoev.ru return; 61020Sigor@sysoev.ru } 61120Sigor@sysoev.ru 61220Sigor@sysoev.ru tp = rt->thread_pools->elts; 61320Sigor@sysoev.ru 61420Sigor@sysoev.ru do { 61520Sigor@sysoev.ru nxt_thread_pool_destroy(*tp); 61620Sigor@sysoev.ru 61720Sigor@sysoev.ru tp++; 61820Sigor@sysoev.ru n--; 61920Sigor@sysoev.ru } while (n != 0); 62020Sigor@sysoev.ru } 62120Sigor@sysoev.ru 62220Sigor@sysoev.ru 62320Sigor@sysoev.ru static void 62420Sigor@sysoev.ru nxt_runtime_thread_pool_init(void) 62520Sigor@sysoev.ru { 62620Sigor@sysoev.ru #if (NXT_REGEX) 62720Sigor@sysoev.ru nxt_regex_init(0); 62820Sigor@sysoev.ru #endif 62920Sigor@sysoev.ru } 63020Sigor@sysoev.ru 63120Sigor@sysoev.ru 63220Sigor@sysoev.ru static void 63320Sigor@sysoev.ru nxt_runtime_thread_pool_exit(nxt_task_t *task, void *obj, void *data) 63420Sigor@sysoev.ru { 63520Sigor@sysoev.ru nxt_uint_t i, n; 63620Sigor@sysoev.ru nxt_runtime_t *rt; 63720Sigor@sysoev.ru nxt_thread_pool_t *tp, **thread_pools; 63820Sigor@sysoev.ru nxt_thread_handle_t handle; 63920Sigor@sysoev.ru 64020Sigor@sysoev.ru tp = obj; 64120Sigor@sysoev.ru 64220Sigor@sysoev.ru if (data != NULL) { 64320Sigor@sysoev.ru handle = (nxt_thread_handle_t) (uintptr_t) data; 64420Sigor@sysoev.ru nxt_thread_wait(handle); 64520Sigor@sysoev.ru } 64620Sigor@sysoev.ru 64720Sigor@sysoev.ru rt = task->thread->runtime; 64820Sigor@sysoev.ru 64920Sigor@sysoev.ru thread_pools = rt->thread_pools->elts; 65020Sigor@sysoev.ru n = rt->thread_pools->nelts; 65120Sigor@sysoev.ru 65220Sigor@sysoev.ru nxt_debug(task, "thread pools: %ui", n); 65320Sigor@sysoev.ru 65420Sigor@sysoev.ru for (i = 0; i < n; i++) { 65520Sigor@sysoev.ru 65620Sigor@sysoev.ru if (tp == thread_pools[i]) { 65720Sigor@sysoev.ru nxt_array_remove(rt->thread_pools, &thread_pools[i]); 65820Sigor@sysoev.ru 65920Sigor@sysoev.ru if (n == 1) { 66020Sigor@sysoev.ru /* The last thread pool. */ 66120Sigor@sysoev.ru rt->continuation(task); 66220Sigor@sysoev.ru } 66320Sigor@sysoev.ru 66420Sigor@sysoev.ru return; 66520Sigor@sysoev.ru } 66620Sigor@sysoev.ru } 66720Sigor@sysoev.ru } 66820Sigor@sysoev.ru 66920Sigor@sysoev.ru 67020Sigor@sysoev.ru static nxt_int_t 67120Sigor@sysoev.ru nxt_runtime_conf_init(nxt_task_t *task, nxt_runtime_t *rt) 67220Sigor@sysoev.ru { 67320Sigor@sysoev.ru nxt_int_t ret; 674251Sigor@sysoev.ru nxt_str_t control; 675251Sigor@sysoev.ru nxt_uint_t n; 67620Sigor@sysoev.ru nxt_file_t *file; 677251Sigor@sysoev.ru const char *slash; 678234Sigor@sysoev.ru nxt_sockaddr_t *sa; 67920Sigor@sysoev.ru nxt_file_name_str_t file_name; 68020Sigor@sysoev.ru const nxt_event_interface_t *interface; 68120Sigor@sysoev.ru 68220Sigor@sysoev.ru rt->daemon = 1; 68320Sigor@sysoev.ru rt->engine_connections = 256; 68420Sigor@sysoev.ru rt->auxiliary_threads = 2; 685232Sigor@sysoev.ru rt->user_cred.user = NXT_USER; 686232Sigor@sysoev.ru rt->group = NXT_GROUP; 687231Sigor@sysoev.ru rt->pid = NXT_PID; 688230Sigor@sysoev.ru rt->log = NXT_LOG; 689233Sigor@sysoev.ru rt->modules = NXT_MODULES; 690314Svbart@nginx.com rt->state = NXT_STATE; 691234Sigor@sysoev.ru rt->control = NXT_CONTROL_SOCK; 69220Sigor@sysoev.ru 69320Sigor@sysoev.ru if (nxt_runtime_conf_read_cmd(task, rt) != NXT_OK) { 69420Sigor@sysoev.ru return NXT_ERROR; 69520Sigor@sysoev.ru } 69620Sigor@sysoev.ru 69720Sigor@sysoev.ru if (nxt_user_cred_get(task, &rt->user_cred, rt->group) != NXT_OK) { 69820Sigor@sysoev.ru return NXT_ERROR; 69920Sigor@sysoev.ru } 70020Sigor@sysoev.ru 70120Sigor@sysoev.ru /* An engine's parameters. */ 70220Sigor@sysoev.ru 70320Sigor@sysoev.ru interface = nxt_service_get(rt->services, "engine", rt->engine); 70420Sigor@sysoev.ru if (interface == NULL) { 70520Sigor@sysoev.ru return NXT_ERROR; 70620Sigor@sysoev.ru } 70720Sigor@sysoev.ru 70820Sigor@sysoev.ru rt->engine = interface->name; 70920Sigor@sysoev.ru 710252Smax.romanov@nginx.com ret = nxt_file_name_create(rt->mem_pool, &file_name, "%s%Z", rt->pid); 71120Sigor@sysoev.ru if (nxt_slow_path(ret != NXT_OK)) { 71220Sigor@sysoev.ru return NXT_ERROR; 71320Sigor@sysoev.ru } 71420Sigor@sysoev.ru 71520Sigor@sysoev.ru rt->pid_file = file_name.start; 71620Sigor@sysoev.ru 717230Sigor@sysoev.ru ret = nxt_file_name_create(rt->mem_pool, &file_name, "%s%Z", rt->log); 71820Sigor@sysoev.ru if (nxt_slow_path(ret != NXT_OK)) { 71920Sigor@sysoev.ru return NXT_ERROR; 72020Sigor@sysoev.ru } 72120Sigor@sysoev.ru 72220Sigor@sysoev.ru file = nxt_list_first(rt->log_files); 72320Sigor@sysoev.ru file->name = file_name.start; 72420Sigor@sysoev.ru 725251Sigor@sysoev.ru slash = ""; 726251Sigor@sysoev.ru n = nxt_strlen(rt->modules); 727251Sigor@sysoev.ru 728251Sigor@sysoev.ru if (n > 1 && rt->modules[n - 1] != '/') { 729251Sigor@sysoev.ru slash = "/"; 730251Sigor@sysoev.ru } 731251Sigor@sysoev.ru 732260Sigor@sysoev.ru ret = nxt_file_name_create(rt->mem_pool, &file_name, "%s%s*.unit.so%Z", 733251Sigor@sysoev.ru rt->modules, slash); 734233Sigor@sysoev.ru if (nxt_slow_path(ret != NXT_OK)) { 735233Sigor@sysoev.ru return NXT_ERROR; 736233Sigor@sysoev.ru } 737233Sigor@sysoev.ru 738233Sigor@sysoev.ru rt->modules = (char *) file_name.start; 739233Sigor@sysoev.ru 740314Svbart@nginx.com slash = ""; 741314Svbart@nginx.com n = nxt_strlen(rt->state); 742314Svbart@nginx.com 743314Svbart@nginx.com if (n > 1 && rt->state[n - 1] != '/') { 744314Svbart@nginx.com slash = "/"; 745314Svbart@nginx.com } 746314Svbart@nginx.com 747314Svbart@nginx.com ret = nxt_file_name_create(rt->mem_pool, &file_name, "%s%sconf.json%Z", 748314Svbart@nginx.com rt->state, slash); 749314Svbart@nginx.com if (nxt_slow_path(ret != NXT_OK)) { 750314Svbart@nginx.com return NXT_ERROR; 751314Svbart@nginx.com } 752314Svbart@nginx.com 753314Svbart@nginx.com rt->conf = (char *) file_name.start; 754314Svbart@nginx.com 755314Svbart@nginx.com ret = nxt_file_name_create(rt->mem_pool, &file_name, "%s.tmp%Z", rt->conf); 756314Svbart@nginx.com if (nxt_slow_path(ret != NXT_OK)) { 757314Svbart@nginx.com return NXT_ERROR; 758314Svbart@nginx.com } 759314Svbart@nginx.com 760314Svbart@nginx.com rt->conf_tmp = (char *) file_name.start; 761314Svbart@nginx.com 762234Sigor@sysoev.ru control.length = nxt_strlen(rt->control); 763234Sigor@sysoev.ru control.start = (u_char *) rt->control; 764234Sigor@sysoev.ru 765649Svbart@nginx.com sa = nxt_sockaddr_parse(rt->mem_pool, &control); 766234Sigor@sysoev.ru if (nxt_slow_path(sa == NULL)) { 767234Sigor@sysoev.ru return NXT_ERROR; 768234Sigor@sysoev.ru } 769234Sigor@sysoev.ru 770649Svbart@nginx.com sa->type = SOCK_STREAM; 771649Svbart@nginx.com 772234Sigor@sysoev.ru rt->controller_listen = sa; 773234Sigor@sysoev.ru 774234Sigor@sysoev.ru if (nxt_runtime_controller_socket(task, rt) != NXT_OK) { 775234Sigor@sysoev.ru return NXT_ERROR; 776234Sigor@sysoev.ru } 777234Sigor@sysoev.ru 77820Sigor@sysoev.ru return NXT_OK; 77920Sigor@sysoev.ru } 78020Sigor@sysoev.ru 78120Sigor@sysoev.ru 78220Sigor@sysoev.ru static nxt_int_t 78320Sigor@sysoev.ru nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt) 78420Sigor@sysoev.ru { 785234Sigor@sysoev.ru char *p, **argv; 786234Sigor@sysoev.ru u_char *end; 787234Sigor@sysoev.ru u_char buf[1024]; 78820Sigor@sysoev.ru 789226Svbart@nginx.com static const char version[] = 790259Sigor@sysoev.ru "unit version: " NXT_VERSION "\n" 791221Sigor@sysoev.ru "configured as ./configure" NXT_CONFIGURE_OPTIONS "\n"; 792221Sigor@sysoev.ru 793234Sigor@sysoev.ru static const char no_control[] = 794234Sigor@sysoev.ru "option \"--control\" requires socket address\n"; 795232Sigor@sysoev.ru static const char no_user[] = "option \"--user\" requires username\n"; 796232Sigor@sysoev.ru static const char no_group[] = "option \"--group\" requires group name\n"; 797231Sigor@sysoev.ru static const char no_pid[] = "option \"--pid\" requires filename\n"; 798230Sigor@sysoev.ru static const char no_log[] = "option \"--log\" requires filename\n"; 799233Sigor@sysoev.ru static const char no_modules[] = 800233Sigor@sysoev.ru "option \"--modules\" requires directory\n"; 801314Svbart@nginx.com static const char no_state[] = "option \"--state\" requires directory\n"; 802230Sigor@sysoev.ru 803235Sigor@sysoev.ru static const char help[] = 804235Sigor@sysoev.ru "\n" 805259Sigor@sysoev.ru "unit options:\n" 806235Sigor@sysoev.ru "\n" 807259Sigor@sysoev.ru " --version print unit version and configure options\n" 808235Sigor@sysoev.ru "\n" 809259Sigor@sysoev.ru " --no-daemon run unit in non-daemon mode\n" 810235Sigor@sysoev.ru "\n" 811235Sigor@sysoev.ru " --control ADDRESS set address of control API socket\n" 812235Sigor@sysoev.ru " default: \"" NXT_CONTROL_SOCK "\"\n" 813235Sigor@sysoev.ru "\n" 814239Sigor@sysoev.ru " --pid FILE set pid filename\n" 815235Sigor@sysoev.ru " default: \"" NXT_PID "\"\n" 816235Sigor@sysoev.ru "\n" 817239Sigor@sysoev.ru " --log FILE set log filename\n" 818235Sigor@sysoev.ru " default: \"" NXT_LOG "\"\n" 819235Sigor@sysoev.ru "\n" 820239Sigor@sysoev.ru " --modules DIRECTORY set modules directory name\n" 821235Sigor@sysoev.ru " default: \"" NXT_MODULES "\"\n" 822235Sigor@sysoev.ru "\n" 823314Svbart@nginx.com " --state DIRECTORY set state directory name\n" 824314Svbart@nginx.com " default: \"" NXT_STATE "\"\n" 825314Svbart@nginx.com "\n" 826235Sigor@sysoev.ru " --user USER set non-privileged processes to run" 827235Sigor@sysoev.ru " as specified user\n" 828235Sigor@sysoev.ru " default: \"" NXT_USER "\"\n" 829235Sigor@sysoev.ru "\n" 830235Sigor@sysoev.ru " --group GROUP set non-privileged processes to run" 831235Sigor@sysoev.ru " as specified group\n" 832235Sigor@sysoev.ru " default: "; 833235Sigor@sysoev.ru 834235Sigor@sysoev.ru static const char group[] = "\"" NXT_GROUP "\"\n\n"; 835235Sigor@sysoev.ru static const char primary[] = "user's primary group\n\n"; 836235Sigor@sysoev.ru 837222Sigor@sysoev.ru argv = &nxt_process_argv[1]; 83820Sigor@sysoev.ru 83920Sigor@sysoev.ru while (*argv != NULL) { 84020Sigor@sysoev.ru p = *argv++; 84120Sigor@sysoev.ru 842234Sigor@sysoev.ru if (nxt_strcmp(p, "--control") == 0) { 84320Sigor@sysoev.ru if (*argv == NULL) { 844234Sigor@sysoev.ru write(STDERR_FILENO, no_control, sizeof(no_control) - 1); 84520Sigor@sysoev.ru return NXT_ERROR; 84620Sigor@sysoev.ru } 84720Sigor@sysoev.ru 84820Sigor@sysoev.ru p = *argv++; 84920Sigor@sysoev.ru 850234Sigor@sysoev.ru rt->control = p; 85120Sigor@sysoev.ru 85220Sigor@sysoev.ru continue; 85320Sigor@sysoev.ru } 85420Sigor@sysoev.ru 85520Sigor@sysoev.ru if (nxt_strcmp(p, "--upstream") == 0) { 85620Sigor@sysoev.ru if (*argv == NULL) { 857564Svbart@nginx.com nxt_alert(task, "no argument for option \"--upstream\""); 85820Sigor@sysoev.ru return NXT_ERROR; 85920Sigor@sysoev.ru } 86020Sigor@sysoev.ru 86120Sigor@sysoev.ru p = *argv++; 86220Sigor@sysoev.ru 86320Sigor@sysoev.ru rt->upstream.length = nxt_strlen(p); 86420Sigor@sysoev.ru rt->upstream.start = (u_char *) p; 86520Sigor@sysoev.ru 86620Sigor@sysoev.ru continue; 86720Sigor@sysoev.ru } 86820Sigor@sysoev.ru 86920Sigor@sysoev.ru if (nxt_strcmp(p, "--user") == 0) { 87020Sigor@sysoev.ru if (*argv == NULL) { 871232Sigor@sysoev.ru write(STDERR_FILENO, no_user, sizeof(no_user) - 1); 87220Sigor@sysoev.ru return NXT_ERROR; 87320Sigor@sysoev.ru } 87420Sigor@sysoev.ru 87520Sigor@sysoev.ru p = *argv++; 87620Sigor@sysoev.ru 87720Sigor@sysoev.ru rt->user_cred.user = p; 87820Sigor@sysoev.ru 87920Sigor@sysoev.ru continue; 88020Sigor@sysoev.ru } 88120Sigor@sysoev.ru 88220Sigor@sysoev.ru if (nxt_strcmp(p, "--group") == 0) { 88320Sigor@sysoev.ru if (*argv == NULL) { 884232Sigor@sysoev.ru write(STDERR_FILENO, no_group, sizeof(no_group) - 1); 88520Sigor@sysoev.ru return NXT_ERROR; 88620Sigor@sysoev.ru } 88720Sigor@sysoev.ru 88820Sigor@sysoev.ru p = *argv++; 88920Sigor@sysoev.ru 89020Sigor@sysoev.ru rt->group = p; 89120Sigor@sysoev.ru 89220Sigor@sysoev.ru continue; 89320Sigor@sysoev.ru } 89420Sigor@sysoev.ru 89520Sigor@sysoev.ru if (nxt_strcmp(p, "--pid") == 0) { 89620Sigor@sysoev.ru if (*argv == NULL) { 897231Sigor@sysoev.ru write(STDERR_FILENO, no_pid, sizeof(no_pid) - 1); 89820Sigor@sysoev.ru return NXT_ERROR; 89920Sigor@sysoev.ru } 90020Sigor@sysoev.ru 90120Sigor@sysoev.ru p = *argv++; 90220Sigor@sysoev.ru 90320Sigor@sysoev.ru rt->pid = p; 90420Sigor@sysoev.ru 90520Sigor@sysoev.ru continue; 90620Sigor@sysoev.ru } 90720Sigor@sysoev.ru 90820Sigor@sysoev.ru if (nxt_strcmp(p, "--log") == 0) { 90920Sigor@sysoev.ru if (*argv == NULL) { 910230Sigor@sysoev.ru write(STDERR_FILENO, no_log, sizeof(no_log) - 1); 91120Sigor@sysoev.ru return NXT_ERROR; 91220Sigor@sysoev.ru } 91320Sigor@sysoev.ru 91420Sigor@sysoev.ru p = *argv++; 91520Sigor@sysoev.ru 916230Sigor@sysoev.ru rt->log = p; 91720Sigor@sysoev.ru 91820Sigor@sysoev.ru continue; 91920Sigor@sysoev.ru } 92020Sigor@sysoev.ru 921233Sigor@sysoev.ru if (nxt_strcmp(p, "--modules") == 0) { 922233Sigor@sysoev.ru if (*argv == NULL) { 923233Sigor@sysoev.ru write(STDERR_FILENO, no_modules, sizeof(no_modules) - 1); 924233Sigor@sysoev.ru return NXT_ERROR; 925233Sigor@sysoev.ru } 926233Sigor@sysoev.ru 927233Sigor@sysoev.ru p = *argv++; 928233Sigor@sysoev.ru 929233Sigor@sysoev.ru rt->modules = p; 930233Sigor@sysoev.ru 931233Sigor@sysoev.ru continue; 932233Sigor@sysoev.ru } 933233Sigor@sysoev.ru 934314Svbart@nginx.com if (nxt_strcmp(p, "--state") == 0) { 935314Svbart@nginx.com if (*argv == NULL) { 936314Svbart@nginx.com write(STDERR_FILENO, no_state, sizeof(no_state) - 1); 937314Svbart@nginx.com return NXT_ERROR; 938314Svbart@nginx.com } 939314Svbart@nginx.com 940314Svbart@nginx.com p = *argv++; 941314Svbart@nginx.com 942314Svbart@nginx.com rt->state = p; 943314Svbart@nginx.com 944314Svbart@nginx.com continue; 945314Svbart@nginx.com } 946314Svbart@nginx.com 947219Sigor@sysoev.ru if (nxt_strcmp(p, "--no-daemon") == 0) { 94820Sigor@sysoev.ru rt->daemon = 0; 94920Sigor@sysoev.ru continue; 95020Sigor@sysoev.ru } 951221Sigor@sysoev.ru 952221Sigor@sysoev.ru if (nxt_strcmp(p, "--version") == 0) { 953221Sigor@sysoev.ru write(STDERR_FILENO, version, sizeof(version) - 1); 954221Sigor@sysoev.ru exit(0); 955221Sigor@sysoev.ru } 956222Sigor@sysoev.ru 957643Svbart@nginx.com if (nxt_strcmp(p, "--help") == 0 || nxt_strcmp(p, "-h") == 0) { 958235Sigor@sysoev.ru write(STDOUT_FILENO, help, sizeof(help) - 1); 959235Sigor@sysoev.ru 960235Sigor@sysoev.ru if (sizeof(NXT_GROUP) == 1) { 961235Sigor@sysoev.ru write(STDOUT_FILENO, primary, sizeof(primary) - 1); 962235Sigor@sysoev.ru 963235Sigor@sysoev.ru } else { 964235Sigor@sysoev.ru write(STDOUT_FILENO, group, sizeof(group) - 1); 965235Sigor@sysoev.ru } 966235Sigor@sysoev.ru 967235Sigor@sysoev.ru exit(0); 968235Sigor@sysoev.ru } 969235Sigor@sysoev.ru 970643Svbart@nginx.com end = nxt_sprintf(buf, buf + sizeof(buf), "unknown option \"%s\", " 971643Svbart@nginx.com "try \"%s -h\" for available options\n", 972643Svbart@nginx.com p, nxt_process_argv[0]); 973643Svbart@nginx.com 974222Sigor@sysoev.ru write(STDERR_FILENO, buf, end - buf); 975222Sigor@sysoev.ru 976222Sigor@sysoev.ru return NXT_ERROR; 97720Sigor@sysoev.ru } 97820Sigor@sysoev.ru 97920Sigor@sysoev.ru return NXT_OK; 98020Sigor@sysoev.ru } 98120Sigor@sysoev.ru 98220Sigor@sysoev.ru 98320Sigor@sysoev.ru nxt_listen_socket_t * 98420Sigor@sysoev.ru nxt_runtime_listen_socket_add(nxt_runtime_t *rt, nxt_sockaddr_t *sa) 98520Sigor@sysoev.ru { 98665Sigor@sysoev.ru nxt_mp_t *mp; 98720Sigor@sysoev.ru nxt_listen_socket_t *ls; 98820Sigor@sysoev.ru 98920Sigor@sysoev.ru ls = nxt_array_zero_add(rt->listen_sockets); 99020Sigor@sysoev.ru if (ls == NULL) { 99120Sigor@sysoev.ru return NULL; 99220Sigor@sysoev.ru } 99320Sigor@sysoev.ru 99420Sigor@sysoev.ru mp = rt->mem_pool; 99520Sigor@sysoev.ru 99620Sigor@sysoev.ru ls->sockaddr = nxt_sockaddr_create(mp, &sa->u.sockaddr, sa->socklen, 99720Sigor@sysoev.ru sa->length); 99820Sigor@sysoev.ru if (ls->sockaddr == NULL) { 99920Sigor@sysoev.ru return NULL; 100020Sigor@sysoev.ru } 100120Sigor@sysoev.ru 100220Sigor@sysoev.ru ls->sockaddr->type = sa->type; 100320Sigor@sysoev.ru 100420Sigor@sysoev.ru nxt_sockaddr_text(ls->sockaddr); 100520Sigor@sysoev.ru 100620Sigor@sysoev.ru ls->socket = -1; 100720Sigor@sysoev.ru ls->backlog = NXT_LISTEN_BACKLOG; 100820Sigor@sysoev.ru 100920Sigor@sysoev.ru return ls; 101020Sigor@sysoev.ru } 101120Sigor@sysoev.ru 101220Sigor@sysoev.ru 101320Sigor@sysoev.ru static nxt_int_t 101420Sigor@sysoev.ru nxt_runtime_hostname(nxt_task_t *task, nxt_runtime_t *rt) 101520Sigor@sysoev.ru { 101620Sigor@sysoev.ru size_t length; 101720Sigor@sysoev.ru char hostname[NXT_MAXHOSTNAMELEN + 1]; 101820Sigor@sysoev.ru 101920Sigor@sysoev.ru if (gethostname(hostname, NXT_MAXHOSTNAMELEN) != 0) { 1020564Svbart@nginx.com nxt_alert(task, "gethostname() failed %E", nxt_errno); 102120Sigor@sysoev.ru return NXT_ERROR; 102220Sigor@sysoev.ru } 102320Sigor@sysoev.ru 102420Sigor@sysoev.ru /* 102520Sigor@sysoev.ru * Linux gethostname(2): 102620Sigor@sysoev.ru * 102720Sigor@sysoev.ru * If the null-terminated hostname is too large to fit, 102820Sigor@sysoev.ru * then the name is truncated, and no error is returned. 102920Sigor@sysoev.ru * 103020Sigor@sysoev.ru * For this reason an additional byte is reserved in the buffer. 103120Sigor@sysoev.ru */ 103220Sigor@sysoev.ru hostname[NXT_MAXHOSTNAMELEN] = '\0'; 103320Sigor@sysoev.ru 103420Sigor@sysoev.ru length = nxt_strlen(hostname); 103520Sigor@sysoev.ru rt->hostname.length = length; 103620Sigor@sysoev.ru 103765Sigor@sysoev.ru rt->hostname.start = nxt_mp_nget(rt->mem_pool, length); 103820Sigor@sysoev.ru 103920Sigor@sysoev.ru if (rt->hostname.start != NULL) { 104020Sigor@sysoev.ru nxt_memcpy_lowcase(rt->hostname.start, (u_char *) hostname, length); 104120Sigor@sysoev.ru return NXT_OK; 104220Sigor@sysoev.ru } 104320Sigor@sysoev.ru 104420Sigor@sysoev.ru return NXT_ERROR; 104520Sigor@sysoev.ru } 104620Sigor@sysoev.ru 104720Sigor@sysoev.ru 104820Sigor@sysoev.ru static nxt_int_t 104920Sigor@sysoev.ru nxt_runtime_log_files_init(nxt_runtime_t *rt) 105020Sigor@sysoev.ru { 105120Sigor@sysoev.ru nxt_file_t *file; 105220Sigor@sysoev.ru nxt_list_t *log_files; 105320Sigor@sysoev.ru 105420Sigor@sysoev.ru log_files = nxt_list_create(rt->mem_pool, 1, sizeof(nxt_file_t)); 105520Sigor@sysoev.ru 105620Sigor@sysoev.ru if (nxt_fast_path(log_files != NULL)) { 105720Sigor@sysoev.ru rt->log_files = log_files; 105820Sigor@sysoev.ru 1059230Sigor@sysoev.ru /* Preallocate the main log. This allocation cannot fail. */ 106020Sigor@sysoev.ru file = nxt_list_zero_add(log_files); 106120Sigor@sysoev.ru 106220Sigor@sysoev.ru file->fd = NXT_FILE_INVALID; 1063564Svbart@nginx.com file->log_level = NXT_LOG_ALERT; 106420Sigor@sysoev.ru 106520Sigor@sysoev.ru return NXT_OK; 106620Sigor@sysoev.ru } 106720Sigor@sysoev.ru 106820Sigor@sysoev.ru return NXT_ERROR; 106920Sigor@sysoev.ru } 107020Sigor@sysoev.ru 107120Sigor@sysoev.ru 107220Sigor@sysoev.ru nxt_file_t * 107320Sigor@sysoev.ru nxt_runtime_log_file_add(nxt_runtime_t *rt, nxt_str_t *name) 107420Sigor@sysoev.ru { 107520Sigor@sysoev.ru nxt_int_t ret; 107620Sigor@sysoev.ru nxt_file_t *file; 107720Sigor@sysoev.ru nxt_file_name_str_t file_name; 107820Sigor@sysoev.ru 1079230Sigor@sysoev.ru ret = nxt_file_name_create(rt->mem_pool, &file_name, "V%Z", name); 108020Sigor@sysoev.ru 108120Sigor@sysoev.ru if (nxt_slow_path(ret != NXT_OK)) { 108220Sigor@sysoev.ru return NULL; 108320Sigor@sysoev.ru } 108420Sigor@sysoev.ru 108520Sigor@sysoev.ru nxt_list_each(file, rt->log_files) { 108620Sigor@sysoev.ru 108720Sigor@sysoev.ru /* STUB: hardecoded case sensitive/insensitive. */ 108820Sigor@sysoev.ru 108920Sigor@sysoev.ru if (file->name != NULL 109020Sigor@sysoev.ru && nxt_file_name_eq(file->name, file_name.start)) 109120Sigor@sysoev.ru { 109220Sigor@sysoev.ru return file; 109320Sigor@sysoev.ru } 109420Sigor@sysoev.ru 109520Sigor@sysoev.ru } nxt_list_loop; 109620Sigor@sysoev.ru 109720Sigor@sysoev.ru file = nxt_list_zero_add(rt->log_files); 109820Sigor@sysoev.ru 109920Sigor@sysoev.ru if (nxt_slow_path(file == NULL)) { 110020Sigor@sysoev.ru return NULL; 110120Sigor@sysoev.ru } 110220Sigor@sysoev.ru 110320Sigor@sysoev.ru file->fd = NXT_FILE_INVALID; 1104564Svbart@nginx.com file->log_level = NXT_LOG_ALERT; 110520Sigor@sysoev.ru file->name = file_name.start; 110620Sigor@sysoev.ru 110720Sigor@sysoev.ru return file; 110820Sigor@sysoev.ru } 110920Sigor@sysoev.ru 111020Sigor@sysoev.ru 111120Sigor@sysoev.ru static nxt_int_t 111220Sigor@sysoev.ru nxt_runtime_log_files_create(nxt_task_t *task, nxt_runtime_t *rt) 111320Sigor@sysoev.ru { 111420Sigor@sysoev.ru nxt_int_t ret; 111520Sigor@sysoev.ru nxt_file_t *file; 111620Sigor@sysoev.ru 111720Sigor@sysoev.ru nxt_list_each(file, rt->log_files) { 111820Sigor@sysoev.ru 111920Sigor@sysoev.ru ret = nxt_file_open(task, file, O_WRONLY | O_APPEND, O_CREAT, 112020Sigor@sysoev.ru NXT_FILE_OWNER_ACCESS); 112120Sigor@sysoev.ru 112220Sigor@sysoev.ru if (ret != NXT_OK) { 112320Sigor@sysoev.ru return NXT_ERROR; 112420Sigor@sysoev.ru } 112520Sigor@sysoev.ru 112620Sigor@sysoev.ru } nxt_list_loop; 112720Sigor@sysoev.ru 112820Sigor@sysoev.ru file = nxt_list_first(rt->log_files); 112920Sigor@sysoev.ru 113020Sigor@sysoev.ru return nxt_file_stderr(file); 113120Sigor@sysoev.ru } 113220Sigor@sysoev.ru 113320Sigor@sysoev.ru 113420Sigor@sysoev.ru nxt_int_t 113520Sigor@sysoev.ru nxt_runtime_listen_sockets_create(nxt_task_t *task, nxt_runtime_t *rt) 113620Sigor@sysoev.ru { 113720Sigor@sysoev.ru nxt_int_t ret; 113820Sigor@sysoev.ru nxt_uint_t c, p, ncurr, nprev; 113920Sigor@sysoev.ru nxt_listen_socket_t *curr, *prev; 114020Sigor@sysoev.ru 114120Sigor@sysoev.ru curr = rt->listen_sockets->elts; 114220Sigor@sysoev.ru ncurr = rt->listen_sockets->nelts; 114320Sigor@sysoev.ru 114420Sigor@sysoev.ru if (rt->inherited_sockets != NULL) { 114520Sigor@sysoev.ru prev = rt->inherited_sockets->elts; 114620Sigor@sysoev.ru nprev = rt->inherited_sockets->nelts; 114720Sigor@sysoev.ru 114820Sigor@sysoev.ru } else { 114920Sigor@sysoev.ru prev = NULL; 115020Sigor@sysoev.ru nprev = 0; 115120Sigor@sysoev.ru } 115220Sigor@sysoev.ru 115320Sigor@sysoev.ru for (c = 0; c < ncurr; c++) { 115420Sigor@sysoev.ru 115520Sigor@sysoev.ru for (p = 0; p < nprev; p++) { 115620Sigor@sysoev.ru 115720Sigor@sysoev.ru if (nxt_sockaddr_cmp(curr[c].sockaddr, prev[p].sockaddr)) { 115820Sigor@sysoev.ru 115920Sigor@sysoev.ru ret = nxt_listen_socket_update(task, &curr[c], &prev[p]); 116020Sigor@sysoev.ru if (ret != NXT_OK) { 116120Sigor@sysoev.ru return NXT_ERROR; 116220Sigor@sysoev.ru } 116320Sigor@sysoev.ru 116420Sigor@sysoev.ru goto next; 116520Sigor@sysoev.ru } 116620Sigor@sysoev.ru } 116720Sigor@sysoev.ru 116820Sigor@sysoev.ru if (nxt_listen_socket_create(task, &curr[c], 0) != NXT_OK) { 116920Sigor@sysoev.ru return NXT_ERROR; 117020Sigor@sysoev.ru } 117120Sigor@sysoev.ru 117220Sigor@sysoev.ru next: 117320Sigor@sysoev.ru 117420Sigor@sysoev.ru continue; 117520Sigor@sysoev.ru } 117620Sigor@sysoev.ru 117720Sigor@sysoev.ru return NXT_OK; 117820Sigor@sysoev.ru } 117920Sigor@sysoev.ru 118020Sigor@sysoev.ru 118120Sigor@sysoev.ru nxt_int_t 118220Sigor@sysoev.ru nxt_runtime_listen_sockets_enable(nxt_task_t *task, nxt_runtime_t *rt) 118320Sigor@sysoev.ru { 118420Sigor@sysoev.ru nxt_uint_t i, n; 118520Sigor@sysoev.ru nxt_listen_socket_t *ls; 118620Sigor@sysoev.ru 118720Sigor@sysoev.ru ls = rt->listen_sockets->elts; 118820Sigor@sysoev.ru n = rt->listen_sockets->nelts; 118920Sigor@sysoev.ru 119020Sigor@sysoev.ru for (i = 0; i < n; i++) { 119120Sigor@sysoev.ru if (ls[i].flags == NXT_NONBLOCK) { 119254Sigor@sysoev.ru if (nxt_listen_event(task, &ls[i]) == NULL) { 119320Sigor@sysoev.ru return NXT_ERROR; 119420Sigor@sysoev.ru } 119520Sigor@sysoev.ru } 119620Sigor@sysoev.ru } 119720Sigor@sysoev.ru 119820Sigor@sysoev.ru return NXT_OK; 119920Sigor@sysoev.ru } 120020Sigor@sysoev.ru 120120Sigor@sysoev.ru 120220Sigor@sysoev.ru nxt_str_t * 120365Sigor@sysoev.ru nxt_current_directory(nxt_mp_t *mp) 120420Sigor@sysoev.ru { 120520Sigor@sysoev.ru size_t length; 120620Sigor@sysoev.ru u_char *p; 120720Sigor@sysoev.ru nxt_str_t *name; 120820Sigor@sysoev.ru char buf[NXT_MAX_PATH_LEN]; 120920Sigor@sysoev.ru 121020Sigor@sysoev.ru length = nxt_dir_current(buf, NXT_MAX_PATH_LEN); 121120Sigor@sysoev.ru 121220Sigor@sysoev.ru if (nxt_fast_path(length != 0)) { 121320Sigor@sysoev.ru name = nxt_str_alloc(mp, length + 1); 121420Sigor@sysoev.ru 121520Sigor@sysoev.ru if (nxt_fast_path(name != NULL)) { 121620Sigor@sysoev.ru p = nxt_cpymem(name->start, buf, length); 121720Sigor@sysoev.ru *p = '/'; 121820Sigor@sysoev.ru 121920Sigor@sysoev.ru return name; 122020Sigor@sysoev.ru } 122120Sigor@sysoev.ru } 122220Sigor@sysoev.ru 122320Sigor@sysoev.ru return NULL; 122420Sigor@sysoev.ru } 122520Sigor@sysoev.ru 122620Sigor@sysoev.ru 122720Sigor@sysoev.ru static nxt_int_t 122820Sigor@sysoev.ru nxt_runtime_pid_file_create(nxt_task_t *task, nxt_file_name_t *pid_file) 122920Sigor@sysoev.ru { 123020Sigor@sysoev.ru ssize_t length; 123120Sigor@sysoev.ru nxt_int_t n; 123220Sigor@sysoev.ru nxt_file_t file; 123320Sigor@sysoev.ru u_char pid[NXT_INT64_T_LEN + NXT_LINEFEED_SIZE]; 123420Sigor@sysoev.ru 123520Sigor@sysoev.ru nxt_memzero(&file, sizeof(nxt_file_t)); 123620Sigor@sysoev.ru 123720Sigor@sysoev.ru file.name = pid_file; 123820Sigor@sysoev.ru 123920Sigor@sysoev.ru n = nxt_file_open(task, &file, O_WRONLY, O_CREAT | O_TRUNC, 124020Sigor@sysoev.ru NXT_FILE_DEFAULT_ACCESS); 124120Sigor@sysoev.ru 124220Sigor@sysoev.ru if (n != NXT_OK) { 124320Sigor@sysoev.ru return NXT_ERROR; 124420Sigor@sysoev.ru } 124520Sigor@sysoev.ru 124620Sigor@sysoev.ru length = nxt_sprintf(pid, pid + sizeof(pid), "%PI%n", nxt_pid) - pid; 124720Sigor@sysoev.ru 124820Sigor@sysoev.ru if (nxt_file_write(&file, pid, length, 0) != length) { 124920Sigor@sysoev.ru return NXT_ERROR; 125020Sigor@sysoev.ru } 125120Sigor@sysoev.ru 125220Sigor@sysoev.ru nxt_file_close(task, &file); 125320Sigor@sysoev.ru 125420Sigor@sysoev.ru return NXT_OK; 125520Sigor@sysoev.ru } 125620Sigor@sysoev.ru 125720Sigor@sysoev.ru 125820Sigor@sysoev.ru nxt_process_t * 125942Smax.romanov@nginx.com nxt_runtime_process_new(nxt_runtime_t *rt) 126020Sigor@sysoev.ru { 126120Sigor@sysoev.ru nxt_process_t *process; 126220Sigor@sysoev.ru 126320Sigor@sysoev.ru /* TODO: memory failures. */ 126420Sigor@sysoev.ru 126565Sigor@sysoev.ru process = nxt_mp_zalloc(rt->mem_pool, sizeof(nxt_process_t)); 126642Smax.romanov@nginx.com if (nxt_slow_path(process == NULL)) { 126742Smax.romanov@nginx.com return NULL; 126842Smax.romanov@nginx.com } 126942Smax.romanov@nginx.com 127042Smax.romanov@nginx.com nxt_queue_init(&process->ports); 127142Smax.romanov@nginx.com 1272364Smax.romanov@nginx.com nxt_thread_mutex_create(&process->incoming.mutex); 1273364Smax.romanov@nginx.com nxt_thread_mutex_create(&process->outgoing.mutex); 1274141Smax.romanov@nginx.com nxt_thread_mutex_create(&process->cp_mutex); 127590Smax.romanov@nginx.com 1276349Smax.romanov@nginx.com process->use_count = 1; 1277349Smax.romanov@nginx.com 127842Smax.romanov@nginx.com return process; 127942Smax.romanov@nginx.com } 128042Smax.romanov@nginx.com 128142Smax.romanov@nginx.com 1282196Smax.romanov@nginx.com static void 1283141Smax.romanov@nginx.com nxt_runtime_process_destroy(nxt_runtime_t *rt, nxt_process_t *process) 1284141Smax.romanov@nginx.com { 1285597Sigor@sysoev.ru nxt_port_t *port; 1286341Smax.romanov@nginx.com 1287349Smax.romanov@nginx.com nxt_assert(process->use_count == 0); 1288196Smax.romanov@nginx.com nxt_assert(process->registered == 0); 1289164Smax.romanov@nginx.com 1290364Smax.romanov@nginx.com nxt_port_mmaps_destroy(&process->incoming, 1); 1291364Smax.romanov@nginx.com nxt_port_mmaps_destroy(&process->outgoing, 1); 1292141Smax.romanov@nginx.com 1293597Sigor@sysoev.ru do { 1294597Sigor@sysoev.ru port = nxt_port_hash_retrieve(&process->connected_ports); 1295597Sigor@sysoev.ru 1296597Sigor@sysoev.ru } while (port != NULL); 1297141Smax.romanov@nginx.com 1298364Smax.romanov@nginx.com nxt_thread_mutex_destroy(&process->incoming.mutex); 1299364Smax.romanov@nginx.com nxt_thread_mutex_destroy(&process->outgoing.mutex); 1300141Smax.romanov@nginx.com nxt_thread_mutex_destroy(&process->cp_mutex); 1301141Smax.romanov@nginx.com 1302141Smax.romanov@nginx.com nxt_mp_free(rt->mem_pool, process); 1303141Smax.romanov@nginx.com } 1304141Smax.romanov@nginx.com 1305141Smax.romanov@nginx.com 130642Smax.romanov@nginx.com static nxt_int_t 130742Smax.romanov@nginx.com nxt_runtime_lvlhsh_pid_test(nxt_lvlhsh_query_t *lhq, void *data) 130842Smax.romanov@nginx.com { 130942Smax.romanov@nginx.com nxt_process_t *process; 131042Smax.romanov@nginx.com 131142Smax.romanov@nginx.com process = data; 131242Smax.romanov@nginx.com 1313277Sigor@sysoev.ru if (lhq->key.length == sizeof(nxt_pid_t) 1314277Sigor@sysoev.ru && *(nxt_pid_t *) lhq->key.start == process->pid) 1315277Sigor@sysoev.ru { 131642Smax.romanov@nginx.com return NXT_OK; 131742Smax.romanov@nginx.com } 131842Smax.romanov@nginx.com 131942Smax.romanov@nginx.com return NXT_DECLINED; 132042Smax.romanov@nginx.com } 132142Smax.romanov@nginx.com 132242Smax.romanov@nginx.com static const nxt_lvlhsh_proto_t lvlhsh_processes_proto nxt_aligned(64) = { 132342Smax.romanov@nginx.com NXT_LVLHSH_DEFAULT, 132442Smax.romanov@nginx.com nxt_runtime_lvlhsh_pid_test, 132542Smax.romanov@nginx.com nxt_lvlhsh_alloc, 132642Smax.romanov@nginx.com nxt_lvlhsh_free, 132742Smax.romanov@nginx.com }; 132842Smax.romanov@nginx.com 132942Smax.romanov@nginx.com 1330196Smax.romanov@nginx.com nxt_inline void 1331196Smax.romanov@nginx.com nxt_runtime_process_lhq_pid(nxt_lvlhsh_query_t *lhq, nxt_pid_t *pid) 1332196Smax.romanov@nginx.com { 1333196Smax.romanov@nginx.com lhq->key_hash = nxt_murmur_hash2(pid, sizeof(*pid)); 1334196Smax.romanov@nginx.com lhq->key.length = sizeof(*pid); 1335196Smax.romanov@nginx.com lhq->key.start = (u_char *) pid; 1336196Smax.romanov@nginx.com lhq->proto = &lvlhsh_processes_proto; 1337196Smax.romanov@nginx.com } 1338196Smax.romanov@nginx.com 1339196Smax.romanov@nginx.com 134042Smax.romanov@nginx.com nxt_process_t * 134142Smax.romanov@nginx.com nxt_runtime_process_find(nxt_runtime_t *rt, nxt_pid_t pid) 134242Smax.romanov@nginx.com { 1343196Smax.romanov@nginx.com nxt_process_t *process; 134442Smax.romanov@nginx.com nxt_lvlhsh_query_t lhq; 134542Smax.romanov@nginx.com 1346196Smax.romanov@nginx.com process = NULL; 1347196Smax.romanov@nginx.com 1348196Smax.romanov@nginx.com nxt_runtime_process_lhq_pid(&lhq, &pid); 1349196Smax.romanov@nginx.com 1350196Smax.romanov@nginx.com nxt_thread_mutex_lock(&rt->processes_mutex); 135142Smax.romanov@nginx.com 135242Smax.romanov@nginx.com if (nxt_lvlhsh_find(&rt->processes, &lhq) == NXT_OK) { 1353196Smax.romanov@nginx.com process = lhq.value; 1354196Smax.romanov@nginx.com 1355196Smax.romanov@nginx.com } else { 1356196Smax.romanov@nginx.com nxt_thread_log_debug("process %PI not found", pid); 135742Smax.romanov@nginx.com } 135842Smax.romanov@nginx.com 1359196Smax.romanov@nginx.com nxt_thread_mutex_unlock(&rt->processes_mutex); 136042Smax.romanov@nginx.com 1361196Smax.romanov@nginx.com return process; 136242Smax.romanov@nginx.com } 136342Smax.romanov@nginx.com 136442Smax.romanov@nginx.com 136542Smax.romanov@nginx.com nxt_process_t * 136642Smax.romanov@nginx.com nxt_runtime_process_get(nxt_runtime_t *rt, nxt_pid_t pid) 136742Smax.romanov@nginx.com { 136842Smax.romanov@nginx.com nxt_process_t *process; 136942Smax.romanov@nginx.com nxt_lvlhsh_query_t lhq; 137042Smax.romanov@nginx.com 1371196Smax.romanov@nginx.com nxt_runtime_process_lhq_pid(&lhq, &pid); 1372196Smax.romanov@nginx.com 1373196Smax.romanov@nginx.com nxt_thread_mutex_lock(&rt->processes_mutex); 137442Smax.romanov@nginx.com 137542Smax.romanov@nginx.com if (nxt_lvlhsh_find(&rt->processes, &lhq) == NXT_OK) { 137642Smax.romanov@nginx.com nxt_thread_log_debug("process %PI found", pid); 1377196Smax.romanov@nginx.com 1378196Smax.romanov@nginx.com nxt_thread_mutex_unlock(&rt->processes_mutex); 1379349Smax.romanov@nginx.com 1380349Smax.romanov@nginx.com process = lhq.value; 1381349Smax.romanov@nginx.com process->use_count++; 1382349Smax.romanov@nginx.com 1383349Smax.romanov@nginx.com return process; 138442Smax.romanov@nginx.com } 138542Smax.romanov@nginx.com 138642Smax.romanov@nginx.com process = nxt_runtime_process_new(rt); 138720Sigor@sysoev.ru if (nxt_slow_path(process == NULL)) { 1388382Smax.romanov@nginx.com 1389382Smax.romanov@nginx.com nxt_thread_mutex_unlock(&rt->processes_mutex); 1390382Smax.romanov@nginx.com 139120Sigor@sysoev.ru return NULL; 139220Sigor@sysoev.ru } 139320Sigor@sysoev.ru 139442Smax.romanov@nginx.com process->pid = pid; 139542Smax.romanov@nginx.com 139642Smax.romanov@nginx.com lhq.replace = 0; 139742Smax.romanov@nginx.com lhq.value = process; 139842Smax.romanov@nginx.com lhq.pool = rt->mem_pool; 139942Smax.romanov@nginx.com 140042Smax.romanov@nginx.com switch (nxt_lvlhsh_insert(&rt->processes, &lhq)) { 140142Smax.romanov@nginx.com 140242Smax.romanov@nginx.com case NXT_OK: 140342Smax.romanov@nginx.com if (rt->nprocesses == 0) { 140442Smax.romanov@nginx.com rt->mprocess = process; 140542Smax.romanov@nginx.com } 140642Smax.romanov@nginx.com 140742Smax.romanov@nginx.com rt->nprocesses++; 140842Smax.romanov@nginx.com 1409196Smax.romanov@nginx.com process->registered = 1; 1410196Smax.romanov@nginx.com 141142Smax.romanov@nginx.com nxt_thread_log_debug("process %PI insert", pid); 141242Smax.romanov@nginx.com break; 141342Smax.romanov@nginx.com 141442Smax.romanov@nginx.com default: 141542Smax.romanov@nginx.com nxt_thread_log_debug("process %PI insert failed", pid); 141642Smax.romanov@nginx.com break; 141720Sigor@sysoev.ru } 141820Sigor@sysoev.ru 1419196Smax.romanov@nginx.com nxt_thread_mutex_unlock(&rt->processes_mutex); 1420196Smax.romanov@nginx.com 142120Sigor@sysoev.ru return process; 142220Sigor@sysoev.ru } 142342Smax.romanov@nginx.com 142442Smax.romanov@nginx.com 142542Smax.romanov@nginx.com void 1426343Smax.romanov@nginx.com nxt_runtime_process_add(nxt_task_t *task, nxt_process_t *process) 142742Smax.romanov@nginx.com { 142842Smax.romanov@nginx.com nxt_port_t *port; 1429343Smax.romanov@nginx.com nxt_runtime_t *rt; 143042Smax.romanov@nginx.com nxt_lvlhsh_query_t lhq; 143142Smax.romanov@nginx.com 1432196Smax.romanov@nginx.com nxt_assert(process->registered == 0); 1433196Smax.romanov@nginx.com 1434343Smax.romanov@nginx.com rt = task->thread->runtime; 1435343Smax.romanov@nginx.com 1436196Smax.romanov@nginx.com nxt_runtime_process_lhq_pid(&lhq, &process->pid); 1437196Smax.romanov@nginx.com 143842Smax.romanov@nginx.com lhq.replace = 0; 143942Smax.romanov@nginx.com lhq.value = process; 144042Smax.romanov@nginx.com lhq.pool = rt->mem_pool; 144142Smax.romanov@nginx.com 1442196Smax.romanov@nginx.com nxt_thread_mutex_lock(&rt->processes_mutex); 1443196Smax.romanov@nginx.com 144442Smax.romanov@nginx.com switch (nxt_lvlhsh_insert(&rt->processes, &lhq)) { 144542Smax.romanov@nginx.com 144642Smax.romanov@nginx.com case NXT_OK: 144742Smax.romanov@nginx.com if (rt->nprocesses == 0) { 144842Smax.romanov@nginx.com rt->mprocess = process; 144942Smax.romanov@nginx.com } 145042Smax.romanov@nginx.com 145142Smax.romanov@nginx.com rt->nprocesses++; 145242Smax.romanov@nginx.com 145342Smax.romanov@nginx.com nxt_process_port_each(process, port) { 145442Smax.romanov@nginx.com 1455141Smax.romanov@nginx.com port->pid = process->pid; 1456141Smax.romanov@nginx.com 1457343Smax.romanov@nginx.com nxt_runtime_port_add(task, port); 145842Smax.romanov@nginx.com 145942Smax.romanov@nginx.com } nxt_process_port_loop; 146042Smax.romanov@nginx.com 1461196Smax.romanov@nginx.com process->registered = 1; 1462196Smax.romanov@nginx.com 1463196Smax.romanov@nginx.com nxt_thread_log_debug("process %PI added", process->pid); 146442Smax.romanov@nginx.com break; 146542Smax.romanov@nginx.com 146642Smax.romanov@nginx.com default: 1467196Smax.romanov@nginx.com nxt_thread_log_debug("process %PI failed to add", process->pid); 146842Smax.romanov@nginx.com break; 146942Smax.romanov@nginx.com } 1470196Smax.romanov@nginx.com 1471196Smax.romanov@nginx.com nxt_thread_mutex_unlock(&rt->processes_mutex); 1472196Smax.romanov@nginx.com } 1473196Smax.romanov@nginx.com 1474196Smax.romanov@nginx.com 1475196Smax.romanov@nginx.com static nxt_process_t * 1476196Smax.romanov@nginx.com nxt_runtime_process_remove_pid(nxt_runtime_t *rt, nxt_pid_t pid) 1477196Smax.romanov@nginx.com { 1478196Smax.romanov@nginx.com nxt_process_t *process; 1479196Smax.romanov@nginx.com nxt_lvlhsh_query_t lhq; 1480196Smax.romanov@nginx.com 1481196Smax.romanov@nginx.com process = NULL; 1482196Smax.romanov@nginx.com 1483196Smax.romanov@nginx.com nxt_runtime_process_lhq_pid(&lhq, &pid); 1484196Smax.romanov@nginx.com 1485196Smax.romanov@nginx.com lhq.pool = rt->mem_pool; 1486196Smax.romanov@nginx.com 1487196Smax.romanov@nginx.com nxt_thread_mutex_lock(&rt->processes_mutex); 1488196Smax.romanov@nginx.com 1489196Smax.romanov@nginx.com switch (nxt_lvlhsh_delete(&rt->processes, &lhq)) { 1490196Smax.romanov@nginx.com 1491196Smax.romanov@nginx.com case NXT_OK: 1492196Smax.romanov@nginx.com rt->nprocesses--; 1493196Smax.romanov@nginx.com 1494196Smax.romanov@nginx.com process = lhq.value; 1495196Smax.romanov@nginx.com 1496196Smax.romanov@nginx.com process->registered = 0; 1497196Smax.romanov@nginx.com 1498196Smax.romanov@nginx.com nxt_thread_log_debug("process %PI removed", pid); 1499196Smax.romanov@nginx.com break; 1500196Smax.romanov@nginx.com 1501196Smax.romanov@nginx.com default: 1502196Smax.romanov@nginx.com nxt_thread_log_debug("process %PI remove failed", pid); 1503196Smax.romanov@nginx.com break; 1504196Smax.romanov@nginx.com } 1505196Smax.romanov@nginx.com 1506196Smax.romanov@nginx.com nxt_thread_mutex_unlock(&rt->processes_mutex); 1507196Smax.romanov@nginx.com 1508196Smax.romanov@nginx.com return process; 150942Smax.romanov@nginx.com } 151042Smax.romanov@nginx.com 151142Smax.romanov@nginx.com 151242Smax.romanov@nginx.com void 1513349Smax.romanov@nginx.com nxt_process_use(nxt_task_t *task, nxt_process_t *process, int i) 151442Smax.romanov@nginx.com { 1515343Smax.romanov@nginx.com nxt_runtime_t *rt; 1516343Smax.romanov@nginx.com 1517349Smax.romanov@nginx.com process->use_count += i; 1518349Smax.romanov@nginx.com 1519349Smax.romanov@nginx.com if (process->use_count == 0) { 1520349Smax.romanov@nginx.com rt = task->thread->runtime; 1521349Smax.romanov@nginx.com 1522196Smax.romanov@nginx.com if (process->registered == 1) { 1523196Smax.romanov@nginx.com nxt_runtime_process_remove_pid(rt, process->pid); 1524164Smax.romanov@nginx.com } 1525164Smax.romanov@nginx.com 1526196Smax.romanov@nginx.com nxt_runtime_process_destroy(rt, process); 152742Smax.romanov@nginx.com } 152842Smax.romanov@nginx.com } 152942Smax.romanov@nginx.com 153042Smax.romanov@nginx.com 153142Smax.romanov@nginx.com nxt_process_t * 153242Smax.romanov@nginx.com nxt_runtime_process_first(nxt_runtime_t *rt, nxt_lvlhsh_each_t *lhe) 153342Smax.romanov@nginx.com { 1534598Sigor@sysoev.ru nxt_lvlhsh_each_init(lhe, &lvlhsh_processes_proto); 153542Smax.romanov@nginx.com 153642Smax.romanov@nginx.com return nxt_runtime_process_next(rt, lhe); 153742Smax.romanov@nginx.com } 153842Smax.romanov@nginx.com 153942Smax.romanov@nginx.com 154042Smax.romanov@nginx.com void 1541343Smax.romanov@nginx.com nxt_runtime_port_add(nxt_task_t *task, nxt_port_t *port) 154242Smax.romanov@nginx.com { 1543348Smax.romanov@nginx.com nxt_int_t res; 1544343Smax.romanov@nginx.com nxt_runtime_t *rt; 1545343Smax.romanov@nginx.com 1546343Smax.romanov@nginx.com rt = task->thread->runtime; 1547343Smax.romanov@nginx.com 1548348Smax.romanov@nginx.com res = nxt_port_hash_add(&rt->ports, port); 1549348Smax.romanov@nginx.com 1550348Smax.romanov@nginx.com if (res != NXT_OK) { 1551348Smax.romanov@nginx.com return; 1552348Smax.romanov@nginx.com } 1553141Smax.romanov@nginx.com 1554141Smax.romanov@nginx.com rt->port_by_type[port->type] = port; 1555343Smax.romanov@nginx.com 1556343Smax.romanov@nginx.com nxt_port_use(task, port, 1); 155742Smax.romanov@nginx.com } 155842Smax.romanov@nginx.com 155942Smax.romanov@nginx.com 156042Smax.romanov@nginx.com void 1561343Smax.romanov@nginx.com nxt_runtime_port_remove(nxt_task_t *task, nxt_port_t *port) 156242Smax.romanov@nginx.com { 1563348Smax.romanov@nginx.com nxt_int_t res; 1564343Smax.romanov@nginx.com nxt_runtime_t *rt; 1565343Smax.romanov@nginx.com 1566343Smax.romanov@nginx.com rt = task->thread->runtime; 1567343Smax.romanov@nginx.com 1568348Smax.romanov@nginx.com res = nxt_port_hash_remove(&rt->ports, port); 1569348Smax.romanov@nginx.com 1570348Smax.romanov@nginx.com if (res != NXT_OK) { 1571348Smax.romanov@nginx.com return; 1572348Smax.romanov@nginx.com } 1573125Smax.romanov@nginx.com 1574141Smax.romanov@nginx.com if (rt->port_by_type[port->type] == port) { 1575141Smax.romanov@nginx.com rt->port_by_type[port->type] = NULL; 1576141Smax.romanov@nginx.com } 1577343Smax.romanov@nginx.com 1578343Smax.romanov@nginx.com nxt_port_use(task, port, -1); 157942Smax.romanov@nginx.com } 158042Smax.romanov@nginx.com 158142Smax.romanov@nginx.com 158242Smax.romanov@nginx.com nxt_port_t * 158342Smax.romanov@nginx.com nxt_runtime_port_find(nxt_runtime_t *rt, nxt_pid_t pid, 158442Smax.romanov@nginx.com nxt_port_id_t port_id) 158542Smax.romanov@nginx.com { 158675Smax.romanov@nginx.com return nxt_port_hash_find(&rt->ports, pid, port_id); 158742Smax.romanov@nginx.com } 1588