xref: /unit/src/nxt_router.c (revision 1926)
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 
21*1926Smax.romanov@nginx.com #define NXT_SHARED_PORT_ID  0xFFFFu
22*1926Smax.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 
72*1926Smax.romanov@nginx.com typedef struct {
73*1926Smax.romanov@nginx.com     nxt_app_joint_t         *app_joint;
74*1926Smax.romanov@nginx.com     uint32_t                generation;
75*1926Smax.romanov@nginx.com } nxt_app_joint_rpc_t;
76*1926Smax.romanov@nginx.com 
77*1926Smax.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);
90*1926Smax.romanov@nginx.com static void nxt_router_app_restart_handler(nxt_task_t *task,
91*1926Smax.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);
1101563Svbart@nginx.com 
111133Sigor@sysoev.ru static nxt_app_t *nxt_router_app_find(nxt_queue_t *queue, nxt_str_t *name);
1121563Svbart@nginx.com static nxt_int_t nxt_router_apps_hash_test(nxt_lvlhsh_query_t *lhq, void *data);
1131563Svbart@nginx.com static nxt_int_t nxt_router_apps_hash_add(nxt_router_conf_t *rtcf,
1141563Svbart@nginx.com     nxt_app_t *app);
1151563Svbart@nginx.com static nxt_app_t *nxt_router_apps_hash_get(nxt_router_conf_t *rtcf,
1161563Svbart@nginx.com     nxt_str_t *name);
1171563Svbart@nginx.com static void nxt_router_apps_hash_use(nxt_task_t *task, nxt_router_conf_t *rtcf,
1181563Svbart@nginx.com     int i);
1191563Svbart@nginx.com 
1201555Smax.romanov@nginx.com static nxt_int_t nxt_router_app_queue_init(nxt_task_t *task,
1211555Smax.romanov@nginx.com     nxt_port_t *port);
1221555Smax.romanov@nginx.com static nxt_int_t nxt_router_port_queue_init(nxt_task_t *task,
1231555Smax.romanov@nginx.com     nxt_port_t *port);
1241555Smax.romanov@nginx.com static nxt_int_t nxt_router_port_queue_map(nxt_task_t *task,
1251555Smax.romanov@nginx.com     nxt_port_t *port, nxt_fd_t fd);
126198Sigor@sysoev.ru static void nxt_router_listen_socket_rpc_create(nxt_task_t *task,
127198Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf, nxt_socket_conf_t *skcf);
128198Sigor@sysoev.ru static void nxt_router_listen_socket_ready(nxt_task_t *task,
129198Sigor@sysoev.ru     nxt_port_recv_msg_t *msg, void *data);
130198Sigor@sysoev.ru static void nxt_router_listen_socket_error(nxt_task_t *task,
131198Sigor@sysoev.ru     nxt_port_recv_msg_t *msg, void *data);
132774Svbart@nginx.com #if (NXT_TLS)
133774Svbart@nginx.com static void nxt_router_tls_rpc_handler(nxt_task_t *task,
134774Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
1351828Sa.suvorov@f5.com static nxt_int_t nxt_router_conf_tls_insert(nxt_router_temp_conf_t *tmcf,
1361920Sa.suvorov@f5.com     nxt_conf_value_t *value, nxt_socket_conf_t *skcf, nxt_tls_init_t *tls_init,
1371920Sa.suvorov@f5.com     nxt_bool_t last);
138774Svbart@nginx.com #endif
139507Smax.romanov@nginx.com static void nxt_router_app_rpc_create(nxt_task_t *task,
140507Smax.romanov@nginx.com     nxt_router_temp_conf_t *tmcf, nxt_app_t *app);
141507Smax.romanov@nginx.com static void nxt_router_app_prefork_ready(nxt_task_t *task,
142507Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, void *data);
143507Smax.romanov@nginx.com static void nxt_router_app_prefork_error(nxt_task_t *task,
144507Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, void *data);
145359Sigor@sysoev.ru static nxt_socket_conf_t *nxt_router_socket_conf(nxt_task_t *task,
146359Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf, nxt_str_t *name);
147359Sigor@sysoev.ru static nxt_int_t nxt_router_listen_socket_find(nxt_router_temp_conf_t *tmcf,
148359Sigor@sysoev.ru     nxt_socket_conf_t *nskcf, nxt_sockaddr_t *sa);
14953Sigor@sysoev.ru 
15053Sigor@sysoev.ru static nxt_int_t nxt_router_engines_create(nxt_task_t *task,
15153Sigor@sysoev.ru     nxt_router_t *router, nxt_router_temp_conf_t *tmcf,
15253Sigor@sysoev.ru     const nxt_event_interface_t *interface);
153115Sigor@sysoev.ru static nxt_int_t nxt_router_engine_conf_create(nxt_router_temp_conf_t *tmcf,
154115Sigor@sysoev.ru     nxt_router_engine_conf_t *recf);
155115Sigor@sysoev.ru static nxt_int_t nxt_router_engine_conf_update(nxt_router_temp_conf_t *tmcf,
156115Sigor@sysoev.ru     nxt_router_engine_conf_t *recf);
157115Sigor@sysoev.ru static nxt_int_t nxt_router_engine_conf_delete(nxt_router_temp_conf_t *tmcf,
158115Sigor@sysoev.ru     nxt_router_engine_conf_t *recf);
159154Sigor@sysoev.ru static nxt_int_t nxt_router_engine_joints_create(nxt_router_temp_conf_t *tmcf,
160154Sigor@sysoev.ru     nxt_router_engine_conf_t *recf, nxt_queue_t *sockets,
161154Sigor@sysoev.ru     nxt_work_handler_t handler);
162313Sigor@sysoev.ru static nxt_int_t nxt_router_engine_quit(nxt_router_temp_conf_t *tmcf,
163313Sigor@sysoev.ru     nxt_router_engine_conf_t *recf);
164139Sigor@sysoev.ru static nxt_int_t nxt_router_engine_joints_delete(nxt_router_temp_conf_t *tmcf,
165139Sigor@sysoev.ru     nxt_router_engine_conf_t *recf, nxt_queue_t *sockets);
16653Sigor@sysoev.ru 
16753Sigor@sysoev.ru static nxt_int_t nxt_router_threads_create(nxt_task_t *task, nxt_runtime_t *rt,
16853Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
16953Sigor@sysoev.ru static nxt_int_t nxt_router_thread_create(nxt_task_t *task, nxt_runtime_t *rt,
17053Sigor@sysoev.ru     nxt_event_engine_t *engine);
171343Smax.romanov@nginx.com static void nxt_router_apps_sort(nxt_task_t *task, nxt_router_t *router,
172133Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
17353Sigor@sysoev.ru 
174315Sigor@sysoev.ru static void nxt_router_engines_post(nxt_router_t *router,
175315Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
176315Sigor@sysoev.ru static void nxt_router_engine_post(nxt_event_engine_t *engine,
177315Sigor@sysoev.ru     nxt_work_t *jobs);
17853Sigor@sysoev.ru 
17953Sigor@sysoev.ru static void nxt_router_thread_start(void *data);
1801545Smax.romanov@nginx.com static void nxt_router_rt_add_port(nxt_task_t *task, void *obj,
1811545Smax.romanov@nginx.com     void *data);
18253Sigor@sysoev.ru static void nxt_router_listen_socket_create(nxt_task_t *task, void *obj,
18353Sigor@sysoev.ru     void *data);
18453Sigor@sysoev.ru static void nxt_router_listen_socket_update(nxt_task_t *task, void *obj,
18553Sigor@sysoev.ru     void *data);
18653Sigor@sysoev.ru static void nxt_router_listen_socket_delete(nxt_task_t *task, void *obj,
18753Sigor@sysoev.ru     void *data);
188313Sigor@sysoev.ru static void nxt_router_worker_thread_quit(nxt_task_t *task, void *obj,
189313Sigor@sysoev.ru     void *data);
19053Sigor@sysoev.ru static void nxt_router_listen_socket_close(nxt_task_t *task, void *obj,
19153Sigor@sysoev.ru     void *data);
19253Sigor@sysoev.ru static void nxt_router_thread_exit_handler(nxt_task_t *task, void *obj,
19353Sigor@sysoev.ru     void *data);
1941547Smax.romanov@nginx.com static void nxt_router_req_headers_ack_handler(nxt_task_t *task,
1951547Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, nxt_request_rpc_data_t *req_rpc_data);
196359Sigor@sysoev.ru static void nxt_router_listen_socket_release(nxt_task_t *task,
197359Sigor@sysoev.ru     nxt_socket_conf_t *skcf);
19853Sigor@sysoev.ru 
199630Svbart@nginx.com static void nxt_router_access_log_writer(nxt_task_t *task,
200630Svbart@nginx.com     nxt_http_request_t *r, nxt_router_access_log_t *access_log);
201630Svbart@nginx.com static u_char *nxt_router_access_log_date(u_char *buf, nxt_realtime_t *now,
202630Svbart@nginx.com     struct tm *tm, size_t size, const char *format);
203630Svbart@nginx.com static void nxt_router_access_log_open(nxt_task_t *task,
204630Svbart@nginx.com     nxt_router_temp_conf_t *tmcf);
205630Svbart@nginx.com static void nxt_router_access_log_ready(nxt_task_t *task,
206630Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
207630Svbart@nginx.com static void nxt_router_access_log_error(nxt_task_t *task,
208630Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
209630Svbart@nginx.com static void nxt_router_access_log_release(nxt_task_t *task,
210630Svbart@nginx.com     nxt_thread_spinlock_t *lock, nxt_router_access_log_t *access_log);
211651Svbart@nginx.com static void nxt_router_access_log_reopen_completion(nxt_task_t *task, void *obj,
212651Svbart@nginx.com     void *data);
213631Svbart@nginx.com static void nxt_router_access_log_reopen_ready(nxt_task_t *task,
214631Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
215631Svbart@nginx.com static void nxt_router_access_log_reopen_error(nxt_task_t *task,
216631Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
217630Svbart@nginx.com 
218343Smax.romanov@nginx.com static void nxt_router_app_port_ready(nxt_task_t *task,
219343Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, void *data);
2201547Smax.romanov@nginx.com static nxt_int_t nxt_router_app_shared_port_send(nxt_task_t *task,
2211547Smax.romanov@nginx.com     nxt_port_t *app_port);
222343Smax.romanov@nginx.com static void nxt_router_app_port_error(nxt_task_t *task,
223343Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, void *data);
224343Smax.romanov@nginx.com 
2251563Svbart@nginx.com static void nxt_router_app_use(nxt_task_t *task, nxt_app_t *app, int i);
226753Smax.romanov@nginx.com static void nxt_router_app_unlink(nxt_task_t *task, nxt_app_t *app);
2271123Smax.romanov@nginx.com 
228343Smax.romanov@nginx.com static void nxt_router_app_port_release(nxt_task_t *task, nxt_port_t *port,
2291123Smax.romanov@nginx.com     nxt_apr_action_t action);
2301547Smax.romanov@nginx.com static void nxt_router_app_port_get(nxt_task_t *task, nxt_app_t *app,
2311547Smax.romanov@nginx.com     nxt_request_rpc_data_t *req_rpc_data);
2321561Smax.romanov@nginx.com static void nxt_router_http_request_error(nxt_task_t *task, void *obj,
2331561Smax.romanov@nginx.com     void *data);
2341547Smax.romanov@nginx.com static void nxt_router_http_request_done(nxt_task_t *task, void *obj,
2351547Smax.romanov@nginx.com     void *data);
236141Smax.romanov@nginx.com 
2371566Smax.romanov@nginx.com static void nxt_router_dummy_buf_completion(nxt_task_t *task, void *obj,
2381566Smax.romanov@nginx.com     void *data);
239425Smax.romanov@nginx.com static void nxt_router_app_prepare_request(nxt_task_t *task,
2401547Smax.romanov@nginx.com     nxt_request_rpc_data_t *req_rpc_data);
2411007Salexander.borisov@nginx.com static nxt_buf_t *nxt_router_prepare_msg(nxt_task_t *task,
2421547Smax.romanov@nginx.com     nxt_http_request_t *r, nxt_app_t *app, const nxt_str_t *prefix);
243510Salexander.borisov@nginx.com 
244318Smax.romanov@nginx.com static void nxt_router_app_timeout(nxt_task_t *task, void *obj, void *data);
245507Smax.romanov@nginx.com static void nxt_router_adjust_idle_timer(nxt_task_t *task, void *obj,
246507Smax.romanov@nginx.com     void *data);
247507Smax.romanov@nginx.com static void nxt_router_app_idle_timeout(nxt_task_t *task, void *obj,
248507Smax.romanov@nginx.com     void *data);
249753Smax.romanov@nginx.com static void nxt_router_app_joint_release_handler(nxt_task_t *task, void *obj,
250507Smax.romanov@nginx.com     void *data);
251753Smax.romanov@nginx.com static void nxt_router_free_app(nxt_task_t *task, void *obj, void *data);
252431Sigor@sysoev.ru 
253431Sigor@sysoev.ru static const nxt_http_request_state_t  nxt_http_request_send_state;
254431Sigor@sysoev.ru static void nxt_http_request_send_body(nxt_task_t *task, void *obj, void *data);
255141Smax.romanov@nginx.com 
256753Smax.romanov@nginx.com static void nxt_router_app_joint_use(nxt_task_t *task,
257753Smax.romanov@nginx.com     nxt_app_joint_t *app_joint, int i);
258753Smax.romanov@nginx.com 
2591547Smax.romanov@nginx.com static void nxt_router_http_request_release_post(nxt_task_t *task,
2601007Salexander.borisov@nginx.com     nxt_http_request_t *r);
2611007Salexander.borisov@nginx.com static void nxt_router_http_request_release(nxt_task_t *task, void *obj,
2621007Salexander.borisov@nginx.com     void *data);
2631321Smax.romanov@nginx.com static void nxt_router_oosm_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
2641545Smax.romanov@nginx.com static void nxt_router_get_port_handler(nxt_task_t *task,
2651545Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
2661546Smax.romanov@nginx.com static void nxt_router_get_mmap_handler(nxt_task_t *task,
2671546Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
2681007Salexander.borisov@nginx.com 
2691149Smax.romanov@nginx.com extern const nxt_http_request_state_t  nxt_http_websocket;
2701131Smax.romanov@nginx.com 
271119Smax.romanov@nginx.com static nxt_router_t  *nxt_router;
27220Sigor@sysoev.ru 
273743Smax.romanov@nginx.com static const nxt_str_t http_prefix = nxt_string("HTTP_");
274743Smax.romanov@nginx.com static const nxt_str_t empty_prefix = nxt_string("");
275743Smax.romanov@nginx.com 
276743Smax.romanov@nginx.com static const nxt_str_t  *nxt_app_msg_prefix[] = {
277804Svbart@nginx.com     &empty_prefix,
2781594Smax.romanov@nginx.com     &empty_prefix,
279743Smax.romanov@nginx.com     &http_prefix,
280743Smax.romanov@nginx.com     &http_prefix,
281743Smax.romanov@nginx.com     &http_prefix,
282977Smax.romanov@gmail.com     &empty_prefix,
283216Sigor@sysoev.ru };
284216Sigor@sysoev.ru 
285216Sigor@sysoev.ru 
2861488St.nateldemoura@f5.com static const nxt_port_handlers_t  nxt_router_process_port_handlers = {
2871488St.nateldemoura@f5.com     .quit         = nxt_signal_quit_handler,
288662Smax.romanov@nginx.com     .new_port     = nxt_router_new_port_handler,
2891545Smax.romanov@nginx.com     .get_port     = nxt_router_get_port_handler,
290662Smax.romanov@nginx.com     .change_file  = nxt_port_change_log_file_handler,
291662Smax.romanov@nginx.com     .mmap         = nxt_port_mmap_handler,
2921546Smax.romanov@nginx.com     .get_mmap     = nxt_router_get_mmap_handler,
293662Smax.romanov@nginx.com     .data         = nxt_router_conf_data_handler,
294*1926Smax.romanov@nginx.com     .app_restart  = nxt_router_app_restart_handler,
295662Smax.romanov@nginx.com     .remove_pid   = nxt_router_remove_pid_handler,
296662Smax.romanov@nginx.com     .access_log   = nxt_router_access_log_reopen_handler,
297662Smax.romanov@nginx.com     .rpc_ready    = nxt_port_rpc_handler,
298662Smax.romanov@nginx.com     .rpc_error    = nxt_port_rpc_handler,
2991321Smax.romanov@nginx.com     .oosm         = nxt_router_oosm_handler,
300662Smax.romanov@nginx.com };
301662Smax.romanov@nginx.com 
302662Smax.romanov@nginx.com 
3031488St.nateldemoura@f5.com const nxt_process_init_t  nxt_router_process = {
3041488St.nateldemoura@f5.com     .name           = "router",
3051488St.nateldemoura@f5.com     .type           = NXT_PROCESS_ROUTER,
3061488St.nateldemoura@f5.com     .prefork        = nxt_router_prefork,
3071488St.nateldemoura@f5.com     .restart        = 1,
3081488St.nateldemoura@f5.com     .setup          = nxt_process_core_setup,
3091488St.nateldemoura@f5.com     .start          = nxt_router_start,
3101488St.nateldemoura@f5.com     .port_handlers  = &nxt_router_process_port_handlers,
3111488St.nateldemoura@f5.com     .signals        = nxt_process_signals,
3121488St.nateldemoura@f5.com };
3131488St.nateldemoura@f5.com 
3141488St.nateldemoura@f5.com 
3151509Sigor@sysoev.ru /* Queues of nxt_socket_conf_t */
3161509Sigor@sysoev.ru nxt_queue_t  creating_sockets;
3171509Sigor@sysoev.ru nxt_queue_t  pending_sockets;
3181509Sigor@sysoev.ru nxt_queue_t  updating_sockets;
3191509Sigor@sysoev.ru nxt_queue_t  keeping_sockets;
3201509Sigor@sysoev.ru nxt_queue_t  deleting_sockets;
3211509Sigor@sysoev.ru 
3221509Sigor@sysoev.ru 
3231488St.nateldemoura@f5.com static nxt_int_t
3241488St.nateldemoura@f5.com nxt_router_prefork(nxt_task_t *task, nxt_process_t *process, nxt_mp_t *mp)
3251488St.nateldemoura@f5.com {
3261488St.nateldemoura@f5.com     nxt_runtime_stop_app_processes(task, task->thread->runtime);
3271488St.nateldemoura@f5.com 
3281488St.nateldemoura@f5.com     return NXT_OK;
3291488St.nateldemoura@f5.com }
3301488St.nateldemoura@f5.com 
3311488St.nateldemoura@f5.com 
3321488St.nateldemoura@f5.com static nxt_int_t
3331488St.nateldemoura@f5.com nxt_router_start(nxt_task_t *task, nxt_process_data_t *data)
33420Sigor@sysoev.ru {
335141Smax.romanov@nginx.com     nxt_int_t      ret;
336662Smax.romanov@nginx.com     nxt_port_t     *controller_port;
337141Smax.romanov@nginx.com     nxt_router_t   *router;
338141Smax.romanov@nginx.com     nxt_runtime_t  *rt;
339141Smax.romanov@nginx.com 
340141Smax.romanov@nginx.com     rt = task->thread->runtime;
34153Sigor@sysoev.ru 
3421488St.nateldemoura@f5.com     nxt_log(task, NXT_LOG_INFO, "router started");
3431488St.nateldemoura@f5.com 
344771Sigor@sysoev.ru #if (NXT_TLS)
345771Sigor@sysoev.ru     rt->tls = nxt_service_get(rt->services, "SSL/TLS", "OpenSSL");
346771Sigor@sysoev.ru     if (nxt_slow_path(rt->tls == NULL)) {
347771Sigor@sysoev.ru         return NXT_ERROR;
348771Sigor@sysoev.ru     }
349771Sigor@sysoev.ru 
350771Sigor@sysoev.ru     ret = rt->tls->library_init(task);
351771Sigor@sysoev.ru     if (nxt_slow_path(ret != NXT_OK)) {
352771Sigor@sysoev.ru         return ret;
353771Sigor@sysoev.ru     }
354771Sigor@sysoev.ru #endif
355771Sigor@sysoev.ru 
3561459Smax.romanov@nginx.com     ret = nxt_http_init(task);
35788Smax.romanov@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
35888Smax.romanov@nginx.com         return ret;
35988Smax.romanov@nginx.com     }
36088Smax.romanov@nginx.com 
36153Sigor@sysoev.ru     router = nxt_zalloc(sizeof(nxt_router_t));
36253Sigor@sysoev.ru     if (nxt_slow_path(router == NULL)) {
36353Sigor@sysoev.ru         return NXT_ERROR;
36453Sigor@sysoev.ru     }
36553Sigor@sysoev.ru 
36653Sigor@sysoev.ru     nxt_queue_init(&router->engines);
36753Sigor@sysoev.ru     nxt_queue_init(&router->sockets);
368133Sigor@sysoev.ru     nxt_queue_init(&router->apps);
36953Sigor@sysoev.ru 
370119Smax.romanov@nginx.com     nxt_router = router;
371119Smax.romanov@nginx.com 
372662Smax.romanov@nginx.com     controller_port = rt->port_by_type[NXT_PROCESS_CONTROLLER];
373662Smax.romanov@nginx.com     if (controller_port != NULL) {
374662Smax.romanov@nginx.com         nxt_router_greet_controller(task, controller_port);
375662Smax.romanov@nginx.com     }
376662Smax.romanov@nginx.com 
377115Sigor@sysoev.ru     return NXT_OK;
378115Sigor@sysoev.ru }
379115Sigor@sysoev.ru 
380115Sigor@sysoev.ru 
381343Smax.romanov@nginx.com static void
382662Smax.romanov@nginx.com nxt_router_greet_controller(nxt_task_t *task, nxt_port_t *controller_port)
383662Smax.romanov@nginx.com {
384662Smax.romanov@nginx.com     nxt_port_socket_write(task, controller_port, NXT_PORT_MSG_PROCESS_READY,
385662Smax.romanov@nginx.com                           -1, 0, 0, NULL);
386662Smax.romanov@nginx.com }
387662Smax.romanov@nginx.com 
388662Smax.romanov@nginx.com 
389662Smax.romanov@nginx.com static void
390507Smax.romanov@nginx.com nxt_router_start_app_process_handler(nxt_task_t *task, nxt_port_t *port,
391507Smax.romanov@nginx.com     void *data)
392167Smax.romanov@nginx.com {
393*1926Smax.romanov@nginx.com     size_t               size;
394*1926Smax.romanov@nginx.com     uint32_t             stream;
395*1926Smax.romanov@nginx.com     nxt_mp_t             *mp;
396*1926Smax.romanov@nginx.com     nxt_int_t            ret;
397*1926Smax.romanov@nginx.com     nxt_app_t            *app;
398*1926Smax.romanov@nginx.com     nxt_buf_t            *b;
399*1926Smax.romanov@nginx.com     nxt_port_t           *main_port;
400*1926Smax.romanov@nginx.com     nxt_runtime_t        *rt;
401*1926Smax.romanov@nginx.com     nxt_app_joint_rpc_t  *app_joint_rpc;
402343Smax.romanov@nginx.com 
403343Smax.romanov@nginx.com     app = data;
404167Smax.romanov@nginx.com 
405167Smax.romanov@nginx.com     rt = task->thread->runtime;
406240Sigor@sysoev.ru     main_port = rt->port_by_type[NXT_PROCESS_MAIN];
407167Smax.romanov@nginx.com 
408507Smax.romanov@nginx.com     nxt_debug(task, "app '%V' %p start process", &app->name, app);
409343Smax.romanov@nginx.com 
410343Smax.romanov@nginx.com     size = app->name.length + 1 + app->conf.length;
411343Smax.romanov@nginx.com 
412343Smax.romanov@nginx.com     b = nxt_buf_mem_ts_alloc(task, task->thread->engine->mem_pool, size);
413343Smax.romanov@nginx.com 
414343Smax.romanov@nginx.com     if (nxt_slow_path(b == NULL)) {
415343Smax.romanov@nginx.com         goto failed;
416167Smax.romanov@nginx.com     }
417167Smax.romanov@nginx.com 
418343Smax.romanov@nginx.com     nxt_buf_cpystr(b, &app->name);
419343Smax.romanov@nginx.com     *b->mem.free++ = '\0';
420343Smax.romanov@nginx.com     nxt_buf_cpystr(b, &app->conf);
421343Smax.romanov@nginx.com 
422*1926Smax.romanov@nginx.com     app_joint_rpc = nxt_port_rpc_register_handler_ex(task, port,
423*1926Smax.romanov@nginx.com                                                      nxt_router_app_port_ready,
424*1926Smax.romanov@nginx.com                                                      nxt_router_app_port_error,
425*1926Smax.romanov@nginx.com                                                    sizeof(nxt_app_joint_rpc_t));
426*1926Smax.romanov@nginx.com     if (nxt_slow_path(app_joint_rpc == NULL)) {
427343Smax.romanov@nginx.com         goto failed;
428343Smax.romanov@nginx.com     }
429343Smax.romanov@nginx.com 
430*1926Smax.romanov@nginx.com     stream = nxt_port_rpc_ex_stream(app_joint_rpc);
431*1926Smax.romanov@nginx.com 
4321488St.nateldemoura@f5.com     ret = nxt_port_socket_write(task, main_port, NXT_PORT_MSG_START_PROCESS,
4331488St.nateldemoura@f5.com                                 -1, stream, port->id, b);
434648Svbart@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
435648Svbart@nginx.com         nxt_port_rpc_cancel(task, port, stream);
436753Smax.romanov@nginx.com 
437648Svbart@nginx.com         goto failed;
438648Svbart@nginx.com     }
439343Smax.romanov@nginx.com 
440*1926Smax.romanov@nginx.com     app_joint_rpc->app_joint = app->joint;
441*1926Smax.romanov@nginx.com     app_joint_rpc->generation = app->generation;
442*1926Smax.romanov@nginx.com 
443*1926Smax.romanov@nginx.com     nxt_router_app_joint_use(task, app->joint, 1);
444*1926Smax.romanov@nginx.com 
445753Smax.romanov@nginx.com     nxt_router_app_use(task, app, -1);
446753Smax.romanov@nginx.com 
447343Smax.romanov@nginx.com     return;
448343Smax.romanov@nginx.com 
449343Smax.romanov@nginx.com failed:
450343Smax.romanov@nginx.com 
451648Svbart@nginx.com     if (b != NULL) {
452648Svbart@nginx.com         mp = b->data;
453648Svbart@nginx.com         nxt_mp_free(mp, b);
454648Svbart@nginx.com         nxt_mp_release(mp);
455648Svbart@nginx.com     }
456648Svbart@nginx.com 
457343Smax.romanov@nginx.com     nxt_thread_mutex_lock(&app->mutex);
458343Smax.romanov@nginx.com 
459507Smax.romanov@nginx.com     app->pending_processes--;
460343Smax.romanov@nginx.com 
461343Smax.romanov@nginx.com     nxt_thread_mutex_unlock(&app->mutex);
462343Smax.romanov@nginx.com 
463343Smax.romanov@nginx.com     nxt_router_app_use(task, app, -1);
464167Smax.romanov@nginx.com }
465167Smax.romanov@nginx.com 
466167Smax.romanov@nginx.com 
467753Smax.romanov@nginx.com static void
468753Smax.romanov@nginx.com nxt_router_app_joint_use(nxt_task_t *task, nxt_app_joint_t *app_joint, int i)
469753Smax.romanov@nginx.com {
470753Smax.romanov@nginx.com     app_joint->use_count += i;
471753Smax.romanov@nginx.com 
472753Smax.romanov@nginx.com     if (app_joint->use_count == 0) {
473753Smax.romanov@nginx.com         nxt_assert(app_joint->app == NULL);
474753Smax.romanov@nginx.com 
475753Smax.romanov@nginx.com         nxt_free(app_joint);
476753Smax.romanov@nginx.com     }
477753Smax.romanov@nginx.com }
478753Smax.romanov@nginx.com 
479753Smax.romanov@nginx.com 
480343Smax.romanov@nginx.com static nxt_int_t
481507Smax.romanov@nginx.com nxt_router_start_app_process(nxt_task_t *task, nxt_app_t *app)
482141Smax.romanov@nginx.com {
483343Smax.romanov@nginx.com     nxt_int_t      res;
484343Smax.romanov@nginx.com     nxt_port_t     *router_port;
485343Smax.romanov@nginx.com     nxt_runtime_t  *rt;
486343Smax.romanov@nginx.com 
4871549Smax.romanov@nginx.com     nxt_debug(task, "app '%V' start process", &app->name);
4881549Smax.romanov@nginx.com 
489343Smax.romanov@nginx.com     rt = task->thread->runtime;
490343Smax.romanov@nginx.com     router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
491343Smax.romanov@nginx.com 
492343Smax.romanov@nginx.com     nxt_router_app_use(task, app, 1);
493343Smax.romanov@nginx.com 
494507Smax.romanov@nginx.com     res = nxt_port_post(task, router_port, nxt_router_start_app_process_handler,
495343Smax.romanov@nginx.com                         app);
496343Smax.romanov@nginx.com 
497343Smax.romanov@nginx.com     if (res == NXT_OK) {
498343Smax.romanov@nginx.com         return res;
499318Smax.romanov@nginx.com     }
500318Smax.romanov@nginx.com 
501343Smax.romanov@nginx.com     nxt_thread_mutex_lock(&app->mutex);
502343Smax.romanov@nginx.com 
503507Smax.romanov@nginx.com     app->pending_processes--;
504343Smax.romanov@nginx.com 
505343Smax.romanov@nginx.com     nxt_thread_mutex_unlock(&app->mutex);
506343Smax.romanov@nginx.com 
507343Smax.romanov@nginx.com     nxt_router_app_use(task, app, -1);
508343Smax.romanov@nginx.com 
509343Smax.romanov@nginx.com     return NXT_ERROR;
510318Smax.romanov@nginx.com }
511318Smax.romanov@nginx.com 
512318Smax.romanov@nginx.com 
513423Smax.romanov@nginx.com nxt_inline nxt_bool_t
5141555Smax.romanov@nginx.com nxt_router_msg_cancel(nxt_task_t *task, nxt_request_rpc_data_t *req_rpc_data)
515423Smax.romanov@nginx.com {
5161555Smax.romanov@nginx.com     nxt_buf_t       *b, *next;
5171555Smax.romanov@nginx.com     nxt_bool_t      cancelled;
518*1926Smax.romanov@nginx.com     nxt_port_t      *app_port;
5191555Smax.romanov@nginx.com     nxt_msg_info_t  *msg_info;
5201555Smax.romanov@nginx.com 
5211555Smax.romanov@nginx.com     msg_info = &req_rpc_data->msg_info;
522423Smax.romanov@nginx.com 
523423Smax.romanov@nginx.com     if (msg_info->buf == NULL) {
524423Smax.romanov@nginx.com         return 0;
525423Smax.romanov@nginx.com     }
526423Smax.romanov@nginx.com 
527*1926Smax.romanov@nginx.com     app_port = req_rpc_data->app_port;
528*1926Smax.romanov@nginx.com 
529*1926Smax.romanov@nginx.com     if (app_port != NULL && app_port->id == NXT_SHARED_PORT_ID) {
530*1926Smax.romanov@nginx.com         cancelled = nxt_app_queue_cancel(app_port->queue,
531*1926Smax.romanov@nginx.com                                          msg_info->tracking_cookie,
532*1926Smax.romanov@nginx.com                                          req_rpc_data->stream);
533*1926Smax.romanov@nginx.com 
534*1926Smax.romanov@nginx.com         if (cancelled) {
535*1926Smax.romanov@nginx.com             nxt_debug(task, "stream #%uD: cancelled by router",
536*1926Smax.romanov@nginx.com                       req_rpc_data->stream);
537*1926Smax.romanov@nginx.com         }
538*1926Smax.romanov@nginx.com 
539*1926Smax.romanov@nginx.com     } else {
540*1926Smax.romanov@nginx.com         cancelled = 0;
541423Smax.romanov@nginx.com     }
542423Smax.romanov@nginx.com 
543423Smax.romanov@nginx.com     for (b = msg_info->buf; b != NULL; b = next) {
544423Smax.romanov@nginx.com         next = b->next;
5451269Sigor@sysoev.ru         b->next = NULL;
546423Smax.romanov@nginx.com 
547423Smax.romanov@nginx.com         if (b->is_port_mmap_sent) {
548423Smax.romanov@nginx.com             b->is_port_mmap_sent = cancelled == 0;
549423Smax.romanov@nginx.com         }
5501829Smax.romanov@nginx.com 
5511829Smax.romanov@nginx.com         b->completion_handler(task, b, b->parent);
552423Smax.romanov@nginx.com     }
553423Smax.romanov@nginx.com 
554423Smax.romanov@nginx.com     msg_info->buf = NULL;
555423Smax.romanov@nginx.com 
556423Smax.romanov@nginx.com     return cancelled;
557423Smax.romanov@nginx.com }
558423Smax.romanov@nginx.com 
559423Smax.romanov@nginx.com 
560425Smax.romanov@nginx.com nxt_inline nxt_bool_t
561425Smax.romanov@nginx.com nxt_queue_chk_remove(nxt_queue_link_t *lnk)
562425Smax.romanov@nginx.com {
563425Smax.romanov@nginx.com     if (lnk->next != NULL) {
564425Smax.romanov@nginx.com         nxt_queue_remove(lnk);
565425Smax.romanov@nginx.com 
566425Smax.romanov@nginx.com         lnk->next = NULL;
567425Smax.romanov@nginx.com 
568425Smax.romanov@nginx.com         return 1;
569425Smax.romanov@nginx.com     }
570425Smax.romanov@nginx.com 
571425Smax.romanov@nginx.com     return 0;
572425Smax.romanov@nginx.com }
573425Smax.romanov@nginx.com 
574425Smax.romanov@nginx.com 
575343Smax.romanov@nginx.com nxt_inline void
5761123Smax.romanov@nginx.com nxt_request_rpc_data_unlink(nxt_task_t *task,
5771123Smax.romanov@nginx.com     nxt_request_rpc_data_t *req_rpc_data)
578343Smax.romanov@nginx.com {
5791561Smax.romanov@nginx.com     nxt_app_t           *app;
5801561Smax.romanov@nginx.com     nxt_bool_t          unlinked;
5811547Smax.romanov@nginx.com     nxt_http_request_t  *r;
5821547Smax.romanov@nginx.com 
5831555Smax.romanov@nginx.com     nxt_router_msg_cancel(task, req_rpc_data);
5841123Smax.romanov@nginx.com 
5851123Smax.romanov@nginx.com     if (req_rpc_data->app_port != NULL) {
5861123Smax.romanov@nginx.com         nxt_router_app_port_release(task, req_rpc_data->app_port,
5871123Smax.romanov@nginx.com                                     req_rpc_data->apr_action);
5881123Smax.romanov@nginx.com 
5891123Smax.romanov@nginx.com         req_rpc_data->app_port = NULL;
5901123Smax.romanov@nginx.com     }
5911123Smax.romanov@nginx.com 
5921561Smax.romanov@nginx.com     app = req_rpc_data->app;
5931547Smax.romanov@nginx.com     r = req_rpc_data->request;
5941547Smax.romanov@nginx.com 
5951547Smax.romanov@nginx.com     if (r != NULL) {
5961547Smax.romanov@nginx.com         r->timer_data = NULL;
5971547Smax.romanov@nginx.com 
5981547Smax.romanov@nginx.com         nxt_router_http_request_release_post(task, r);
5991547Smax.romanov@nginx.com 
6001547Smax.romanov@nginx.com         r->req_rpc_data = NULL;
6011123Smax.romanov@nginx.com         req_rpc_data->request = NULL;
6021561Smax.romanov@nginx.com 
6031561Smax.romanov@nginx.com         if (app != NULL) {
6041561Smax.romanov@nginx.com             unlinked = 0;
6051561Smax.romanov@nginx.com 
6061561Smax.romanov@nginx.com             nxt_thread_mutex_lock(&app->mutex);
6071561Smax.romanov@nginx.com 
6081561Smax.romanov@nginx.com             if (r->app_link.next != NULL) {
6091561Smax.romanov@nginx.com                 nxt_queue_remove(&r->app_link);
6101561Smax.romanov@nginx.com                 r->app_link.next = NULL;
6111561Smax.romanov@nginx.com 
6121561Smax.romanov@nginx.com                 unlinked = 1;
6131561Smax.romanov@nginx.com             }
6141561Smax.romanov@nginx.com 
6151561Smax.romanov@nginx.com             nxt_thread_mutex_unlock(&app->mutex);
6161561Smax.romanov@nginx.com 
6171561Smax.romanov@nginx.com             if (unlinked) {
6181561Smax.romanov@nginx.com                 nxt_mp_release(r->mem_pool);
6191561Smax.romanov@nginx.com             }
6201561Smax.romanov@nginx.com         }
6211561Smax.romanov@nginx.com     }
6221561Smax.romanov@nginx.com 
6231561Smax.romanov@nginx.com     if (app != NULL) {
6241561Smax.romanov@nginx.com         nxt_router_app_use(task, app, -1);
6251561Smax.romanov@nginx.com 
6261561Smax.romanov@nginx.com         req_rpc_data->app = NULL;
627346Smax.romanov@nginx.com     }
6281547Smax.romanov@nginx.com 
6291547Smax.romanov@nginx.com     if (req_rpc_data->msg_info.body_fd != -1) {
6301547Smax.romanov@nginx.com         nxt_fd_close(req_rpc_data->msg_info.body_fd);
6311547Smax.romanov@nginx.com 
6321547Smax.romanov@nginx.com         req_rpc_data->msg_info.body_fd = -1;
6331547Smax.romanov@nginx.com     }
6341547Smax.romanov@nginx.com 
6351547Smax.romanov@nginx.com     if (req_rpc_data->rpc_cancel) {
6361547Smax.romanov@nginx.com         req_rpc_data->rpc_cancel = 0;
6371547Smax.romanov@nginx.com 
6381547Smax.romanov@nginx.com         nxt_port_rpc_cancel(task, task->thread->engine->port,
6391547Smax.romanov@nginx.com                             req_rpc_data->stream);
6401547Smax.romanov@nginx.com     }
641343Smax.romanov@nginx.com }
642343Smax.romanov@nginx.com 
643343Smax.romanov@nginx.com 
6441552Smax.romanov@nginx.com static void
645141Smax.romanov@nginx.com nxt_router_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
646141Smax.romanov@nginx.com {
6471555Smax.romanov@nginx.com     nxt_int_t      res;
6481547Smax.romanov@nginx.com     nxt_app_t      *app;
6491547Smax.romanov@nginx.com     nxt_port_t     *port, *main_app_port;
6501547Smax.romanov@nginx.com     nxt_runtime_t  *rt;
6511547Smax.romanov@nginx.com 
652141Smax.romanov@nginx.com     nxt_port_new_port_handler(task, msg);
653141Smax.romanov@nginx.com 
6541547Smax.romanov@nginx.com     port = msg->u.new_port;
6551547Smax.romanov@nginx.com 
6561547Smax.romanov@nginx.com     if (port != NULL && port->type == NXT_PROCESS_CONTROLLER) {
657662Smax.romanov@nginx.com         nxt_router_greet_controller(task, msg->u.new_port);
658662Smax.romanov@nginx.com     }
659662Smax.romanov@nginx.com 
6601547Smax.romanov@nginx.com     if (port == NULL || port->type != NXT_PROCESS_APP) {
6611547Smax.romanov@nginx.com 
6621547Smax.romanov@nginx.com         if (msg->port_msg.stream == 0) {
6631547Smax.romanov@nginx.com             return;
6641547Smax.romanov@nginx.com         }
6651547Smax.romanov@nginx.com 
6661547Smax.romanov@nginx.com         msg->port_msg.type = _NXT_PORT_MSG_RPC_ERROR;
6671555Smax.romanov@nginx.com 
6681555Smax.romanov@nginx.com     } else {
6691558Smax.romanov@nginx.com         if (msg->fd[1] != -1) {
6701558Smax.romanov@nginx.com             res = nxt_router_port_queue_map(task, port, msg->fd[1]);
6711555Smax.romanov@nginx.com             if (nxt_slow_path(res != NXT_OK)) {
6721555Smax.romanov@nginx.com                 return;
6731555Smax.romanov@nginx.com             }
6741555Smax.romanov@nginx.com 
6751558Smax.romanov@nginx.com             nxt_fd_close(msg->fd[1]);
6761558Smax.romanov@nginx.com             msg->fd[1] = -1;
6771555Smax.romanov@nginx.com         }
6781547Smax.romanov@nginx.com     }
6791547Smax.romanov@nginx.com 
6801547Smax.romanov@nginx.com     if (msg->port_msg.stream != 0) {
6811547Smax.romanov@nginx.com         nxt_port_rpc_handler(task, msg);
682141Smax.romanov@nginx.com         return;
683141Smax.romanov@nginx.com     }
684141Smax.romanov@nginx.com 
6851547Smax.romanov@nginx.com     /*
6861547Smax.romanov@nginx.com      * Port with "id == 0" is application 'main' port and it always
6871547Smax.romanov@nginx.com      * should come with non-zero stream.
6881547Smax.romanov@nginx.com      */
6891547Smax.romanov@nginx.com     nxt_assert(port->id != 0);
6901547Smax.romanov@nginx.com 
6911547Smax.romanov@nginx.com     /* Find 'main' app port and get app reference. */
6921547Smax.romanov@nginx.com     rt = task->thread->runtime;
6931547Smax.romanov@nginx.com 
6941547Smax.romanov@nginx.com     /*
6951547Smax.romanov@nginx.com      * It is safe to access 'runtime->ports' hash because 'NEW_PORT'
6961547Smax.romanov@nginx.com      * sent to main port (with id == 0) and processed in main thread.
6971547Smax.romanov@nginx.com      */
6981547Smax.romanov@nginx.com     main_app_port = nxt_port_hash_find(&rt->ports, port->pid, 0);
6991547Smax.romanov@nginx.com     nxt_assert(main_app_port != NULL);
7001547Smax.romanov@nginx.com 
7011547Smax.romanov@nginx.com     app = main_app_port->app;
7021915Smax.romanov@nginx.com 
7031915Smax.romanov@nginx.com     if (nxt_fast_path(app != NULL)) {
7041915Smax.romanov@nginx.com         nxt_thread_mutex_lock(&app->mutex);
7051915Smax.romanov@nginx.com 
7061915Smax.romanov@nginx.com         /* TODO here should be find-and-add code because there can be
7071915Smax.romanov@nginx.com            port waiters in port_hash */
7081915Smax.romanov@nginx.com         nxt_port_hash_add(&app->port_hash, port);
7091915Smax.romanov@nginx.com         app->port_hash_count++;
7101915Smax.romanov@nginx.com 
7111915Smax.romanov@nginx.com         nxt_thread_mutex_unlock(&app->mutex);
7121915Smax.romanov@nginx.com 
7131915Smax.romanov@nginx.com         port->app = app;
7141915Smax.romanov@nginx.com     }
7151915Smax.romanov@nginx.com 
7161547Smax.romanov@nginx.com     port->main_app_port = main_app_port;
7171666Smax.romanov@nginx.com 
7181666Smax.romanov@nginx.com     nxt_port_socket_write(task, port, NXT_PORT_MSG_PORT_ACK, -1, 0, 0, NULL);
719141Smax.romanov@nginx.com }
720141Smax.romanov@nginx.com 
721141Smax.romanov@nginx.com 
7221552Smax.romanov@nginx.com static void
723139Sigor@sysoev.ru nxt_router_conf_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
724115Sigor@sysoev.ru {
725