xref: /unit/src/nxt_router.c (revision 1566)
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 
853Sigor@sysoev.ru #include <nxt_router.h>
9115Sigor@sysoev.ru #include <nxt_conf.h>
10774Svbart@nginx.com #if (NXT_TLS)
11774Svbart@nginx.com #include <nxt_cert.h>
12774Svbart@nginx.com #endif
13431Sigor@sysoev.ru #include <nxt_http.h>
14743Smax.romanov@nginx.com #include <nxt_port_memory_int.h>
15743Smax.romanov@nginx.com #include <nxt_unit_request.h>
16743Smax.romanov@nginx.com #include <nxt_unit_response.h>
171131Smax.romanov@nginx.com #include <nxt_router_request.h>
181555Smax.romanov@nginx.com #include <nxt_app_queue.h>
191555Smax.romanov@nginx.com #include <nxt_port_queue.h>
2020Sigor@sysoev.ru 
21115Sigor@sysoev.ru typedef struct {
22318Smax.romanov@nginx.com     nxt_str_t         type;
23507Smax.romanov@nginx.com     uint32_t          processes;
24507Smax.romanov@nginx.com     uint32_t          max_processes;
25507Smax.romanov@nginx.com     uint32_t          spare_processes;
26318Smax.romanov@nginx.com     nxt_msec_t        timeout;
27427Smax.romanov@nginx.com     nxt_msec_t        res_timeout;
28507Smax.romanov@nginx.com     nxt_msec_t        idle_timeout;
29318Smax.romanov@nginx.com     uint32_t          requests;
30318Smax.romanov@nginx.com     nxt_conf_value_t  *limits_value;
31507Smax.romanov@nginx.com     nxt_conf_value_t  *processes_value;
321473Svbart@nginx.com     nxt_conf_value_t  *targets_value;
33133Sigor@sysoev.ru } nxt_router_app_conf_t;
34133Sigor@sysoev.ru 
35133Sigor@sysoev.ru 
36133Sigor@sysoev.ru typedef struct {
37964Sigor@sysoev.ru     nxt_str_t         pass;
38964Sigor@sysoev.ru     nxt_str_t         application;
39115Sigor@sysoev.ru } nxt_router_listener_conf_t;
40115Sigor@sysoev.ru 
41115Sigor@sysoev.ru 
42774Svbart@nginx.com #if (NXT_TLS)
43774Svbart@nginx.com 
44774Svbart@nginx.com typedef struct {
45774Svbart@nginx.com     nxt_str_t          name;
46774Svbart@nginx.com     nxt_socket_conf_t  *conf;
47774Svbart@nginx.com 
48774Svbart@nginx.com     nxt_queue_link_t   link;  /* for nxt_socket_conf_t.tls */
49774Svbart@nginx.com } nxt_router_tlssock_t;
50774Svbart@nginx.com 
51774Svbart@nginx.com #endif
52774Svbart@nginx.com 
53774Svbart@nginx.com 
54198Sigor@sysoev.ru typedef struct {
55198Sigor@sysoev.ru     nxt_socket_conf_t       *socket_conf;
56198Sigor@sysoev.ru     nxt_router_temp_conf_t  *temp_conf;
57198Sigor@sysoev.ru } nxt_socket_rpc_t;
58198Sigor@sysoev.ru 
59198Sigor@sysoev.ru 
60507Smax.romanov@nginx.com typedef struct {
61507Smax.romanov@nginx.com     nxt_app_t               *app;
62507Smax.romanov@nginx.com     nxt_router_temp_conf_t  *temp_conf;
63507Smax.romanov@nginx.com } nxt_app_rpc_t;
64507Smax.romanov@nginx.com 
65507Smax.romanov@nginx.com 
661488St.nateldemoura@f5.com static nxt_int_t nxt_router_prefork(nxt_task_t *task, nxt_process_t *process,
671488St.nateldemoura@f5.com     nxt_mp_t *mp);
681488St.nateldemoura@f5.com static nxt_int_t nxt_router_start(nxt_task_t *task, nxt_process_data_t *data);
69662Smax.romanov@nginx.com static void nxt_router_greet_controller(nxt_task_t *task,
70662Smax.romanov@nginx.com     nxt_port_t *controller_port);
71662Smax.romanov@nginx.com 
72507Smax.romanov@nginx.com static nxt_int_t nxt_router_start_app_process(nxt_task_t *task, nxt_app_t *app);
73425Smax.romanov@nginx.com 
741552Smax.romanov@nginx.com static void nxt_router_new_port_handler(nxt_task_t *task,
751552Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
761552Smax.romanov@nginx.com static void nxt_router_conf_data_handler(nxt_task_t *task,
771552Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
781552Smax.romanov@nginx.com static void nxt_router_remove_pid_handler(nxt_task_t *task,
791552Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
801552Smax.romanov@nginx.com static void nxt_router_access_log_reopen_handler(nxt_task_t *task,
811552Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
821552Smax.romanov@nginx.com 
83139Sigor@sysoev.ru static nxt_router_temp_conf_t *nxt_router_temp_conf(nxt_task_t *task);
84198Sigor@sysoev.ru static void nxt_router_conf_apply(nxt_task_t *task, void *obj, void *data);
85198Sigor@sysoev.ru static void nxt_router_conf_ready(nxt_task_t *task,
86139Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
87139Sigor@sysoev.ru static void nxt_router_conf_error(nxt_task_t *task,
88139Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
89139Sigor@sysoev.ru static void nxt_router_conf_send(nxt_task_t *task,
90193Smax.romanov@nginx.com     nxt_router_temp_conf_t *tmcf, nxt_port_msg_type_t type);
9153Sigor@sysoev.ru 
92115Sigor@sysoev.ru static nxt_int_t nxt_router_conf_create(nxt_task_t *task,
93115Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf, u_char *start, u_char *end);
941183Svbart@nginx.com static nxt_int_t nxt_router_conf_process_static(nxt_task_t *task,
951183Svbart@nginx.com     nxt_router_conf_t *rtcf, nxt_conf_value_t *conf);
961563Svbart@nginx.com 
97133Sigor@sysoev.ru static nxt_app_t *nxt_router_app_find(nxt_queue_t *queue, nxt_str_t *name);
981563Svbart@nginx.com static nxt_int_t nxt_router_apps_hash_test(nxt_lvlhsh_query_t *lhq, void *data);
991563Svbart@nginx.com static nxt_int_t nxt_router_apps_hash_add(nxt_router_conf_t *rtcf,
1001563Svbart@nginx.com     nxt_app_t *app);
1011563Svbart@nginx.com static nxt_app_t *nxt_router_apps_hash_get(nxt_router_conf_t *rtcf,
1021563Svbart@nginx.com     nxt_str_t *name);
1031563Svbart@nginx.com static void nxt_router_apps_hash_use(nxt_task_t *task, nxt_router_conf_t *rtcf,
1041563Svbart@nginx.com     int i);
1051563Svbart@nginx.com 
1061555Smax.romanov@nginx.com static nxt_int_t nxt_router_app_queue_init(nxt_task_t *task,
1071555Smax.romanov@nginx.com     nxt_port_t *port);
1081555Smax.romanov@nginx.com static nxt_int_t nxt_router_port_queue_init(nxt_task_t *task,
1091555Smax.romanov@nginx.com     nxt_port_t *port);
1101555Smax.romanov@nginx.com static nxt_int_t nxt_router_port_queue_map(nxt_task_t *task,
1111555Smax.romanov@nginx.com     nxt_port_t *port, nxt_fd_t fd);
112198Sigor@sysoev.ru static void nxt_router_listen_socket_rpc_create(nxt_task_t *task,
113198Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf, nxt_socket_conf_t *skcf);
114198Sigor@sysoev.ru static void nxt_router_listen_socket_ready(nxt_task_t *task,
115198Sigor@sysoev.ru     nxt_port_recv_msg_t *msg, void *data);
116198Sigor@sysoev.ru static void nxt_router_listen_socket_error(nxt_task_t *task,
117198Sigor@sysoev.ru     nxt_port_recv_msg_t *msg, void *data);
118774Svbart@nginx.com #if (NXT_TLS)
119774Svbart@nginx.com static void nxt_router_tls_rpc_create(nxt_task_t *task,
120774Svbart@nginx.com     nxt_router_temp_conf_t *tmcf, nxt_router_tlssock_t *tls);
121774Svbart@nginx.com static void nxt_router_tls_rpc_handler(nxt_task_t *task,
122774Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
123774Svbart@nginx.com #endif
124507Smax.romanov@nginx.com static void nxt_router_app_rpc_create(nxt_task_t *task,
125507Smax.romanov@nginx.com     nxt_router_temp_conf_t *tmcf, nxt_app_t *app);
126507Smax.romanov@nginx.com static void nxt_router_app_prefork_ready(nxt_task_t *task,
127507Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, void *data);
128507Smax.romanov@nginx.com static void nxt_router_app_prefork_error(nxt_task_t *task,
129507Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, void *data);
130359Sigor@sysoev.ru static nxt_socket_conf_t *nxt_router_socket_conf(nxt_task_t *task,
131359Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf, nxt_str_t *name);
132359Sigor@sysoev.ru static nxt_int_t nxt_router_listen_socket_find(nxt_router_temp_conf_t *tmcf,
133359Sigor@sysoev.ru     nxt_socket_conf_t *nskcf, nxt_sockaddr_t *sa);
13453Sigor@sysoev.ru 
13553Sigor@sysoev.ru static nxt_int_t nxt_router_engines_create(nxt_task_t *task,
13653Sigor@sysoev.ru     nxt_router_t *router, nxt_router_temp_conf_t *tmcf,
13753Sigor@sysoev.ru     const nxt_event_interface_t *interface);
138115Sigor@sysoev.ru static nxt_int_t nxt_router_engine_conf_create(nxt_router_temp_conf_t *tmcf,
139115Sigor@sysoev.ru     nxt_router_engine_conf_t *recf);
140115Sigor@sysoev.ru static nxt_int_t nxt_router_engine_conf_update(nxt_router_temp_conf_t *tmcf,
141115Sigor@sysoev.ru     nxt_router_engine_conf_t *recf);
142115Sigor@sysoev.ru static nxt_int_t nxt_router_engine_conf_delete(nxt_router_temp_conf_t *tmcf,
143115Sigor@sysoev.ru     nxt_router_engine_conf_t *recf);
144154Sigor@sysoev.ru static nxt_int_t nxt_router_engine_joints_create(nxt_router_temp_conf_t *tmcf,
145154Sigor@sysoev.ru     nxt_router_engine_conf_t *recf, nxt_queue_t *sockets,
146154Sigor@sysoev.ru     nxt_work_handler_t handler);
147313Sigor@sysoev.ru static nxt_int_t nxt_router_engine_quit(nxt_router_temp_conf_t *tmcf,
148313Sigor@sysoev.ru     nxt_router_engine_conf_t *recf);
149139Sigor@sysoev.ru static nxt_int_t nxt_router_engine_joints_delete(nxt_router_temp_conf_t *tmcf,
150139Sigor@sysoev.ru     nxt_router_engine_conf_t *recf, nxt_queue_t *sockets);
15153Sigor@sysoev.ru 
15253Sigor@sysoev.ru static nxt_int_t nxt_router_threads_create(nxt_task_t *task, nxt_runtime_t *rt,
15353Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
15453Sigor@sysoev.ru static nxt_int_t nxt_router_thread_create(nxt_task_t *task, nxt_runtime_t *rt,
15553Sigor@sysoev.ru     nxt_event_engine_t *engine);
156343Smax.romanov@nginx.com static void nxt_router_apps_sort(nxt_task_t *task, nxt_router_t *router,
157133Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
15853Sigor@sysoev.ru 
159315Sigor@sysoev.ru static void nxt_router_engines_post(nxt_router_t *router,
160315Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
161315Sigor@sysoev.ru static void nxt_router_engine_post(nxt_event_engine_t *engine,
162315Sigor@sysoev.ru     nxt_work_t *jobs);
16353Sigor@sysoev.ru 
16453Sigor@sysoev.ru static void nxt_router_thread_start(void *data);
1651545Smax.romanov@nginx.com static void nxt_router_rt_add_port(nxt_task_t *task, void *obj,
1661545Smax.romanov@nginx.com     void *data);
16753Sigor@sysoev.ru static void nxt_router_listen_socket_create(nxt_task_t *task, void *obj,
16853Sigor@sysoev.ru     void *data);
16953Sigor@sysoev.ru static void nxt_router_listen_socket_update(nxt_task_t *task, void *obj,
17053Sigor@sysoev.ru     void *data);
17153Sigor@sysoev.ru static void nxt_router_listen_socket_delete(nxt_task_t *task, void *obj,
17253Sigor@sysoev.ru     void *data);
173313Sigor@sysoev.ru static void nxt_router_worker_thread_quit(nxt_task_t *task, void *obj,
174313Sigor@sysoev.ru     void *data);
17553Sigor@sysoev.ru static void nxt_router_listen_socket_close(nxt_task_t *task, void *obj,
17653Sigor@sysoev.ru     void *data);
17753Sigor@sysoev.ru static void nxt_router_thread_exit_handler(nxt_task_t *task, void *obj,
17853Sigor@sysoev.ru     void *data);
1791547Smax.romanov@nginx.com static void nxt_router_req_headers_ack_handler(nxt_task_t *task,
1801547Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, nxt_request_rpc_data_t *req_rpc_data);
181359Sigor@sysoev.ru static void nxt_router_listen_socket_release(nxt_task_t *task,
182359Sigor@sysoev.ru     nxt_socket_conf_t *skcf);
18353Sigor@sysoev.ru 
184630Svbart@nginx.com static void nxt_router_access_log_writer(nxt_task_t *task,
185630Svbart@nginx.com     nxt_http_request_t *r, nxt_router_access_log_t *access_log);
186630Svbart@nginx.com static u_char *nxt_router_access_log_date(u_char *buf, nxt_realtime_t *now,
187630Svbart@nginx.com     struct tm *tm, size_t size, const char *format);
188630Svbart@nginx.com static void nxt_router_access_log_open(nxt_task_t *task,
189630Svbart@nginx.com     nxt_router_temp_conf_t *tmcf);
190630Svbart@nginx.com static void nxt_router_access_log_ready(nxt_task_t *task,
191630Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
192630Svbart@nginx.com static void nxt_router_access_log_error(nxt_task_t *task,
193630Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
194630Svbart@nginx.com static void nxt_router_access_log_release(nxt_task_t *task,
195630Svbart@nginx.com     nxt_thread_spinlock_t *lock, nxt_router_access_log_t *access_log);
196651Svbart@nginx.com static void nxt_router_access_log_reopen_completion(nxt_task_t *task, void *obj,
197651Svbart@nginx.com     void *data);
198631Svbart@nginx.com static void nxt_router_access_log_reopen_ready(nxt_task_t *task,
199631Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
200631Svbart@nginx.com static void nxt_router_access_log_reopen_error(nxt_task_t *task,
201631Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
202630Svbart@nginx.com 
203343Smax.romanov@nginx.com static void nxt_router_app_port_ready(nxt_task_t *task,
204343Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, void *data);
2051547Smax.romanov@nginx.com static nxt_int_t nxt_router_app_shared_port_send(nxt_task_t *task,
2061547Smax.romanov@nginx.com     nxt_port_t *app_port);
207343Smax.romanov@nginx.com static void nxt_router_app_port_error(nxt_task_t *task,
208343Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, void *data);
209343Smax.romanov@nginx.com 
2101563Svbart@nginx.com static void nxt_router_app_use(nxt_task_t *task, nxt_app_t *app, int i);
211753Smax.romanov@nginx.com static void nxt_router_app_unlink(nxt_task_t *task, nxt_app_t *app);
2121123Smax.romanov@nginx.com 
213343Smax.romanov@nginx.com static void nxt_router_app_port_release(nxt_task_t *task, nxt_port_t *port,
2141123Smax.romanov@nginx.com     nxt_apr_action_t action);
2151547Smax.romanov@nginx.com static void nxt_router_app_port_get(nxt_task_t *task, nxt_app_t *app,
2161547Smax.romanov@nginx.com     nxt_request_rpc_data_t *req_rpc_data);
2171561Smax.romanov@nginx.com static void nxt_router_http_request_error(nxt_task_t *task, void *obj,
2181561Smax.romanov@nginx.com     void *data);
2191547Smax.romanov@nginx.com static void nxt_router_http_request_done(nxt_task_t *task, void *obj,
2201547Smax.romanov@nginx.com     void *data);
221141Smax.romanov@nginx.com 
222*1566Smax.romanov@nginx.com static void nxt_router_dummy_buf_completion(nxt_task_t *task, void *obj,
223*1566Smax.romanov@nginx.com     void *data);
224425Smax.romanov@nginx.com static void nxt_router_app_prepare_request(nxt_task_t *task,
2251547Smax.romanov@nginx.com     nxt_request_rpc_data_t *req_rpc_data);
2261007Salexander.borisov@nginx.com static nxt_buf_t *nxt_router_prepare_msg(nxt_task_t *task,
2271547Smax.romanov@nginx.com     nxt_http_request_t *r, nxt_app_t *app, const nxt_str_t *prefix);
228510Salexander.borisov@nginx.com 
229318Smax.romanov@nginx.com static void nxt_router_app_timeout(nxt_task_t *task, void *obj, void *data);
230507Smax.romanov@nginx.com static void nxt_router_adjust_idle_timer(nxt_task_t *task, void *obj,
231507Smax.romanov@nginx.com     void *data);
232507Smax.romanov@nginx.com static void nxt_router_app_idle_timeout(nxt_task_t *task, void *obj,
233507Smax.romanov@nginx.com     void *data);
234753Smax.romanov@nginx.com static void nxt_router_app_joint_release_handler(nxt_task_t *task, void *obj,
235507Smax.romanov@nginx.com     void *data);
236753Smax.romanov@nginx.com static void nxt_router_free_app(nxt_task_t *task, void *obj, void *data);
237431Sigor@sysoev.ru 
238431Sigor@sysoev.ru static const nxt_http_request_state_t  nxt_http_request_send_state;
239431Sigor@sysoev.ru static void nxt_http_request_send_body(nxt_task_t *task, void *obj, void *data);
240141Smax.romanov@nginx.com 
241753Smax.romanov@nginx.com static void nxt_router_app_joint_use(nxt_task_t *task,
242753Smax.romanov@nginx.com     nxt_app_joint_t *app_joint, int i);
243753Smax.romanov@nginx.com 
2441547Smax.romanov@nginx.com static void nxt_router_http_request_release_post(nxt_task_t *task,
2451007Salexander.borisov@nginx.com     nxt_http_request_t *r);
2461007Salexander.borisov@nginx.com static void nxt_router_http_request_release(nxt_task_t *task, void *obj,
2471007Salexander.borisov@nginx.com     void *data);
2481321Smax.romanov@nginx.com static void nxt_router_oosm_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
2491545Smax.romanov@nginx.com static void nxt_router_get_port_handler(nxt_task_t *task,
2501545Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
2511546Smax.romanov@nginx.com static void nxt_router_get_mmap_handler(nxt_task_t *task,
2521546Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
2531007Salexander.borisov@nginx.com 
2541149Smax.romanov@nginx.com extern const nxt_http_request_state_t  nxt_http_websocket;
2551131Smax.romanov@nginx.com 
256119Smax.romanov@nginx.com static nxt_router_t  *nxt_router;
25720Sigor@sysoev.ru 
258743Smax.romanov@nginx.com static const nxt_str_t http_prefix = nxt_string("HTTP_");
259743Smax.romanov@nginx.com static const nxt_str_t empty_prefix = nxt_string("");
260743Smax.romanov@nginx.com 
261743Smax.romanov@nginx.com static const nxt_str_t  *nxt_app_msg_prefix[] = {
262804Svbart@nginx.com     &empty_prefix,
263743Smax.romanov@nginx.com     &http_prefix,
264743Smax.romanov@nginx.com     &http_prefix,
265743Smax.romanov@nginx.com     &http_prefix,
266743Smax.romanov@nginx.com     &http_prefix,
267977Smax.romanov@gmail.com     &empty_prefix,
268216Sigor@sysoev.ru };
269216Sigor@sysoev.ru 
270216Sigor@sysoev.ru 
2711488St.nateldemoura@f5.com static const nxt_port_handlers_t  nxt_router_process_port_handlers = {
2721488St.nateldemoura@f5.com     .quit         = nxt_signal_quit_handler,
273662Smax.romanov@nginx.com     .new_port     = nxt_router_new_port_handler,
2741545Smax.romanov@nginx.com     .get_port     = nxt_router_get_port_handler,
275662Smax.romanov@nginx.com     .change_file  = nxt_port_change_log_file_handler,
276662Smax.romanov@nginx.com     .mmap         = nxt_port_mmap_handler,
2771546Smax.romanov@nginx.com     .get_mmap     = nxt_router_get_mmap_handler,
278662Smax.romanov@nginx.com     .data         = nxt_router_conf_data_handler,
279662Smax.romanov@nginx.com     .remove_pid   = nxt_router_remove_pid_handler,
280662Smax.romanov@nginx.com     .access_log   = nxt_router_access_log_reopen_handler,
281662Smax.romanov@nginx.com     .rpc_ready    = nxt_port_rpc_handler,
282662Smax.romanov@nginx.com     .rpc_error    = nxt_port_rpc_handler,
2831321Smax.romanov@nginx.com     .oosm         = nxt_router_oosm_handler,
284662Smax.romanov@nginx.com };
285662Smax.romanov@nginx.com 
286662Smax.romanov@nginx.com 
2871488St.nateldemoura@f5.com const nxt_process_init_t  nxt_router_process = {
2881488St.nateldemoura@f5.com     .name           = "router",
2891488St.nateldemoura@f5.com     .type           = NXT_PROCESS_ROUTER,
2901488St.nateldemoura@f5.com     .prefork        = nxt_router_prefork,
2911488St.nateldemoura@f5.com     .restart        = 1,
2921488St.nateldemoura@f5.com     .setup          = nxt_process_core_setup,
2931488St.nateldemoura@f5.com     .start          = nxt_router_start,
2941488St.nateldemoura@f5.com     .port_handlers  = &nxt_router_process_port_handlers,
2951488St.nateldemoura@f5.com     .signals        = nxt_process_signals,
2961488St.nateldemoura@f5.com };
2971488St.nateldemoura@f5.com 
2981488St.nateldemoura@f5.com 
2991509Sigor@sysoev.ru /* Queues of nxt_socket_conf_t */
3001509Sigor@sysoev.ru nxt_queue_t  creating_sockets;
3011509Sigor@sysoev.ru nxt_queue_t  pending_sockets;
3021509Sigor@sysoev.ru nxt_queue_t  updating_sockets;
3031509Sigor@sysoev.ru nxt_queue_t  keeping_sockets;
3041509Sigor@sysoev.ru nxt_queue_t  deleting_sockets;
3051509Sigor@sysoev.ru 
3061509Sigor@sysoev.ru 
3071488St.nateldemoura@f5.com static nxt_int_t
3081488St.nateldemoura@f5.com nxt_router_prefork(nxt_task_t *task, nxt_process_t *process, nxt_mp_t *mp)
3091488St.nateldemoura@f5.com {
3101488St.nateldemoura@f5.com     nxt_runtime_stop_app_processes(task, task->thread->runtime);
3111488St.nateldemoura@f5.com 
3121488St.nateldemoura@f5.com     return NXT_OK;
3131488St.nateldemoura@f5.com }
3141488St.nateldemoura@f5.com 
3151488St.nateldemoura@f5.com 
3161488St.nateldemoura@f5.com static nxt_int_t
3171488St.nateldemoura@f5.com nxt_router_start(nxt_task_t *task, nxt_process_data_t *data)
31820Sigor@sysoev.ru {
319141Smax.romanov@nginx.com     nxt_int_t      ret;
320662Smax.romanov@nginx.com     nxt_port_t     *controller_port;
321141Smax.romanov@nginx.com     nxt_router_t   *router;
322141Smax.romanov@nginx.com     nxt_runtime_t  *rt;
323141Smax.romanov@nginx.com 
324141Smax.romanov@nginx.com     rt = task->thread->runtime;
32553Sigor@sysoev.ru 
3261488St.nateldemoura@f5.com     nxt_log(task, NXT_LOG_INFO, "router started");
3271488St.nateldemoura@f5.com 
328771Sigor@sysoev.ru #if (NXT_TLS)
329771Sigor@sysoev.ru     rt->tls = nxt_service_get(rt->services, "SSL/TLS", "OpenSSL");
330771Sigor@sysoev.ru     if (nxt_slow_path(rt->tls == NULL)) {
331771Sigor@sysoev.ru         return NXT_ERROR;
332771Sigor@sysoev.ru     }
333771Sigor@sysoev.ru 
334771Sigor@sysoev.ru     ret = rt->tls->library_init(task);
335771Sigor@sysoev.ru     if (nxt_slow_path(ret != NXT_OK)) {
336771Sigor@sysoev.ru         return ret;
337771Sigor@sysoev.ru     }
338771Sigor@sysoev.ru #endif
339771Sigor@sysoev.ru 
3401459Smax.romanov@nginx.com     ret = nxt_http_init(task);
34188Smax.romanov@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
34288Smax.romanov@nginx.com         return ret;
34388Smax.romanov@nginx.com     }
34488Smax.romanov@nginx.com 
34553Sigor@sysoev.ru     router = nxt_zalloc(sizeof(nxt_router_t));
34653Sigor@sysoev.ru     if (nxt_slow_path(router == NULL)) {
34753Sigor@sysoev.ru         return NXT_ERROR;
34853Sigor@sysoev.ru     }
34953Sigor@sysoev.ru 
35053Sigor@sysoev.ru     nxt_queue_init(&router->engines);
35153Sigor@sysoev.ru     nxt_queue_init(&router->sockets);
352133Sigor@sysoev.ru     nxt_queue_init(&router->apps);
35353Sigor@sysoev.ru 
354119Smax.romanov@nginx.com     nxt_router = router;
355119Smax.romanov@nginx.com 
356662Smax.romanov@nginx.com     controller_port = rt->port_by_type[NXT_PROCESS_CONTROLLER];
357662Smax.romanov@nginx.com     if (controller_port != NULL) {
358662Smax.romanov@nginx.com         nxt_router_greet_controller(task, controller_port);
359662Smax.romanov@nginx.com     }
360662Smax.romanov@nginx.com 
361115Sigor@sysoev.ru     return NXT_OK;
362115Sigor@sysoev.ru }
363115Sigor@sysoev.ru 
364115Sigor@sysoev.ru 
365343Smax.romanov@nginx.com static void
366662Smax.romanov@nginx.com nxt_router_greet_controller(nxt_task_t *task, nxt_port_t *controller_port)
367662Smax.romanov@nginx.com {
368662Smax.romanov@nginx.com     nxt_port_socket_write(task, controller_port, NXT_PORT_MSG_PROCESS_READY,
369662Smax.romanov@nginx.com                           -1, 0, 0, NULL);
370662Smax.romanov@nginx.com }
371662Smax.romanov@nginx.com 
372662Smax.romanov@nginx.com 
373662Smax.romanov@nginx.com static void
374507Smax.romanov@nginx.com nxt_router_start_app_process_handler(nxt_task_t *task, nxt_port_t *port,
375507Smax.romanov@nginx.com     void *data)
376167Smax.romanov@nginx.com {
377343Smax.romanov@nginx.com     size_t         size;
378343Smax.romanov@nginx.com     uint32_t       stream;
379430Sigor@sysoev.ru     nxt_mp_t       *mp;
380648Svbart@nginx.com     nxt_int_t      ret;
381343Smax.romanov@nginx.com     nxt_app_t      *app;
382343Smax.romanov@nginx.com     nxt_buf_t      *b;
383343Smax.romanov@nginx.com     nxt_port_t     *main_port;
384343Smax.romanov@nginx.com     nxt_runtime_t  *rt;
385343Smax.romanov@nginx.com 
386343Smax.romanov@nginx.com     app = data;
387167Smax.romanov@nginx.com 
388167Smax.romanov@nginx.com     rt = task->thread->runtime;
389240Sigor@sysoev.ru     main_port = rt->port_by_type[NXT_PROCESS_MAIN];
390167Smax.romanov@nginx.com 
391507Smax.romanov@nginx.com     nxt_debug(task, "app '%V' %p start process", &app->name, app);
392343Smax.romanov@nginx.com 
393343Smax.romanov@nginx.com     size = app->name.length + 1 + app->conf.length;
394343Smax.romanov@nginx.com 
395343Smax.romanov@nginx.com     b = nxt_buf_mem_ts_alloc(task, task->thread->engine->mem_pool, size);
396343Smax.romanov@nginx.com 
397343Smax.romanov@nginx.com     if (nxt_slow_path(b == NULL)) {
398343Smax.romanov@nginx.com         goto failed;
399167Smax.romanov@nginx.com     }
400167Smax.romanov@nginx.com 
401343Smax.romanov@nginx.com     nxt_buf_cpystr(b, &app->name);
402343Smax.romanov@nginx.com     *b->mem.free++ = '\0';
403343Smax.romanov@nginx.com     nxt_buf_cpystr(b, &app->conf);
404343Smax.romanov@nginx.com 
405753Smax.romanov@nginx.com     nxt_router_app_joint_use(task, app->joint, 1);
406753Smax.romanov@nginx.com 
407343Smax.romanov@nginx.com     stream = nxt_port_rpc_register_handler(task, port,
408343Smax.romanov@nginx.com                                            nxt_router_app_port_ready,
409343Smax.romanov@nginx.com                                            nxt_router_app_port_error,
410753Smax.romanov@nginx.com                                            -1, app->joint);
411343Smax.romanov@nginx.com 
412343Smax.romanov@nginx.com     if (nxt_slow_path(stream == 0)) {
413753Smax.romanov@nginx.com         nxt_router_app_joint_use(task, app->joint, -1);
414753Smax.romanov@nginx.com 
415343Smax.romanov@nginx.com         goto failed;
416343Smax.romanov@nginx.com     }
417343Smax.romanov@nginx.com 
4181488St.nateldemoura@f5.com     ret = nxt_port_socket_write(task, main_port, NXT_PORT_MSG_START_PROCESS,
4191488St.nateldemoura@f5.com                                 -1, stream, port->id, b);
420648Svbart@nginx.com 
421648Svbart@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
422648Svbart@nginx.com         nxt_port_rpc_cancel(task, port, stream);
423753Smax.romanov@nginx.com 
424753Smax.romanov@nginx.com         nxt_router_app_joint_use(task, app->joint, -1);
425753Smax.romanov@nginx.com 
426648Svbart@nginx.com         goto failed;
427648Svbart@nginx.com     }
428343Smax.romanov@nginx.com 
429753Smax.romanov@nginx.com     nxt_router_app_use(task, app, -1);
430753Smax.romanov@nginx.com 
431343Smax.romanov@nginx.com     return;
432343Smax.romanov@nginx.com 
433343Smax.romanov@nginx.com failed:
434343Smax.romanov@nginx.com 
435648Svbart@nginx.com     if (b != NULL) {
436648Svbart@nginx.com         mp = b->data;
437648Svbart@nginx.com         nxt_mp_free(mp, b);
438648Svbart@nginx.com         nxt_mp_release(mp);
439648Svbart@nginx.com     }
440648Svbart@nginx.com 
441343Smax.romanov@nginx.com     nxt_thread_mutex_lock(&app->mutex);
442343Smax.romanov@nginx.com 
443507Smax.romanov@nginx.com     app->pending_processes--;
444343Smax.romanov@nginx.com 
445343Smax.romanov@nginx.com     nxt_thread_mutex_unlock(&app->mutex);
446343Smax.romanov@nginx.com 
447343Smax.romanov@nginx.com     nxt_router_app_use(task, app, -1);
448167Smax.romanov@nginx.com }
449167Smax.romanov@nginx.com 
450167Smax.romanov@nginx.com 
451753Smax.romanov@nginx.com static void
452753Smax.romanov@nginx.com nxt_router_app_joint_use(nxt_task_t *task, nxt_app_joint_t *app_joint, int i)
453753Smax.romanov@nginx.com {
454753Smax.romanov@nginx.com     app_joint->use_count += i;
455753Smax.romanov@nginx.com 
456753Smax.romanov@nginx.com     if (app_joint->use_count == 0) {
457753Smax.romanov@nginx.com         nxt_assert(app_joint->app == NULL);
458753Smax.romanov@nginx.com 
459753Smax.romanov@nginx.com         nxt_free(app_joint);
460753Smax.romanov@nginx.com     }
461753Smax.romanov@nginx.com }
462753Smax.romanov@nginx.com 
463753Smax.romanov@nginx.com 
464343Smax.romanov@nginx.com static nxt_int_t
465507Smax.romanov@nginx.com nxt_router_start_app_process(nxt_task_t *task, nxt_app_t *app)
466141Smax.romanov@nginx.com {
467343Smax.romanov@nginx.com     nxt_int_t      res;
468343Smax.romanov@nginx.com     nxt_port_t     *router_port;
469343Smax.romanov@nginx.com     nxt_runtime_t  *rt;
470343Smax.romanov@nginx.com 
4711549Smax.romanov@nginx.com     nxt_debug(task, "app '%V' start process", &app->name);
4721549Smax.romanov@nginx.com 
473343Smax.romanov@nginx.com     rt = task->thread->runtime;
474343Smax.romanov@nginx.com     router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
475343Smax.romanov@nginx.com 
476343Smax.romanov@nginx.com     nxt_router_app_use(task, app, 1);
477343Smax.romanov@nginx.com 
478507Smax.romanov@nginx.com     res = nxt_port_post(task, router_port, nxt_router_start_app_process_handler,
479343Smax.romanov@nginx.com                         app);
480343Smax.romanov@nginx.com 
481343Smax.romanov@nginx.com     if (res == NXT_OK) {
482343Smax.romanov@nginx.com         return res;
483318Smax.romanov@nginx.com     }
484318Smax.romanov@nginx.com 
485343Smax.romanov@nginx.com     nxt_thread_mutex_lock(&app->mutex);
486343Smax.romanov@nginx.com 
487507Smax.romanov@nginx.com     app->pending_processes--;
488343Smax.romanov@nginx.com 
489343Smax.romanov@nginx.com     nxt_thread_mutex_unlock(&app->mutex);
490343Smax.romanov@nginx.com 
491343Smax.romanov@nginx.com     nxt_router_app_use(task, app, -1);
492343Smax.romanov@nginx.com 
493343Smax.romanov@nginx.com     return NXT_ERROR;
494318Smax.romanov@nginx.com }
495318Smax.romanov@nginx.com 
496318Smax.romanov@nginx.com 
497423Smax.romanov@nginx.com nxt_inline nxt_bool_t
4981555Smax.romanov@nginx.com nxt_router_msg_cancel(nxt_task_t *task, nxt_request_rpc_data_t *req_rpc_data)
499423Smax.romanov@nginx.com {
5001555Smax.romanov@nginx.com     nxt_buf_t       *b, *next;
5011555Smax.romanov@nginx.com     nxt_bool_t      cancelled;
5021555Smax.romanov@nginx.com     nxt_msg_info_t  *msg_info;
5031555Smax.romanov@nginx.com 
5041555Smax.romanov@nginx.com     msg_info = &req_rpc_data->msg_info;
505423Smax.romanov@nginx.com 
506423Smax.romanov@nginx.com     if (msg_info->buf == NULL) {
507423Smax.romanov@nginx.com         return 0;
508423Smax.romanov@nginx.com     }
509423Smax.romanov@nginx.com 
5101555Smax.romanov@nginx.com     cancelled = nxt_app_queue_cancel(req_rpc_data->app->shared_port->queue,
5111555Smax.romanov@nginx.com                                      msg_info->tracking_cookie,
5121555Smax.romanov@nginx.com                                      req_rpc_data->stream);
513423Smax.romanov@nginx.com 
514423Smax.romanov@nginx.com     if (cancelled) {
5151555Smax.romanov@nginx.com         nxt_debug(task, "stream #%uD: cancelled by router",
5161555Smax.romanov@nginx.com                   req_rpc_data->stream);
517423Smax.romanov@nginx.com     }
518423Smax.romanov@nginx.com 
519423Smax.romanov@nginx.com     for (b = msg_info->buf; b != NULL; b = next) {
520423Smax.romanov@nginx.com         next = b->next;
5211269Sigor@sysoev.ru         b->next = NULL;
522423Smax.romanov@nginx.com 
523423Smax.romanov@nginx.com         b->completion_handler = msg_info->completion_handler;
524423Smax.romanov@nginx.com 
525423Smax.romanov@nginx.com         if (b->is_port_mmap_sent) {
526423Smax.romanov@nginx.com             b->is_port_mmap_sent = cancelled == 0;
527423Smax.romanov@nginx.com             b->completion_handler(task, b, b->parent);
528423Smax.romanov@nginx.com         }
529423Smax.romanov@nginx.com     }
530423Smax.romanov@nginx.com 
531423Smax.romanov@nginx.com     msg_info->buf = NULL;
532423Smax.romanov@nginx.com 
533423Smax.romanov@nginx.com     return cancelled;
534423Smax.romanov@nginx.com }
535423Smax.romanov@nginx.com 
536423Smax.romanov@nginx.com 
537425Smax.romanov@nginx.com nxt_inline nxt_bool_t
538425Smax.romanov@nginx.com nxt_queue_chk_remove(nxt_queue_link_t *lnk)
539425Smax.romanov@nginx.com {
540425Smax.romanov@nginx.com     if (lnk->next != NULL) {
541425Smax.romanov@nginx.com         nxt_queue_remove(lnk);
542425Smax.romanov@nginx.com 
543425Smax.romanov@nginx.com         lnk->next = NULL;
544425Smax.romanov@nginx.com 
545425Smax.romanov@nginx.com         return 1;
546425Smax.romanov@nginx.com     }
547425Smax.romanov@nginx.com 
548425Smax.romanov@nginx.com     return 0;
549425Smax.romanov@nginx.com }
550425Smax.romanov@nginx.com 
551425Smax.romanov@nginx.com 
552343Smax.romanov@nginx.com nxt_inline void
5531123Smax.romanov@nginx.com nxt_request_rpc_data_unlink(nxt_task_t *task,
5541123Smax.romanov@nginx.com     nxt_request_rpc_data_t *req_rpc_data)
555343Smax.romanov@nginx.com {
5561561Smax.romanov@nginx.com     nxt_app_t           *app;
5571561Smax.romanov@nginx.com     nxt_bool_t          unlinked;
5581547Smax.romanov@nginx.com     nxt_http_request_t  *r;
5591547Smax.romanov@nginx.com 
5601555Smax.romanov@nginx.com     nxt_router_msg_cancel(task, req_rpc_data);
5611123Smax.romanov@nginx.com 
5621123Smax.romanov@nginx.com     if (req_rpc_data->app_port != NULL) {
5631123Smax.romanov@nginx.com         nxt_router_app_port_release(task, req_rpc_data->app_port,
5641123Smax.romanov@nginx.com                                     req_rpc_data->apr_action);
5651123Smax.romanov@nginx.com 
5661123Smax.romanov@nginx.com         req_rpc_data->app_port = NULL;
5671123Smax.romanov@nginx.com     }
5681123Smax.romanov@nginx.com 
5691561Smax.romanov@nginx.com     app = req_rpc_data->app;
5701547Smax.romanov@nginx.com     r = req_rpc_data->request;
5711547Smax.romanov@nginx.com 
5721547Smax.romanov@nginx.com     if (r != NULL) {
5731547Smax.romanov@nginx.com         r->timer_data = NULL;
5741547Smax.romanov@nginx.com 
5751547Smax.romanov@nginx.com         nxt_router_http_request_release_post(task, r);
5761547Smax.romanov@nginx.com 
5771547Smax.romanov@nginx.com         r->req_rpc_data = NULL;
5781123Smax.romanov@nginx.com         req_rpc_data->request = NULL;
5791561Smax.romanov@nginx.com 
5801561Smax.romanov@nginx.com         if (app != NULL) {
5811561Smax.romanov@nginx.com             unlinked = 0;
5821561Smax.romanov@nginx.com 
5831561Smax.romanov@nginx.com             nxt_thread_mutex_lock(&app->mutex);
5841561Smax.romanov@nginx.com 
5851561Smax.romanov@nginx.com             if (r->app_link.next != NULL) {
5861561Smax.romanov@nginx.com                 nxt_queue_remove(&r->app_link);
5871561Smax.romanov@nginx.com                 r->app_link.next = NULL;
5881561Smax.romanov@nginx.com 
5891561Smax.romanov@nginx.com                 unlinked = 1;
5901561Smax.romanov@nginx.com             }
5911561Smax.romanov@nginx.com 
5921561Smax.romanov@nginx.com             nxt_thread_mutex_unlock(&app->mutex);
5931561Smax.romanov@nginx.com 
5941561Smax.romanov@nginx.com             if (unlinked) {
5951561Smax.romanov@nginx.com                 nxt_mp_release(r->mem_pool);
5961561Smax.romanov@nginx.com             }
5971561Smax.romanov@nginx.com         }
5981561Smax.romanov@nginx.com     }
5991561Smax.romanov@nginx.com 
6001561Smax.romanov@nginx.com     if (app != NULL) {
6011561Smax.romanov@nginx.com         nxt_router_app_use(task, app, -1);
6021561Smax.romanov@nginx.com 
6031561Smax.romanov@nginx.com         req_rpc_data->app = NULL;
604346Smax.romanov@nginx.com     }
6051547Smax.romanov@nginx.com 
6061547Smax.romanov@nginx.com     if (req_rpc_data->msg_info.body_fd != -1) {
6071547Smax.romanov@nginx.com         nxt_fd_close(req_rpc_data->msg_info.body_fd);
6081547Smax.romanov@nginx.com 
6091547Smax.romanov@nginx.com         req_rpc_data->msg_info.body_fd = -1;
6101547Smax.romanov@nginx.com     }
6111547Smax.romanov@nginx.com 
6121547Smax.romanov@nginx.com     if (req_rpc_data->rpc_cancel) {
6131547Smax.romanov@nginx.com         req_rpc_data->rpc_cancel = 0;
6141547Smax.romanov@nginx.com 
6151547Smax.romanov@nginx.com         nxt_port_rpc_cancel(task, task->thread->engine->port,
6161547Smax.romanov@nginx.com                             req_rpc_data->stream);
6171547Smax.romanov@nginx.com     }
618343Smax.romanov@nginx.com }
619343Smax.romanov@nginx.com 
620343Smax.romanov@nginx.com 
6211552Smax.romanov@nginx.com static void
622141Smax.romanov@nginx.com nxt_router_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
623141Smax.romanov@nginx.com {
6241555Smax.romanov@nginx.com     nxt_int_t      res;
6251547Smax.romanov@nginx.com     nxt_app_t      *app;
6261547Smax.romanov@nginx.com     nxt_port_t     *port, *main_app_port;
6271547Smax.romanov@nginx.com     nxt_runtime_t  *rt;
6281547Smax.romanov@nginx.com 
629141Smax.romanov@nginx.com     nxt_port_new_port_handler(task, msg);
630141Smax.romanov@nginx.com 
6311547Smax.romanov@nginx.com     port = msg->u.new_port;
6321547Smax.romanov@nginx.com 
6331547Smax.romanov@nginx.com     if (port != NULL && port->type == NXT_PROCESS_CONTROLLER) {
634662Smax.romanov@nginx.com         nxt_router_greet_controller(task, msg->u.new_port);
635662Smax.romanov@nginx.com     }
636662Smax.romanov@nginx.com 
6371547Smax.romanov@nginx.com     if (port == NULL || port->type != NXT_PROCESS_APP) {
6381547Smax.romanov@nginx.com 
6391547Smax.romanov@nginx.com         if (msg->port_msg.stream == 0) {
6401547Smax.romanov@nginx.com             return;
6411547Smax.romanov@nginx.com         }
6421547Smax.romanov@nginx.com 
6431547Smax.romanov@nginx.com         msg->port_msg.type = _NXT_PORT_MSG_RPC_ERROR;
6441555Smax.romanov@nginx.com 
6451555Smax.romanov@nginx.com     } else {
6461558Smax.romanov@nginx.com         if (msg->fd[1] != -1) {
6471558Smax.romanov@nginx.com             res = nxt_router_port_queue_map(task, port, msg->fd[1]);
6481555Smax.romanov@nginx.com             if (nxt_slow_path(res != NXT_OK)) {
6491555Smax.romanov@nginx.com                 return;
6501555Smax.romanov@nginx.com             }
6511555Smax.romanov@nginx.com 
6521558Smax.romanov@nginx.com             nxt_fd_close(msg->fd[1]);
6531558Smax.romanov@nginx.com             msg->fd[1] = -1;
6541555Smax.romanov@nginx.com         }
6551547Smax.romanov@nginx.com     }
6561547Smax.romanov@nginx.com 
6571547Smax.romanov@nginx.com     if (msg->port_msg.stream != 0) {
6581547Smax.romanov@nginx.com         nxt_port_rpc_handler(task, msg);
659141Smax.romanov@nginx.com         return;
660141Smax.romanov@nginx.com     }
661141Smax.romanov@nginx.com 
6621547Smax.romanov@nginx.com     /*
6631547Smax.romanov@nginx.com      * Port with "id == 0" is application 'main' port and it always
6641547Smax.romanov@nginx.com      * should come with non-zero stream.
6651547Smax.romanov@nginx.com      */
6661547Smax.romanov@nginx.com     nxt_assert(port->id != 0);
6671547Smax.romanov@nginx.com 
6681547Smax.romanov@nginx.com     /* Find 'main' app port and get app reference. */
6691547Smax.romanov@nginx.com     rt = task->thread->runtime;
6701547Smax.romanov@nginx.com 
6711547Smax.romanov@nginx.com     /*
6721547Smax.romanov@nginx.com      * It is safe to access 'runtime->ports' hash because 'NEW_PORT'
6731547Smax.romanov@nginx.com      * sent to main port (with id == 0) and processed in main thread.
6741547Smax.romanov@nginx.com      */
6751547Smax.romanov@nginx.com     main_app_port = nxt_port_hash_find(&rt->ports, port->pid, 0);
6761547Smax.romanov@nginx.com     nxt_assert(main_app_port != NULL);
6771547Smax.romanov@nginx.com 
6781547Smax.romanov@nginx.com     app = main_app_port->app;
6791547Smax.romanov@nginx.com     nxt_assert(app != NULL);
6801547Smax.romanov@nginx.com 
6811547Smax.romanov@nginx.com     nxt_thread_mutex_lock(&app->mutex);
6821547Smax.romanov@nginx.com 
6831547Smax.romanov@nginx.com     /* TODO here should be find-and-add code because there can be
6841547Smax.romanov@nginx.com        port waiters in port_hash */
6851547Smax.romanov@nginx.com     nxt_port_hash_add(&app->port_hash, port);
6861547Smax.romanov@nginx.com     app->port_hash_count++;
6871547Smax.romanov@nginx.com 
6881547Smax.romanov@nginx.com     nxt_thread_mutex_unlock(&app->mutex);
6891547Smax.romanov@nginx.com 
6901547Smax.romanov@nginx.com     port->app = app;
6911547Smax.romanov@nginx.com     port->main_app_port = main_app_port;
692141Smax.romanov@nginx.com }
693141Smax.romanov@nginx.com 
694141Smax.romanov@nginx.com 
6951552Smax.romanov@nginx.com static void
696139Sigor@sysoev.ru nxt_router_conf_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
697115Sigor@sysoev.ru {
6981526Smax.romanov@nginx.com     void                    *p;
6991526Smax.romanov@nginx.com     size_t                  size;
700198Sigor@sysoev.ru     nxt_int_t               ret;
701139Sigor@sysoev.ru     nxt_router_temp_conf_t  *tmcf;
702139Sigor@sysoev.ru 
703139Sigor@sysoev.ru     tmcf = nxt_router_temp_conf(task);
704139Sigor@sysoev.ru     if (nxt_slow_path(tmcf == NULL)) {
705139Sigor@sysoev.ru         return;
70653Sigor@sysoev.ru     }
70753Sigor@sysoev.ru 
7081558Smax.romanov@nginx.com     if (nxt_slow_path(msg->fd[0] == -1)) {
7091526Smax.romanov@nginx.com         nxt_alert(task, "conf_data_handler: invalid file shm fd");
7101526Smax.romanov@nginx.com         return;
7111526Smax.romanov@nginx.com     }
7121526Smax.romanov@nginx.com 
7131526Smax.romanov@nginx.com     if (nxt_buf_mem_used_size(&msg->buf->mem) != sizeof(size_t)) {
7141526Smax.romanov@nginx.com         nxt_alert(task, "conf_data_handler: unexpected buffer size (%d)",
7151526Smax.romanov@nginx.com                   (int) nxt_buf_mem_used_size(&msg->buf->mem));
7161526Smax.romanov@nginx.com 
7171558Smax.romanov@nginx.com         nxt_fd_close(msg->fd[0]);
7181558Smax.romanov@nginx.com         msg->fd[0] = -1;
7191526Smax.romanov@nginx.com 
7201526Smax.romanov@nginx.com         return;
7211526Smax.romanov@nginx.com     }
7221526Smax.romanov@nginx.com 
7231526Smax.romanov@nginx.com     nxt_memcpy(&size, msg->buf->mem.pos, sizeof(size_t));
7241526Smax.romanov@nginx.com 
7251558Smax.romanov@nginx.com     p = nxt_mem_mmap(NULL, size, PROT_READ, MAP_SHARED, msg->fd[0], 0);
7261558Smax.romanov@nginx.com 
7271558Smax.romanov@nginx.com     nxt_fd_close(msg->fd[0]);
7281558Smax.romanov@nginx.com     msg->fd[0] = -1;
7291526Smax.romanov@nginx.com 
7301526Smax.romanov@nginx.com     if (nxt_slow_path(p ==