xref: /unit/src/nxt_router.c (revision 2033)
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 
211926Smax.romanov@nginx.com #define NXT_SHARED_PORT_ID  0xFFFFu
221926Smax.romanov@nginx.com 
23115Sigor@sysoev.ru typedef struct {
24318Smax.romanov@nginx.com     nxt_str_t         type;
25507Smax.romanov@nginx.com     uint32_t          processes;
26507Smax.romanov@nginx.com     uint32_t          max_processes;
27507Smax.romanov@nginx.com     uint32_t          spare_processes;
28318Smax.romanov@nginx.com     nxt_msec_t        timeout;
29507Smax.romanov@nginx.com     nxt_msec_t        idle_timeout;
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 {
451885Sa.suvorov@f5.com     nxt_str_t               name;
461885Sa.suvorov@f5.com     nxt_socket_conf_t       *socket_conf;
471885Sa.suvorov@f5.com     nxt_router_temp_conf_t  *temp_conf;
481920Sa.suvorov@f5.com     nxt_tls_init_t          *tls_init;
491885Sa.suvorov@f5.com     nxt_bool_t              last;
50774Svbart@nginx.com 
511920Sa.suvorov@f5.com     nxt_queue_link_t        link;  /* for nxt_socket_conf_t.tls */
52774Svbart@nginx.com } nxt_router_tlssock_t;
53774Svbart@nginx.com 
54774Svbart@nginx.com #endif
55774Svbart@nginx.com 
56774Svbart@nginx.com 
57198Sigor@sysoev.ru typedef struct {
581828Sa.suvorov@f5.com     nxt_str_t               *name;
59198Sigor@sysoev.ru     nxt_socket_conf_t       *socket_conf;
60198Sigor@sysoev.ru     nxt_router_temp_conf_t  *temp_conf;
611828Sa.suvorov@f5.com     nxt_bool_t              last;
62198Sigor@sysoev.ru } nxt_socket_rpc_t;
63198Sigor@sysoev.ru 
64198Sigor@sysoev.ru 
65507Smax.romanov@nginx.com typedef struct {
66507Smax.romanov@nginx.com     nxt_app_t               *app;
67507Smax.romanov@nginx.com     nxt_router_temp_conf_t  *temp_conf;
681998St.nateldemoura@f5.com     uint8_t                 proto;  /* 1 bit */
69507Smax.romanov@nginx.com } nxt_app_rpc_t;
70507Smax.romanov@nginx.com 
71507Smax.romanov@nginx.com 
721926Smax.romanov@nginx.com typedef struct {
731926Smax.romanov@nginx.com     nxt_app_joint_t         *app_joint;
741926Smax.romanov@nginx.com     uint32_t                generation;
751998St.nateldemoura@f5.com     uint8_t                 proto;  /* 1 bit */
761926Smax.romanov@nginx.com } nxt_app_joint_rpc_t;
771926Smax.romanov@nginx.com 
781926Smax.romanov@nginx.com 
791488St.nateldemoura@f5.com static nxt_int_t nxt_router_prefork(nxt_task_t *task, nxt_process_t *process,
801488St.nateldemoura@f5.com     nxt_mp_t *mp);
811488St.nateldemoura@f5.com static nxt_int_t nxt_router_start(nxt_task_t *task, nxt_process_data_t *data);
82662Smax.romanov@nginx.com static void nxt_router_greet_controller(nxt_task_t *task,
83662Smax.romanov@nginx.com     nxt_port_t *controller_port);
84662Smax.romanov@nginx.com 
85507Smax.romanov@nginx.com static nxt_int_t nxt_router_start_app_process(nxt_task_t *task, nxt_app_t *app);
86425Smax.romanov@nginx.com 
871552Smax.romanov@nginx.com static void nxt_router_new_port_handler(nxt_task_t *task,
881552Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
891552Smax.romanov@nginx.com static void nxt_router_conf_data_handler(nxt_task_t *task,
901552Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
911926Smax.romanov@nginx.com static void nxt_router_app_restart_handler(nxt_task_t *task,
921926Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
931552Smax.romanov@nginx.com static void nxt_router_remove_pid_handler(nxt_task_t *task,
941552Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
951552Smax.romanov@nginx.com static void nxt_router_access_log_reopen_handler(nxt_task_t *task,
961552Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
971552Smax.romanov@nginx.com 
98139Sigor@sysoev.ru static nxt_router_temp_conf_t *nxt_router_temp_conf(nxt_task_t *task);
99198Sigor@sysoev.ru static void nxt_router_conf_apply(nxt_task_t *task, void *obj, void *data);
100198Sigor@sysoev.ru static void nxt_router_conf_ready(nxt_task_t *task,
101139Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
102139Sigor@sysoev.ru static void nxt_router_conf_error(nxt_task_t *task,
103139Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
104139Sigor@sysoev.ru static void nxt_router_conf_send(nxt_task_t *task,
105193Smax.romanov@nginx.com     nxt_router_temp_conf_t *tmcf, nxt_port_msg_type_t type);
10653Sigor@sysoev.ru 
107115Sigor@sysoev.ru static nxt_int_t nxt_router_conf_create(nxt_task_t *task,
108115Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf, u_char *start, u_char *end);
1091183Svbart@nginx.com static nxt_int_t nxt_router_conf_process_static(nxt_task_t *task,
1101183Svbart@nginx.com     nxt_router_conf_t *rtcf, nxt_conf_value_t *conf);
1111936So.canty@f5.com static nxt_int_t nxt_router_conf_process_client_ip(nxt_task_t *task,
1121936So.canty@f5.com     nxt_router_temp_conf_t *tmcf, nxt_socket_conf_t *skcf,
1131936So.canty@f5.com     nxt_conf_value_t *conf);
1141563Svbart@nginx.com 
115133Sigor@sysoev.ru static nxt_app_t *nxt_router_app_find(nxt_queue_t *queue, nxt_str_t *name);
1161563Svbart@nginx.com static nxt_int_t nxt_router_apps_hash_test(nxt_lvlhsh_query_t *lhq, void *data);
1171563Svbart@nginx.com static nxt_int_t nxt_router_apps_hash_add(nxt_router_conf_t *rtcf,
1181563Svbart@nginx.com     nxt_app_t *app);
1191563Svbart@nginx.com static nxt_app_t *nxt_router_apps_hash_get(nxt_router_conf_t *rtcf,
1201563Svbart@nginx.com     nxt_str_t *name);
1211563Svbart@nginx.com static void nxt_router_apps_hash_use(nxt_task_t *task, nxt_router_conf_t *rtcf,
1221563Svbart@nginx.com     int i);
1231563Svbart@nginx.com 
1241555Smax.romanov@nginx.com static nxt_int_t nxt_router_app_queue_init(nxt_task_t *task,
1251555Smax.romanov@nginx.com     nxt_port_t *port);
1261555Smax.romanov@nginx.com static nxt_int_t nxt_router_port_queue_init(nxt_task_t *task,
1271555Smax.romanov@nginx.com     nxt_port_t *port);
1281555Smax.romanov@nginx.com static nxt_int_t nxt_router_port_queue_map(nxt_task_t *task,
1291555Smax.romanov@nginx.com     nxt_port_t *port, nxt_fd_t fd);
130198Sigor@sysoev.ru static void nxt_router_listen_socket_rpc_create(nxt_task_t *task,
131198Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf, nxt_socket_conf_t *skcf);
132198Sigor@sysoev.ru static void nxt_router_listen_socket_ready(nxt_task_t *task,
133198Sigor@sysoev.ru     nxt_port_recv_msg_t *msg, void *data);
134198Sigor@sysoev.ru static void nxt_router_listen_socket_error(nxt_task_t *task,
135198Sigor@sysoev.ru     nxt_port_recv_msg_t *msg, void *data);
136774Svbart@nginx.com #if (NXT_TLS)
137774Svbart@nginx.com static void nxt_router_tls_rpc_handler(nxt_task_t *task,
138774Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
1391828Sa.suvorov@f5.com static nxt_int_t nxt_router_conf_tls_insert(nxt_router_temp_conf_t *tmcf,
1401920Sa.suvorov@f5.com     nxt_conf_value_t *value, nxt_socket_conf_t *skcf, nxt_tls_init_t *tls_init,
1411920Sa.suvorov@f5.com     nxt_bool_t last);
142774Svbart@nginx.com #endif
143507Smax.romanov@nginx.com static void nxt_router_app_rpc_create(nxt_task_t *task,
144507Smax.romanov@nginx.com     nxt_router_temp_conf_t *tmcf, nxt_app_t *app);
145507Smax.romanov@nginx.com static void nxt_router_app_prefork_ready(nxt_task_t *task,
146507Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, void *data);
147507Smax.romanov@nginx.com static void nxt_router_app_prefork_error(nxt_task_t *task,
148507Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, void *data);
149359Sigor@sysoev.ru static nxt_socket_conf_t *nxt_router_socket_conf(nxt_task_t *task,
150359Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf, nxt_str_t *name);
151359Sigor@sysoev.ru static nxt_int_t nxt_router_listen_socket_find(nxt_router_temp_conf_t *tmcf,
152359Sigor@sysoev.ru     nxt_socket_conf_t *nskcf, nxt_sockaddr_t *sa);
15353Sigor@sysoev.ru 
15453Sigor@sysoev.ru static nxt_int_t nxt_router_engines_create(nxt_task_t *task,
15553Sigor@sysoev.ru     nxt_router_t *router, nxt_router_temp_conf_t *tmcf,
15653Sigor@sysoev.ru     const nxt_event_interface_t *interface);
157115Sigor@sysoev.ru static nxt_int_t nxt_router_engine_conf_create(nxt_router_temp_conf_t *tmcf,
158115Sigor@sysoev.ru     nxt_router_engine_conf_t *recf);
159115Sigor@sysoev.ru static nxt_int_t nxt_router_engine_conf_update(nxt_router_temp_conf_t *tmcf,
160115Sigor@sysoev.ru     nxt_router_engine_conf_t *recf);
161115Sigor@sysoev.ru static nxt_int_t nxt_router_engine_conf_delete(nxt_router_temp_conf_t *tmcf,
162115Sigor@sysoev.ru     nxt_router_engine_conf_t *recf);
163154Sigor@sysoev.ru static nxt_int_t nxt_router_engine_joints_create(nxt_router_temp_conf_t *tmcf,
164154Sigor@sysoev.ru     nxt_router_engine_conf_t *recf, nxt_queue_t *sockets,
165154Sigor@sysoev.ru     nxt_work_handler_t handler);
166313Sigor@sysoev.ru static nxt_int_t nxt_router_engine_quit(nxt_router_temp_conf_t *tmcf,
167313Sigor@sysoev.ru     nxt_router_engine_conf_t *recf);
168139Sigor@sysoev.ru static nxt_int_t nxt_router_engine_joints_delete(nxt_router_temp_conf_t *tmcf,
169139Sigor@sysoev.ru     nxt_router_engine_conf_t *recf, nxt_queue_t *sockets);
17053Sigor@sysoev.ru 
17153Sigor@sysoev.ru static nxt_int_t nxt_router_threads_create(nxt_task_t *task, nxt_runtime_t *rt,
17253Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
17353Sigor@sysoev.ru static nxt_int_t nxt_router_thread_create(nxt_task_t *task, nxt_runtime_t *rt,
17453Sigor@sysoev.ru     nxt_event_engine_t *engine);
175343Smax.romanov@nginx.com static void nxt_router_apps_sort(nxt_task_t *task, nxt_router_t *router,
176133Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
17753Sigor@sysoev.ru 
178315Sigor@sysoev.ru static void nxt_router_engines_post(nxt_router_t *router,
179315Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
180315Sigor@sysoev.ru static void nxt_router_engine_post(nxt_event_engine_t *engine,
181315Sigor@sysoev.ru     nxt_work_t *jobs);
18253Sigor@sysoev.ru 
18353Sigor@sysoev.ru static void nxt_router_thread_start(void *data);
1841545Smax.romanov@nginx.com static void nxt_router_rt_add_port(nxt_task_t *task, void *obj,
1851545Smax.romanov@nginx.com     void *data);
18653Sigor@sysoev.ru static void nxt_router_listen_socket_create(nxt_task_t *task, void *obj,
18753Sigor@sysoev.ru     void *data);
18853Sigor@sysoev.ru static void nxt_router_listen_socket_update(nxt_task_t *task, void *obj,
18953Sigor@sysoev.ru     void *data);
19053Sigor@sysoev.ru static void nxt_router_listen_socket_delete(nxt_task_t *task, void *obj,
19153Sigor@sysoev.ru     void *data);
192313Sigor@sysoev.ru static void nxt_router_worker_thread_quit(nxt_task_t *task, void *obj,
193313Sigor@sysoev.ru     void *data);
19453Sigor@sysoev.ru static void nxt_router_listen_socket_close(nxt_task_t *task, void *obj,
19553Sigor@sysoev.ru     void *data);
19653Sigor@sysoev.ru static void nxt_router_thread_exit_handler(nxt_task_t *task, void *obj,
19753Sigor@sysoev.ru     void *data);
1981547Smax.romanov@nginx.com static void nxt_router_req_headers_ack_handler(nxt_task_t *task,
1991547Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, nxt_request_rpc_data_t *req_rpc_data);
200359Sigor@sysoev.ru static void nxt_router_listen_socket_release(nxt_task_t *task,
201359Sigor@sysoev.ru     nxt_socket_conf_t *skcf);
20253Sigor@sysoev.ru 
203630Svbart@nginx.com static void nxt_router_access_log_writer(nxt_task_t *task,
204630Svbart@nginx.com     nxt_http_request_t *r, nxt_router_access_log_t *access_log);
205630Svbart@nginx.com static u_char *nxt_router_access_log_date(u_char *buf, nxt_realtime_t *now,
206630Svbart@nginx.com     struct tm *tm, size_t size, const char *format);
207630Svbart@nginx.com static void nxt_router_access_log_open(nxt_task_t *task,
208630Svbart@nginx.com     nxt_router_temp_conf_t *tmcf);
209630Svbart@nginx.com static void nxt_router_access_log_ready(nxt_task_t *task,
210630Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
211630Svbart@nginx.com static void nxt_router_access_log_error(nxt_task_t *task,
212630Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
213*2033SN/A static void nxt_router_access_log_use(nxt_thread_spinlock_t *lock,
214*2033SN/A     nxt_router_access_log_t *access_log);
215630Svbart@nginx.com static void nxt_router_access_log_release(nxt_task_t *task,
216630Svbart@nginx.com     nxt_thread_spinlock_t *lock, nxt_router_access_log_t *access_log);
217651Svbart@nginx.com static void nxt_router_access_log_reopen_completion(nxt_task_t *task, void *obj,
218651Svbart@nginx.com     void *data);
219631Svbart@nginx.com static void nxt_router_access_log_reopen_ready(nxt_task_t *task,
220631Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
221631Svbart@nginx.com static void nxt_router_access_log_reopen_error(nxt_task_t *task,
222631Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
223630Svbart@nginx.com 
224343Smax.romanov@nginx.com static void nxt_router_app_port_ready(nxt_task_t *task,
225343Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, void *data);
2261547Smax.romanov@nginx.com static nxt_int_t nxt_router_app_shared_port_send(nxt_task_t *task,
2271547Smax.romanov@nginx.com     nxt_port_t *app_port);
228343Smax.romanov@nginx.com static void nxt_router_app_port_error(nxt_task_t *task,
229343Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, void *data);
230343Smax.romanov@nginx.com 
2311563Svbart@nginx.com static void nxt_router_app_use(nxt_task_t *task, nxt_app_t *app, int i);
232753Smax.romanov@nginx.com static void nxt_router_app_unlink(nxt_task_t *task, nxt_app_t *app);
2331123Smax.romanov@nginx.com 
2341978Smax.romanov@nginx.com static void nxt_router_app_port_release(nxt_task_t *task, nxt_app_t *app,
2351978Smax.romanov@nginx.com     nxt_port_t *port, nxt_apr_action_t action);
2361547Smax.romanov@nginx.com static void nxt_router_app_port_get(nxt_task_t *task, nxt_app_t *app,
2371547Smax.romanov@nginx.com     nxt_request_rpc_data_t *req_rpc_data);
2381561Smax.romanov@nginx.com static void nxt_router_http_request_error(nxt_task_t *task, void *obj,
2391561Smax.romanov@nginx.com     void *data);
2401547Smax.romanov@nginx.com static void nxt_router_http_request_done(nxt_task_t *task, void *obj,
2411547Smax.romanov@nginx.com     void *data);
242141Smax.romanov@nginx.com 
243425Smax.romanov@nginx.com static void nxt_router_app_prepare_request(nxt_task_t *task,
2441547Smax.romanov@nginx.com     nxt_request_rpc_data_t *req_rpc_data);
2451007Salexander.borisov@nginx.com static nxt_buf_t *nxt_router_prepare_msg(nxt_task_t *task,
2461547Smax.romanov@nginx.com     nxt_http_request_t *r, nxt_app_t *app, const nxt_str_t *prefix);
247510Salexander.borisov@nginx.com 
248318Smax.romanov@nginx.com static void nxt_router_app_timeout(nxt_task_t *task, void *obj, void *data);
249507Smax.romanov@nginx.com static void nxt_router_adjust_idle_timer(nxt_task_t *task, void *obj,
250507Smax.romanov@nginx.com     void *data);
251507Smax.romanov@nginx.com static void nxt_router_app_idle_timeout(nxt_task_t *task, void *obj,
252507Smax.romanov@nginx.com     void *data);
253753Smax.romanov@nginx.com static void nxt_router_app_joint_release_handler(nxt_task_t *task, void *obj,
254507Smax.romanov@nginx.com     void *data);
255753Smax.romanov@nginx.com static void nxt_router_free_app(nxt_task_t *task, void *obj, void *data);
256431Sigor@sysoev.ru 
257431Sigor@sysoev.ru static const nxt_http_request_state_t  nxt_http_request_send_state;
258431Sigor@sysoev.ru static void nxt_http_request_send_body(nxt_task_t *task, void *obj, void *data);
259141Smax.romanov@nginx.com 
260753Smax.romanov@nginx.com static void nxt_router_app_joint_use(nxt_task_t *task,
261753Smax.romanov@nginx.com     nxt_app_joint_t *app_joint, int i);
262753Smax.romanov@nginx.com 
2631547Smax.romanov@nginx.com static void nxt_router_http_request_release_post(nxt_task_t *task,
2641007Salexander.borisov@nginx.com     nxt_http_request_t *r);
2651007Salexander.borisov@nginx.com static void nxt_router_http_request_release(nxt_task_t *task, void *obj,
2661007Salexander.borisov@nginx.com     void *data);
2671321Smax.romanov@nginx.com static void nxt_router_oosm_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
2681545Smax.romanov@nginx.com static void nxt_router_get_port_handler(nxt_task_t *task,
2691545Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
2701546Smax.romanov@nginx.com static void nxt_router_get_mmap_handler(nxt_task_t *task,
2711546Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
2721007Salexander.borisov@nginx.com 
2731149Smax.romanov@nginx.com extern const nxt_http_request_state_t  nxt_http_websocket;
2741131Smax.romanov@nginx.com 
275119Smax.romanov@nginx.com static nxt_router_t  *nxt_router;
27620Sigor@sysoev.ru 
277743Smax.romanov@nginx.com static const nxt_str_t http_prefix = nxt_string("HTTP_");
278743Smax.romanov@nginx.com static const nxt_str_t empty_prefix = nxt_string("");
279743Smax.romanov@nginx.com 
280743Smax.romanov@nginx.com static const nxt_str_t  *nxt_app_msg_prefix[] = {
281804Svbart@nginx.com     &empty_prefix,
2821594Smax.romanov@nginx.com     &empty_prefix,
283743Smax.romanov@nginx.com     &http_prefix,
284743Smax.romanov@nginx.com     &http_prefix,
285743Smax.romanov@nginx.com     &http_prefix,
286977Smax.romanov@gmail.com     &empty_prefix,
287216Sigor@sysoev.ru };
288216Sigor@sysoev.ru 
289216Sigor@sysoev.ru 
2901488St.nateldemoura@f5.com static const nxt_port_handlers_t  nxt_router_process_port_handlers = {
2911488St.nateldemoura@f5.com     .quit         = nxt_signal_quit_handler,
292662Smax.romanov@nginx.com     .new_port     = nxt_router_new_port_handler,
2931545Smax.romanov@nginx.com     .get_port     = nxt_router_get_port_handler,
294662Smax.romanov@nginx.com     .change_file  = nxt_port_change_log_file_handler,
295662Smax.romanov@nginx.com     .mmap         = nxt_port_mmap_handler,
2961546Smax.romanov@nginx.com     .get_mmap     = nxt_router_get_mmap_handler,
297662Smax.romanov@nginx.com     .data         = nxt_router_conf_data_handler,
2981926Smax.romanov@nginx.com     .app_restart  = nxt_router_app_restart_handler,
299662Smax.romanov@nginx.com     .remove_pid   = nxt_router_remove_pid_handler,
300662Smax.romanov@nginx.com     .access_log   = nxt_router_access_log_reopen_handler,
301662Smax.romanov@nginx.com     .rpc_ready    = nxt_port_rpc_handler,
302662Smax.romanov@nginx.com     .rpc_error    = nxt_port_rpc_handler,
3031321Smax.romanov@nginx.com     .oosm         = nxt_router_oosm_handler,
304662Smax.romanov@nginx.com };
305662Smax.romanov@nginx.com 
306662Smax.romanov@nginx.com 
3071488St.nateldemoura@f5.com const nxt_process_init_t  nxt_router_process = {
3081488St.nateldemoura@f5.com     .name           = "router",
3091488St.nateldemoura@f5.com     .type           = NXT_PROCESS_ROUTER,
3101488St.nateldemoura@f5.com     .prefork        = nxt_router_prefork,
3111488St.nateldemoura@f5.com     .restart        = 1,
3121488St.nateldemoura@f5.com     .setup          = nxt_process_core_setup,
3131488St.nateldemoura@f5.com     .start          = nxt_router_start,
3141488St.nateldemoura@f5.com     .port_handlers  = &nxt_router_process_port_handlers,
3151488St.nateldemoura@f5.com     .signals        = nxt_process_signals,
3161488St.nateldemoura@f5.com };
3171488St.nateldemoura@f5.com 
3181488St.nateldemoura@f5.com 
3191509Sigor@sysoev.ru /* Queues of nxt_socket_conf_t */
3201509Sigor@sysoev.ru nxt_queue_t  creating_sockets;
3211509Sigor@sysoev.ru nxt_queue_t  pending_sockets;
3221509Sigor@sysoev.ru nxt_queue_t  updating_sockets;
3231509Sigor@sysoev.ru nxt_queue_t  keeping_sockets;
3241509Sigor@sysoev.ru nxt_queue_t  deleting_sockets;
3251509Sigor@sysoev.ru 
3261509Sigor@sysoev.ru 
3271488St.nateldemoura@f5.com static nxt_int_t
3281488St.nateldemoura@f5.com nxt_router_prefork(nxt_task_t *task, nxt_process_t *process, nxt_mp_t *mp)
3291488St.nateldemoura@f5.com {
3301488St.nateldemoura@f5.com     nxt_runtime_stop_app_processes(task, task->thread->runtime);
3311488St.nateldemoura@f5.com 
3321488St.nateldemoura@f5.com     return NXT_OK;
3331488St.nateldemoura@f5.com }
3341488St.nateldemoura@f5.com 
3351488St.nateldemoura@f5.com 
3361488St.nateldemoura@f5.com static nxt_int_t
3371488St.nateldemoura@f5.com nxt_router_start(nxt_task_t *task, nxt_process_data_t *data)
33820Sigor@sysoev.ru {
339141Smax.romanov@nginx.com     nxt_int_t      ret;
340662Smax.romanov@nginx.com     nxt_port_t     *controller_port;
341141Smax.romanov@nginx.com     nxt_router_t   *router;
342141Smax.romanov@nginx.com     nxt_runtime_t  *rt;
343141Smax.romanov@nginx.com 
344141Smax.romanov@nginx.com     rt = task->thread->runtime;
34553Sigor@sysoev.ru 
3461488St.nateldemoura@f5.com     nxt_log(task, NXT_LOG_INFO, "router started");
3471488St.nateldemoura@f5.com 
348771Sigor@sysoev.ru #if (NXT_TLS)
349771Sigor@sysoev.ru     rt->tls = nxt_service_get(rt->services, "SSL/TLS", "OpenSSL");
350771Sigor@sysoev.ru     if (nxt_slow_path(rt->tls == NULL)) {
351771Sigor@sysoev.ru         return NXT_ERROR;
352771Sigor@sysoev.ru     }
353771Sigor@sysoev.ru 
354771Sigor@sysoev.ru     ret = rt->tls->library_init(task);
355771Sigor@sysoev.ru     if (nxt_slow_path(ret != NXT_OK)) {
356771Sigor@sysoev.ru         return ret;
357771Sigor@sysoev.ru     }
358771Sigor@sysoev.ru #endif
359771Sigor@sysoev.ru 
3601459Smax.romanov@nginx.com     ret = nxt_http_init(task);
36188Smax.romanov@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
36288Smax.romanov@nginx.com         return ret;
36388Smax.romanov@nginx.com     }
36488Smax.romanov@nginx.com 
36553Sigor@sysoev.ru     router = nxt_zalloc(sizeof(nxt_router_t));
36653Sigor@sysoev.ru     if (nxt_slow_path(router == NULL)) {
36753Sigor@sysoev.ru         return NXT_ERROR;
36853Sigor@sysoev.ru     }
36953Sigor@sysoev.ru 
37053Sigor@sysoev.ru     nxt_queue_init(&router->engines);
37153Sigor@sysoev.ru     nxt_queue_init(&router->sockets);
372133Sigor@sysoev.ru     nxt_queue_init(&router->apps);
37353Sigor@sysoev.ru 
374119Smax.romanov@nginx.com     nxt_router = router;
375119Smax.romanov@nginx.com 
376662Smax.romanov@nginx.com     controller_port = rt->port_by_type[NXT_PROCESS_CONTROLLER];
377662Smax.romanov@nginx.com     if (controller_port != NULL) {
378662Smax.romanov@nginx.com         nxt_router_greet_controller(task, controller_port);
379662Smax.romanov@nginx.com     }
380662Smax.romanov@nginx.com 
381115Sigor@sysoev.ru     return NXT_OK;
382115Sigor@sysoev.ru }
383115Sigor@sysoev.ru 
384115Sigor@sysoev.ru 
385343Smax.romanov@nginx.com static void
386662Smax.romanov@nginx.com nxt_router_greet_controller(nxt_task_t *task, nxt_port_t *controller_port)
387662Smax.romanov@nginx.com {
388662Smax.romanov@nginx.com     nxt_port_socket_write(task, controller_port, NXT_PORT_MSG_PROCESS_READY,
389662Smax.romanov@nginx.com                           -1, 0, 0, NULL);
390662Smax.romanov@nginx.com }
391662Smax.romanov@nginx.com 
392662Smax.romanov@nginx.com 
393662Smax.romanov@nginx.com static void
394507Smax.romanov@nginx.com nxt_router_start_app_process_handler(nxt_task_t *task, nxt_port_t *port,
395507Smax.romanov@nginx.com     void *data)
396167Smax.romanov@nginx.com {
3971926Smax.romanov@nginx.com     size_t               size;
3981926Smax.romanov@nginx.com     uint32_t             stream;
3991926Smax.romanov@nginx.com     nxt_int_t            ret;
4001926Smax.romanov@nginx.com     nxt_app_t            *app;
4011926Smax.romanov@nginx.com     nxt_buf_t            *b;
4021998St.nateldemoura@f5.com     nxt_port_t           *dport;
4031926Smax.romanov@nginx.com     nxt_runtime_t        *rt;
4041926Smax.romanov@nginx.com     nxt_app_joint_rpc_t  *app_joint_rpc;
405343Smax.romanov@nginx.com 
406343Smax.romanov@nginx.com     app = data;
407167Smax.romanov@nginx.com 
4081998St.nateldemoura@f5.com     nxt_thread_mutex_lock(&app->mutex);
4091998St.nateldemoura@f5.com 
4101998St.nateldemoura@f5.com     dport = app->proto_port;
4111998St.nateldemoura@f5.com 
4121998St.nateldemoura@f5.com     nxt_thread_mutex_unlock(&app->mutex);
4131998St.nateldemoura@f5.com 
4141998St.nateldemoura@f5.com     if (dport != NULL) {
4151998St.nateldemoura@f5.com         nxt_debug(task, "app '%V' %p start process", &app->name, app);
4161998St.nateldemoura@f5.com 
4171998St.nateldemoura@f5.com         b = NULL;
4181998St.nateldemoura@f5.com 
4191998St.nateldemoura@f5.com     } else {
4201998St.nateldemoura@f5.com         if (app->proto_port_requests > 0) {
4211998St.nateldemoura@f5.com             nxt_debug(task, "app '%V' %p wait for prototype process",
4221998St.nateldemoura@f5.com                       &app->name, app);
4231998St.nateldemoura@f5.com 
4241998St.nateldemoura@f5.com             app->proto_port_requests++;
4251998St.nateldemoura@f5.com 
4261998St.nateldemoura@f5.com             goto skip;
4271998St.nateldemoura@f5.com         }
4281998St.nateldemoura@f5.com 
4291998St.nateldemoura@f5.com         nxt_debug(task, "app '%V' %p start prototype process", &app->name, app);
4301998St.nateldemoura@f5.com 
4311998St.nateldemoura@f5.com         rt = task->thread->runtime;
4321998St.nateldemoura@f5.com         dport = rt->port_by_type[NXT_PROCESS_MAIN];
4331998St.nateldemoura@f5.com 
4341998St.nateldemoura@f5.com         size = app->name.length + 1 + app->conf.length;
4351998St.nateldemoura@f5.com 
4361998St.nateldemoura@f5.com         b = nxt_buf_mem_alloc(task->thread->engine->mem_pool, size, 0);
4371998St.nateldemoura@f5.com         if (nxt_slow_path(b == NULL)) {
4381998St.nateldemoura@f5.com             goto failed;
4391998St.nateldemoura@f5.com         }
4401998St.nateldemoura@f5.com 
4411998St.nateldemoura@f5.com         nxt_buf_cpystr(b, &app->name);
4421998St.nateldemoura@f5.com         *b->mem.free++ = '\0';
4431998St.nateldemoura@f5.com         nxt_buf_cpystr(b, &app->conf);
4441998St.nateldemoura@f5.com     }
445343Smax.romanov@nginx.com 
4461926Smax.romanov@nginx.com     app_joint_rpc = nxt_port_rpc_register_handler_ex(task, port,
4471926Smax.romanov@nginx.com                                                      nxt_router_app_port_ready,
4481926Smax.romanov@nginx.com                                                      nxt_router_app_port_error,
4491926Smax.romanov@nginx.com                                                    sizeof(nxt_app_joint_rpc_t));
4501926Smax.romanov@nginx.com     if (nxt_slow_path(app_joint_rpc == NULL)) {
451343Smax.romanov@nginx.com         goto failed;
452343Smax.romanov@nginx.com     }
453343Smax.romanov@nginx.com 
4541926Smax.romanov@nginx.com     stream = nxt_port_rpc_ex_stream(app_joint_rpc);
4551926Smax.romanov@nginx.com 
4561998St.nateldemoura@f5.com     ret = nxt_port_socket_write(task, dport, NXT_PORT_MSG_START_PROCESS,
4571488St.nateldemoura@f5.com                                 -1, stream, port->id, b);
458648Svbart@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
459648Svbart@nginx.com         nxt_port_rpc_cancel(task, port, stream);
460753Smax.romanov@nginx.com 
461648Svbart@nginx.com         goto failed;
462648Svbart@nginx.com     }
463343Smax.romanov@nginx.com 
4641926Smax.romanov@nginx.com     app_joint_rpc->app_joint = app->joint;
4651926Smax.romanov@nginx.com     app_joint_rpc->generation = app->generation;
4661998St.nateldemoura@f5.com     app_joint_rpc->proto = (b != NULL);
4671998St.nateldemoura@f5.com 
4681998St.nateldemoura@f5.com     if (b != NULL) {
4691998St.nateldemoura@f5.com         app->proto_port_requests++;
4701998St.nateldemoura@f5.com 
4711998St.nateldemoura@f5.com         b = NULL;
4721998St.nateldemoura@f5.com     }
4731926Smax.romanov@nginx.com 
4741926Smax.romanov@nginx.com     nxt_router_app_joint_use(task, app->joint, 1);
4751926Smax.romanov@nginx.com 
476343Smax.romanov@nginx.com failed:
477343Smax.romanov@nginx.com 
478648Svbart@nginx.com     if (b != NULL) {
4791998St.nateldemoura@f5.com         nxt_mp_free(b->data, b);
4801998St.nateldemoura@f5.com     }
4811998St.nateldemoura@f5.com 
4821998St.nateldemoura@f5.com skip:
483343Smax.romanov@nginx.com 
484343Smax.romanov@nginx.com     nxt_router_app_use(task, app, -1);
485167Smax.romanov@nginx.com }
486167Smax.romanov@nginx.com 
487167Smax.romanov@nginx.com 
488753Smax.romanov@nginx.com static void
489753Smax.romanov@nginx.com nxt_router_app_joint_use(nxt_task_t *task, nxt_app_joint_t *app_joint, int i)
490753Smax.romanov@nginx.com {
491753Smax.romanov@nginx.com     app_joint->use_count += i;
492753Smax.romanov@nginx.com 
493753Smax.romanov@nginx.com     if (app_joint->use_count == 0) {
494753Smax.romanov@nginx.com         nxt_assert(app_joint->app == NULL);
495753Smax.romanov@nginx.com 
496753Smax.romanov@nginx.com         nxt_free(app_joint);
497753Smax.romanov@nginx.com     }
498753Smax.romanov@nginx.com }
499753Smax.romanov@nginx.com 
500753Smax.romanov@nginx.com 
501343Smax.romanov@nginx.com static nxt_int_t
502507Smax.romanov@nginx.com nxt_router_start_app_process(nxt_task_t *task, nxt_app_t *app)
503141Smax.romanov@nginx.com {
504343Smax.romanov@nginx.com     nxt_int_t      res;
505343Smax.romanov@nginx.com     nxt_port_t     *router_port;
506343Smax.romanov@nginx.com     nxt_runtime_t  *rt;
507343Smax.romanov@nginx.com 
5081549Smax.romanov@nginx.com     nxt_debug(task, "app '%V' start process", &app->name);
5091549Smax.romanov@nginx.com 
510343Smax.romanov@nginx.com     rt = task->thread->runtime;
511343Smax.romanov@nginx.com     router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
512343Smax.romanov@nginx.com 
513343Smax.romanov@nginx.com     nxt_router_app_use(task, app, 1);
514343Smax.romanov@nginx.com 
515507Smax.romanov@nginx.com     res = nxt_port_post(task, router_port, nxt_router_start_app_process_handler,
516343Smax.romanov@nginx.com                         app);
517343Smax.romanov@nginx.com 
518343Smax.romanov@nginx.com     if (res == NXT_OK) {
519343Smax.romanov@nginx.com         return res;
520318Smax.romanov@nginx.com     }
521318Smax.romanov@nginx.com 
522343Smax.romanov@nginx.com     nxt_thread_mutex_lock(&app->mutex);
523343Smax.romanov@nginx.com 
524507Smax.romanov@nginx.com     app->pending_processes--;
525343Smax.romanov@nginx.com 
526343Smax.romanov@nginx.com     nxt_thread_mutex_unlock(&app->mutex);
527343Smax.romanov@nginx.com 
528343Smax.romanov@nginx.com     nxt_router_app_use(task, app, -1);
529343Smax.romanov@nginx.com 
530343Smax.romanov@nginx.com     return NXT_ERROR;
531318Smax.romanov@nginx.com }
532318Smax.romanov@nginx.com 
533318Smax.romanov@nginx.com 
534423Smax.romanov@nginx.com nxt_inline nxt_bool_t
5351555Smax.romanov@nginx.com nxt_router_msg_cancel(nxt_task_t *task, nxt_request_rpc_data_t *req_rpc_data)
536423Smax.romanov@nginx.com {
5371555Smax.romanov@nginx.com     nxt_buf_t       *b, *next;
5381555Smax.romanov@nginx.com     nxt_bool_t      cancelled;
5391926Smax.romanov@nginx.com     nxt_port_t      *app_port;
5401555Smax.romanov@nginx.com     nxt_msg_info_t  *msg_info;
5411555Smax.romanov@nginx.com 
5421555Smax.romanov@nginx.com     msg_info = &req_rpc_data->msg_info;
543423Smax.romanov@nginx.com 
544423Smax.romanov@nginx.com     if (msg_info->buf == NULL) {
545423Smax.romanov@nginx.com         return 0;
546423Smax.romanov@nginx.com     }
547423Smax.romanov@nginx.com 
5481926Smax.romanov@nginx.com     app_port = req_rpc_data->app_port;
5491926Smax.romanov@nginx.com 
5501926Smax.romanov@nginx.com     if (app_port != NULL && app_port->id == NXT_SHARED_PORT_ID) {
5511926Smax.romanov@nginx.com         cancelled = nxt_app_queue_cancel(app_port->queue,
5521926Smax.romanov@nginx.com                                          msg_info->tracking_cookie,
5531926Smax.romanov@nginx.com                                          req_rpc_data->stream);
5541926Smax.romanov@nginx.com 
5551926Smax.romanov@nginx.com         if (cancelled) {
5561926Smax.romanov@nginx.com             nxt_debug(task, "stream #%uD: cancelled by router",
5571926Smax.romanov@nginx.com                       req_rpc_data->stream);
5581926Smax.romanov@nginx.com         }
5591926Smax.romanov@nginx.com 
5601926Smax.romanov@nginx.com     } else {
5611926Smax.romanov@nginx.com         cancelled = 0;
562423Smax.romanov@nginx.com     }
563423Smax.romanov@nginx.com 
564423Smax.romanov@nginx.com     for (b = msg_info->buf; b != NULL; b = next) {
565423Smax.romanov@nginx.com         next = b->next;
5661269Sigor@sysoev.ru         b->next = NULL;
567423Smax.romanov@nginx.com 
568423Smax.romanov@nginx.com         if (b->is_port_mmap_sent) {
569423Smax.romanov@nginx.com             b->is_port_mmap_sent = cancelled == 0;
570423Smax.romanov@nginx.com         }
5711829Smax.romanov@nginx.com 
5721829Smax.romanov@nginx.com         b->completion_handler(task, b, b->parent);
573423Smax.romanov@nginx.com     }
574423Smax.romanov@nginx.com 
575423Smax.romanov@nginx.com     msg_info->buf = NULL;
576423Smax.romanov@nginx.com 
577423Smax.romanov@nginx.com     return cancelled;
578423Smax.romanov@nginx.com }
579423Smax.romanov@nginx.com 
580423Smax.romanov@nginx.com 
581425Smax.romanov@nginx.com nxt_inline nxt_bool_t
582425Smax.romanov@nginx.com nxt_queue_chk_remove(nxt_queue_link_t *lnk)
583425Smax.romanov@nginx.com {
584425Smax.romanov@nginx.com     if (lnk->next != NULL) {
585425Smax.romanov@nginx.com         nxt_queue_remove(lnk);
586425Smax.romanov@nginx.com 
587425Smax.romanov@nginx.com         lnk->next = NULL;
588425Smax.romanov@nginx.com 
589425Smax.romanov@nginx.com         return 1;
590425Smax.romanov@nginx.com     }
591425Smax.romanov@nginx.com 
592425Smax.romanov@nginx.com     return 0;
593425Smax.romanov@nginx.com }
594425Smax.romanov@nginx.com 
595425Smax.romanov@nginx.com 
596343Smax.romanov@nginx.com nxt_inline void
5971123Smax.romanov@nginx.com nxt_request_rpc_data_unlink(nxt_task_t *task,
5981123Smax.romanov@nginx.com     nxt_request_rpc_data_t *req_rpc_data)
599343Smax.romanov@nginx.com {
6001561Smax.romanov@nginx.com     nxt_app_t           *app;
6011561Smax.romanov@nginx.com     nxt_bool_t          unlinked;
6021547Smax.romanov@nginx.com     nxt_http_request_t  *r;
6031547Smax.romanov@nginx.com 
6041555Smax.romanov@nginx.com     nxt_router_msg_cancel(task, req_rpc_data);
6051123Smax.romanov@nginx.com 
6061978Smax.romanov@nginx.com     app = req_rpc_data->app;
6071978Smax.romanov@nginx.com 
6081123Smax.romanov@nginx.com     if (req_rpc_data->app_port != NULL) {
6091978Smax.romanov@nginx.com         nxt_router_app_port_release(task, app, req_rpc_data->app_port,
6101123Smax.romanov@nginx.com                                     req_rpc_data->apr_action);
6111123Smax.romanov@nginx.com 
6121123Smax.romanov@nginx.com         req_rpc_data->app_port = NULL;
6131123Smax.romanov@nginx.com     }
6141123Smax.romanov@nginx.com 
6151547Smax.romanov@nginx.com     r = req_rpc_data->request;
6161547Smax.romanov@nginx.com 
6171547Smax.romanov@nginx.com     if (r != NULL) {
6181547Smax.romanov@nginx.com         r->timer_data = NULL;
6191547Smax.romanov@nginx.com 
6201547Smax.romanov@nginx.com         nxt_router_http_request_release_post(task, r);
6211547Smax.romanov@nginx.com 
6221547Smax.romanov@nginx.com         r->req_rpc_data = NULL;
6231123Smax.romanov@nginx.com         req_rpc_data->request = NULL;
6241561Smax.romanov@nginx.com 
6251561Smax.romanov@nginx.com         if (app != NULL) {
6261561Smax.romanov@nginx.com             unlinked = 0;
6271561Smax.romanov@nginx.com 
6281561Smax.romanov@nginx.com             nxt_thread_mutex_lock(&app->mutex);
6291561Smax.romanov@nginx.com 
6301561Smax.romanov@nginx.com             if (r->app_link.next != NULL) {
6311561Smax.romanov@nginx.com                 nxt_queue_remove(&r->app_link);
6321561Smax.romanov@nginx.com                 r->app_link.next = NULL;
6331561Smax.romanov@nginx.com 
6341561Smax.romanov@nginx.com                 unlinked = 1;
6351561Smax.romanov@nginx.com             }
6361561Smax.romanov@nginx.com 
6371561Smax.romanov@nginx.com             nxt_thread_mutex_unlock(&app->mutex);
6381561Smax.romanov@nginx.com 
6391561Smax.romanov@nginx.com             if (unlinked) {
6401561Smax.romanov@nginx.com                 nxt_mp_release(r->mem_pool);
6411561Smax.romanov@nginx.com             }
6421561Smax.romanov@nginx.com         }
6431561Smax.romanov@nginx.com     }
6441561Smax.romanov@nginx.com 
6451561Smax.romanov@nginx.com     if (app != NULL) {
6461561Smax.romanov@nginx.com         nxt_router_app_use(task, app, -1);
6471561Smax.romanov@nginx.com 
6481561Smax.romanov@nginx.com         req_rpc_data->app = NULL;
649346Smax.romanov@nginx.com     }
6501547Smax.romanov@nginx.com 
6511547Smax.romanov@nginx.com     if (req_rpc_data->msg_info.body_fd != -1) {
6521547Smax.romanov@nginx.com         nxt_fd_close(req_rpc_data->msg_info.body_fd);
6531547Smax.romanov@nginx.com 
6541547Smax.romanov@nginx.com         req_rpc_data->msg_info.body_fd = -1;
6551547Smax.romanov@nginx.com     }
6561547Smax.romanov@nginx.com 
6571547Smax.romanov@nginx.com     if (req_rpc_data->rpc_cancel) {
6581547Smax.romanov@nginx.com         req_rpc_data->rpc_cancel = 0;
6591547Smax.romanov@nginx.com 
6601547Smax.romanov@nginx.com         nxt_port_rpc_cancel(task, task->thread->engine->port,
6611547Smax.romanov@nginx.com                             req_rpc_data->stream);
6621547Smax.romanov@nginx.com     }
663343Smax.romanov@nginx.com }
664343Smax.romanov@nginx.com 
665343Smax.romanov@nginx.com 
6661552Smax.romanov@nginx.com static void
667141Smax.romanov@nginx.com nxt_router_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
668141Smax.romanov@nginx.com {
6691555Smax.romanov@nginx.com     nxt_int_t      res;
6701547Smax.romanov@nginx.com     nxt_app_t      *app;
6711547Smax.romanov@nginx.com     nxt_port_t     *port, *main_app_port;
6721547Smax.romanov@nginx.com     nxt_runtime_t  *rt;
6731547Smax.romanov@nginx.com 
674141Smax.romanov@nginx.com     nxt_port_new_port_handler(task, msg);
675141Smax.romanov@nginx.com 
6761547Smax.romanov@nginx.com     port = msg->u.new_port;
6771547Smax.romanov@nginx.com 
6781547Smax.romanov@nginx.com     if (port != NULL && port->type == NXT_PROCESS_CONTROLLER) {
679662Smax.romanov@nginx.com         nxt_router_greet_controller(task, msg->u.new_port);
680662Smax.romanov@nginx.com     }
681662Smax.romanov@nginx.com 
6821998St.nateldemoura@f5.com     if (port != NULL && port->type == NXT_PROCESS_PROTOTYPE)  {
6831998St.nateldemoura@f5.com         nxt_port_rpc_handler(task, msg);
6841998St.nateldemoura@f5.com 
6851998St.nateldemoura@f5.com         return;
6861998St.nateldemoura@f5.com     }
6871998St.nateldemoura@f5.com 
6881547Smax.romanov@nginx.com     if (port == NULL || port->type != NXT_PROCESS_APP) {
6891547Smax.romanov@nginx.com 
6901547Smax.romanov@nginx.com         if (msg->port_msg.stream == 0) {
6911547Smax.romanov@nginx.com             return;
6921547Smax.romanov@nginx.com         }
6931547Smax.romanov@nginx.com 
6941547Smax.romanov@nginx.com         msg->port_msg.type = _NXT_PORT_MSG_RPC_ERROR;
6951555Smax.romanov@nginx.com 
6961555Smax.romanov@nginx.com     } else {
6971558Smax.romanov@nginx.com         if (msg->fd[1] != -1) {
6981558Smax.romanov@nginx.com             res = nxt_router_port_queue_map(task, port, msg->fd[1]);
6991555Smax.romanov@nginx.com             if (nxt_slow_path(res != NXT_OK)) {
7001555Smax.romanov@nginx.com                 return;
7011555Smax.romanov@nginx.com             }
7021555Smax.romanov@nginx.com 
7031558Smax.romanov@nginx.com             nxt_fd_close(msg->fd[1]);
7041558Smax.romanov@nginx.com             msg->fd[1] = -1;
7051555Smax.romanov@nginx.com         }
7061547Smax.romanov@nginx.com     }
7071547Smax.romanov@nginx.com 
7081547Smax.romanov@nginx.com     if (msg->port_msg.stream != 0) {
7091547Smax.romanov@nginx.com         nxt_port_rpc_handler(task, msg);
710141Smax.romanov@nginx.com         return;
711141Smax.romanov@nginx.com     }
712141Smax.romanov@nginx.com 
7131998St.nateldemoura@f5.com     nxt_debug(task, "new port id %d (%d)", port->id, port->type);
7141998St.nateldemoura@f5.com 
7151547Smax.romanov@nginx.com     /*
7161547Smax.romanov@nginx.com      * Port with "id == 0" is application 'main' port and it always
7171547Smax.romanov@nginx.com      * should come with non-zero stream.
7181547Smax.romanov@nginx.com      */
7191547Smax.romanov@nginx.com     nxt_assert(port->id != 0);
7201547Smax.romanov@nginx.com 
7211547Smax.romanov@nginx.com     /* Find 'main' app port and get app reference. */
7221547Smax.romanov@nginx.com     rt = task->thread->runtime;
7231547Smax.romanov@nginx.com 
7241547Smax.romanov@nginx.com     /*
7251547Smax.romanov@nginx.com      * It is safe to access 'runtime->ports' hash because 'NEW_PORT'
7261547Smax.romanov@nginx.com      * sent to main port (with id == 0) and processed in main thread.
7271547Smax.romanov@nginx.com      */
7281547Smax.romanov@nginx.com     main_app_port = nxt_port_hash_find(&rt->ports, port->pid, 0);
7291547Smax.romanov@nginx.com     nxt_assert(main_app_port != NULL);
7301547Smax.romanov@nginx.com 
7311547Smax.romanov@nginx.com     app = main_app_port->app;
7321915Smax.romanov@nginx.com 
7331915Smax.romanov@nginx.com     if (nxt_fast_path(app != NULL)) {
7341915Smax.romanov@nginx.com         nxt_thread_mutex_lock(&app->mutex);
7351915Smax.romanov@nginx.com 
7361915Smax.romanov@nginx.com         /* TODO here should be find-and-add code because there can be
7371915Smax.romanov@nginx.com            port waiters in port_hash */
738