xref: /unit/src/nxt_router.c (revision 1936)
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     uint32_t          requests;
31318Smax.romanov@nginx.com     nxt_conf_value_t  *limits_value;
32507Smax.romanov@nginx.com     nxt_conf_value_t  *processes_value;
331473Svbart@nginx.com     nxt_conf_value_t  *targets_value;
34133Sigor@sysoev.ru } nxt_router_app_conf_t;
35133Sigor@sysoev.ru 
36133Sigor@sysoev.ru 
37133Sigor@sysoev.ru typedef struct {
38964Sigor@sysoev.ru     nxt_str_t         pass;
39964Sigor@sysoev.ru     nxt_str_t         application;
40115Sigor@sysoev.ru } nxt_router_listener_conf_t;
41115Sigor@sysoev.ru 
42115Sigor@sysoev.ru 
43774Svbart@nginx.com #if (NXT_TLS)
44774Svbart@nginx.com 
45774Svbart@nginx.com typedef struct {
461885Sa.suvorov@f5.com     nxt_str_t               name;
471885Sa.suvorov@f5.com     nxt_socket_conf_t       *socket_conf;
481885Sa.suvorov@f5.com     nxt_router_temp_conf_t  *temp_conf;
491920Sa.suvorov@f5.com     nxt_tls_init_t          *tls_init;
501885Sa.suvorov@f5.com     nxt_bool_t              last;
51774Svbart@nginx.com 
521920Sa.suvorov@f5.com     nxt_queue_link_t        link;  /* for nxt_socket_conf_t.tls */
53774Svbart@nginx.com } nxt_router_tlssock_t;
54774Svbart@nginx.com 
55774Svbart@nginx.com #endif
56774Svbart@nginx.com 
57774Svbart@nginx.com 
58198Sigor@sysoev.ru typedef struct {
591828Sa.suvorov@f5.com     nxt_str_t               *name;
60198Sigor@sysoev.ru     nxt_socket_conf_t       *socket_conf;
61198Sigor@sysoev.ru     nxt_router_temp_conf_t  *temp_conf;
621828Sa.suvorov@f5.com     nxt_bool_t              last;
63198Sigor@sysoev.ru } nxt_socket_rpc_t;
64198Sigor@sysoev.ru 
65198Sigor@sysoev.ru 
66507Smax.romanov@nginx.com typedef struct {
67507Smax.romanov@nginx.com     nxt_app_t               *app;
68507Smax.romanov@nginx.com     nxt_router_temp_conf_t  *temp_conf;
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;
751926Smax.romanov@nginx.com } nxt_app_joint_rpc_t;
761926Smax.romanov@nginx.com 
771926Smax.romanov@nginx.com 
781488St.nateldemoura@f5.com static nxt_int_t nxt_router_prefork(nxt_task_t *task, nxt_process_t *process,
791488St.nateldemoura@f5.com     nxt_mp_t *mp);
801488St.nateldemoura@f5.com static nxt_int_t nxt_router_start(nxt_task_t *task, nxt_process_data_t *data);
81662Smax.romanov@nginx.com static void nxt_router_greet_controller(nxt_task_t *task,
82662Smax.romanov@nginx.com     nxt_port_t *controller_port);
83662Smax.romanov@nginx.com 
84507Smax.romanov@nginx.com static nxt_int_t nxt_router_start_app_process(nxt_task_t *task, nxt_app_t *app);
85425Smax.romanov@nginx.com 
861552Smax.romanov@nginx.com static void nxt_router_new_port_handler(nxt_task_t *task,
871552Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
881552Smax.romanov@nginx.com static void nxt_router_conf_data_handler(nxt_task_t *task,
891552Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
901926Smax.romanov@nginx.com static void nxt_router_app_restart_handler(nxt_task_t *task,
911926Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
921552Smax.romanov@nginx.com static void nxt_router_remove_pid_handler(nxt_task_t *task,
931552Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
941552Smax.romanov@nginx.com static void nxt_router_access_log_reopen_handler(nxt_task_t *task,
951552Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
961552Smax.romanov@nginx.com 
97139Sigor@sysoev.ru static nxt_router_temp_conf_t *nxt_router_temp_conf(nxt_task_t *task);
98198Sigor@sysoev.ru static void nxt_router_conf_apply(nxt_task_t *task, void *obj, void *data);
99198Sigor@sysoev.ru static void nxt_router_conf_ready(nxt_task_t *task,
100139Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
101139Sigor@sysoev.ru static void nxt_router_conf_error(nxt_task_t *task,
102139Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
103139Sigor@sysoev.ru static void nxt_router_conf_send(nxt_task_t *task,
104193Smax.romanov@nginx.com     nxt_router_temp_conf_t *tmcf, nxt_port_msg_type_t type);
10553Sigor@sysoev.ru 
106115Sigor@sysoev.ru static nxt_int_t nxt_router_conf_create(nxt_task_t *task,
107115Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf, u_char *start, u_char *end);
1081183Svbart@nginx.com static nxt_int_t nxt_router_conf_process_static(nxt_task_t *task,
1091183Svbart@nginx.com     nxt_router_conf_t *rtcf, nxt_conf_value_t *conf);
110*1936So.canty@f5.com static nxt_int_t nxt_router_conf_process_client_ip(nxt_task_t *task,
111*1936So.canty@f5.com     nxt_router_temp_conf_t *tmcf, nxt_socket_conf_t *skcf,
112*1936So.canty@f5.com     nxt_conf_value_t *conf);
1131563Svbart@nginx.com 
114133Sigor@sysoev.ru static nxt_app_t *nxt_router_app_find(nxt_queue_t *queue, nxt_str_t *name);
1151563Svbart@nginx.com static nxt_int_t nxt_router_apps_hash_test(nxt_lvlhsh_query_t *lhq, void *data);
1161563Svbart@nginx.com static nxt_int_t nxt_router_apps_hash_add(nxt_router_conf_t *rtcf,
1171563Svbart@nginx.com     nxt_app_t *app);
1181563Svbart@nginx.com static nxt_app_t *nxt_router_apps_hash_get(nxt_router_conf_t *rtcf,
1191563Svbart@nginx.com     nxt_str_t *name);
1201563Svbart@nginx.com static void nxt_router_apps_hash_use(nxt_task_t *task, nxt_router_conf_t *rtcf,
1211563Svbart@nginx.com     int i);
1221563Svbart@nginx.com 
1231555Smax.romanov@nginx.com static nxt_int_t nxt_router_app_queue_init(nxt_task_t *task,
1241555Smax.romanov@nginx.com     nxt_port_t *port);
1251555Smax.romanov@nginx.com static nxt_int_t nxt_router_port_queue_init(nxt_task_t *task,
1261555Smax.romanov@nginx.com     nxt_port_t *port);
1271555Smax.romanov@nginx.com static nxt_int_t nxt_router_port_queue_map(nxt_task_t *task,
1281555Smax.romanov@nginx.com     nxt_port_t *port, nxt_fd_t fd);
129198Sigor@sysoev.ru static void nxt_router_listen_socket_rpc_create(nxt_task_t *task,
130198Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf, nxt_socket_conf_t *skcf);
131198Sigor@sysoev.ru static void nxt_router_listen_socket_ready(nxt_task_t *task,
132198Sigor@sysoev.ru     nxt_port_recv_msg_t *msg, void *data);
133198Sigor@sysoev.ru static void nxt_router_listen_socket_error(nxt_task_t *task,
134198Sigor@sysoev.ru     nxt_port_recv_msg_t *msg, void *data);
135774Svbart@nginx.com #if (NXT_TLS)
136774Svbart@nginx.com static void nxt_router_tls_rpc_handler(nxt_task_t *task,
137774Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
1381828Sa.suvorov@f5.com static nxt_int_t nxt_router_conf_tls_insert(nxt_router_temp_conf_t *tmcf,
1391920Sa.suvorov@f5.com     nxt_conf_value_t *value, nxt_socket_conf_t *skcf, nxt_tls_init_t *tls_init,
1401920Sa.suvorov@f5.com     nxt_bool_t last);
141774Svbart@nginx.com #endif
142507Smax.romanov@nginx.com static void nxt_router_app_rpc_create(nxt_task_t *task,
143507Smax.romanov@nginx.com     nxt_router_temp_conf_t *tmcf, nxt_app_t *app);
144507Smax.romanov@nginx.com static void nxt_router_app_prefork_ready(nxt_task_t *task,
145507Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, void *data);
146507Smax.romanov@nginx.com static void nxt_router_app_prefork_error(nxt_task_t *task,
147507Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, void *data);
148359Sigor@sysoev.ru static nxt_socket_conf_t *nxt_router_socket_conf(nxt_task_t *task,
149359Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf, nxt_str_t *name);
150359Sigor@sysoev.ru static nxt_int_t nxt_router_listen_socket_find(nxt_router_temp_conf_t *tmcf,
151359Sigor@sysoev.ru     nxt_socket_conf_t *nskcf, nxt_sockaddr_t *sa);
15253Sigor@sysoev.ru 
15353Sigor@sysoev.ru static nxt_int_t nxt_router_engines_create(nxt_task_t *task,
15453Sigor@sysoev.ru     nxt_router_t *router, nxt_router_temp_conf_t *tmcf,
15553Sigor@sysoev.ru     const nxt_event_interface_t *interface);
156115Sigor@sysoev.ru static nxt_int_t nxt_router_engine_conf_create(nxt_router_temp_conf_t *tmcf,
157115Sigor@sysoev.ru     nxt_router_engine_conf_t *recf);
158115Sigor@sysoev.ru static nxt_int_t nxt_router_engine_conf_update(nxt_router_temp_conf_t *tmcf,
159115Sigor@sysoev.ru     nxt_router_engine_conf_t *recf);
160115Sigor@sysoev.ru static nxt_int_t nxt_router_engine_conf_delete(nxt_router_temp_conf_t *tmcf,
161115Sigor@sysoev.ru     nxt_router_engine_conf_t *recf);
162154Sigor@sysoev.ru static nxt_int_t nxt_router_engine_joints_create(nxt_router_temp_conf_t *tmcf,
163154Sigor@sysoev.ru     nxt_router_engine_conf_t *recf, nxt_queue_t *sockets,
164154Sigor@sysoev.ru     nxt_work_handler_t handler);
165313Sigor@sysoev.ru static nxt_int_t nxt_router_engine_quit(nxt_router_temp_conf_t *tmcf,
166313Sigor@sysoev.ru     nxt_router_engine_conf_t *recf);
167139Sigor@sysoev.ru static nxt_int_t nxt_router_engine_joints_delete(nxt_router_temp_conf_t *tmcf,
168139Sigor@sysoev.ru     nxt_router_engine_conf_t *recf, nxt_queue_t *sockets);
16953Sigor@sysoev.ru 
17053Sigor@sysoev.ru static nxt_int_t nxt_router_threads_create(nxt_task_t *task, nxt_runtime_t *rt,
17153Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
17253Sigor@sysoev.ru static nxt_int_t nxt_router_thread_create(nxt_task_t *task, nxt_runtime_t *rt,
17353Sigor@sysoev.ru     nxt_event_engine_t *engine);
174343Smax.romanov@nginx.com static void nxt_router_apps_sort(nxt_task_t *task, nxt_router_t *router,
175133Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
17653Sigor@sysoev.ru 
177315Sigor@sysoev.ru static void nxt_router_engines_post(nxt_router_t *router,
178315Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
179315Sigor@sysoev.ru static void nxt_router_engine_post(nxt_event_engine_t *engine,
180315Sigor@sysoev.ru     nxt_work_t *jobs);
18153Sigor@sysoev.ru 
18253Sigor@sysoev.ru static void nxt_router_thread_start(void *data);
1831545Smax.romanov@nginx.com static void nxt_router_rt_add_port(nxt_task_t *task, void *obj,
1841545Smax.romanov@nginx.com     void *data);
18553Sigor@sysoev.ru static void nxt_router_listen_socket_create(nxt_task_t *task, void *obj,
18653Sigor@sysoev.ru     void *data);
18753Sigor@sysoev.ru static void nxt_router_listen_socket_update(nxt_task_t *task, void *obj,
18853Sigor@sysoev.ru     void *data);
18953Sigor@sysoev.ru static void nxt_router_listen_socket_delete(nxt_task_t *task, void *obj,
19053Sigor@sysoev.ru     void *data);
191313Sigor@sysoev.ru static void nxt_router_worker_thread_quit(nxt_task_t *task, void *obj,
192313Sigor@sysoev.ru     void *data);
19353Sigor@sysoev.ru static void nxt_router_listen_socket_close(nxt_task_t *task, void *obj,
19453Sigor@sysoev.ru     void *data);
19553Sigor@sysoev.ru static void nxt_router_thread_exit_handler(nxt_task_t *task, void *obj,
19653Sigor@sysoev.ru     void *data);
1971547Smax.romanov@nginx.com static void nxt_router_req_headers_ack_handler(nxt_task_t *task,
1981547Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, nxt_request_rpc_data_t *req_rpc_data);
199359Sigor@sysoev.ru static void nxt_router_listen_socket_release(nxt_task_t *task,
200359Sigor@sysoev.ru     nxt_socket_conf_t *skcf);
20153Sigor@sysoev.ru 
202630Svbart@nginx.com static void nxt_router_access_log_writer(nxt_task_t *task,
203630Svbart@nginx.com     nxt_http_request_t *r, nxt_router_access_log_t *access_log);
204630Svbart@nginx.com static u_char *nxt_router_access_log_date(u_char *buf, nxt_realtime_t *now,
205630Svbart@nginx.com     struct tm *tm, size_t size, const char *format);
206630Svbart@nginx.com static void nxt_router_access_log_open(nxt_task_t *task,
207630Svbart@nginx.com     nxt_router_temp_conf_t *tmcf);
208630Svbart@nginx.com static void nxt_router_access_log_ready(nxt_task_t *task,
209630Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
210630Svbart@nginx.com static void nxt_router_access_log_error(nxt_task_t *task,
211630Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
212630Svbart@nginx.com static void nxt_router_access_log_release(nxt_task_t *task,
213630Svbart@nginx.com     nxt_thread_spinlock_t *lock, nxt_router_access_log_t *access_log);
214651Svbart@nginx.com static void nxt_router_access_log_reopen_completion(nxt_task_t *task, void *obj,
215651Svbart@nginx.com     void *data);
216631Svbart@nginx.com static void nxt_router_access_log_reopen_ready(nxt_task_t *task,
217631Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
218631Svbart@nginx.com static void nxt_router_access_log_reopen_error(nxt_task_t *task,
219631Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
220630Svbart@nginx.com 
221343Smax.romanov@nginx.com static void nxt_router_app_port_ready(nxt_task_t *task,
222343Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, void *data);
2231547Smax.romanov@nginx.com static nxt_int_t nxt_router_app_shared_port_send(nxt_task_t *task,
2241547Smax.romanov@nginx.com     nxt_port_t *app_port);
225343Smax.romanov@nginx.com static void nxt_router_app_port_error(nxt_task_t *task,
226343Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, void *data);
227343Smax.romanov@nginx.com 
2281563Svbart@nginx.com static void nxt_router_app_use(nxt_task_t *task, nxt_app_t *app, int i);
229753Smax.romanov@nginx.com static void nxt_router_app_unlink(nxt_task_t *task, nxt_app_t *app);
2301123Smax.romanov@nginx.com 
231343Smax.romanov@nginx.com static void nxt_router_app_port_release(nxt_task_t *task, nxt_port_t *port,
2321123Smax.romanov@nginx.com     nxt_apr_action_t action);
2331547Smax.romanov@nginx.com static void nxt_router_app_port_get(nxt_task_t *task, nxt_app_t *app,
2341547Smax.romanov@nginx.com     nxt_request_rpc_data_t *req_rpc_data);
2351561Smax.romanov@nginx.com static void nxt_router_http_request_error(nxt_task_t *task, void *obj,
2361561Smax.romanov@nginx.com     void *data);
2371547Smax.romanov@nginx.com static void nxt_router_http_request_done(nxt_task_t *task, void *obj,
2381547Smax.romanov@nginx.com     void *data);
239141Smax.romanov@nginx.com 
2401566Smax.romanov@nginx.com static void nxt_router_dummy_buf_completion(nxt_task_t *task, void *obj,
2411566Smax.romanov@nginx.com     void *data);
242425Smax.romanov@nginx.com static void nxt_router_app_prepare_request(nxt_task_t *task,
2431547Smax.romanov@nginx.com     nxt_request_rpc_data_t *req_rpc_data);
2441007Salexander.borisov@nginx.com static nxt_buf_t *nxt_router_prepare_msg(nxt_task_t *task,
2451547Smax.romanov@nginx.com     nxt_http_request_t *r, nxt_app_t *app, const nxt_str_t *prefix);
246510Salexander.borisov@nginx.com 
247318Smax.romanov@nginx.com static void nxt_router_app_timeout(nxt_task_t *task, void *obj, void *data);
248507Smax.romanov@nginx.com static void nxt_router_adjust_idle_timer(nxt_task_t *task, void *obj,
249507Smax.romanov@nginx.com     void *data);
250507Smax.romanov@nginx.com static void nxt_router_app_idle_timeout(nxt_task_t *task, void *obj,
251507Smax.romanov@nginx.com     void *data);
252753Smax.romanov@nginx.com static void nxt_router_app_joint_release_handler(nxt_task_t *task, void *obj,
253507Smax.romanov@nginx.com     void *data);
254753Smax.romanov@nginx.com static void nxt_router_free_app(nxt_task_t *task, void *obj, void *data);
255431Sigor@sysoev.ru 
256431Sigor@sysoev.ru static const nxt_http_request_state_t  nxt_http_request_send_state;
257431Sigor@sysoev.ru static void nxt_http_request_send_body(nxt_task_t *task, void *obj, void *data);
258141Smax.romanov@nginx.com 
259753Smax.romanov@nginx.com static void nxt_router_app_joint_use(nxt_task_t *task,
260753Smax.romanov@nginx.com     nxt_app_joint_t *app_joint, int i);
261753Smax.romanov@nginx.com 
2621547Smax.romanov@nginx.com static void nxt_router_http_request_release_post(nxt_task_t *task,
2631007Salexander.borisov@nginx.com     nxt_http_request_t *r);
2641007Salexander.borisov@nginx.com static void nxt_router_http_request_release(nxt_task_t *task, void *obj,
2651007Salexander.borisov@nginx.com     void *data);
2661321Smax.romanov@nginx.com static void nxt_router_oosm_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
2671545Smax.romanov@nginx.com static void nxt_router_get_port_handler(nxt_task_t *task,
2681545Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
2691546Smax.romanov@nginx.com static void nxt_router_get_mmap_handler(nxt_task_t *task,
2701546Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
2711007Salexander.borisov@nginx.com 
2721149Smax.romanov@nginx.com extern const nxt_http_request_state_t  nxt_http_websocket;
2731131Smax.romanov@nginx.com 
274119Smax.romanov@nginx.com static nxt_router_t  *nxt_router;
27520Sigor@sysoev.ru 
276743Smax.romanov@nginx.com static const nxt_str_t http_prefix = nxt_string("HTTP_");
277743Smax.romanov@nginx.com static const nxt_str_t empty_prefix = nxt_string("");
278743Smax.romanov@nginx.com 
279743Smax.romanov@nginx.com static const nxt_str_t  *nxt_app_msg_prefix[] = {
280804Svbart@nginx.com     &empty_prefix,
2811594Smax.romanov@nginx.com     &empty_prefix,
282743Smax.romanov@nginx.com     &http_prefix,
283743Smax.romanov@nginx.com     &http_prefix,
284743Smax.romanov@nginx.com     &http_prefix,
285977Smax.romanov@gmail.com     &empty_prefix,
286216Sigor@sysoev.ru };
287216Sigor@sysoev.ru 
288216Sigor@sysoev.ru 
2891488St.nateldemoura@f5.com static const nxt_port_handlers_t  nxt_router_process_port_handlers = {
2901488St.nateldemoura@f5.com     .quit         = nxt_signal_quit_handler,
291662Smax.romanov@nginx.com     .new_port     = nxt_router_new_port_handler,
2921545Smax.romanov@nginx.com     .get_port     = nxt_router_get_port_handler,
293662Smax.romanov@nginx.com     .change_file  = nxt_port_change_log_file_handler,
294662Smax.romanov@nginx.com     .mmap         = nxt_port_mmap_handler,
2951546Smax.romanov@nginx.com     .get_mmap     = nxt_router_get_mmap_handler,
296662Smax.romanov@nginx.com     .data         = nxt_router_conf_data_handler,
2971926Smax.romanov@nginx.com     .app_restart  = nxt_router_app_restart_handler,
298662Smax.romanov@nginx.com     .remove_pid   = nxt_router_remove_pid_handler,
299662Smax.romanov@nginx.com     .access_log   = nxt_router_access_log_reopen_handler,
300662Smax.romanov@nginx.com     .rpc_ready    = nxt_port_rpc_handler,
301662Smax.romanov@nginx.com     .rpc_error    = nxt_port_rpc_handler,
3021321Smax.romanov@nginx.com     .oosm         = nxt_router_oosm_handler,
303662Smax.romanov@nginx.com };
304662Smax.romanov@nginx.com 
305662Smax.romanov@nginx.com 
3061488St.nateldemoura@f5.com const nxt_process_init_t  nxt_router_process = {
3071488St.nateldemoura@f5.com     .name           = "router",
3081488St.nateldemoura@f5.com     .type           = NXT_PROCESS_ROUTER,
3091488St.nateldemoura@f5.com     .prefork        = nxt_router_prefork,
3101488St.nateldemoura@f5.com     .restart        = 1,
3111488St.nateldemoura@f5.com     .setup          = nxt_process_core_setup,
3121488St.nateldemoura@f5.com     .start          = nxt_router_start,
3131488St.nateldemoura@f5.com     .port_handlers  = &nxt_router_process_port_handlers,
3141488St.nateldemoura@f5.com     .signals        = nxt_process_signals,
3151488St.nateldemoura@f5.com };
3161488St.nateldemoura@f5.com 
3171488St.nateldemoura@f5.com 
3181509Sigor@sysoev.ru /* Queues of nxt_socket_conf_t */
3191509Sigor@sysoev.ru nxt_queue_t  creating_sockets;
3201509Sigor@sysoev.ru nxt_queue_t  pending_sockets;
3211509Sigor@sysoev.ru nxt_queue_t  updating_sockets;
3221509Sigor@sysoev.ru nxt_queue_t  keeping_sockets;
3231509Sigor@sysoev.ru nxt_queue_t  deleting_sockets;
3241509Sigor@sysoev.ru 
3251509Sigor@sysoev.ru 
3261488St.nateldemoura@f5.com static nxt_int_t
3271488St.nateldemoura@f5.com nxt_router_prefork(nxt_task_t *task, nxt_process_t *process, nxt_mp_t *mp)
3281488St.nateldemoura@f5.com {
3291488St.nateldemoura@f5.com     nxt_runtime_stop_app_processes(task, task->thread->runtime);
3301488St.nateldemoura@f5.com 
3311488St.nateldemoura@f5.com     return NXT_OK;
3321488St.nateldemoura@f5.com }
3331488St.nateldemoura@f5.com 
3341488St.nateldemoura@f5.com 
3351488St.nateldemoura@f5.com static nxt_int_t
3361488St.nateldemoura@f5.com nxt_router_start(nxt_task_t *task, nxt_process_data_t *data)
33720Sigor@sysoev.ru {
338141Smax.romanov@nginx.com     nxt_int_t      ret;
339662Smax.romanov@nginx.com     nxt_port_t     *controller_port;
340141Smax.romanov@nginx.com     nxt_router_t   *router;
341141Smax.romanov@nginx.com     nxt_runtime_t  *rt;
342141Smax.romanov@nginx.com 
343141Smax.romanov@nginx.com     rt = task->thread->runtime;
34453Sigor@sysoev.ru 
3451488St.nateldemoura@f5.com     nxt_log(task, NXT_LOG_INFO, "router started");
3461488St.nateldemoura@f5.com 
347771Sigor@sysoev.ru #if (NXT_TLS)
348771Sigor@sysoev.ru     rt->tls = nxt_service_get(rt->services, "SSL/TLS", "OpenSSL");
349771Sigor@sysoev.ru     if (nxt_slow_path(rt->tls == NULL)) {
350771Sigor@sysoev.ru         return NXT_ERROR;
351771Sigor@sysoev.ru     }
352771Sigor@sysoev.ru 
353771Sigor@sysoev.ru     ret = rt->tls->library_init(task);
354771Sigor@sysoev.ru     if (nxt_slow_path(ret != NXT_OK)) {
355771Sigor@sysoev.ru         return ret;
356771Sigor@sysoev.ru     }
357771Sigor@sysoev.ru #endif
358771Sigor@sysoev.ru 
3591459Smax.romanov@nginx.com     ret = nxt_http_init(task);
36088Smax.romanov@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
36188Smax.romanov@nginx.com         return ret;
36288Smax.romanov@nginx.com     }
36388Smax.romanov@nginx.com 
36453Sigor@sysoev.ru     router = nxt_zalloc(sizeof(nxt_router_t));
36553Sigor@sysoev.ru     if (nxt_slow_path(router == NULL)) {
36653Sigor@sysoev.ru         return NXT_ERROR;
36753Sigor@sysoev.ru     }
36853Sigor@sysoev.ru 
36953Sigor@sysoev.ru     nxt_queue_init(&router->engines);
37053Sigor@sysoev.ru     nxt_queue_init(&router->sockets);
371133Sigor@sysoev.ru     nxt_queue_init(&router->apps);
37253Sigor@sysoev.ru 
373119Smax.romanov@nginx.com     nxt_router = router;
374119Smax.romanov@nginx.com 
375662Smax.romanov@nginx.com     controller_port = rt->port_by_type[NXT_PROCESS_CONTROLLER];
376662Smax.romanov@nginx.com     if (controller_port != NULL) {
377662Smax.romanov@nginx.com         nxt_router_greet_controller(task, controller_port);
378662Smax.romanov@nginx.com     }
379662Smax.romanov@nginx.com 
380115Sigor@sysoev.ru     return NXT_OK;
381115Sigor@sysoev.ru }
382115Sigor@sysoev.ru 
383115Sigor@sysoev.ru 
384343Smax.romanov@nginx.com static void
385662Smax.romanov@nginx.com nxt_router_greet_controller(nxt_task_t *task, nxt_port_t *controller_port)
386662Smax.romanov@nginx.com {
387662Smax.romanov@nginx.com     nxt_port_socket_write(task, controller_port, NXT_PORT_MSG_PROCESS_READY,
388662Smax.romanov@nginx.com                           -1, 0, 0, NULL);
389662Smax.romanov@nginx.com }
390662Smax.romanov@nginx.com 
391662Smax.romanov@nginx.com 
392662Smax.romanov@nginx.com static void
393507Smax.romanov@nginx.com nxt_router_start_app_process_handler(nxt_task_t *task, nxt_port_t *port,
394507Smax.romanov@nginx.com     void *data)
395167Smax.romanov@nginx.com {
3961926Smax.romanov@nginx.com     size_t               size;
3971926Smax.romanov@nginx.com     uint32_t             stream;
3981926Smax.romanov@nginx.com     nxt_mp_t             *mp;
3991926Smax.romanov@nginx.com     nxt_int_t            ret;
4001926Smax.romanov@nginx.com     nxt_app_t            *app;
4011926Smax.romanov@nginx.com     nxt_buf_t            *b;
4021926Smax.romanov@nginx.com     nxt_port_t           *main_port;
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 
408167Smax.romanov@nginx.com     rt = task->thread->runtime;
409240Sigor@sysoev.ru     main_port = rt->port_by_type[NXT_PROCESS_MAIN];
410167Smax.romanov@nginx.com 
411507Smax.romanov@nginx.com     nxt_debug(task, "app '%V' %p start process", &app->name, app);
412343Smax.romanov@nginx.com 
413343Smax.romanov@nginx.com     size = app->name.length + 1 + app->conf.length;
414343Smax.romanov@nginx.com 
415343Smax.romanov@nginx.com     b = nxt_buf_mem_ts_alloc(task, task->thread->engine->mem_pool, size);
416343Smax.romanov@nginx.com 
417343Smax.romanov@nginx.com     if (nxt_slow_path(b == NULL)) {
418343Smax.romanov@nginx.com         goto failed;
419167Smax.romanov@nginx.com     }
420167Smax.romanov@nginx.com 
421343Smax.romanov@nginx.com     nxt_buf_cpystr(b, &app->name);
422343Smax.romanov@nginx.com     *b->mem.free++ = '\0';
423343Smax.romanov@nginx.com     nxt_buf_cpystr(b, &app->conf);
424343Smax.romanov@nginx.com 
4251926Smax.romanov@nginx.com     app_joint_rpc = nxt_port_rpc_register_handler_ex(task, port,
4261926Smax.romanov@nginx.com                                                      nxt_router_app_port_ready,
4271926Smax.romanov@nginx.com                                                      nxt_router_app_port_error,
4281926Smax.romanov@nginx.com                                                    sizeof(nxt_app_joint_rpc_t));
4291926Smax.romanov@nginx.com     if (nxt_slow_path(app_joint_rpc == NULL)) {
430343Smax.romanov@nginx.com         goto failed;
431343Smax.romanov@nginx.com     }
432343Smax.romanov@nginx.com 
4331926Smax.romanov@nginx.com     stream = nxt_port_rpc_ex_stream(app_joint_rpc);
4341926Smax.romanov@nginx.com 
4351488St.nateldemoura@f5.com     ret = nxt_port_socket_write(task, main_port, NXT_PORT_MSG_START_PROCESS,
4361488St.nateldemoura@f5.com                                 -1, stream, port->id, b);
437648Svbart@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
438648Svbart@nginx.com         nxt_port_rpc_cancel(task, port, stream);
439753Smax.romanov@nginx.com 
440648Svbart@nginx.com         goto failed;
441648Svbart@nginx.com     }
442343Smax.romanov@nginx.com 
4431926Smax.romanov@nginx.com     app_joint_rpc->app_joint = app->joint;
4441926Smax.romanov@nginx.com     app_joint_rpc->generation = app->generation;
4451926Smax.romanov@nginx.com 
4461926Smax.romanov@nginx.com     nxt_router_app_joint_use(task, app->joint, 1);
4471926Smax.romanov@nginx.com 
448753Smax.romanov@nginx.com     nxt_router_app_use(task, app, -1);
449753Smax.romanov@nginx.com 
450343Smax.romanov@nginx.com     return;
451343Smax.romanov@nginx.com 
452343Smax.romanov@nginx.com failed:
453343Smax.romanov@nginx.com 
454648Svbart@nginx.com     if (b != NULL) {
455648Svbart@nginx.com         mp = b->data;
456648Svbart@nginx.com         nxt_mp_free(mp, b);
457648Svbart@nginx.com         nxt_mp_release(mp);
458648Svbart@nginx.com     }
459648Svbart@nginx.com 
460343Smax.romanov@nginx.com     nxt_thread_mutex_lock(&app->mutex);
461343Smax.romanov@nginx.com 
462507Smax.romanov@nginx.com     app->pending_processes--;
463343Smax.romanov@nginx.com 
464343Smax.romanov@nginx.com     nxt_thread_mutex_unlock(&app->mutex);
465343Smax.romanov@nginx.com 
466343Smax.romanov@nginx.com     nxt_router_app_use(task, app, -1);
467167Smax.romanov@nginx.com }
468167Smax.romanov@nginx.com 
469167Smax.romanov@nginx.com 
470753Smax.romanov@nginx.com static void
471753Smax.romanov@nginx.com nxt_router_app_joint_use(nxt_task_t *task, nxt_app_joint_t *app_joint, int i)
472753Smax.romanov@nginx.com {
473753Smax.romanov@nginx.com     app_joint->use_count += i;
474753Smax.romanov@nginx.com 
475753Smax.romanov@nginx.com     if (app_joint->use_count == 0) {
476753Smax.romanov@nginx.com         nxt_assert(app_joint->app == NULL);
477753Smax.romanov@nginx.com 
478753Smax.romanov@nginx.com         nxt_free(app_joint);
479753Smax.romanov@nginx.com     }
480753Smax.romanov@nginx.com }
481753Smax.romanov@nginx.com 
482753Smax.romanov@nginx.com 
483343Smax.romanov@nginx.com static nxt_int_t
484507Smax.romanov@nginx.com nxt_router_start_app_process(nxt_task_t *task, nxt_app_t *app)
485141Smax.romanov@nginx.com {
486343Smax.romanov@nginx.com     nxt_int_t      res;
487343Smax.romanov@nginx.com     nxt_port_t     *router_port;
488343Smax.romanov@nginx.com     nxt_runtime_t  *rt;
489343Smax.romanov@nginx.com 
4901549Smax.romanov@nginx.com     nxt_debug(task, "app '%V' start process", &app->name);
4911549Smax.romanov@nginx.com 
492343Smax.romanov@nginx.com     rt = task->thread->runtime;
493343Smax.romanov@nginx.com     router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
494343Smax.romanov@nginx.com 
495343Smax.romanov@nginx.com     nxt_router_app_use(task, app, 1);
496343Smax.romanov@nginx.com 
497507Smax.romanov@nginx.com     res = nxt_port_post(task, router_port, nxt_router_start_app_process_handler,
498343Smax.romanov@nginx.com                         app);
499343Smax.romanov@nginx.com 
500343Smax.romanov@nginx.com     if (res == NXT_OK) {
501343Smax.romanov@nginx.com         return res;
502318Smax.romanov@nginx.com     }
503318Smax.romanov@nginx.com 
504343Smax.romanov@nginx.com     nxt_thread_mutex_lock(&app->mutex);
505343Smax.romanov@nginx.com 
506507Smax.romanov@nginx.com     app->pending_processes--;
507343Smax.romanov@nginx.com 
508343Smax.romanov@nginx.com     nxt_thread_mutex_unlock(&app->mutex);
509343Smax.romanov@nginx.com 
510343Smax.romanov@nginx.com     nxt_router_app_use(task, app, -1);
511343Smax.romanov@nginx.com 
512343Smax.romanov@nginx.com     return NXT_ERROR;
513318Smax.romanov@nginx.com }
514318Smax.romanov@nginx.com 
515318Smax.romanov@nginx.com 
516423Smax.romanov@nginx.com nxt_inline nxt_bool_t
5171555Smax.romanov@nginx.com nxt_router_msg_cancel(nxt_task_t *task, nxt_request_rpc_data_t *req_rpc_data)
518423Smax.romanov@nginx.com {
5191555Smax.romanov@nginx.com     nxt_buf_t       *b, *next;
5201555Smax.romanov@nginx.com     nxt_bool_t      cancelled;
5211926Smax.romanov@nginx.com     nxt_port_t      *app_port;
5221555Smax.romanov@nginx.com     nxt_msg_info_t  *msg_info;
5231555Smax.romanov@nginx.com 
5241555Smax.romanov@nginx.com     msg_info = &req_rpc_data->msg_info;
525423Smax.romanov@nginx.com 
526423Smax.romanov@nginx.com     if (msg_info->buf == NULL) {
527423Smax.romanov@nginx.com         return 0;
528423Smax.romanov@nginx.com     }
529423Smax.romanov@nginx.com 
5301926Smax.romanov@nginx.com     app_port = req_rpc_data->app_port;
5311926Smax.romanov@nginx.com 
5321926Smax.romanov@nginx.com     if (app_port != NULL && app_port->id == NXT_SHARED_PORT_ID) {
5331926Smax.romanov@nginx.com         cancelled = nxt_app_queue_cancel(app_port->queue,
5341926Smax.romanov@nginx.com                                          msg_info->tracking_cookie,
5351926Smax.romanov@nginx.com                                          req_rpc_data->stream);
5361926Smax.romanov@nginx.com 
5371926Smax.romanov@nginx.com         if (cancelled) {
5381926Smax.romanov@nginx.com             nxt_debug(task, "stream #%uD: cancelled by router",
5391926Smax.romanov@nginx.com                       req_rpc_data->stream);
5401926Smax.romanov@nginx.com         }
5411926Smax.romanov@nginx.com 
5421926Smax.romanov@nginx.com     } else {
5431926Smax.romanov@nginx.com         cancelled = 0;
544423Smax.romanov@nginx.com     }
545423Smax.romanov@nginx.com 
546423Smax.romanov@nginx.com     for (b = msg_info->buf; b != NULL; b = next) {
547423Smax.romanov@nginx.com         next = b->next;
5481269Sigor@sysoev.ru         b->next = NULL;
549423Smax.romanov@nginx.com 
550423Smax.romanov@nginx.com         if (b->is_port_mmap_sent) {
551423Smax.romanov@nginx.com             b->is_port_mmap_sent = cancelled == 0;
552423Smax.romanov@nginx.com         }
5531829Smax.romanov@nginx.com 
5541829Smax.romanov@nginx.com         b->completion_handler(task, b, b->parent);
555423Smax.romanov@nginx.com     }
556423Smax.romanov@nginx.com 
557423Smax.romanov@nginx.com     msg_info->buf = NULL;
558423Smax.romanov@nginx.com 
559423Smax.romanov@nginx.com     return cancelled;
560423Smax.romanov@nginx.com }
561423Smax.romanov@nginx.com 
562423Smax.romanov@nginx.com 
563425Smax.romanov@nginx.com nxt_inline nxt_bool_t
564425Smax.romanov@nginx.com nxt_queue_chk_remove(nxt_queue_link_t *lnk)
565425Smax.romanov@nginx.com {
566425Smax.romanov@nginx.com     if (lnk->next != NULL) {
567425Smax.romanov@nginx.com         nxt_queue_remove(lnk);
568425Smax.romanov@nginx.com 
569425Smax.romanov@nginx.com         lnk->next = NULL;
570425Smax.romanov@nginx.com 
571425Smax.romanov@nginx.com         return 1;
572425Smax.romanov@nginx.com     }
573425Smax.romanov@nginx.com 
574425Smax.romanov@nginx.com     return 0;
575425Smax.romanov@nginx.com }
576425Smax.romanov@nginx.com 
577425Smax.romanov@nginx.com 
578343Smax.romanov@nginx.com nxt_inline void
5791123Smax.romanov@nginx.com nxt_request_rpc_data_unlink(nxt_task_t *task,
5801123Smax.romanov@nginx.com     nxt_request_rpc_data_t *req_rpc_data)
581343Smax.romanov@nginx.com {
5821561Smax.romanov@nginx.com     nxt_app_t           *app;
5831561Smax.romanov@nginx.com     nxt_bool_t          unlinked;
5841547Smax.romanov@nginx.com     nxt_http_request_t  *r;
5851547Smax.romanov@nginx.com 
5861555Smax.romanov@nginx.com     nxt_router_msg_cancel(task, req_rpc_data);
5871123Smax.romanov@nginx.com 
5881123Smax.romanov@nginx.com     if (req_rpc_data->app_port != NULL) {
5891123Smax.romanov@nginx.com         nxt_router_app_port_release(task, req_rpc_data->app_port,
5901123Smax.romanov@nginx.com                                     req_rpc_data->apr_action);
5911123Smax.romanov@nginx.com 
5921123Smax.romanov@nginx.com         req_rpc_data->app_port = NULL;
5931123Smax.romanov@nginx.com     }
5941123Smax.romanov@nginx.com 
5951561Smax.romanov@nginx.com     app = req_rpc_data->app;
5961547Smax.romanov@nginx.com     r = req_rpc_data->request;
5971547Smax.romanov@nginx.com 
5981547Smax.romanov@nginx.com     if (r != NULL) {
5991547Smax.romanov@nginx.com         r->timer_data = NULL;
6001547Smax.romanov@nginx.com 
6011547Smax.romanov@nginx.com         nxt_router_http_request_release_post(task, r);
6021547Smax.romanov@nginx.com 
6031547Smax.romanov@nginx.com         r->req_rpc_data = NULL;
6041123Smax.romanov@nginx.com         req_rpc_data->request = NULL;
6051561Smax.romanov@nginx.com 
6061561Smax.romanov@nginx.com         if (app != NULL) {
6071561Smax.romanov@nginx.com             unlinked = 0;
6081561Smax.romanov@nginx.com 
6091561Smax.romanov@nginx.com             nxt_thread_mutex_lock(&app->mutex);
6101561Smax.romanov@nginx.com 
6111561Smax.romanov@nginx.com             if (r->app_link.next != NULL) {
6121561Smax.romanov@nginx.com                 nxt_queue_remove(&r->app_link);
6131561Smax.romanov@nginx.com                 r->app_link.next = NULL;
6141561Smax.romanov@nginx.com 
6151561Smax.romanov@nginx.com                 unlinked = 1;
6161561Smax.romanov@nginx.com             }
6171561Smax.romanov@nginx.com 
6181561Smax.romanov@nginx.com             nxt_thread_mutex_unlock(&app->mutex);
6191561Smax.romanov@nginx.com 
6201561Smax.romanov@nginx.com             if (unlinked) {
6211561Smax.romanov@nginx.com                 nxt_mp_release(r->mem_pool);
6221561Smax.romanov@nginx.com             }
6231561Smax.romanov@nginx.com         }
6241561Smax.romanov@nginx.com     }
6251561Smax.romanov@nginx.com 
6261561Smax.romanov@nginx.com     if (app != NULL) {
6271561Smax.romanov@nginx.com         nxt_router_app_use(task, app, -1);
6281561Smax.romanov@nginx.com 
6291561Smax.romanov@nginx.com         req_rpc_data->app = NULL;
630346Smax.romanov@nginx.com     }
6311547Smax.romanov@nginx.com 
6321547Smax.romanov@nginx.com     if (req_rpc_data->msg_info.body_fd != -1) {
6331547Smax.romanov@nginx.com         nxt_fd_close(req_rpc_data->msg_info.body_fd);
6341547Smax.romanov@nginx.com 
6351547Smax.romanov@nginx.com         req_rpc_data->msg_info.body_fd = -1;
6361547Smax.romanov@nginx.com     }
6371547Smax.romanov@nginx.com 
6381547Smax.romanov@nginx.com     if (req_rpc_data->rpc_cancel) {
6391547Smax.romanov@nginx.com         req_rpc_data->rpc_cancel = 0;
6401547Smax.romanov@nginx.com 
6411547Smax.romanov@nginx.com         nxt_port_rpc_cancel(task, task->thread->engine->port,
6421547Smax.romanov@nginx.com                             req_rpc_data->stream);
6431547Smax.romanov@nginx.com     }
644343Smax.romanov@nginx.com }
645343Smax.romanov@nginx.com 
646343Smax.romanov@nginx.com 
6471552Smax.romanov@nginx.com static void
648141Smax.romanov@nginx.com nxt_router_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
649141Smax.romanov@nginx.com {
6501555Smax.romanov@nginx.com     nxt_int_t      res;
6511547Smax.romanov@nginx.com     nxt_app_t      *app;
6521547Smax.romanov@nginx.com     nxt_port_t     *port, *main_app_port;
6531547Smax.romanov@nginx.com     nxt_runtime_t  *rt;
6541547Smax.romanov@nginx.com 
655141Smax.romanov@nginx.com     nxt_port_new_port_handler(task, msg);
656141Smax.romanov@nginx.com 
6571547Smax.romanov@nginx.com     port = msg->u.new_port;
6581547Smax.romanov@nginx.com 
6591547Smax.romanov@nginx.com     if (port != NULL && port->type == NXT_PROCESS_CONTROLLER) {
660662Smax.romanov@nginx.com         nxt_router_greet_controller(task, msg->u.new_port);
661662Smax.romanov@nginx.com     }
662662Smax.romanov@nginx.com 
6631547Smax.romanov@nginx.com     if (port == NULL || port->type != NXT_PROCESS_APP) {
6641547Smax.romanov@nginx.com 
6651547Smax.romanov@nginx.com         if (msg->port_msg.stream == 0) {
6661547Smax.romanov@nginx.com             return;
6671547Smax.romanov@nginx.com         }
6681547Smax.romanov@nginx.com 
6691547Smax.romanov@nginx.com         msg->port_msg.type = _NXT_PORT_MSG_RPC_ERROR;
6701555Smax.romanov@nginx.com 
6711555Smax.romanov@nginx.com     } else {
6721558Smax.romanov@nginx.com         if (msg->fd[1] != -1) {
6731558Smax.romanov@nginx.com             res = nxt_router_port_queue_map(task, port, msg->fd[1]);
6741555Smax.romanov@nginx.com             if (nxt_slow_path(res != NXT_OK)) {
6751555Smax.romanov@nginx.com                 return;
6761555Smax.romanov@nginx.com             }
6771555Smax.romanov@nginx.com 
6781558Smax.romanov@nginx.com             nxt_fd_close(msg->fd[1]);
6791558Smax.romanov@nginx.com             msg->fd[1] = -1;
6801555Smax.romanov@nginx.com         }
6811547Smax.romanov@nginx.com     }
6821547Smax.romanov@nginx.com 
6831547Smax.romanov@nginx.com     if (msg->port_msg.stream != 0) {
6841547Smax.romanov@nginx.com         nxt_port_rpc_handler(task, msg);
685141Smax.romanov@nginx.com         return;
686141Smax.romanov@nginx.com     }
687141Smax.romanov@nginx.com 
6881547Smax.romanov@nginx.com     /*
6891547Smax.romanov@nginx.com      * Port with "id == 0" is application 'main' port and it always
6901547Smax.romanov@nginx.com      * should come with non-zero stream.
6911547Smax.romanov@nginx.com      */
6921547Smax.romanov@nginx.com     nxt_assert(port->id != 0);
6931547Smax.romanov@nginx.com 
6941547Smax.romanov@nginx.com     /* Find 'main' app port and get app reference. */
6951547Smax.romanov@nginx.com     rt = task->thread->runtime;
6961547Smax.romanov@nginx.com 
6971547Smax.romanov@nginx.com     /*
6981547Smax.romanov@nginx.com      * It is safe to access 'runtime->ports' hash because 'NEW_PORT'
6991547Smax.romanov@nginx.com      * sent to main port (with id == 0) and processed in main thread.
7001547Smax.romanov@nginx.com      */
7011547Smax.romanov@nginx.com     main_app_port = nxt_port_hash_find(&rt->ports, port->pid, 0);
7021547Smax.romanov@nginx.com     nxt_assert(main_app_port != NULL);
7031547Smax.romanov@nginx.com 
7041547Smax.romanov@nginx.com     app = main_app_port->app;
7051915Smax.romanov@nginx.com 
7061915Smax.romanov@nginx.com     if (nxt_fast_path(app != NULL)) {
7071915Smax.romanov@nginx.com         nxt_thread_mutex_lock(&app->mutex);
7081915Smax.romanov@nginx.com 
7091915Smax.romanov@nginx.com         /* TODO here should be find-and-add code because there can be
7101915Smax.romanov@nginx.com            port waiters in port_hash */
7111915Smax.romanov@nginx.com         nxt_port_hash_add(&app->port_hash, port);
7121915Smax.romanov@nginx.com         app->port_hash_count++;
7131915Smax.romanov@nginx.com 
7141915Smax.romanov@nginx.com         nxt_thread_mutex_unlock(&app->mutex);
715