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); 22697Sigor@sysoev.ru static void nxt_runtime_initial_start(nxt_task_t *task, nxt_uint_t status); 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 354697Sigor@sysoev.ru nxt_runtime_quit(task, 1); 35520Sigor@sysoev.ru } 35620Sigor@sysoev.ru 35720Sigor@sysoev.ru 35820Sigor@sysoev.ru static void 359697Sigor@sysoev.ru nxt_runtime_initial_start(nxt_task_t *task, nxt_uint_t status) 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 410697Sigor@sysoev.ru nxt_runtime_quit(task, 1); 41120Sigor@sysoev.ru } 41220Sigor@sysoev.ru 41320Sigor@sysoev.ru 41420Sigor@sysoev.ru void 415697Sigor@sysoev.ru nxt_runtime_quit(nxt_task_t *task, nxt_uint_t status) 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; 422697Sigor@sysoev.ru rt->status |= status; 42320Sigor@sysoev.ru engine = task->thread->engine; 42420Sigor@sysoev.ru 42520Sigor@sysoev.ru nxt_debug(task, "exiting"); 42620Sigor@sysoev.ru 42720Sigor@sysoev.ru done = 1; 42820Sigor@sysoev.ru 42920Sigor@sysoev.ru if (!engine->shutdown) { 43020Sigor@sysoev.ru engine->shutdown = 1; 43120Sigor@sysoev.ru 43220Sigor@sysoev.ru if (!nxt_array_is_empty(rt->thread_pools)) { 43320Sigor@sysoev.ru nxt_runtime_thread_pool_destroy(task, rt, nxt_runtime_quit); 43420Sigor@sysoev.ru done = 0; 43520Sigor@sysoev.ru } 43620Sigor@sysoev.ru 437696Sigor@sysoev.ru if (rt->type == NXT_PROCESS_MAIN) { 438754Smax.romanov@nginx.com nxt_main_stop_all_processes(task, rt); 43920Sigor@sysoev.ru done = 0; 44020Sigor@sysoev.ru } 44120Sigor@sysoev.ru } 44220Sigor@sysoev.ru 44320Sigor@sysoev.ru nxt_runtime_close_idle_connections(engine); 44420Sigor@sysoev.ru 44520Sigor@sysoev.ru if (done) { 44620Sigor@sysoev.ru nxt_work_queue_add(&engine->fast_work_queue, nxt_runtime_exit, 44720Sigor@sysoev.ru task, rt, engine); 44820Sigor@sysoev.ru } 44920Sigor@sysoev.ru } 45020Sigor@sysoev.ru 45120Sigor@sysoev.ru 45220Sigor@sysoev.ru static void 45320Sigor@sysoev.ru nxt_runtime_close_idle_connections(nxt_event_engine_t *engine) 45420Sigor@sysoev.ru { 45562Sigor@sysoev.ru nxt_conn_t *c; 45620Sigor@sysoev.ru nxt_queue_t *idle; 45720Sigor@sysoev.ru nxt_queue_link_t *link, *next; 45820Sigor@sysoev.ru 45920Sigor@sysoev.ru nxt_debug(&engine->task, "close idle connections"); 46020Sigor@sysoev.ru 46120Sigor@sysoev.ru idle = &engine->idle_connections; 46220Sigor@sysoev.ru 46320Sigor@sysoev.ru for (link = nxt_queue_head(idle); 46420Sigor@sysoev.ru link != nxt_queue_tail(idle); 46520Sigor@sysoev.ru link = next) 46620Sigor@sysoev.ru { 46720Sigor@sysoev.ru next = nxt_queue_next(link); 46862Sigor@sysoev.ru c = nxt_queue_link_data(link, nxt_conn_t, link); 46920Sigor@sysoev.ru 47020Sigor@sysoev.ru if (!c->socket.read_ready) { 47120Sigor@sysoev.ru nxt_queue_remove(link); 47262Sigor@sysoev.ru nxt_conn_close(engine, c); 47320Sigor@sysoev.ru } 47420Sigor@sysoev.ru } 47520Sigor@sysoev.ru } 47620Sigor@sysoev.ru 47720Sigor@sysoev.ru 47820Sigor@sysoev.ru static void 47920Sigor@sysoev.ru nxt_runtime_exit(nxt_task_t *task, void *obj, void *data) 48020Sigor@sysoev.ru { 481697Sigor@sysoev.ru int status; 482125Smax.romanov@nginx.com nxt_runtime_t *rt; 483125Smax.romanov@nginx.com nxt_process_t *process; 48420Sigor@sysoev.ru nxt_event_engine_t *engine; 48520Sigor@sysoev.ru 48620Sigor@sysoev.ru rt = obj; 48720Sigor@sysoev.ru engine = data; 48820Sigor@sysoev.ru 48920Sigor@sysoev.ru nxt_debug(task, "thread pools: %d", rt->thread_pools->nelts); 49020Sigor@sysoev.ru 49120Sigor@sysoev.ru if (!nxt_array_is_empty(rt->thread_pools)) { 49220Sigor@sysoev.ru return; 49320Sigor@sysoev.ru } 49420Sigor@sysoev.ru 495696Sigor@sysoev.ru if (rt->type == NXT_PROCESS_MAIN) { 49620Sigor@sysoev.ru if (rt->pid_file != NULL) { 49720Sigor@sysoev.ru nxt_file_delete(rt->pid_file); 49820Sigor@sysoev.ru } 499234Sigor@sysoev.ru 500234Sigor@sysoev.ru #if (NXT_HAVE_UNIX_DOMAIN) 501234Sigor@sysoev.ru { 502234Sigor@sysoev.ru nxt_sockaddr_t *sa; 503234Sigor@sysoev.ru nxt_file_name_t *name; 504234Sigor@sysoev.ru 505234Sigor@sysoev.ru sa = rt->controller_listen; 506234Sigor@sysoev.ru 507234Sigor@sysoev.ru if (sa->u.sockaddr.sa_family == AF_UNIX) { 508234Sigor@sysoev.ru name = (nxt_file_name_t *) sa->u.sockaddr_un.sun_path; 509234Sigor@sysoev.ru (void) nxt_file_delete(name); 510234Sigor@sysoev.ru } 511234Sigor@sysoev.ru } 512234Sigor@sysoev.ru #endif 51320Sigor@sysoev.ru } 51420Sigor@sysoev.ru 51520Sigor@sysoev.ru if (!engine->event.signal_support) { 51620Sigor@sysoev.ru nxt_event_engine_signals_stop(engine); 51720Sigor@sysoev.ru } 51820Sigor@sysoev.ru 519125Smax.romanov@nginx.com nxt_runtime_process_each(rt, process) { 520125Smax.romanov@nginx.com 521349Smax.romanov@nginx.com nxt_process_close_ports(task, process); 522125Smax.romanov@nginx.com 523125Smax.romanov@nginx.com } nxt_runtime_process_loop; 524125Smax.romanov@nginx.com 525196Smax.romanov@nginx.com nxt_thread_mutex_destroy(&rt->processes_mutex); 526196Smax.romanov@nginx.com 527697Sigor@sysoev.ru status = rt->status; 528125Smax.romanov@nginx.com nxt_mp_destroy(rt->mem_pool); 529125Smax.romanov@nginx.com 530697Sigor@sysoev.ru nxt_debug(task, "exit: %d", status); 53120Sigor@sysoev.ru 532697Sigor@sysoev.ru exit(status); 53320Sigor@sysoev.ru nxt_unreachable(); 53420Sigor@sysoev.ru } 53520Sigor@sysoev.ru 53620Sigor@sysoev.ru 53720Sigor@sysoev.ru static nxt_int_t 53820Sigor@sysoev.ru nxt_runtime_event_engine_change(nxt_task_t *task, nxt_runtime_t *rt) 53920Sigor@sysoev.ru { 54020Sigor@sysoev.ru nxt_event_engine_t *engine; 54120Sigor@sysoev.ru const nxt_event_interface_t *interface; 54220Sigor@sysoev.ru 54320Sigor@sysoev.ru engine = task->thread->engine; 54420Sigor@sysoev.ru 54520Sigor@sysoev.ru if (engine->batch == rt->batch 54620Sigor@sysoev.ru && nxt_strcmp(engine->event.name, rt->engine) == 0) 54720Sigor@sysoev.ru { 54820Sigor@sysoev.ru return NXT_OK; 54920Sigor@sysoev.ru } 55020Sigor@sysoev.ru 55120Sigor@sysoev.ru interface = nxt_service_get(rt->services, "engine", rt->engine); 55220Sigor@sysoev.ru 55320Sigor@sysoev.ru if (interface != NULL) { 55420Sigor@sysoev.ru return nxt_event_engine_change(engine, interface, rt->batch); 55520Sigor@sysoev.ru } 55620Sigor@sysoev.ru 55720Sigor@sysoev.ru return NXT_ERROR; 55820Sigor@sysoev.ru } 55920Sigor@sysoev.ru 56020Sigor@sysoev.ru 56120Sigor@sysoev.ru void 56220Sigor@sysoev.ru nxt_runtime_event_engine_free(nxt_runtime_t *rt) 56320Sigor@sysoev.ru { 56453Sigor@sysoev.ru nxt_queue_link_t *link; 56553Sigor@sysoev.ru nxt_event_engine_t *engine; 56620Sigor@sysoev.ru 56753Sigor@sysoev.ru link = nxt_queue_first(&rt->engines); 56853Sigor@sysoev.ru nxt_queue_remove(link); 56920Sigor@sysoev.ru 57053Sigor@sysoev.ru engine = nxt_queue_link_data(link, nxt_event_engine_t, link); 57120Sigor@sysoev.ru nxt_event_engine_free(engine); 57220Sigor@sysoev.ru } 57320Sigor@sysoev.ru 57420Sigor@sysoev.ru 57520Sigor@sysoev.ru nxt_int_t 57620Sigor@sysoev.ru nxt_runtime_thread_pool_create(nxt_thread_t *thr, nxt_runtime_t *rt, 57720Sigor@sysoev.ru nxt_uint_t max_threads, nxt_nsec_t timeout) 57820Sigor@sysoev.ru { 57920Sigor@sysoev.ru nxt_thread_pool_t *thread_pool, **tp; 58020Sigor@sysoev.ru 58120Sigor@sysoev.ru tp = nxt_array_add(rt->thread_pools); 58220Sigor@sysoev.ru if (tp == NULL) { 58320Sigor@sysoev.ru return NXT_ERROR; 58420Sigor@sysoev.ru } 58520Sigor@sysoev.ru 58620Sigor@sysoev.ru thread_pool = nxt_thread_pool_create(max_threads, timeout, 58720Sigor@sysoev.ru nxt_runtime_thread_pool_init, 58820Sigor@sysoev.ru thr->engine, 58920Sigor@sysoev.ru nxt_runtime_thread_pool_exit); 59020Sigor@sysoev.ru 59120Sigor@sysoev.ru if (nxt_fast_path(thread_pool != NULL)) { 59220Sigor@sysoev.ru *tp = thread_pool; 59320Sigor@sysoev.ru } 59420Sigor@sysoev.ru 59520Sigor@sysoev.ru return NXT_OK; 59620Sigor@sysoev.ru } 59720Sigor@sysoev.ru 59820Sigor@sysoev.ru 59920Sigor@sysoev.ru static void 60020Sigor@sysoev.ru nxt_runtime_thread_pool_destroy(nxt_task_t *task, nxt_runtime_t *rt, 60120Sigor@sysoev.ru nxt_runtime_cont_t cont) 60220Sigor@sysoev.ru { 60320Sigor@sysoev.ru nxt_uint_t n; 60420Sigor@sysoev.ru nxt_thread_pool_t **tp; 60520Sigor@sysoev.ru 60620Sigor@sysoev.ru rt->continuation = cont; 60720Sigor@sysoev.ru 60820Sigor@sysoev.ru n = rt->thread_pools->nelts; 60920Sigor@sysoev.ru 61020Sigor@sysoev.ru if (n == 0) { 611697Sigor@sysoev.ru cont(task, 0); 61220Sigor@sysoev.ru return; 61320Sigor@sysoev.ru } 61420Sigor@sysoev.ru 61520Sigor@sysoev.ru tp = rt->thread_pools->elts; 61620Sigor@sysoev.ru 61720Sigor@sysoev.ru do { 61820Sigor@sysoev.ru nxt_thread_pool_destroy(*tp); 61920Sigor@sysoev.ru 62020Sigor@sysoev.ru tp++; 62120Sigor@sysoev.ru n--; 62220Sigor@sysoev.ru } while (n != 0); 62320Sigor@sysoev.ru } 62420Sigor@sysoev.ru 62520Sigor@sysoev.ru 62620Sigor@sysoev.ru static void 62720Sigor@sysoev.ru nxt_runtime_thread_pool_init(void) 62820Sigor@sysoev.ru { 62920Sigor@sysoev.ru #if (NXT_REGEX) 63020Sigor@sysoev.ru nxt_regex_init(0); 63120Sigor@sysoev.ru #endif 63220Sigor@sysoev.ru } 63320Sigor@sysoev.ru 63420Sigor@sysoev.ru 63520Sigor@sysoev.ru static void 63620Sigor@sysoev.ru nxt_runtime_thread_pool_exit(nxt_task_t *task, void *obj, void *data) 63720Sigor@sysoev.ru { 63820Sigor@sysoev.ru nxt_uint_t i, n; 63920Sigor@sysoev.ru nxt_runtime_t *rt; 64020Sigor@sysoev.ru nxt_thread_pool_t *tp, **thread_pools; 64120Sigor@sysoev.ru nxt_thread_handle_t handle; 64220Sigor@sysoev.ru 64320Sigor@sysoev.ru tp = obj; 64420Sigor@sysoev.ru 64520Sigor@sysoev.ru if (data != NULL) { 64620Sigor@sysoev.ru handle = (nxt_thread_handle_t) (uintptr_t) data; 64720Sigor@sysoev.ru nxt_thread_wait(handle); 64820Sigor@sysoev.ru } 64920Sigor@sysoev.ru 65020Sigor@sysoev.ru rt = task->thread->runtime; 65120Sigor@sysoev.ru 65220Sigor@sysoev.ru thread_pools = rt->thread_pools->elts; 65320Sigor@sysoev.ru n = rt->thread_pools->nelts; 65420Sigor@sysoev.ru 65520Sigor@sysoev.ru nxt_debug(task, "thread pools: %ui", n); 65620Sigor@sysoev.ru 65720Sigor@sysoev.ru for (i = 0; i < n; i++) { 65820Sigor@sysoev.ru 65920Sigor@sysoev.ru if (tp == thread_pools[i]) { 66020Sigor@sysoev.ru nxt_array_remove(rt->thread_pools, &thread_pools[i]); 66120Sigor@sysoev.ru 66220Sigor@sysoev.ru if (n == 1) { 66320Sigor@sysoev.ru /* The last thread pool. */ 664697Sigor@sysoev.ru rt->continuation(task, 0); 66520Sigor@sysoev.ru } 66620Sigor@sysoev.ru 66720Sigor@sysoev.ru return; 66820Sigor@sysoev.ru } 66920Sigor@sysoev.ru } 67020Sigor@sysoev.ru } 67120Sigor@sysoev.ru 67220Sigor@sysoev.ru 67320Sigor@sysoev.ru static nxt_int_t 67420Sigor@sysoev.ru nxt_runtime_conf_init(nxt_task_t *task, nxt_runtime_t *rt) 67520Sigor@sysoev.ru { 67620Sigor@sysoev.ru nxt_int_t ret; 677251Sigor@sysoev.ru nxt_str_t control; 678251Sigor@sysoev.ru nxt_uint_t n; 67920Sigor@sysoev.ru nxt_file_t *file; 680251Sigor@sysoev.ru const char *slash; 681234Sigor@sysoev.ru nxt_sockaddr_t *sa; 68220Sigor@sysoev.ru nxt_file_name_str_t file_name; 68320Sigor@sysoev.ru const nxt_event_interface_t *interface; 68420Sigor@sysoev.ru 68520Sigor@sysoev.ru rt->daemon = 1; 68620Sigor@sysoev.ru rt->engine_connections = 256; 68720Sigor@sysoev.ru rt->auxiliary_threads = 2; 688232Sigor@sysoev.ru rt->user_cred.user = NXT_USER; 689232Sigor@sysoev.ru rt->group = NXT_GROUP; 690231Sigor@sysoev.ru rt->pid = NXT_PID; 691230Sigor@sysoev.ru rt->log = NXT_LOG; 692233Sigor@sysoev.ru rt->modules = NXT_MODULES; 693314Svbart@nginx.com rt->state = NXT_STATE; 694234Sigor@sysoev.ru rt->control = NXT_CONTROL_SOCK; 69520Sigor@sysoev.ru 69620Sigor@sysoev.ru if (nxt_runtime_conf_read_cmd(task, rt) != NXT_OK) { 69720Sigor@sysoev.ru return NXT_ERROR; 69820Sigor@sysoev.ru } 69920Sigor@sysoev.ru 70020Sigor@sysoev.ru if (nxt_user_cred_get(task, &rt->user_cred, rt->group) != NXT_OK) { 70120Sigor@sysoev.ru return NXT_ERROR; 70220Sigor@sysoev.ru } 70320Sigor@sysoev.ru 70420Sigor@sysoev.ru /* An engine's parameters. */ 70520Sigor@sysoev.ru 70620Sigor@sysoev.ru interface = nxt_service_get(rt->services, "engine", rt->engine); 70720Sigor@sysoev.ru if (interface == NULL) { 70820Sigor@sysoev.ru return NXT_ERROR; 70920Sigor@sysoev.ru } 71020Sigor@sysoev.ru 71120Sigor@sysoev.ru rt->engine = interface->name; 71220Sigor@sysoev.ru 713252Smax.romanov@nginx.com ret = nxt_file_name_create(rt->mem_pool, &file_name, "%s%Z", rt->pid); 71420Sigor@sysoev.ru if (nxt_slow_path(ret != NXT_OK)) { 71520Sigor@sysoev.ru return NXT_ERROR; 71620Sigor@sysoev.ru } 71720Sigor@sysoev.ru 71820Sigor@sysoev.ru rt->pid_file = file_name.start; 71920Sigor@sysoev.ru 720230Sigor@sysoev.ru ret = nxt_file_name_create(rt->mem_pool, &file_name, "%s%Z", rt->log); 72120Sigor@sysoev.ru if (nxt_slow_path(ret != NXT_OK)) { 72220Sigor@sysoev.ru return NXT_ERROR; 72320Sigor@sysoev.ru } 72420Sigor@sysoev.ru 72520Sigor@sysoev.ru file = nxt_list_first(rt->log_files); 72620Sigor@sysoev.ru file->name = file_name.start; 72720Sigor@sysoev.ru 728251Sigor@sysoev.ru slash = ""; 729251Sigor@sysoev.ru n = nxt_strlen(rt->modules); 730251Sigor@sysoev.ru 731251Sigor@sysoev.ru if (n > 1 && rt->modules[n - 1] != '/') { 732251Sigor@sysoev.ru slash = "/"; 733251Sigor@sysoev.ru } 734251Sigor@sysoev.ru 735260Sigor@sysoev.ru ret = nxt_file_name_create(rt->mem_pool, &file_name, "%s%s*.unit.so%Z", 736251Sigor@sysoev.ru rt->modules, slash); 737233Sigor@sysoev.ru if (nxt_slow_path(ret != NXT_OK)) { 738233Sigor@sysoev.ru return NXT_ERROR; 739233Sigor@sysoev.ru } 740233Sigor@sysoev.ru 741233Sigor@sysoev.ru rt->modules = (char *) file_name.start; 742233Sigor@sysoev.ru 743314Svbart@nginx.com slash = ""; 744314Svbart@nginx.com n = nxt_strlen(rt->state); 745314Svbart@nginx.com 746314Svbart@nginx.com if (n > 1 && rt->state[n - 1] != '/') { 747314Svbart@nginx.com slash = "/"; 748314Svbart@nginx.com } 749314Svbart@nginx.com 750314Svbart@nginx.com ret = nxt_file_name_create(rt->mem_pool, &file_name, "%s%sconf.json%Z", 751314Svbart@nginx.com rt->state, slash); 752314Svbart@nginx.com if (nxt_slow_path(ret != NXT_OK)) { 753314Svbart@nginx.com return NXT_ERROR; 754314Svbart@nginx.com } 755314Svbart@nginx.com 756314Svbart@nginx.com rt->conf = (char *) file_name.start; 757314Svbart@nginx.com 758314Svbart@nginx.com ret = nxt_file_name_create(rt->mem_pool, &file_name, "%s.tmp%Z", rt->conf); 759314Svbart@nginx.com if (nxt_slow_path(ret != NXT_OK)) { 760314Svbart@nginx.com return NXT_ERROR; 761314Svbart@nginx.com } 762314Svbart@nginx.com 763314Svbart@nginx.com rt->conf_tmp = (char *) file_name.start; 764314Svbart@nginx.com 765*774Svbart@nginx.com ret = nxt_file_name_create(rt->mem_pool, &file_name, "%s%scerts/%Z", 766*774Svbart@nginx.com rt->state, slash); 767*774Svbart@nginx.com if (nxt_slow_path(ret != NXT_OK)) { 768*774Svbart@nginx.com return NXT_ERROR; 769*774Svbart@nginx.com } 770*774Svbart@nginx.com 771*774Svbart@nginx.com ret = mkdir((char *) file_name.start, S_IRWXU); 772*774Svbart@nginx.com 773*774Svbart@nginx.com if (nxt_fast_path(ret == 0 || nxt_errno == EEXIST)) { 774*774Svbart@nginx.com rt->certs.length = file_name.len; 775*774Svbart@nginx.com rt->certs.start = file_name.start; 776*774Svbart@nginx.com 777*774Svbart@nginx.com } else { 778*774Svbart@nginx.com nxt_alert(task, "Unable to create certificates storage directory: " 779*774Svbart@nginx.com "mkdir(%s) failed %E", file_name.start, nxt_errno); 780*774Svbart@nginx.com } 781*774Svbart@nginx.com 782234Sigor@sysoev.ru control.length = nxt_strlen(rt->control); 783234Sigor@sysoev.ru control.start = (u_char *) rt->control; 784234Sigor@sysoev.ru 785649Svbart@nginx.com sa = nxt_sockaddr_parse(rt->mem_pool, &control); 786234Sigor@sysoev.ru if (nxt_slow_path(sa == NULL)) { 787234Sigor@sysoev.ru return NXT_ERROR; 788234Sigor@sysoev.ru } 789234Sigor@sysoev.ru 790649Svbart@nginx.com sa->type = SOCK_STREAM; 791649Svbart@nginx.com 792234Sigor@sysoev.ru rt->controller_listen = sa; 793234Sigor@sysoev.ru 794234Sigor@sysoev.ru if (nxt_runtime_controller_socket(task, rt) != NXT_OK) { 795234Sigor@sysoev.ru return NXT_ERROR; 796234Sigor@sysoev.ru } 797234Sigor@sysoev.ru 79820Sigor@sysoev.ru return NXT_OK; 79920Sigor@sysoev.ru } 80020Sigor@sysoev.ru 80120Sigor@sysoev.ru 80220Sigor@sysoev.ru static nxt_int_t 80320Sigor@sysoev.ru nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt) 80420Sigor@sysoev.ru { 805234Sigor@sysoev.ru char *p, **argv; 806234Sigor@sysoev.ru u_char *end; 807234Sigor@sysoev.ru u_char buf[1024]; 80820Sigor@sysoev.ru 809226Svbart@nginx.com static const char version[] = 810259Sigor@sysoev.ru "unit version: " NXT_VERSION "\n" 811221Sigor@sysoev.ru "configured as ./configure" NXT_CONFIGURE_OPTIONS "\n"; 812221Sigor@sysoev.ru 813234Sigor@sysoev.ru static const char no_control[] = 814234Sigor@sysoev.ru "option \"--control\" requires socket address\n"; 815232Sigor@sysoev.ru static const char no_user[] = "option \"--user\" requires username\n"; 816232Sigor@sysoev.ru static const char no_group[] = "option \"--group\" requires group name\n"; 817231Sigor@sysoev.ru static const char no_pid[] = "option \"--pid\" requires filename\n"; 818230Sigor@sysoev.ru static const char no_log[] = "option \"--log\" requires filename\n"; 819233Sigor@sysoev.ru static const char no_modules[] = 820233Sigor@sysoev.ru "option \"--modules\" requires directory\n"; 821314Svbart@nginx.com static const char no_state[] = "option \"--state\" requires directory\n"; 822230Sigor@sysoev.ru 823235Sigor@sysoev.ru static const char help[] = 824235Sigor@sysoev.ru "\n" 825259Sigor@sysoev.ru "unit options:\n" 826235Sigor@sysoev.ru "\n" 827259Sigor@sysoev.ru " --version print unit version and configure options\n" 828235Sigor@sysoev.ru "\n" 829259Sigor@sysoev.ru " --no-daemon run unit in non-daemon mode\n" 830235Sigor@sysoev.ru "\n" 831235Sigor@sysoev.ru " --control ADDRESS set address of control API socket\n" 832235Sigor@sysoev.ru " default: \"" NXT_CONTROL_SOCK "\"\n" 833235Sigor@sysoev.ru "\n" 834239Sigor@sysoev.ru " --pid FILE set pid filename\n" 835235Sigor@sysoev.ru " default: \"" NXT_PID "\"\n" 836235Sigor@sysoev.ru "\n" 837239Sigor@sysoev.ru " --log FILE set log filename\n" 838235Sigor@sysoev.ru " default: \"" NXT_LOG "\"\n" 839235Sigor@sysoev.ru "\n" 840239Sigor@sysoev.ru " --modules DIRECTORY set modules directory name\n" 841235Sigor@sysoev.ru " default: \"" NXT_MODULES "\"\n" 842235Sigor@sysoev.ru "\n" 843314Svbart@nginx.com " --state DIRECTORY set state directory name\n" 844314Svbart@nginx.com " default: \"" NXT_STATE "\"\n" 845314Svbart@nginx.com "\n" 846235Sigor@sysoev.ru " --user USER set non-privileged processes to run" 847235Sigor@sysoev.ru " as specified user\n" 848235Sigor@sysoev.ru " default: \"" NXT_USER "\"\n" 849235Sigor@sysoev.ru "\n" 850235Sigor@sysoev.ru " --group GROUP set non-privileged processes to run" 851235Sigor@sysoev.ru " as specified group\n" 852235Sigor@sysoev.ru " default: "; 853235Sigor@sysoev.ru 854235Sigor@sysoev.ru static const char group[] = "\"" NXT_GROUP "\"\n\n"; 855235Sigor@sysoev.ru static const char primary[] = "user's primary group\n\n"; 856235Sigor@sysoev.ru 857222Sigor@sysoev.ru argv = &nxt_process_argv[1]; 85820Sigor@sysoev.ru 85920Sigor@sysoev.ru while (*argv != NULL) { 86020Sigor@sysoev.ru p = *argv++; 86120Sigor@sysoev.ru 862234Sigor@sysoev.ru if (nxt_strcmp(p, "--control") == 0) { 86320Sigor@sysoev.ru if (*argv == NULL) { 864703Svbart@nginx.com write(STDERR_FILENO, no_control, nxt_length(no_control)); 86520Sigor@sysoev.ru return NXT_ERROR; 86620Sigor@sysoev.ru } 86720Sigor@sysoev.ru 86820Sigor@sysoev.ru p = *argv++; 86920Sigor@sysoev.ru 870234Sigor@sysoev.ru rt->control = p; 87120Sigor@sysoev.ru 87220Sigor@sysoev.ru continue; 87320Sigor@sysoev.ru } 87420Sigor@sysoev.ru 87520Sigor@sysoev.ru if (nxt_strcmp(p, "--upstream") == 0) { 87620Sigor@sysoev.ru if (*argv == NULL) { 877564Svbart@nginx.com nxt_alert(task, "no argument for option \"--upstream\""); 87820Sigor@sysoev.ru return NXT_ERROR; 87920Sigor@sysoev.ru } 88020Sigor@sysoev.ru 88120Sigor@sysoev.ru p = *argv++; 88220Sigor@sysoev.ru 88320Sigor@sysoev.ru rt->upstream.length = nxt_strlen(p); 88420Sigor@sysoev.ru rt->upstream.start = (u_char *) p; 88520Sigor@sysoev.ru 88620Sigor@sysoev.ru continue; 88720Sigor@sysoev.ru } 88820Sigor@sysoev.ru 88920Sigor@sysoev.ru if (nxt_strcmp(p, "--user") == 0) { 89020Sigor@sysoev.ru if (*argv == NULL) { 891703Svbart@nginx.com write(STDERR_FILENO, no_user, nxt_length(no_user)); 89220Sigor@sysoev.ru return NXT_ERROR; 89320Sigor@sysoev.ru } 89420Sigor@sysoev.ru 89520Sigor@sysoev.ru p = *argv++; 89620Sigor@sysoev.ru 89720Sigor@sysoev.ru rt->user_cred.user = p; 89820Sigor@sysoev.ru 89920Sigor@sysoev.ru continue; 90020Sigor@sysoev.ru } 90120Sigor@sysoev.ru 90220Sigor@sysoev.ru if (nxt_strcmp(p, "--group") == 0) { 90320Sigor@sysoev.ru if (*argv == NULL) { 904703Svbart@nginx.com write(STDERR_FILENO, no_group, nxt_length(no_group)); 90520Sigor@sysoev.ru return NXT_ERROR; 90620Sigor@sysoev.ru } 90720Sigor@sysoev.ru 90820Sigor@sysoev.ru p = *argv++; 90920Sigor@sysoev.ru 91020Sigor@sysoev.ru rt->group = p; 91120Sigor@sysoev.ru 91220Sigor@sysoev.ru continue; 91320Sigor@sysoev.ru } 91420Sigor@sysoev.ru 91520Sigor@sysoev.ru if (nxt_strcmp(p, "--pid") == 0) { 91620Sigor@sysoev.ru if (*argv == NULL) { 917703Svbart@nginx.com write(STDERR_FILENO, no_pid, nxt_length(no_pid)); 91820Sigor@sysoev.ru return NXT_ERROR; 91920Sigor@sysoev.ru } 92020Sigor@sysoev.ru 92120Sigor@sysoev.ru p = *argv++; 92220Sigor@sysoev.ru 92320Sigor@sysoev.ru rt->pid = p; 92420Sigor@sysoev.ru 92520Sigor@sysoev.ru continue; 92620Sigor@sysoev.ru } 92720Sigor@sysoev.ru 92820Sigor@sysoev.ru if (nxt_strcmp(p, "--log") == 0) { 92920Sigor@sysoev.ru if (*argv == NULL) { 930703Svbart@nginx.com write(STDERR_FILENO, no_log, nxt_length(no_log)); 93120Sigor@sysoev.ru return NXT_ERROR; 93220Sigor@sysoev.ru } 93320Sigor@sysoev.ru 93420Sigor@sysoev.ru p = *argv++; 93520Sigor@sysoev.ru 936230Sigor@sysoev.ru rt->log = p; 93720Sigor@sysoev.ru 93820Sigor@sysoev.ru continue; 93920Sigor@sysoev.ru } 94020Sigor@sysoev.ru 941233Sigor@sysoev.ru if (nxt_strcmp(p, "--modules") == 0) { 942233Sigor@sysoev.ru if (*argv == NULL) { 943703Svbart@nginx.com write(STDERR_FILENO, no_modules, nxt_length(no_modules)); 944233Sigor@sysoev.ru return NXT_ERROR; 945233Sigor@sysoev.ru } 946233Sigor@sysoev.ru 947233Sigor@sysoev.ru p = *argv++; 948233Sigor@sysoev.ru 949233Sigor@sysoev.ru rt->modules = p; 950233Sigor@sysoev.ru 951233Sigor@sysoev.ru continue; 952233Sigor@sysoev.ru } 953233Sigor@sysoev.ru 954314Svbart@nginx.com if (nxt_strcmp(p, "--state") == 0) { 955314Svbart@nginx.com if (*argv == NULL) { 956703Svbart@nginx.com write(STDERR_FILENO, no_state, nxt_length(no_state)); 957314Svbart@nginx.com return NXT_ERROR; 958314Svbart@nginx.com } 959314Svbart@nginx.com 960314Svbart@nginx.com p = *argv++; 961314Svbart@nginx.com 962314Svbart@nginx.com rt->state = p; 963314Svbart@nginx.com 964314Svbart@nginx.com continue; 965314Svbart@nginx.com } 966314Svbart@nginx.com 967219Sigor@sysoev.ru if (nxt_strcmp(p, "--no-daemon") == 0) { 96820Sigor@sysoev.ru rt->daemon = 0; 96920Sigor@sysoev.ru continue; 97020Sigor@sysoev.ru } 971221Sigor@sysoev.ru 972221Sigor@sysoev.ru if (nxt_strcmp(p, "--version") == 0) { 973703Svbart@nginx.com write(STDERR_FILENO, version, nxt_length(version)); 974221Sigor@sysoev.ru exit(0); 975221Sigor@sysoev.ru } 976222Sigor@sysoev.ru 977643Svbart@nginx.com if (nxt_strcmp(p, "--help") == 0 || nxt_strcmp(p, "-h") == 0) { 978703Svbart@nginx.com write(STDOUT_FILENO, help, nxt_length(help)); 979235Sigor@sysoev.ru 980235Sigor@sysoev.ru if (sizeof(NXT_GROUP) == 1) { 981703Svbart@nginx.com write(STDOUT_FILENO, primary, nxt_length(primary)); 982235Sigor@sysoev.ru 983235Sigor@sysoev.ru } else { 984703Svbart@nginx.com write(STDOUT_FILENO, group, nxt_length(group)); 985235Sigor@sysoev.ru } 986235Sigor@sysoev.ru 987235Sigor@sysoev.ru exit(0); 988235Sigor@sysoev.ru } 989235Sigor@sysoev.ru 990643Svbart@nginx.com end = nxt_sprintf(buf, buf + sizeof(buf), "unknown option \"%s\", " 991643Svbart@nginx.com "try \"%s -h\" for available options\n", 992643Svbart@nginx.com p, nxt_process_argv[0]); 993643Svbart@nginx.com 994222Sigor@sysoev.ru write(STDERR_FILENO, buf, end - buf); 995222Sigor@sysoev.ru 996222Sigor@sysoev.ru return NXT_ERROR; 99720Sigor@sysoev.ru } 99820Sigor@sysoev.ru 99920Sigor@sysoev.ru return NXT_OK; 100020Sigor@sysoev.ru } 100120Sigor@sysoev.ru 100220Sigor@sysoev.ru 100320Sigor@sysoev.ru nxt_listen_socket_t * 100420Sigor@sysoev.ru nxt_runtime_listen_socket_add(nxt_runtime_t *rt, nxt_sockaddr_t *sa) 100520Sigor@sysoev.ru { 100665Sigor@sysoev.ru nxt_mp_t *mp; 100720Sigor@sysoev.ru nxt_listen_socket_t *ls; 100820Sigor@sysoev.ru 100920Sigor@sysoev.ru ls = nxt_array_zero_add(rt->listen_sockets); 101020Sigor@sysoev.ru if (ls == NULL) { 101120Sigor@sysoev.ru return NULL; 101220Sigor@sysoev.ru } 101320Sigor@sysoev.ru 101420Sigor@sysoev.ru mp = rt->mem_pool; 101520Sigor@sysoev.ru 101620Sigor@sysoev.ru ls->sockaddr = nxt_sockaddr_create(mp, &sa->u.sockaddr, sa->socklen, 101720Sigor@sysoev.ru sa->length); 101820Sigor@sysoev.ru if (ls->sockaddr == NULL) { 101920Sigor@sysoev.ru return NULL; 102020Sigor@sysoev.ru } 102120Sigor@sysoev.ru 102220Sigor@sysoev.ru ls->sockaddr->type = sa->type; 102320Sigor@sysoev.ru 102420Sigor@sysoev.ru nxt_sockaddr_text(ls->sockaddr); 102520Sigor@sysoev.ru 102620Sigor@sysoev.ru ls->socket = -1; 102720Sigor@sysoev.ru ls->backlog = NXT_LISTEN_BACKLOG; 102820Sigor@sysoev.ru 102920Sigor@sysoev.ru return ls; 103020Sigor@sysoev.ru } 103120Sigor@sysoev.ru 103220Sigor@sysoev.ru 103320Sigor@sysoev.ru static nxt_int_t 103420Sigor@sysoev.ru nxt_runtime_hostname(nxt_task_t *task, nxt_runtime_t *rt) 103520Sigor@sysoev.ru { 103620Sigor@sysoev.ru size_t length; 103720Sigor@sysoev.ru char hostname[NXT_MAXHOSTNAMELEN + 1]; 103820Sigor@sysoev.ru 103920Sigor@sysoev.ru if (gethostname(hostname, NXT_MAXHOSTNAMELEN) != 0) { 1040564Svbart@nginx.com nxt_alert(task, "gethostname() failed %E", nxt_errno); 104120Sigor@sysoev.ru return NXT_ERROR; 104220Sigor@sysoev.ru } 104320Sigor@sysoev.ru 104420Sigor@sysoev.ru /* 104520Sigor@sysoev.ru * Linux gethostname(2): 104620Sigor@sysoev.ru * 104720Sigor@sysoev.ru * If the null-terminated hostname is too large to fit, 104820Sigor@sysoev.ru * then the name is truncated, and no error is returned. 104920Sigor@sysoev.ru * 105020Sigor@sysoev.ru * For this reason an additional byte is reserved in the buffer. 105120Sigor@sysoev.ru */ 105220Sigor@sysoev.ru hostname[NXT_MAXHOSTNAMELEN] = '\0'; 105320Sigor@sysoev.ru 105420Sigor@sysoev.ru length = nxt_strlen(hostname); 105520Sigor@sysoev.ru rt->hostname.length = length; 105620Sigor@sysoev.ru 105765Sigor@sysoev.ru rt->hostname.start = nxt_mp_nget(rt->mem_pool, length); 105820Sigor@sysoev.ru 105920Sigor@sysoev.ru if (rt->hostname.start != NULL) { 106020Sigor@sysoev.ru nxt_memcpy_lowcase(rt->hostname.start, (u_char *) hostname, length); 106120Sigor@sysoev.ru return NXT_OK; 106220Sigor@sysoev.ru } 106320Sigor@sysoev.ru 106420Sigor@sysoev.ru return NXT_ERROR; 106520Sigor@sysoev.ru } 106620Sigor@sysoev.ru 106720Sigor@sysoev.ru 106820Sigor@sysoev.ru static nxt_int_t 106920Sigor@sysoev.ru nxt_runtime_log_files_init(nxt_runtime_t *rt) 107020Sigor@sysoev.ru { 107120Sigor@sysoev.ru nxt_file_t *file; 107220Sigor@sysoev.ru nxt_list_t *log_files; 107320Sigor@sysoev.ru 107420Sigor@sysoev.ru log_files = nxt_list_create(rt->mem_pool, 1, sizeof(nxt_file_t)); 107520Sigor@sysoev.ru 107620Sigor@sysoev.ru if (nxt_fast_path(log_files != NULL)) { 107720Sigor@sysoev.ru rt->log_files = log_files; 107820Sigor@sysoev.ru 1079230Sigor@sysoev.ru /* Preallocate the main log. This allocation cannot fail. */ 108020Sigor@sysoev.ru file = nxt_list_zero_add(log_files); 108120Sigor@sysoev.ru 108220Sigor@sysoev.ru file->fd = NXT_FILE_INVALID; 1083564Svbart@nginx.com file->log_level = NXT_LOG_ALERT; 108420Sigor@sysoev.ru 108520Sigor@sysoev.ru return NXT_OK; 108620Sigor@sysoev.ru } 108720Sigor@sysoev.ru 108820Sigor@sysoev.ru return NXT_ERROR; 108920Sigor@sysoev.ru } 109020Sigor@sysoev.ru 109120Sigor@sysoev.ru 109220Sigor@sysoev.ru nxt_file_t * 109320Sigor@sysoev.ru nxt_runtime_log_file_add(nxt_runtime_t *rt, nxt_str_t *name) 109420Sigor@sysoev.ru { 109520Sigor@sysoev.ru nxt_int_t ret; 109620Sigor@sysoev.ru nxt_file_t *file; 109720Sigor@sysoev.ru nxt_file_name_str_t file_name; 109820Sigor@sysoev.ru 1099230Sigor@sysoev.ru ret = nxt_file_name_create(rt->mem_pool, &file_name, "V%Z", name); 110020Sigor@sysoev.ru 110120Sigor@sysoev.ru if (nxt_slow_path(ret != NXT_OK)) { 110220Sigor@sysoev.ru return NULL; 110320Sigor@sysoev.ru } 110420Sigor@sysoev.ru 110520Sigor@sysoev.ru nxt_list_each(file, rt->log_files) { 110620Sigor@sysoev.ru 110720Sigor@sysoev.ru /* STUB: hardecoded case sensitive/insensitive. */ 110820Sigor@sysoev.ru 110920Sigor@sysoev.ru if (file->name != NULL 111020Sigor@sysoev.ru && nxt_file_name_eq(file->name, file_name.start)) 111120Sigor@sysoev.ru { 111220Sigor@sysoev.ru return file; 111320Sigor@sysoev.ru } 111420Sigor@sysoev.ru 111520Sigor@sysoev.ru } nxt_list_loop; 111620Sigor@sysoev.ru 111720Sigor@sysoev.ru file = nxt_list_zero_add(rt->log_files); 111820Sigor@sysoev.ru 111920Sigor@sysoev.ru if (nxt_slow_path(file == NULL)) { 112020Sigor@sysoev.ru return NULL; 112120Sigor@sysoev.ru } 112220Sigor@sysoev.ru 112320Sigor@sysoev.ru file->fd = NXT_FILE_INVALID; 1124564Svbart@nginx.com file->log_level = NXT_LOG_ALERT; 112520Sigor@sysoev.ru file->name = file_name.start; 112620Sigor@sysoev.ru 112720Sigor@sysoev.ru return file; 112820Sigor@sysoev.ru } 112920Sigor@sysoev.ru 113020Sigor@sysoev.ru 113120Sigor@sysoev.ru static nxt_int_t 113220Sigor@sysoev.ru nxt_runtime_log_files_create(nxt_task_t *task, nxt_runtime_t *rt) 113320Sigor@sysoev.ru { 113420Sigor@sysoev.ru nxt_int_t ret; 113520Sigor@sysoev.ru nxt_file_t *file; 113620Sigor@sysoev.ru 113720Sigor@sysoev.ru nxt_list_each(file, rt->log_files) { 113820Sigor@sysoev.ru 113920Sigor@sysoev.ru ret = nxt_file_open(task, file, O_WRONLY | O_APPEND, O_CREAT, 114020Sigor@sysoev.ru NXT_FILE_OWNER_ACCESS); 114120Sigor@sysoev.ru 114220Sigor@sysoev.ru if (ret != NXT_OK) { 114320Sigor@sysoev.ru return NXT_ERROR; 114420Sigor@sysoev.ru } 114520Sigor@sysoev.ru 114620Sigor@sysoev.ru } nxt_list_loop; 114720Sigor@sysoev.ru 114820Sigor@sysoev.ru file = nxt_list_first(rt->log_files); 114920Sigor@sysoev.ru 115020Sigor@sysoev.ru return nxt_file_stderr(file); 115120Sigor@sysoev.ru } 115220Sigor@sysoev.ru 115320Sigor@sysoev.ru 115420Sigor@sysoev.ru nxt_int_t 115520Sigor@sysoev.ru nxt_runtime_listen_sockets_create(nxt_task_t *task, nxt_runtime_t *rt) 115620Sigor@sysoev.ru { 115720Sigor@sysoev.ru nxt_int_t ret; 115820Sigor@sysoev.ru nxt_uint_t c, p, ncurr, nprev; 115920Sigor@sysoev.ru nxt_listen_socket_t *curr, *prev; 116020Sigor@sysoev.ru 116120Sigor@sysoev.ru curr = rt->listen_sockets->elts; 116220Sigor@sysoev.ru ncurr = rt->listen_sockets->nelts; 116320Sigor@sysoev.ru 116420Sigor@sysoev.ru if (rt->inherited_sockets != NULL) { 116520Sigor@sysoev.ru prev = rt->inherited_sockets->elts; 116620Sigor@sysoev.ru nprev = rt->inherited_sockets->nelts; 116720Sigor@sysoev.ru 116820Sigor@sysoev.ru } else { 116920Sigor@sysoev.ru prev = NULL; 117020Sigor@sysoev.ru nprev = 0; 117120Sigor@sysoev.ru } 117220Sigor@sysoev.ru 117320Sigor@sysoev.ru for (c = 0; c < ncurr; c++) { 117420Sigor@sysoev.ru 117520Sigor@sysoev.ru for (p = 0; p < nprev; p++) { 117620Sigor@sysoev.ru 117720Sigor@sysoev.ru if (nxt_sockaddr_cmp(curr[c].sockaddr, prev[p].sockaddr)) { 117820Sigor@sysoev.ru 117920Sigor@sysoev.ru ret = nxt_listen_socket_update(task, &curr[c], &prev[p]); 118020Sigor@sysoev.ru if (ret != NXT_OK) { 118120Sigor@sysoev.ru return NXT_ERROR; 118220Sigor@sysoev.ru } 118320Sigor@sysoev.ru 118420Sigor@sysoev.ru goto next; 118520Sigor@sysoev.ru } 118620Sigor@sysoev.ru } 118720Sigor@sysoev.ru 118820Sigor@sysoev.ru if (nxt_listen_socket_create(task, &curr[c], 0) != NXT_OK) { 118920Sigor@sysoev.ru return NXT_ERROR; 119020Sigor@sysoev.ru } 119120Sigor@sysoev.ru 119220Sigor@sysoev.ru next: 119320Sigor@sysoev.ru 119420Sigor@sysoev.ru continue; 119520Sigor@sysoev.ru } 119620Sigor@sysoev.ru 119720Sigor@sysoev.ru return NXT_OK; 119820Sigor@sysoev.ru } 119920Sigor@sysoev.ru 120020Sigor@sysoev.ru 120120Sigor@sysoev.ru nxt_int_t 120220Sigor@sysoev.ru nxt_runtime_listen_sockets_enable(nxt_task_t *task, nxt_runtime_t *rt) 120320Sigor@sysoev.ru { 120420Sigor@sysoev.ru nxt_uint_t i, n; 120520Sigor@sysoev.ru nxt_listen_socket_t *ls; 120620Sigor@sysoev.ru 120720Sigor@sysoev.ru ls = rt->listen_sockets->elts; 120820Sigor@sysoev.ru n = rt->listen_sockets->nelts; 120920Sigor@sysoev.ru 121020Sigor@sysoev.ru for (i = 0; i < n; i++) { 121120Sigor@sysoev.ru if (ls[i].flags == NXT_NONBLOCK) { 121254Sigor@sysoev.ru if (nxt_listen_event(task, &ls[i]) == NULL) { 121320Sigor@sysoev.ru return NXT_ERROR; 121420Sigor@sysoev.ru } 121520Sigor@sysoev.ru } 121620Sigor@sysoev.ru } 121720Sigor@sysoev.ru 121820Sigor@sysoev.ru return NXT_OK; 121920Sigor@sysoev.ru } 122020Sigor@sysoev.ru 122120Sigor@sysoev.ru 122220Sigor@sysoev.ru nxt_str_t * 122365Sigor@sysoev.ru nxt_current_directory(nxt_mp_t *mp) 122420Sigor@sysoev.ru { 122520Sigor@sysoev.ru size_t length; 122620Sigor@sysoev.ru u_char *p; 122720Sigor@sysoev.ru nxt_str_t *name; 122820Sigor@sysoev.ru char buf[NXT_MAX_PATH_LEN]; 122920Sigor@sysoev.ru 123020Sigor@sysoev.ru length = nxt_dir_current(buf, NXT_MAX_PATH_LEN); 123120Sigor@sysoev.ru 123220Sigor@sysoev.ru if (nxt_fast_path(length != 0)) { 123320Sigor@sysoev.ru name = nxt_str_alloc(mp, length + 1); 123420Sigor@sysoev.ru 123520Sigor@sysoev.ru if (nxt_fast_path(name != NULL)) { 123620Sigor@sysoev.ru p = nxt_cpymem(name->start, buf, length); 123720Sigor@sysoev.ru *p = '/'; 123820Sigor@sysoev.ru 123920Sigor@sysoev.ru return name; 124020Sigor@sysoev.ru } 124120Sigor@sysoev.ru } 124220Sigor@sysoev.ru 124320Sigor@sysoev.ru return NULL; 124420Sigor@sysoev.ru } 124520Sigor@sysoev.ru 124620Sigor@sysoev.ru 124720Sigor@sysoev.ru static nxt_int_t 124820Sigor@sysoev.ru nxt_runtime_pid_file_create(nxt_task_t *task, nxt_file_name_t *pid_file) 124920Sigor@sysoev.ru { 125020Sigor@sysoev.ru ssize_t length; 125120Sigor@sysoev.ru nxt_int_t n; 125220Sigor@sysoev.ru nxt_file_t file; 1253704Sigor@sysoev.ru u_char pid[NXT_INT64_T_LEN + nxt_length("\n")]; 125420Sigor@sysoev.ru 125520Sigor@sysoev.ru nxt_memzero(&file, sizeof(nxt_file_t)); 125620Sigor@sysoev.ru 125720Sigor@sysoev.ru file.name = pid_file; 125820Sigor@sysoev.ru 125920Sigor@sysoev.ru n = nxt_file_open(task, &file, O_WRONLY, O_CREAT | O_TRUNC, 126020Sigor@sysoev.ru NXT_FILE_DEFAULT_ACCESS); 126120Sigor@sysoev.ru 126220Sigor@sysoev.ru if (n != NXT_OK) { 126320Sigor@sysoev.ru return NXT_ERROR; 126420Sigor@sysoev.ru } 126520Sigor@sysoev.ru 126620Sigor@sysoev.ru length = nxt_sprintf(pid, pid + sizeof(pid), "%PI%n", nxt_pid) - pid; 126720Sigor@sysoev.ru 126820Sigor@sysoev.ru if (nxt_file_write(&file, pid, length, 0) != length) { 126920Sigor@sysoev.ru return NXT_ERROR; 127020Sigor@sysoev.ru } 127120Sigor@sysoev.ru 127220Sigor@sysoev.ru nxt_file_close(task, &file); 127320Sigor@sysoev.ru 127420Sigor@sysoev.ru return NXT_OK; 127520Sigor@sysoev.ru } 127620Sigor@sysoev.ru 127720Sigor@sysoev.ru 127820Sigor@sysoev.ru nxt_process_t * 127942Smax.romanov@nginx.com nxt_runtime_process_new(nxt_runtime_t *rt) 128020Sigor@sysoev.ru { 128120Sigor@sysoev.ru nxt_process_t *process; 128220Sigor@sysoev.ru 128320Sigor@sysoev.ru /* TODO: memory failures. */ 128420Sigor@sysoev.ru 128565Sigor@sysoev.ru process = nxt_mp_zalloc(rt->mem_pool, sizeof(nxt_process_t)); 128642Smax.romanov@nginx.com if (nxt_slow_path(process == NULL)) { 128742Smax.romanov@nginx.com return NULL; 128842Smax.romanov@nginx.com } 128942Smax.romanov@nginx.com 129042Smax.romanov@nginx.com nxt_queue_init(&process->ports); 129142Smax.romanov@nginx.com 1292364Smax.romanov@nginx.com nxt_thread_mutex_create(&process->incoming.mutex); 1293364Smax.romanov@nginx.com nxt_thread_mutex_create(&process->outgoing.mutex); 1294141Smax.romanov@nginx.com nxt_thread_mutex_create(&process->cp_mutex); 129590Smax.romanov@nginx.com 1296349Smax.romanov@nginx.com process->use_count = 1; 1297349Smax.romanov@nginx.com 129842Smax.romanov@nginx.com return process; 129942Smax.romanov@nginx.com } 130042Smax.romanov@nginx.com 130142Smax.romanov@nginx.com 1302196Smax.romanov@nginx.com static void 1303141Smax.romanov@nginx.com nxt_runtime_process_destroy(nxt_runtime_t *rt, nxt_process_t *process) 1304141Smax.romanov@nginx.com { 1305597Sigor@sysoev.ru nxt_port_t *port; 1306341Smax.romanov@nginx.com 1307349Smax.romanov@nginx.com nxt_assert(process->use_count == 0); 1308196Smax.romanov@nginx.com nxt_assert(process->registered == 0); 1309164Smax.romanov@nginx.com 1310364Smax.romanov@nginx.com nxt_port_mmaps_destroy(&process->incoming, 1); 1311364Smax.romanov@nginx.com nxt_port_mmaps_destroy(&process->outgoing, 1); 1312141Smax.romanov@nginx.com 1313597Sigor@sysoev.ru do { 1314597Sigor@sysoev.ru port = nxt_port_hash_retrieve(&process->connected_ports); 1315597Sigor@sysoev.ru 1316597Sigor@sysoev.ru } while (port != NULL); 1317141Smax.romanov@nginx.com 1318364Smax.romanov@nginx.com nxt_thread_mutex_destroy(&process->incoming.mutex); 1319364Smax.romanov@nginx.com nxt_thread_mutex_destroy(&process->outgoing.mutex); 1320141Smax.romanov@nginx.com nxt_thread_mutex_destroy(&process->cp_mutex); 1321141Smax.romanov@nginx.com 1322141Smax.romanov@nginx.com nxt_mp_free(rt->mem_pool, process); 1323141Smax.romanov@nginx.com } 1324141Smax.romanov@nginx.com 1325141Smax.romanov@nginx.com 132642Smax.romanov@nginx.com static nxt_int_t 132742Smax.romanov@nginx.com nxt_runtime_lvlhsh_pid_test(nxt_lvlhsh_query_t *lhq, void *data) 132842Smax.romanov@nginx.com { 132942Smax.romanov@nginx.com nxt_process_t *process; 133042Smax.romanov@nginx.com 133142Smax.romanov@nginx.com process = data; 133242Smax.romanov@nginx.com 1333277Sigor@sysoev.ru if (lhq->key.length == sizeof(nxt_pid_t) 1334277Sigor@sysoev.ru && *(nxt_pid_t *) lhq->key.start == process->pid) 1335277Sigor@sysoev.ru { 133642Smax.romanov@nginx.com return NXT_OK; 133742Smax.romanov@nginx.com } 133842Smax.romanov@nginx.com 133942Smax.romanov@nginx.com return NXT_DECLINED; 134042Smax.romanov@nginx.com } 134142Smax.romanov@nginx.com 134242Smax.romanov@nginx.com static const nxt_lvlhsh_proto_t lvlhsh_processes_proto nxt_aligned(64) = { 134342Smax.romanov@nginx.com NXT_LVLHSH_DEFAULT, 134442Smax.romanov@nginx.com nxt_runtime_lvlhsh_pid_test, 134542Smax.romanov@nginx.com nxt_lvlhsh_alloc, 134642Smax.romanov@nginx.com nxt_lvlhsh_free, 134742Smax.romanov@nginx.com }; 134842Smax.romanov@nginx.com 134942Smax.romanov@nginx.com 1350196Smax.romanov@nginx.com nxt_inline void 1351196Smax.romanov@nginx.com nxt_runtime_process_lhq_pid(nxt_lvlhsh_query_t *lhq, nxt_pid_t *pid) 1352196Smax.romanov@nginx.com { 1353196Smax.romanov@nginx.com lhq->key_hash = nxt_murmur_hash2(pid, sizeof(*pid)); 1354196Smax.romanov@nginx.com lhq->key.length = sizeof(*pid); 1355196Smax.romanov@nginx.com lhq->key.start = (u_char *) pid; 1356196Smax.romanov@nginx.com lhq->proto = &lvlhsh_processes_proto; 1357196Smax.romanov@nginx.com } 1358196Smax.romanov@nginx.com 1359196Smax.romanov@nginx.com 136042Smax.romanov@nginx.com nxt_process_t * 136142Smax.romanov@nginx.com nxt_runtime_process_find(nxt_runtime_t *rt, nxt_pid_t pid) 136242Smax.romanov@nginx.com { 1363196Smax.romanov@nginx.com nxt_process_t *process; 136442Smax.romanov@nginx.com nxt_lvlhsh_query_t lhq; 136542Smax.romanov@nginx.com 1366196Smax.romanov@nginx.com process = NULL; 1367196Smax.romanov@nginx.com 1368196Smax.romanov@nginx.com nxt_runtime_process_lhq_pid(&lhq, &pid); 1369196Smax.romanov@nginx.com 1370196Smax.romanov@nginx.com nxt_thread_mutex_lock(&rt->processes_mutex); 137142Smax.romanov@nginx.com 137242Smax.romanov@nginx.com if (nxt_lvlhsh_find(&rt->processes, &lhq) == NXT_OK) { 1373196Smax.romanov@nginx.com process = lhq.value; 1374196Smax.romanov@nginx.com 1375196Smax.romanov@nginx.com } else { 1376196Smax.romanov@nginx.com nxt_thread_log_debug("process %PI not found", pid); 137742Smax.romanov@nginx.com } 137842Smax.romanov@nginx.com 1379196Smax.romanov@nginx.com nxt_thread_mutex_unlock(&rt->processes_mutex); 138042Smax.romanov@nginx.com 1381196Smax.romanov@nginx.com return process; 138242Smax.romanov@nginx.com } 138342Smax.romanov@nginx.com 138442Smax.romanov@nginx.com 138542Smax.romanov@nginx.com nxt_process_t * 138642Smax.romanov@nginx.com nxt_runtime_process_get(nxt_runtime_t *rt, nxt_pid_t pid) 138742Smax.romanov@nginx.com { 138842Smax.romanov@nginx.com nxt_process_t *process; 138942Smax.romanov@nginx.com nxt_lvlhsh_query_t lhq; 139042Smax.romanov@nginx.com 1391196Smax.romanov@nginx.com nxt_runtime_process_lhq_pid(&lhq, &pid); 1392196Smax.romanov@nginx.com 1393196Smax.romanov@nginx.com nxt_thread_mutex_lock(&rt->processes_mutex); 139442Smax.romanov@nginx.com 139542Smax.romanov@nginx.com if (nxt_lvlhsh_find(&rt->processes, &lhq) == NXT_OK) { 139642Smax.romanov@nginx.com nxt_thread_log_debug("process %PI found", pid); 1397196Smax.romanov@nginx.com 1398196Smax.romanov@nginx.com nxt_thread_mutex_unlock(&rt->processes_mutex); 1399349Smax.romanov@nginx.com 1400349Smax.romanov@nginx.com process = lhq.value; 1401349Smax.romanov@nginx.com process->use_count++; 1402349Smax.romanov@nginx.com 1403349Smax.romanov@nginx.com return process; 140442Smax.romanov@nginx.com } 140542Smax.romanov@nginx.com 140642Smax.romanov@nginx.com process = nxt_runtime_process_new(rt); 140720Sigor@sysoev.ru if (nxt_slow_path(process == NULL)) { 1408382Smax.romanov@nginx.com 1409382Smax.romanov@nginx.com nxt_thread_mutex_unlock(&rt->processes_mutex); 1410382Smax.romanov@nginx.com 141120Sigor@sysoev.ru return NULL; 141220Sigor@sysoev.ru } 141320Sigor@sysoev.ru 141442Smax.romanov@nginx.com process->pid = pid; 141542Smax.romanov@nginx.com 141642Smax.romanov@nginx.com lhq.replace = 0; 141742Smax.romanov@nginx.com lhq.value = process; 141842Smax.romanov@nginx.com lhq.pool = rt->mem_pool; 141942Smax.romanov@nginx.com 142042Smax.romanov@nginx.com switch (nxt_lvlhsh_insert(&rt->processes, &lhq)) { 142142Smax.romanov@nginx.com 142242Smax.romanov@nginx.com case NXT_OK: 142342Smax.romanov@nginx.com if (rt->nprocesses == 0) { 142442Smax.romanov@nginx.com rt->mprocess = process; 142542Smax.romanov@nginx.com } 142642Smax.romanov@nginx.com 142742Smax.romanov@nginx.com rt->nprocesses++; 142842Smax.romanov@nginx.com 1429196Smax.romanov@nginx.com process->registered = 1; 1430196Smax.romanov@nginx.com 143142Smax.romanov@nginx.com nxt_thread_log_debug("process %PI insert", pid); 143242Smax.romanov@nginx.com break; 143342Smax.romanov@nginx.com 143442Smax.romanov@nginx.com default: 143542Smax.romanov@nginx.com nxt_thread_log_debug("process %PI insert failed", pid); 143642Smax.romanov@nginx.com break; 143720Sigor@sysoev.ru } 143820Sigor@sysoev.ru 1439196Smax.romanov@nginx.com nxt_thread_mutex_unlock(&rt->processes_mutex); 1440196Smax.romanov@nginx.com 144120Sigor@sysoev.ru return process; 144220Sigor@sysoev.ru } 144342Smax.romanov@nginx.com 144442Smax.romanov@nginx.com 144542Smax.romanov@nginx.com void 1446343Smax.romanov@nginx.com nxt_runtime_process_add(nxt_task_t *task, nxt_process_t *process) 144742Smax.romanov@nginx.com { 144842Smax.romanov@nginx.com nxt_port_t *port; 1449343Smax.romanov@nginx.com nxt_runtime_t *rt; 145042Smax.romanov@nginx.com nxt_lvlhsh_query_t lhq; 145142Smax.romanov@nginx.com 1452196Smax.romanov@nginx.com nxt_assert(process->registered == 0); 1453196Smax.romanov@nginx.com 1454343Smax.romanov@nginx.com rt = task->thread->runtime; 1455343Smax.romanov@nginx.com 1456196Smax.romanov@nginx.com nxt_runtime_process_lhq_pid(&lhq, &process->pid); 1457196Smax.romanov@nginx.com 145842Smax.romanov@nginx.com lhq.replace = 0; 145942Smax.romanov@nginx.com lhq.value = process; 146042Smax.romanov@nginx.com lhq.pool = rt->mem_pool; 146142Smax.romanov@nginx.com 1462196Smax.romanov@nginx.com nxt_thread_mutex_lock(&rt->processes_mutex); 1463196Smax.romanov@nginx.com 146442Smax.romanov@nginx.com switch (nxt_lvlhsh_insert(&rt->processes, &lhq)) { 146542Smax.romanov@nginx.com 146642Smax.romanov@nginx.com case NXT_OK: 146742Smax.romanov@nginx.com if (rt->nprocesses == 0) { 146842Smax.romanov@nginx.com rt->mprocess = process; 146942Smax.romanov@nginx.com } 147042Smax.romanov@nginx.com 147142Smax.romanov@nginx.com rt->nprocesses++; 147242Smax.romanov@nginx.com 147342Smax.romanov@nginx.com nxt_process_port_each(process, port) { 147442Smax.romanov@nginx.com 1475141Smax.romanov@nginx.com port->pid = process->pid; 1476141Smax.romanov@nginx.com 1477343Smax.romanov@nginx.com nxt_runtime_port_add(task, port); 147842Smax.romanov@nginx.com 147942Smax.romanov@nginx.com } nxt_process_port_loop; 148042Smax.romanov@nginx.com 1481196Smax.romanov@nginx.com process->registered = 1; 1482196Smax.romanov@nginx.com 1483196Smax.romanov@nginx.com nxt_thread_log_debug("process %PI added", process->pid); 148442Smax.romanov@nginx.com break; 148542Smax.romanov@nginx.com 148642Smax.romanov@nginx.com default: 1487196Smax.romanov@nginx.com nxt_thread_log_debug("process %PI failed to add", process->pid); 148842Smax.romanov@nginx.com break; 148942Smax.romanov@nginx.com } 1490196Smax.romanov@nginx.com 1491196Smax.romanov@nginx.com nxt_thread_mutex_unlock(&rt->processes_mutex); 1492196Smax.romanov@nginx.com } 1493196Smax.romanov@nginx.com 1494196Smax.romanov@nginx.com 1495196Smax.romanov@nginx.com static nxt_process_t * 1496196Smax.romanov@nginx.com nxt_runtime_process_remove_pid(nxt_runtime_t *rt, nxt_pid_t pid) 1497196Smax.romanov@nginx.com { 1498196Smax.romanov@nginx.com nxt_process_t *process; 1499196Smax.romanov@nginx.com nxt_lvlhsh_query_t lhq; 1500196Smax.romanov@nginx.com 1501196Smax.romanov@nginx.com process = NULL; 1502196Smax.romanov@nginx.com 1503196Smax.romanov@nginx.com nxt_runtime_process_lhq_pid(&lhq, &pid); 1504196Smax.romanov@nginx.com 1505196Smax.romanov@nginx.com lhq.pool = rt->mem_pool; 1506196Smax.romanov@nginx.com 1507196Smax.romanov@nginx.com nxt_thread_mutex_lock(&rt->processes_mutex); 1508196Smax.romanov@nginx.com 1509196Smax.romanov@nginx.com switch (nxt_lvlhsh_delete(&rt->processes, &lhq)) { 1510196Smax.romanov@nginx.com 1511196Smax.romanov@nginx.com case NXT_OK: 1512196Smax.romanov@nginx.com rt->nprocesses--; 1513196Smax.romanov@nginx.com 1514196Smax.romanov@nginx.com process = lhq.value; 1515196Smax.romanov@nginx.com 1516196Smax.romanov@nginx.com process->registered = 0; 1517196Smax.romanov@nginx.com 1518196Smax.romanov@nginx.com nxt_thread_log_debug("process %PI removed", pid); 1519196Smax.romanov@nginx.com break; 1520196Smax.romanov@nginx.com 1521196Smax.romanov@nginx.com default: 1522196Smax.romanov@nginx.com nxt_thread_log_debug("process %PI remove failed", pid); 1523196Smax.romanov@nginx.com break; 1524196Smax.romanov@nginx.com } 1525196Smax.romanov@nginx.com 1526196Smax.romanov@nginx.com nxt_thread_mutex_unlock(&rt->processes_mutex); 1527196Smax.romanov@nginx.com 1528196Smax.romanov@nginx.com return process; 152942Smax.romanov@nginx.com } 153042Smax.romanov@nginx.com 153142Smax.romanov@nginx.com 153242Smax.romanov@nginx.com void 1533349Smax.romanov@nginx.com nxt_process_use(nxt_task_t *task, nxt_process_t *process, int i) 153442Smax.romanov@nginx.com { 1535343Smax.romanov@nginx.com nxt_runtime_t *rt; 1536343Smax.romanov@nginx.com 1537349Smax.romanov@nginx.com process->use_count += i; 1538349Smax.romanov@nginx.com 1539349Smax.romanov@nginx.com if (process->use_count == 0) { 1540349Smax.romanov@nginx.com rt = task->thread->runtime; 1541349Smax.romanov@nginx.com 1542196Smax.romanov@nginx.com if (process->registered == 1) { 1543196Smax.romanov@nginx.com nxt_runtime_process_remove_pid(rt, process->pid); 1544164Smax.romanov@nginx.com } 1545164Smax.romanov@nginx.com 1546196Smax.romanov@nginx.com nxt_runtime_process_destroy(rt, process); 154742Smax.romanov@nginx.com } 154842Smax.romanov@nginx.com } 154942Smax.romanov@nginx.com 155042Smax.romanov@nginx.com 155142Smax.romanov@nginx.com nxt_process_t * 155242Smax.romanov@nginx.com nxt_runtime_process_first(nxt_runtime_t *rt, nxt_lvlhsh_each_t *lhe) 155342Smax.romanov@nginx.com { 1554598Sigor@sysoev.ru nxt_lvlhsh_each_init(lhe, &lvlhsh_processes_proto); 155542Smax.romanov@nginx.com 155642Smax.romanov@nginx.com return nxt_runtime_process_next(rt, lhe); 155742Smax.romanov@nginx.com } 155842Smax.romanov@nginx.com 155942Smax.romanov@nginx.com 156042Smax.romanov@nginx.com void 1561343Smax.romanov@nginx.com nxt_runtime_port_add(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_add(&rt->ports, port); 1569348Smax.romanov@nginx.com 1570348Smax.romanov@nginx.com if (res != NXT_OK) { 1571348Smax.romanov@nginx.com return; 1572348Smax.romanov@nginx.com } 1573141Smax.romanov@nginx.com 1574141Smax.romanov@nginx.com rt->port_by_type[port->type] = port; 1575343Smax.romanov@nginx.com 1576343Smax.romanov@nginx.com nxt_port_use(task, port, 1); 157742Smax.romanov@nginx.com } 157842Smax.romanov@nginx.com 157942Smax.romanov@nginx.com 158042Smax.romanov@nginx.com void 1581343Smax.romanov@nginx.com nxt_runtime_port_remove(nxt_task_t *task, nxt_port_t *port) 158242Smax.romanov@nginx.com { 1583348Smax.romanov@nginx.com nxt_int_t res; 1584343Smax.romanov@nginx.com nxt_runtime_t *rt; 1585343Smax.romanov@nginx.com 1586343Smax.romanov@nginx.com rt = task->thread->runtime; 1587343Smax.romanov@nginx.com 1588348Smax.romanov@nginx.com res = nxt_port_hash_remove(&rt->ports, port); 1589348Smax.romanov@nginx.com 1590348Smax.romanov@nginx.com if (res != NXT_OK) { 1591348Smax.romanov@nginx.com return; 1592348Smax.romanov@nginx.com } 1593125Smax.romanov@nginx.com 1594141Smax.romanov@nginx.com if (rt->port_by_type[port->type] == port) { 1595141Smax.romanov@nginx.com rt->port_by_type[port->type] = NULL; 1596141Smax.romanov@nginx.com } 1597343Smax.romanov@nginx.com 1598343Smax.romanov@nginx.com nxt_port_use(task, port, -1); 159942Smax.romanov@nginx.com } 160042Smax.romanov@nginx.com 160142Smax.romanov@nginx.com 160242Smax.romanov@nginx.com nxt_port_t * 160342Smax.romanov@nginx.com nxt_runtime_port_find(nxt_runtime_t *rt, nxt_pid_t pid, 160442Smax.romanov@nginx.com nxt_port_id_t port_id) 160542Smax.romanov@nginx.com { 160675Smax.romanov@nginx.com return nxt_port_hash_find(&rt->ports, pid, port_id); 160742Smax.romanov@nginx.com } 1608