xref: /unit/src/nxt_router.c (revision 1563)
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);
96*1563Svbart@nginx.com 
97133Sigor@sysoev.ru static nxt_app_t *nxt_router_app_find(nxt_queue_t *queue, nxt_str_t *name);
98*1563Svbart@nginx.com static nxt_int_t nxt_router_apps_hash_test(nxt_lvlhsh_query_t *lhq, void *data);
99*1563Svbart@nginx.com static nxt_int_t nxt_router_apps_hash_add(nxt_router_conf_t *rtcf,
100*1563Svbart@nginx.com     nxt_app_t *app);
101*1563Svbart@nginx.com static nxt_app_t *nxt_router_apps_hash_get(nxt_router_conf_t *rtcf,
102*1563Svbart@nginx.com     nxt_str_t *name);
103*1563Svbart@nginx.com static void nxt_router_apps_hash_use(nxt_task_t *task, nxt_router_conf_t *rtcf,
104*1563Svbart@nginx.com     int i);
105*1563Svbart@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 
210*1563Svbart@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 
222425Smax.romanov@nginx.com static void nxt_router_app_prepare_request(nxt_task_t *task,
2231547Smax.romanov@nginx.com     nxt_request_rpc_data_t *req_rpc_data);
2241007Salexander.borisov@nginx.com static nxt_buf_t *nxt_router_prepare_msg(nxt_task_t *task,
2251547Smax.romanov@nginx.com     nxt_http_request_t *r, nxt_app_t *app, const nxt_str_t *prefix);
226510Salexander.borisov@nginx.com 
227318Smax.romanov@nginx.com static void nxt_router_app_timeout(nxt_task_t *task, void *obj, void *data);
228507Smax.romanov@nginx.com static void nxt_router_adjust_idle_timer(nxt_task_t *task, void *obj,
229507Smax.romanov@nginx.com     void *data);
230507Smax.romanov@nginx.com static void nxt_router_app_idle_timeout(nxt_task_t *task, void *obj,
231507Smax.romanov@nginx.com     void *data);
232753Smax.romanov@nginx.com static void nxt_router_app_joint_release_handler(nxt_task_t *task, void *obj,
233507Smax.romanov@nginx.com     void *data);
234753Smax.romanov@nginx.com static void nxt_router_free_app(nxt_task_t *task, void *obj, void *data);
235431Sigor@sysoev.ru 
236431Sigor@sysoev.ru static const nxt_http_request_state_t  nxt_http_request_send_state;
237431Sigor@sysoev.ru static void nxt_http_request_send_body(nxt_task_t *task, void *obj, void *data);
238141Smax.romanov@nginx.com 
239753Smax.romanov@nginx.com static void nxt_router_app_joint_use(nxt_task_t *task,
240753Smax.romanov@nginx.com     nxt_app_joint_t *app_joint, int i);
241753Smax.romanov@nginx.com 
2421547Smax.romanov@nginx.com static void nxt_router_http_request_release_post(nxt_task_t *task,
2431007Salexander.borisov@nginx.com     nxt_http_request_t *r);
2441007Salexander.borisov@nginx.com static void nxt_router_http_request_release(nxt_task_t *task, void *obj,
2451007Salexander.borisov@nginx.com     void *data);
2461321Smax.romanov@nginx.com static void nxt_router_oosm_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
2471545Smax.romanov@nginx.com static void nxt_router_get_port_handler(nxt_task_t *task,
2481545Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
2491546Smax.romanov@nginx.com static void nxt_router_get_mmap_handler(nxt_task_t *task,
2501546Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
2511007Salexander.borisov@nginx.com 
2521149Smax.romanov@nginx.com extern const nxt_http_request_state_t  nxt_http_websocket;
2531131Smax.romanov@nginx.com 
254119Smax.romanov@nginx.com static nxt_router_t  *nxt_router;
25520Sigor@sysoev.ru 
256743Smax.romanov@nginx.com static const nxt_str_t http_prefix = nxt_string("HTTP_");
257743Smax.romanov@nginx.com static const nxt_str_t empty_prefix = nxt_string("");
258743Smax.romanov@nginx.com 
259743Smax.romanov@nginx.com static const nxt_str_t  *nxt_app_msg_prefix[] = {
260804Svbart@nginx.com     &empty_prefix,
261743Smax.romanov@nginx.com     &http_prefix,
262743Smax.romanov@nginx.com     &http_prefix,
263743Smax.romanov@nginx.com     &http_prefix,
264743Smax.romanov@nginx.com     &http_prefix,
265977Smax.romanov@gmail.com     &empty_prefix,
266216Sigor@sysoev.ru };
267216Sigor@sysoev.ru 
268216Sigor@sysoev.ru 
2691488St.nateldemoura@f5.com static const nxt_port_handlers_t  nxt_router_process_port_handlers = {
2701488St.nateldemoura@f5.com     .quit         = nxt_signal_quit_handler,
271662Smax.romanov@nginx.com     .new_port     = nxt_router_new_port_handler,
2721545Smax.romanov@nginx.com     .get_port     = nxt_router_get_port_handler,
273662Smax.romanov@nginx.com     .change_file  = nxt_port_change_log_file_handler,
274662Smax.romanov@nginx.com     .mmap         = nxt_port_mmap_handler,
2751546Smax.romanov@nginx.com     .get_mmap     = nxt_router_get_mmap_handler,
276662Smax.romanov@nginx.com     .data         = nxt_router_conf_data_handler,
277662Smax.romanov@nginx.com     .remove_pid   = nxt_router_remove_pid_handler,
278662Smax.romanov@nginx.com     .access_log   = nxt_router_access_log_reopen_handler,
279662Smax.romanov@nginx.com     .rpc_ready    = nxt_port_rpc_handler,
280662Smax.romanov@nginx.com     .rpc_error    = nxt_port_rpc_handler,
2811321Smax.romanov@nginx.com     .oosm         = nxt_router_oosm_handler,
282662Smax.romanov@nginx.com };
283662Smax.romanov@nginx.com 
284662Smax.romanov@nginx.com 
2851488St.nateldemoura@f5.com const nxt_process_init_t  nxt_router_process = {
2861488St.nateldemoura@f5.com     .name           = "router",
2871488St.nateldemoura@f5.com     .type           = NXT_PROCESS_ROUTER,
2881488St.nateldemoura@f5.com     .prefork        = nxt_router_prefork,
2891488St.nateldemoura@f5.com     .restart        = 1,
2901488St.nateldemoura@f5.com     .setup          = nxt_process_core_setup,
2911488St.nateldemoura@f5.com     .start          = nxt_router_start,
2921488St.nateldemoura@f5.com     .port_handlers  = &nxt_router_process_port_handlers,
2931488St.nateldemoura@f5.com     .signals        = nxt_process_signals,
2941488St.nateldemoura@f5.com };
2951488St.nateldemoura@f5.com 
2961488St.nateldemoura@f5.com 
2971509Sigor@sysoev.ru /* Queues of nxt_socket_conf_t */
2981509Sigor@sysoev.ru nxt_queue_t  creating_sockets;
2991509Sigor@sysoev.ru nxt_queue_t  pending_sockets;
3001509Sigor@sysoev.ru nxt_queue_t  updating_sockets;
3011509Sigor@sysoev.ru nxt_queue_t  keeping_sockets;
3021509Sigor@sysoev.ru nxt_queue_t  deleting_sockets;
3031509Sigor@sysoev.ru 
3041509Sigor@sysoev.ru 
3051488St.nateldemoura@f5.com static nxt_int_t
3061488St.nateldemoura@f5.com nxt_router_prefork(nxt_task_t *task, nxt_process_t *process, nxt_mp_t *mp)
3071488St.nateldemoura@f5.com {
3081488St.nateldemoura@f5.com     nxt_runtime_stop_app_processes(task, task->thread->runtime);
3091488St.nateldemoura@f5.com 
3101488St.nateldemoura@f5.com     return NXT_OK;
3111488St.nateldemoura@f5.com }
3121488St.nateldemoura@f5.com 
3131488St.nateldemoura@f5.com 
3141488St.nateldemoura@f5.com static nxt_int_t
3151488St.nateldemoura@f5.com nxt_router_start(nxt_task_t *task, nxt_process_data_t *data)
31620Sigor@sysoev.ru {
317141Smax.romanov@nginx.com     nxt_int_t      ret;
318662Smax.romanov@nginx.com     nxt_port_t     *controller_port;
319141Smax.romanov@nginx.com     nxt_router_t   *router;
320141Smax.romanov@nginx.com     nxt_runtime_t  *rt;
321141Smax.romanov@nginx.com 
322141Smax.romanov@nginx.com     rt = task->thread->runtime;
32353Sigor@sysoev.ru 
3241488St.nateldemoura@f5.com     nxt_log(task, NXT_LOG_INFO, "router started");
3251488St.nateldemoura@f5.com 
326771Sigor@sysoev.ru #if (NXT_TLS)
327771Sigor@sysoev.ru     rt->tls = nxt_service_get(rt->services, "SSL/TLS", "OpenSSL");
328771Sigor@sysoev.ru     if (nxt_slow_path(rt->tls == NULL)) {
329771Sigor@sysoev.ru         return NXT_ERROR;
330771Sigor@sysoev.ru     }
331771Sigor@sysoev.ru 
332771Sigor@sysoev.ru     ret = rt->tls->library_init(task);
333771Sigor@sysoev.ru     if (nxt_slow_path(ret != NXT_OK)) {
334771Sigor@sysoev.ru         return ret;
335771Sigor@sysoev.ru     }
336771Sigor@sysoev.ru #endif
337771Sigor@sysoev.ru 
3381459Smax.romanov@nginx.com     ret = nxt_http_init(task);
33988Smax.romanov@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
34088Smax.romanov@nginx.com         return ret;
34188Smax.romanov@nginx.com     }
34288Smax.romanov@nginx.com 
34353Sigor@sysoev.ru     router = nxt_zalloc(sizeof(nxt_router_t));
34453Sigor@sysoev.ru     if (nxt_slow_path(router == NULL)) {
34553Sigor@sysoev.ru         return NXT_ERROR;
34653Sigor@sysoev.ru     }
34753Sigor@sysoev.ru 
34853Sigor@sysoev.ru     nxt_queue_init(&router->engines);
34953Sigor@sysoev.ru     nxt_queue_init(&router->sockets);
350133Sigor@sysoev.ru     nxt_queue_init(&router->apps);
35153Sigor@sysoev.ru 
352119Smax.romanov@nginx.com     nxt_router = router;
353119Smax.romanov@nginx.com 
354662Smax.romanov@nginx.com     controller_port = rt->port_by_type[NXT_PROCESS_CONTROLLER];
355662Smax.romanov@nginx.com     if (controller_port != NULL) {
356662Smax.romanov@nginx.com         nxt_router_greet_controller(task, controller_port);
357662Smax.romanov@nginx.com     }
358662Smax.romanov@nginx.com 
359115Sigor@sysoev.ru     return NXT_OK;
360115Sigor@sysoev.ru }
361115Sigor@sysoev.ru 
362115Sigor@sysoev.ru 
363343Smax.romanov@nginx.com static void
364662Smax.romanov@nginx.com nxt_router_greet_controller(nxt_task_t *task, nxt_port_t *controller_port)
365662Smax.romanov@nginx.com {
366662Smax.romanov@nginx.com     nxt_port_socket_write(task, controller_port, NXT_PORT_MSG_PROCESS_READY,
367662Smax.romanov@nginx.com                           -1, 0, 0, NULL);
368662Smax.romanov@nginx.com }
369662Smax.romanov@nginx.com 
370662Smax.romanov@nginx.com 
371662Smax.romanov@nginx.com static void
372507Smax.romanov@nginx.com nxt_router_start_app_process_handler(nxt_task_t *task, nxt_port_t *port,
373507Smax.romanov@nginx.com     void *data)
374167Smax.romanov@nginx.com {
375343Smax.romanov@nginx.com     size_t         size;
376343Smax.romanov@nginx.com     uint32_t       stream;
377430Sigor@sysoev.ru     nxt_mp_t       *mp;
378648Svbart@nginx.com     nxt_int_t      ret;
379343Smax.romanov@nginx.com     nxt_app_t      *app;
380343Smax.romanov@nginx.com     nxt_buf_t      *b;
381343Smax.romanov@nginx.com     nxt_port_t     *main_port;
382343Smax.romanov@nginx.com     nxt_runtime_t  *rt;
383343Smax.romanov@nginx.com 
384343Smax.romanov@nginx.com     app = data;
385167Smax.romanov@nginx.com 
386167Smax.romanov@nginx.com     rt = task->thread->runtime;
387240Sigor@sysoev.ru     main_port = rt->port_by_type[NXT_PROCESS_MAIN];
388167Smax.romanov@nginx.com 
389507Smax.romanov@nginx.com     nxt_debug(task, "app '%V' %p start process", &app->name, app);
390343Smax.romanov@nginx.com 
391343Smax.romanov@nginx.com     size = app->name.length + 1 + app->conf.length;
392343Smax.romanov@nginx.com 
393343Smax.romanov@nginx.com     b = nxt_buf_mem_ts_alloc(task, task->thread->engine->mem_pool, size);
394343Smax.romanov@nginx.com 
395343Smax.romanov@nginx.com     if (nxt_slow_path(b == NULL)) {
396343Smax.romanov@nginx.com         goto failed;
397167Smax.romanov@nginx.com     }
398167Smax.romanov@nginx.com 
399343Smax.romanov@nginx.com     nxt_buf_cpystr(b, &app->name);
400343Smax.romanov@nginx.com     *b->mem.free++ = '\0';
401343Smax.romanov@nginx.com     nxt_buf_cpystr(b, &app->conf);
402343Smax.romanov@nginx.com 
403753Smax.romanov@nginx.com     nxt_router_app_joint_use(task, app->joint, 1);
404753Smax.romanov@nginx.com 
405343Smax.romanov@nginx.com     stream = nxt_port_rpc_register_handler(task, port,
406343Smax.romanov@nginx.com                                            nxt_router_app_port_ready,
407343Smax.romanov@nginx.com                                            nxt_router_app_port_error,
408753Smax.romanov@nginx.com                                            -1, app->joint);
409343Smax.romanov@nginx.com 
410343Smax.romanov@nginx.com     if (nxt_slow_path(stream == 0)) {
411753Smax.romanov@nginx.com         nxt_router_app_joint_use(task, app->joint, -1);
412753Smax.romanov@nginx.com 
413343Smax.romanov@nginx.com         goto failed;
414343Smax.romanov@nginx.com     }
415343Smax.romanov@nginx.com 
4161488St.nateldemoura@f5.com     ret = nxt_port_socket_write(task, main_port, NXT_PORT_MSG_START_PROCESS,
4171488St.nateldemoura@f5.com                                 -1, stream, port->id, b);
418648Svbart@nginx.com 
419648Svbart@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
420648Svbart@nginx.com         nxt_port_rpc_cancel(task, port, stream);
421753Smax.romanov@nginx.com 
422753Smax.romanov@nginx.com         nxt_router_app_joint_use(task, app->joint, -1);
423753Smax.romanov@nginx.com 
424648Svbart@nginx.com         goto failed;
425648Svbart@nginx.com     }
426343Smax.romanov@nginx.com 
427753Smax.romanov@nginx.com     nxt_router_app_use(task, app, -1);
428753Smax.romanov@nginx.com 
429343Smax.romanov@nginx.com     return;
430343Smax.romanov@nginx.com 
431343Smax.romanov@nginx.com failed:
432343Smax.romanov@nginx.com 
433648Svbart@nginx.com     if (b != NULL) {
434648Svbart@nginx.com         mp = b->data;
435648Svbart@nginx.com         nxt_mp_free(mp, b);
436648Svbart@nginx.com         nxt_mp_release(mp);
437648Svbart@nginx.com     }
438648Svbart@nginx.com 
439343Smax.romanov@nginx.com     nxt_thread_mutex_lock(&app->mutex);
440343Smax.romanov@nginx.com 
441507Smax.romanov@nginx.com     app->pending_processes--;
442343Smax.romanov@nginx.com 
443343Smax.romanov@nginx.com     nxt_thread_mutex_unlock(&app->mutex);
444343Smax.romanov@nginx.com 
445343Smax.romanov@nginx.com     nxt_router_app_use(task, app, -1);
446167Smax.romanov@nginx.com }
447167Smax.romanov@nginx.com 
448167Smax.romanov@nginx.com 
449753Smax.romanov@nginx.com static void
450753Smax.romanov@nginx.com nxt_router_app_joint_use(nxt_task_t *task, nxt_app_joint_t *app_joint, int i)
451753Smax.romanov@nginx.com {
452753Smax.romanov@nginx.com     app_joint->use_count += i;
453753Smax.romanov@nginx.com 
454753Smax.romanov@nginx.com     if (app_joint->use_count == 0) {
455753Smax.romanov@nginx.com         nxt_assert(app_joint->app == NULL);
456753Smax.romanov@nginx.com 
457753Smax.romanov@nginx.com         nxt_free(app_joint);
458753Smax.romanov@nginx.com     }
459753Smax.romanov@nginx.com }
460753Smax.romanov@nginx.com 
461753Smax.romanov@nginx.com 
462343Smax.romanov@nginx.com static nxt_int_t
463507Smax.romanov@nginx.com nxt_router_start_app_process(nxt_task_t *task, nxt_app_t *app)
464141Smax.romanov@nginx.com {
465343Smax.romanov@nginx.com     nxt_int_t      res;
466343Smax.romanov@nginx.com     nxt_port_t     *router_port;
467343Smax.romanov@nginx.com     nxt_runtime_t  *rt;
468343Smax.romanov@nginx.com 
4691549Smax.romanov@nginx.com     nxt_debug(task, "app '%V' start process", &app->name);
4701549Smax.romanov@nginx.com 
471343Smax.romanov@nginx.com     rt = task->thread->runtime;
472343Smax.romanov@nginx.com     router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
473343Smax.romanov@nginx.com 
474343Smax.romanov@nginx.com     nxt_router_app_use(task, app, 1);
475343Smax.romanov@nginx.com 
476507Smax.romanov@nginx.com     res = nxt_port_post(task, router_port, nxt_router_start_app_process_handler,
477343Smax.romanov@nginx.com                         app);
478343Smax.romanov@nginx.com 
479343Smax.romanov@nginx.com     if (res == NXT_OK) {
480343Smax.romanov@nginx.com         return res;
481318Smax.romanov@nginx.com     }
482318Smax.romanov@nginx.com 
483343Smax.romanov@nginx.com     nxt_thread_mutex_lock(&app->mutex);
484343Smax.romanov@nginx.com 
485507Smax.romanov@nginx.com     app->pending_processes--;
486343Smax.romanov@nginx.com 
487343Smax.romanov@nginx.com     nxt_thread_mutex_unlock(&app->mutex);
488343Smax.romanov@nginx.com 
489343Smax.romanov@nginx.com     nxt_router_app_use(task, app, -1);
490343Smax.romanov@nginx.com 
491343Smax.romanov@nginx.com     return NXT_ERROR;
492318Smax.romanov@nginx.com }
493318Smax.romanov@nginx.com 
494318Smax.romanov@nginx.com 
495423Smax.romanov@nginx.com nxt_inline nxt_bool_t
4961555Smax.romanov@nginx.com nxt_router_msg_cancel(nxt_task_t *task, nxt_request_rpc_data_t *req_rpc_data)
497423Smax.romanov@nginx.com {
4981555Smax.romanov@nginx.com     nxt_buf_t       *b, *next;
4991555Smax.romanov@nginx.com     nxt_bool_t      cancelled;
5001555Smax.romanov@nginx.com     nxt_msg_info_t  *msg_info;
5011555Smax.romanov@nginx.com 
5021555Smax.romanov@nginx.com     msg_info = &req_rpc_data->msg_info;
503423Smax.romanov@nginx.com 
504423Smax.romanov@nginx.com     if (msg_info->buf == NULL) {
505423Smax.romanov@nginx.com         return 0;
506423Smax.romanov@nginx.com     }
507423Smax.romanov@nginx.com 
5081555Smax.romanov@nginx.com     cancelled = nxt_app_queue_cancel(req_rpc_data->app->shared_port->queue,
5091555Smax.romanov@nginx.com                                      msg_info->tracking_cookie,
5101555Smax.romanov@nginx.com                                      req_rpc_data->stream);
511423Smax.romanov@nginx.com 
512423Smax.romanov@nginx.com     if (cancelled) {
5131555Smax.romanov@nginx.com         nxt_debug(task, "stream #%uD: cancelled by router",
5141555Smax.romanov@nginx.com                   req_rpc_data->stream);
515423Smax.romanov@nginx.com     }
516423Smax.romanov@nginx.com 
517423Smax.romanov@nginx.com     for (b = msg_info->buf; b != NULL; b = next) {
518423Smax.romanov@nginx.com         next = b->next;
5191269Sigor@sysoev.ru         b->next = NULL;
520423Smax.romanov@nginx.com 
521423Smax.romanov@nginx.com         b->completion_handler = msg_info->completion_handler;
522423Smax.romanov@nginx.com 
523423Smax.romanov@nginx.com         if (b->is_port_mmap_sent) {
524423Smax.romanov@nginx.com             b->is_port_mmap_sent = cancelled == 0;
525423Smax.romanov@nginx.com             b->completion_handler(task, b, b->parent);
526423Smax.romanov@nginx.com         }
527423Smax.romanov@nginx.com     }
528423Smax.romanov@nginx.com 
529423Smax.romanov@nginx.com     msg_info->buf = NULL;
530423Smax.romanov@nginx.com 
531423Smax.romanov@nginx.com     return cancelled;
532423Smax.romanov@nginx.com }
533423Smax.romanov@nginx.com 
534423Smax.romanov@nginx.com 
535425Smax.romanov@nginx.com nxt_inline nxt_bool_t
536425Smax.romanov@nginx.com nxt_queue_chk_remove(nxt_queue_link_t *lnk)
537425Smax.romanov@nginx.com {
538425Smax.romanov@nginx.com     if (lnk->next != NULL) {
539425Smax.romanov@nginx.com         nxt_queue_remove(lnk);
540425Smax.romanov@nginx.com 
541425Smax.romanov@nginx.com         lnk->next = NULL;
542425Smax.romanov@nginx.com 
543425Smax.romanov@nginx.com         return 1;
544425Smax.romanov@nginx.com     }
545425Smax.romanov@nginx.com 
546425Smax.romanov@nginx.com     return 0;
547425Smax.romanov@nginx.com }
548425Smax.romanov@nginx.com 
549425Smax.romanov@nginx.com 
550343Smax.romanov@nginx.com nxt_inline void
5511123Smax.romanov@nginx.com nxt_request_rpc_data_unlink(nxt_task_t *task,
5521123Smax.romanov@nginx.com     nxt_request_rpc_data_t *req_rpc_data)
553343Smax.romanov@nginx.com {
5541561Smax.romanov@nginx.com     nxt_app_t           *app;
5551561Smax.romanov@nginx.com     nxt_bool_t          unlinked;
5561547Smax.romanov@nginx.com     nxt_http_request_t  *r;
5571547Smax.romanov@nginx.com 
5581555Smax.romanov@nginx.com     nxt_router_msg_cancel(task, req_rpc_data);
5591123Smax.romanov@nginx.com 
5601123Smax.romanov@nginx.com     if (req_rpc_data->app_port != NULL) {
5611123Smax.romanov@nginx.com         nxt_router_app_port_release(task, req_rpc_data->app_port,
5621123Smax.romanov@nginx.com                                     req_rpc_data->apr_action);
5631123Smax.romanov@nginx.com 
5641123Smax.romanov@nginx.com         req_rpc_data->app_port = NULL;
5651123Smax.romanov@nginx.com     }
5661123Smax.romanov@nginx.com 
5671561Smax.romanov@nginx.com     app = req_rpc_data->app;
5681547Smax.romanov@nginx.com     r = req_rpc_data->request;
5691547Smax.romanov@nginx.com 
5701547Smax.romanov@nginx.com     if (r != NULL) {
5711547Smax.romanov@nginx.com         r->timer_data = NULL;
5721547Smax.romanov@nginx.com 
5731547Smax.romanov@nginx.com         nxt_router_http_request_release_post(task, r);
5741547Smax.romanov@nginx.com 
5751547Smax.romanov@nginx.com         r->req_rpc_data = NULL;
5761123Smax.romanov@nginx.com         req_rpc_data->request = NULL;
5771561Smax.romanov@nginx.com 
5781561Smax.romanov@nginx.com         if (app != NULL) {
5791561Smax.romanov@nginx.com             unlinked = 0;
5801561Smax.romanov@nginx.com 
5811561Smax.romanov@nginx.com             nxt_thread_mutex_lock(&app->mutex);
5821561Smax.romanov@nginx.com 
5831561Smax.romanov@nginx.com             if (r->app_link.next != NULL) {
5841561Smax.romanov@nginx.com                 nxt_queue_remove(&r->app_link);
5851561Smax.romanov@nginx.com                 r->app_link.next = NULL;
5861561Smax.romanov@nginx.com 
5871561Smax.romanov@nginx.com                 unlinked = 1;
5881561Smax.romanov@nginx.com             }
5891561Smax.romanov@nginx.com 
5901561Smax.romanov@nginx.com             nxt_thread_mutex_unlock(&app->mutex);
5911561Smax.romanov@nginx.com 
5921561Smax.romanov@nginx.com             if (unlinked) {
5931561Smax.romanov@nginx.com                 nxt_mp_release(r->mem_pool);
5941561Smax.romanov@nginx.com             }
5951561Smax.romanov@nginx.com         }
5961561Smax.romanov@nginx.com     }
5971561Smax.romanov@nginx.com 
5981561Smax.romanov@nginx.com     if (app != NULL) {
5991561Smax.romanov@nginx.com         nxt_router_app_use(task, app, -1);
6001561Smax.romanov@nginx.com 
6011561Smax.romanov@nginx.com         req_rpc_data->app = NULL;
602346Smax.romanov@nginx.com     }
6031547Smax.romanov@nginx.com 
6041547Smax.romanov@nginx.com     if (req_rpc_data->msg_info.body_fd != -1) {
6051547Smax.romanov@nginx.com         nxt_fd_close(req_rpc_data->msg_info.body_fd);
6061547Smax.romanov@nginx.com 
6071547Smax.romanov@nginx.com         req_rpc_data->msg_info.body_fd = -1;
6081547Smax.romanov@nginx.com     }
6091547Smax.romanov@nginx.com 
6101547Smax.romanov@nginx.com     if (req_rpc_data->rpc_cancel) {
6111547Smax.romanov@nginx.com         req_rpc_data->rpc_cancel = 0;
6121547Smax.romanov@nginx.com 
6131547Smax.romanov@nginx.com         nxt_port_rpc_cancel(task, task->thread->engine->port,
6141547Smax.romanov@nginx.com                             req_rpc_data->stream);
6151547Smax.romanov@nginx.com     }
616343Smax.romanov@nginx.com }
617343Smax.romanov@nginx.com 
618343Smax.romanov@nginx.com 
6191552Smax.romanov@nginx.com static void
620141Smax.romanov@nginx.com nxt_router_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
621141Smax.romanov@nginx.com {
6221555Smax.romanov@nginx.com     nxt_int_t      res;
6231547Smax.romanov@nginx.com     nxt_app_t      *app;
6241547Smax.romanov@nginx.com     nxt_port_t     *port, *main_app_port;
6251547Smax.romanov@nginx.com     nxt_runtime_t  *rt;
6261547Smax.romanov@nginx.com 
627141Smax.romanov@nginx.com     nxt_port_new_port_handler(task, msg);
628141Smax.romanov@nginx.com 
6291547Smax.romanov@nginx.com     port = msg->u.new_port;
6301547Smax.romanov@nginx.com 
6311547Smax.romanov@nginx.com     if (port != NULL && port->type == NXT_PROCESS_CONTROLLER) {
632662Smax.romanov@nginx.com         nxt_router_greet_controller(task, msg->u.new_port);
633662Smax.romanov@nginx.com     }
634662Smax.romanov@nginx.com 
6351547Smax.romanov@nginx.com     if (port == NULL || port->type != NXT_PROCESS_APP) {
6361547Smax.romanov@nginx.com 
6371547Smax.romanov@nginx.com         if (msg->port_msg.stream == 0) {
6381547Smax.romanov@nginx.com             return;
6391547Smax.romanov@nginx.com         }
6401547Smax.romanov@nginx.com 
6411547Smax.romanov@nginx.com         msg->port_msg.type = _NXT_PORT_MSG_RPC_ERROR;
6421555Smax.romanov@nginx.com 
6431555Smax.romanov@nginx.com     } else {
6441558Smax.romanov@nginx.com         if (msg->fd[1] != -1) {
6451558Smax.romanov@nginx.com             res = nxt_router_port_queue_map(task, port, msg->fd[1]);
6461555Smax.romanov@nginx.com             if (nxt_slow_path(res != NXT_OK)) {
6471555Smax.romanov@nginx.com                 return;
6481555Smax.romanov@nginx.com             }
6491555Smax.romanov@nginx.com 
6501558Smax.romanov@nginx.com             nxt_fd_close(msg->fd[1]);
6511558Smax.romanov@nginx.com             msg->fd[1] = -1;
6521555Smax.romanov@nginx.com         }
6531547Smax.romanov@nginx.com     }
6541547Smax.romanov@nginx.com 
6551547Smax.romanov@nginx.com     if (msg->port_msg.stream != 0) {
6561547Smax.romanov@nginx.com         nxt_port_rpc_handler(task, msg);
657141Smax.romanov@nginx.com         return;
658141Smax.romanov@nginx.com     }
659141Smax.romanov@nginx.com 
6601547Smax.romanov@nginx.com     /*
6611547Smax.romanov@nginx.com      * Port with "id == 0" is application 'main' port and it always
6621547Smax.romanov@nginx.com      * should come with non-zero stream.
6631547Smax.romanov@nginx.com      */
6641547Smax.romanov@nginx.com     nxt_assert(port->id != 0);
6651547Smax.romanov@nginx.com 
6661547Smax.romanov@nginx.com     /* Find 'main' app port and get app reference. */
6671547Smax.romanov@nginx.com     rt = task->thread->runtime;
6681547Smax.romanov@nginx.com 
6691547Smax.romanov@nginx.com     /*
6701547Smax.romanov@nginx.com      * It is safe to access 'runtime->ports' hash because 'NEW_PORT'
6711547Smax.romanov@nginx.com      * sent to main port (with id == 0) and processed in main thread.
6721547Smax.romanov@nginx.com      */
6731547Smax.romanov@nginx.com     main_app_port = nxt_port_hash_find(&rt->ports, port->pid, 0);
6741547Smax.romanov@nginx.com     nxt_assert(main_app_port != NULL);
6751547Smax.romanov@nginx.com 
6761547Smax.romanov@nginx.com     app = main_app_port->app;
6771547Smax.romanov@nginx.com     nxt_assert(app != NULL);
6781547Smax.romanov@nginx.com 
6791547Smax.romanov@nginx.com     nxt_thread_mutex_lock(&app->mutex);
6801547Smax.romanov@nginx.com 
6811547Smax.romanov@nginx.com     /* TODO here should be find-and-add code because there can be
6821547Smax.romanov@nginx.com        port waiters in port_hash */
6831547Smax.romanov@nginx.com     nxt_port_hash_add(&app->port_hash, port);
6841547Smax.romanov@nginx.com     app->port_hash_count++;
6851547Smax.romanov@nginx.com 
6861547Smax.romanov@nginx.com     nxt_thread_mutex_unlock(&app->mutex);
6871547Smax.romanov@nginx.com 
6881547Smax.romanov@nginx.com     port->app = app;
6891547Smax.romanov@nginx.com     port->main_app_port = main_app_port;
690141Smax.romanov@nginx.com }
691141Smax.romanov@nginx.com 
692141Smax.romanov@nginx.com 
6931552Smax.romanov@nginx.com static void
694139Sigor@sysoev.ru nxt_router_conf_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
695115Sigor@sysoev.ru {
6961526Smax.romanov@nginx.com     void                    *p;
6971526Smax.romanov@nginx.com     size_t                  size;
698198Sigor@sysoev.ru     nxt_int_t               ret;
699139Sigor@sysoev.ru     nxt_router_temp_conf_t  *tmcf;
700139Sigor@sysoev.ru 
701139Sigor@sysoev.ru     tmcf = nxt_router_temp_conf(task);
702139Sigor@sysoev.ru     if (nxt_slow_path(tmcf == NULL)) {
703139Sigor@sysoev.ru         return;
70453Sigor@sysoev.ru     }
70553Sigor@sysoev.ru 
7061558Smax.romanov@nginx.com     if (nxt_slow_path(msg->fd[0] == -1)) {
7071526Smax.romanov@nginx.com         nxt_alert(task, "conf_data_handler: invalid file shm fd");
7081526Smax.romanov@nginx.com         return;
7091526Smax.romanov@nginx.com     }
7101526Smax.romanov@nginx.com 
7111526Smax.romanov@nginx.com     if (nxt_buf_mem_used_size(&msg->buf->mem) != sizeof(size_t)) {
7121526Smax.romanov@nginx.com         nxt_alert(task, "conf_data_handler: unexpected buffer size (%d)",
7131526Smax.romanov@nginx.com                   (int) nxt_buf_mem_used_size(&msg->buf->mem));
7141526Smax.romanov@nginx.com 
7151558Smax.romanov@nginx.com         nxt_fd_close(msg->fd[0]);
7161558Smax.romanov@nginx.com         msg->fd[0] = -1;
7171526Smax.romanov@nginx.com 
7181526Smax.romanov@nginx.com         return;
7191526Smax.romanov@nginx.com     }
7201526Smax.romanov@nginx.com 
7211526Smax.romanov@nginx.com     nxt_memcpy(&size, msg->buf->mem.pos, sizeof(size_t));
7221526Smax.romanov@nginx.com 
7231558Smax.romanov@nginx.com     p = nxt_mem_mmap(NULL, size, PROT_READ, MAP_SHARED, msg->fd[0], 0);
7241558Smax.romanov@nginx.com 
7251558Smax.romanov@nginx.com     nxt_fd_close(msg->fd[0]);
7261558Smax.romanov@nginx.com     msg->fd[0] = -1;
7271526Smax.romanov@nginx.com 
7281526Smax.romanov@nginx.com     if (nxt_slow_path(p == MAP_FAILED)) {
7291526Smax.romanov@nginx.com         return;
7301526Smax.romanov@nginx.com     }
7311526