xref: /unit/src/nxt_main_process.c (revision 2014:f8a0992944df)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #include <nxt_main.h>
8 #include <nxt_runtime.h>
9 #include <nxt_port.h>
10 #include <nxt_main_process.h>
11 #include <nxt_conf.h>
12 #include <nxt_router.h>
13 #include <nxt_port_queue.h>
14 #if (NXT_TLS)
15 #include <nxt_cert.h>
16 #endif
17 
18 #include <sys/mount.h>
19 
20 
21 typedef struct {
22     nxt_socket_t        socket;
23     nxt_socket_error_t  error;
24     u_char              *start;
25     u_char              *end;
26 } nxt_listening_socket_t;
27 
28 
29 typedef struct {
30     nxt_uint_t          size;
31     nxt_conf_map_t      *map;
32 } nxt_conf_app_map_t;
33 
34 
35 static nxt_int_t nxt_main_process_port_create(nxt_task_t *task,
36     nxt_runtime_t *rt);
37 static void nxt_main_process_title(nxt_task_t *task);
38 static void nxt_main_process_sigterm_handler(nxt_task_t *task, void *obj,
39     void *data);
40 static void nxt_main_process_sigquit_handler(nxt_task_t *task, void *obj,
41     void *data);
42 static void nxt_main_process_sigusr1_handler(nxt_task_t *task, void *obj,
43     void *data);
44 static void nxt_main_process_sigchld_handler(nxt_task_t *task, void *obj,
45     void *data);
46 static void nxt_main_process_signal_handler(nxt_task_t *task, void *obj,
47     void *data);
48 static void nxt_main_process_cleanup(nxt_task_t *task, nxt_process_t *process);
49 static void nxt_main_port_socket_handler(nxt_task_t *task,
50     nxt_port_recv_msg_t *msg);
51 static nxt_int_t nxt_main_listening_socket(nxt_sockaddr_t *sa,
52     nxt_listening_socket_t *ls);
53 static void nxt_main_port_modules_handler(nxt_task_t *task,
54     nxt_port_recv_msg_t *msg);
55 static int nxt_cdecl nxt_app_lang_compare(const void *v1, const void *v2);
56 static void nxt_main_process_whoami_handler(nxt_task_t *task,
57     nxt_port_recv_msg_t *msg);
58 static void nxt_main_port_conf_store_handler(nxt_task_t *task,
59     nxt_port_recv_msg_t *msg);
60 static nxt_int_t nxt_main_file_store(nxt_task_t *task, const char *tmp_name,
61     const char *name, u_char *buf, size_t size);
62 static void nxt_main_port_access_log_handler(nxt_task_t *task,
63     nxt_port_recv_msg_t *msg);
64 
65 const nxt_sig_event_t  nxt_main_process_signals[] = {
66     nxt_event_signal(SIGHUP,  nxt_main_process_signal_handler),
67     nxt_event_signal(SIGINT,  nxt_main_process_sigterm_handler),
68     nxt_event_signal(SIGQUIT, nxt_main_process_sigquit_handler),
69     nxt_event_signal(SIGTERM, nxt_main_process_sigterm_handler),
70     nxt_event_signal(SIGCHLD, nxt_main_process_sigchld_handler),
71     nxt_event_signal(SIGUSR1, nxt_main_process_sigusr1_handler),
72     nxt_event_signal_end,
73 };
74 
75 
76 nxt_uint_t  nxt_conf_ver;
77 
78 static nxt_bool_t  nxt_exiting;
79 
80 
81 nxt_int_t
82 nxt_main_process_start(nxt_thread_t *thr, nxt_task_t *task,
83     nxt_runtime_t *rt)
84 {
85     rt->type = NXT_PROCESS_MAIN;
86 
87     if (nxt_main_process_port_create(task, rt) != NXT_OK) {
88         return NXT_ERROR;
89     }
90 
91     nxt_main_process_title(task);
92 
93     /*
94      * The discovery process will send a message processed by
95      * nxt_main_port_modules_handler() which starts the controller
96      * and router processes.
97      */
98     return nxt_process_init_start(task, nxt_discovery_process);
99 }
100 
101 
102 static nxt_conf_map_t  nxt_common_app_conf[] = {
103     {
104         nxt_string("type"),
105         NXT_CONF_MAP_STR,
106         offsetof(nxt_common_app_conf_t, type),
107     },
108 
109     {
110         nxt_string("user"),
111         NXT_CONF_MAP_STR,
112         offsetof(nxt_common_app_conf_t, user),
113     },
114 
115     {
116         nxt_string("group"),
117         NXT_CONF_MAP_STR,
118         offsetof(nxt_common_app_conf_t, group),
119     },
120 
121     {
122         nxt_string("working_directory"),
123         NXT_CONF_MAP_CSTRZ,
124         offsetof(nxt_common_app_conf_t, working_directory),
125     },
126 
127     {
128         nxt_string("environment"),
129         NXT_CONF_MAP_PTR,
130         offsetof(nxt_common_app_conf_t, environment),
131     },
132 
133     {
134         nxt_string("isolation"),
135         NXT_CONF_MAP_PTR,
136         offsetof(nxt_common_app_conf_t, isolation),
137     },
138 
139     {
140         nxt_string("limits"),
141         NXT_CONF_MAP_PTR,
142         offsetof(nxt_common_app_conf_t, limits),
143     },
144 
145 };
146 
147 
148 static nxt_conf_map_t  nxt_common_app_limits_conf[] = {
149     {
150         nxt_string("shm"),
151         NXT_CONF_MAP_SIZE,
152         offsetof(nxt_common_app_conf_t, shm_limit),
153     },
154 
155     {
156         nxt_string("requests"),
157         NXT_CONF_MAP_INT32,
158         offsetof(nxt_common_app_conf_t, request_limit),
159     },
160 
161 };
162 
163 
164 static nxt_conf_map_t  nxt_external_app_conf[] = {
165     {
166         nxt_string("executable"),
167         NXT_CONF_MAP_CSTRZ,
168         offsetof(nxt_common_app_conf_t, u.external.executable),
169     },
170 
171     {
172         nxt_string("arguments"),
173         NXT_CONF_MAP_PTR,
174         offsetof(nxt_common_app_conf_t, u.external.arguments),
175     },
176 
177 };
178 
179 
180 static nxt_conf_map_t  nxt_python_app_conf[] = {
181     {
182         nxt_string("home"),
183         NXT_CONF_MAP_CSTRZ,
184         offsetof(nxt_common_app_conf_t, u.python.home),
185     },
186 
187     {
188         nxt_string("path"),
189         NXT_CONF_MAP_PTR,
190         offsetof(nxt_common_app_conf_t, u.python.path),
191     },
192 
193     {
194         nxt_string("module"),
195         NXT_CONF_MAP_STR,
196         offsetof(nxt_common_app_conf_t, u.python.module),
197     },
198 
199     {
200         nxt_string("callable"),
201         NXT_CONF_MAP_CSTRZ,
202         offsetof(nxt_common_app_conf_t, u.python.callable),
203     },
204 
205     {
206         nxt_string("protocol"),
207         NXT_CONF_MAP_STR,
208         offsetof(nxt_common_app_conf_t, u.python.protocol),
209     },
210 
211     {
212         nxt_string("threads"),
213         NXT_CONF_MAP_INT32,
214         offsetof(nxt_common_app_conf_t, u.python.threads),
215     },
216 
217     {
218         nxt_string("targets"),
219         NXT_CONF_MAP_PTR,
220         offsetof(nxt_common_app_conf_t, u.python.targets),
221     },
222 
223     {
224         nxt_string("thread_stack_size"),
225         NXT_CONF_MAP_INT32,
226         offsetof(nxt_common_app_conf_t, u.python.thread_stack_size),
227     },
228 };
229 
230 
231 static nxt_conf_map_t  nxt_php_app_conf[] = {
232     {
233         nxt_string("targets"),
234         NXT_CONF_MAP_PTR,
235         offsetof(nxt_common_app_conf_t, u.php.targets),
236     },
237 
238     {
239         nxt_string("options"),
240         NXT_CONF_MAP_PTR,
241         offsetof(nxt_common_app_conf_t, u.php.options),
242     },
243 };
244 
245 
246 static nxt_conf_map_t  nxt_perl_app_conf[] = {
247     {
248         nxt_string("script"),
249         NXT_CONF_MAP_CSTRZ,
250         offsetof(nxt_common_app_conf_t, u.perl.script),
251     },
252 
253     {
254         nxt_string("threads"),
255         NXT_CONF_MAP_INT32,
256         offsetof(nxt_common_app_conf_t, u.perl.threads),
257     },
258 
259     {
260         nxt_string("thread_stack_size"),
261         NXT_CONF_MAP_INT32,
262         offsetof(nxt_common_app_conf_t, u.perl.thread_stack_size),
263     },
264 };
265 
266 
267 static nxt_conf_map_t  nxt_ruby_app_conf[] = {
268     {
269         nxt_string("script"),
270         NXT_CONF_MAP_STR,
271         offsetof(nxt_common_app_conf_t, u.ruby.script),
272     },
273     {
274         nxt_string("threads"),
275         NXT_CONF_MAP_INT32,
276         offsetof(nxt_common_app_conf_t, u.ruby.threads),
277     },
278     {
279         nxt_string("hooks"),
280         NXT_CONF_MAP_STR,
281         offsetof(nxt_common_app_conf_t, u.ruby.hooks),
282     }
283 };
284 
285 
286 static nxt_conf_map_t  nxt_java_app_conf[] = {
287     {
288         nxt_string("classpath"),
289         NXT_CONF_MAP_PTR,
290         offsetof(nxt_common_app_conf_t, u.java.classpath),
291     },
292     {
293         nxt_string("webapp"),
294         NXT_CONF_MAP_CSTRZ,
295         offsetof(nxt_common_app_conf_t, u.java.webapp),
296     },
297     {
298         nxt_string("options"),
299         NXT_CONF_MAP_PTR,
300         offsetof(nxt_common_app_conf_t, u.java.options),
301     },
302     {
303         nxt_string("unit_jars"),
304         NXT_CONF_MAP_CSTRZ,
305         offsetof(nxt_common_app_conf_t, u.java.unit_jars),
306     },
307     {
308         nxt_string("threads"),
309         NXT_CONF_MAP_INT32,
310         offsetof(nxt_common_app_conf_t, u.java.threads),
311     },
312     {
313         nxt_string("thread_stack_size"),
314         NXT_CONF_MAP_INT32,
315         offsetof(nxt_common_app_conf_t, u.java.thread_stack_size),
316     },
317 
318 };
319 
320 
321 static nxt_conf_app_map_t  nxt_app_maps[] = {
322     { nxt_nitems(nxt_external_app_conf),  nxt_external_app_conf },
323     { nxt_nitems(nxt_python_app_conf),    nxt_python_app_conf },
324     { nxt_nitems(nxt_php_app_conf),       nxt_php_app_conf },
325     { nxt_nitems(nxt_perl_app_conf),      nxt_perl_app_conf },
326     { nxt_nitems(nxt_ruby_app_conf),      nxt_ruby_app_conf },
327     { nxt_nitems(nxt_java_app_conf),      nxt_java_app_conf },
328 };
329 
330 
331 static void
332 nxt_main_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
333 {
334     nxt_debug(task, "main data: %*s",
335               nxt_buf_mem_used_size(&msg->buf->mem), msg->buf->mem.pos);
336 }
337 
338 
339 static void
340 nxt_main_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
341 {
342     void        *mem;
343     nxt_port_t  *port;
344 
345     nxt_port_new_port_handler(task, msg);
346 
347     port = msg->u.new_port;
348 
349     if (port != NULL
350         && port->type == NXT_PROCESS_APP
351         && msg->fd[1] != -1)
352     {
353         mem = nxt_mem_mmap(NULL, sizeof(nxt_port_queue_t),
354                            PROT_READ | PROT_WRITE, MAP_SHARED, msg->fd[1], 0);
355         if (nxt_fast_path(mem != MAP_FAILED)) {
356             port->queue = mem;
357         }
358 
359         nxt_fd_close(msg->fd[1]);
360         msg->fd[1] = -1;
361     }
362 }
363 
364 
365 static void
366 nxt_main_start_process_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
367 {
368     u_char                 *start, *p, ch;
369     size_t                 type_len;
370     nxt_int_t              ret;
371     nxt_buf_t              *b;
372     nxt_port_t             *port;
373     nxt_runtime_t          *rt;
374     nxt_process_t          *process;
375     nxt_app_type_t         idx;
376     nxt_conf_value_t       *conf;
377     nxt_process_init_t     *init;
378     nxt_common_app_conf_t  *app_conf;
379 
380     rt = task->thread->runtime;
381 
382     port = rt->port_by_type[NXT_PROCESS_ROUTER];
383     if (nxt_slow_path(port == NULL)) {
384         nxt_alert(task, "router port not found");
385         goto close_fds;
386     }
387 
388     if (nxt_slow_path(port->pid != nxt_recv_msg_cmsg_pid(msg))) {
389         nxt_alert(task, "process %PI cannot start processes",
390                   nxt_recv_msg_cmsg_pid(msg));
391 
392         goto close_fds;
393     }
394 
395     process = nxt_process_new(rt);
396     if (nxt_slow_path(process == NULL)) {
397         goto close_fds;
398     }
399 
400     process->mem_pool = nxt_mp_create(1024, 128, 256, 32);
401     if (process->mem_pool == NULL) {
402         nxt_process_use(task, process, -1);
403         goto close_fds;
404     }
405 
406     process->parent_port = rt->port_by_type[NXT_PROCESS_MAIN];
407 
408     init = nxt_process_init(process);
409 
410     *init = nxt_proto_process;
411 
412     b = nxt_buf_chk_make_plain(process->mem_pool, msg->buf, msg->size);
413     if (b == NULL) {
414         goto failed;
415     }
416 
417     nxt_debug(task, "main start prototype: %*s", b->mem.free - b->mem.pos,
418               b->mem.pos);
419 
420     app_conf = nxt_mp_zalloc(process->mem_pool, sizeof(nxt_common_app_conf_t));
421     if (nxt_slow_path(app_conf == NULL)) {
422         goto failed;
423     }
424 
425     app_conf->shared_port_fd = msg->fd[0];
426     app_conf->shared_queue_fd = msg->fd[1];
427 
428     start = b->mem.pos;
429 
430     app_conf->name.start = start;
431     app_conf->name.length = nxt_strlen(start);
432 
433     init->name = (const char *) start;
434 
435     process->name = nxt_mp_alloc(process->mem_pool, app_conf->name.length
436                                  + sizeof("\"\" prototype") + 1);
437 
438     if (nxt_slow_path(process->name == NULL)) {
439         goto failed;
440     }
441 
442     p = (u_char *) process->name;
443     *p++ = '"';
444     p = nxt_cpymem(p, init->name, app_conf->name.length);
445     p = nxt_cpymem(p, "\" prototype", 11);
446     *p = '\0';
447 
448     app_conf->shm_limit = 100 * 1024 * 1024;
449     app_conf->request_limit = 0;
450 
451     start += app_conf->name.length + 1;
452 
453     conf = nxt_conf_json_parse(process->mem_pool, start, b->mem.free, NULL);
454     if (conf == NULL) {
455         nxt_alert(task, "router app configuration parsing error");
456 
457         goto failed;
458     }
459 
460     rt = task->thread->runtime;
461 
462     app_conf->user.start  = (u_char*)rt->user_cred.user;
463     app_conf->user.length = nxt_strlen(rt->user_cred.user);
464 
465     ret = nxt_conf_map_object(process->mem_pool, conf, nxt_common_app_conf,
466                               nxt_nitems(nxt_common_app_conf), app_conf);
467 
468     if (ret != NXT_OK) {
469         nxt_alert(task, "failed to map common app conf received from router");
470         goto failed;
471     }
472 
473     for (type_len = 0; type_len != app_conf->type.length; type_len++) {
474         ch = app_conf->type.start[type_len];
475 
476         if (ch == ' ' || nxt_isdigit(ch)) {
477             break;
478         }
479     }
480 
481     idx = nxt_app_parse_type(app_conf->type.start, type_len);
482 
483     if (nxt_slow_path(idx >= nxt_nitems(nxt_app_maps))) {
484         nxt_alert(task, "invalid app type %d received from router", (int) idx);
485         goto failed;
486     }
487 
488     ret = nxt_conf_map_object(process->mem_pool, conf, nxt_app_maps[idx].map,
489                               nxt_app_maps[idx].size, app_conf);
490 
491     if (nxt_slow_path(ret != NXT_OK)) {
492         nxt_alert(task, "failed to map app conf received from router");
493         goto failed;
494     }
495 
496     if (app_conf->limits != NULL) {
497         ret = nxt_conf_map_object(process->mem_pool, app_conf->limits,
498                                   nxt_common_app_limits_conf,
499                                   nxt_nitems(nxt_common_app_limits_conf),
500                                   app_conf);
501 
502         if (nxt_slow_path(ret != NXT_OK)) {
503             nxt_alert(task, "failed to map app limits received from router");
504             goto failed;
505         }
506     }
507 
508     app_conf->self = conf;
509 
510     process->stream = msg->port_msg.stream;
511     process->data.app = app_conf;
512 
513     ret = nxt_process_start(task, process);
514     if (nxt_fast_path(ret == NXT_OK || ret == NXT_AGAIN)) {
515 
516         /* Close shared port fds only in main process. */
517         if (ret == NXT_OK) {
518             nxt_fd_close(app_conf->shared_port_fd);
519             nxt_fd_close(app_conf->shared_queue_fd);
520         }
521 
522         /* Avoid fds close in caller. */
523         msg->fd[0] = -1;
524         msg->fd[1] = -1;
525 
526         return;
527     }
528 
529 failed:
530 
531     nxt_process_use(task, process, -1);
532 
533     port = nxt_runtime_port_find(rt, msg->port_msg.pid,
534                                  msg->port_msg.reply_port);
535 
536     if (nxt_fast_path(port != NULL)) {
537         nxt_port_socket_write(task, port, NXT_PORT_MSG_RPC_ERROR,
538                               -1, msg->port_msg.stream, 0, NULL);
539     }
540 
541 close_fds:
542 
543     nxt_fd_close(msg->fd[0]);
544     msg->fd[0] = -1;
545 
546     nxt_fd_close(msg->fd[1]);
547     msg->fd[1] = -1;
548 }
549 
550 
551 static void
552 nxt_main_process_created_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
553 {
554     nxt_port_t     *port;
555     nxt_process_t  *process;
556     nxt_runtime_t  *rt;
557 
558     rt = task->thread->runtime;
559 
560     port = nxt_runtime_port_find(rt, msg->port_msg.pid,
561                                  msg->port_msg.reply_port);
562     if (nxt_slow_path(port == NULL)) {
563         return;
564     }
565 
566     process = port->process;
567 
568     nxt_assert(process != NULL);
569     nxt_assert(process->state == NXT_PROCESS_STATE_CREATING);
570 
571 #if (NXT_HAVE_CLONE && NXT_HAVE_CLONE_NEWUSER)
572     if (nxt_is_clone_flag_set(process->isolation.clone.flags, NEWUSER)) {
573         if (nxt_slow_path(nxt_clone_credential_map(task, process->pid,
574                                                    process->user_cred,
575                                                    &process->isolation.clone)
576                           != NXT_OK))
577         {
578             (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_RPC_ERROR,
579                                          -1, msg->port_msg.stream, 0, NULL);
580             return;
581         }
582      }
583 
584 #endif
585 
586     process->state = NXT_PROCESS_STATE_CREATED;
587 
588     (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_RPC_READY_LAST,
589                                  -1, msg->port_msg.stream, 0, NULL);
590 }
591 
592 
593 static nxt_port_handlers_t  nxt_main_process_port_handlers = {
594     .data             = nxt_main_data_handler,
595     .new_port         = nxt_main_new_port_handler,
596     .process_created  = nxt_main_process_created_handler,
597     .process_ready    = nxt_port_process_ready_handler,
598     .whoami           = nxt_main_process_whoami_handler,
599     .remove_pid       = nxt_port_remove_pid_handler,
600     .start_process    = nxt_main_start_process_handler,
601     .socket           = nxt_main_port_socket_handler,
602     .modules          = nxt_main_port_modules_handler,
603     .conf_store       = nxt_main_port_conf_store_handler,
604 #if (NXT_TLS)
605     .cert_get         = nxt_cert_store_get_handler,
606     .cert_delete      = nxt_cert_store_delete_handler,
607 #endif
608     .access_log       = nxt_main_port_access_log_handler,
609     .rpc_ready        = nxt_port_rpc_handler,
610     .rpc_error        = nxt_port_rpc_handler,
611 };
612 
613 
614 static void
615 nxt_main_process_whoami_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
616 {
617     nxt_buf_t      *buf;
618     nxt_pid_t      pid, ppid;
619     nxt_port_t     *port;
620     nxt_runtime_t  *rt;
621     nxt_process_t  *pprocess;
622 
623     nxt_assert(msg->port_msg.reply_port == 0);
624 
625     if (nxt_slow_path(msg->buf == NULL
626         || nxt_buf_used_size(msg->buf) != sizeof(nxt_pid_t)))
627     {
628         nxt_alert(task, "whoami: buffer is NULL or unexpected size");
629         goto fail;
630     }
631 
632     nxt_memcpy(&ppid, msg->buf->mem.pos, sizeof(nxt_pid_t));
633 
634     rt = task->thread->runtime;
635 
636     pprocess = nxt_runtime_process_find(rt, ppid);
637     if (nxt_slow_path(pprocess == NULL)) {
638         nxt_alert(task, "whoami: parent process %PI not found", ppid);
639         goto fail;
640     }
641 
642     pid = nxt_recv_msg_cmsg_pid(msg);
643 
644     nxt_debug(task, "whoami: from %PI, parent %PI, fd %d", pid, ppid,
645               msg->fd[0]);
646 
647     if (msg->fd[0] != -1) {
648         port = nxt_runtime_process_port_create(task, rt, pid, 0,
649                                                NXT_PROCESS_APP);
650         if (nxt_slow_path(port == NULL)) {
651             goto fail;
652         }
653 
654         nxt_fd_nonblocking(task, msg->fd[0]);
655 
656         port->pair[0] = -1;
657         port->pair[1] = msg->fd[0];
658         msg->fd[0] = -1;
659 
660         port->max_size = 16 * 1024;
661         port->max_share = 64 * 1024;
662         port->socket.task = task;
663 
664         nxt_port_write_enable(task, port);
665 
666     } else {
667         port = nxt_runtime_port_find(rt, pid, 0);
668         if (nxt_slow_path(port == NULL)) {
669             goto fail;
670         }
671     }
672 
673     if (ppid != nxt_pid) {
674         nxt_queue_insert_tail(&pprocess->children, &port->process->link);
675     }
676 
677     buf = nxt_buf_mem_alloc(task->thread->engine->mem_pool,
678                             sizeof(nxt_pid_t), 0);
679     if (nxt_slow_path(buf == NULL)) {
680         goto fail;
681     }
682 
683     buf->mem.free = nxt_cpymem(buf->mem.free, &pid, sizeof(nxt_pid_t));
684 
685     (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_RPC_READY_LAST, -1,
686                                  msg->port_msg.stream, 0, buf);
687 
688 fail:
689 
690     if (msg->fd[0] != -1) {
691         nxt_fd_close(msg->fd[0]);
692     }
693 }
694 
695 
696 static nxt_int_t
697 nxt_main_process_port_create(nxt_task_t *task, nxt_runtime_t *rt)
698 {
699     nxt_int_t      ret;
700     nxt_port_t     *port;
701     nxt_process_t  *process;
702 
703     port = nxt_runtime_process_port_create(task, rt, nxt_pid, 0,
704                                            NXT_PROCESS_MAIN);
705     if (nxt_slow_path(port == NULL)) {
706         return NXT_ERROR;
707     }
708 
709     process = port->process;
710 
711     ret = nxt_port_socket_init(task, port, 0);
712     if (nxt_slow_path(ret != NXT_OK)) {
713         nxt_port_use(task, port, -1);
714         return ret;
715     }
716 
717     /*
718      * A main process port.  A write port is not closed
719      * since it should be inherited by processes.
720      */
721     nxt_port_enable(task, port, &nxt_main_process_port_handlers);
722 
723     process->state = NXT_PROCESS_STATE_READY;
724 
725     return NXT_OK;
726 }
727 
728 
729 static void
730 nxt_main_process_title(nxt_task_t *task)
731 {
732     u_char      *p, *end;
733     nxt_uint_t  i;
734     u_char      title[2048];
735 
736     end = title + sizeof(title) - 1;
737 
738     p = nxt_sprintf(title, end, "unit: main v" NXT_VERSION " [%s",
739                     nxt_process_argv[0]);
740 
741     for (i = 1; nxt_process_argv[i] != NULL; i++) {
742         p = nxt_sprintf(p, end, " %s", nxt_process_argv[i]);
743     }
744 
745     if (p < end) {
746         *p++ = ']';
747     }
748 
749     *p = '\0';
750 
751     nxt_process_title(task, "%s", title);
752 }
753 
754 
755 static void
756 nxt_main_process_sigterm_handler(nxt_task_t *task, void *obj, void *data)
757 {
758     nxt_debug(task, "sigterm handler signo:%d (%s)",
759               (int) (uintptr_t) obj, data);
760 
761     /* TODO: fast exit. */
762 
763     nxt_exiting = 1;
764 
765     nxt_runtime_quit(task, 0);
766 }
767 
768 
769 static void
770 nxt_main_process_sigquit_handler(nxt_task_t *task, void *obj, void *data)
771 {
772     nxt_debug(task, "sigquit handler signo:%d (%s)",
773               (int) (uintptr_t) obj, data);
774 
775     /* TODO: graceful exit. */
776 
777     nxt_exiting = 1;
778 
779     nxt_runtime_quit(task, 0);
780 }
781 
782 
783 static void
784 nxt_main_process_sigusr1_handler(nxt_task_t *task, void *obj, void *data)
785 {
786     nxt_mp_t        *mp;
787     nxt_int_t       ret;
788     nxt_uint_t      n;
789     nxt_port_t      *port;
790     nxt_file_t      *file, *new_file;
791     nxt_array_t     *new_files;
792     nxt_runtime_t   *rt;
793 
794     nxt_log(task, NXT_LOG_NOTICE, "signal %d (%s) recevied, %s",
795             (int) (uintptr_t) obj, data, "log files rotation");
796 
797     rt = task->thread->runtime;
798 
799     port = rt->port_by_type[NXT_PROCESS_ROUTER];
800 
801     if (nxt_fast_path(port != NULL)) {
802         (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_ACCESS_LOG,
803                                      -1, 0, 0, NULL);
804     }
805 
806     mp = nxt_mp_create(1024, 128, 256, 32);
807     if (mp == NULL) {
808         return;
809     }
810 
811     n = nxt_list_nelts(rt->log_files);
812 
813     new_files = nxt_array_create(mp, n, sizeof(nxt_file_t));
814     if (new_files == NULL) {
815         nxt_mp_destroy(mp);
816         return;
817     }
818 
819     nxt_list_each(file, rt->log_files) {
820 
821         /* This allocation cannot fail. */
822         new_file = nxt_array_add(new_files);
823 
824         new_file->name = file->name;
825         new_file->fd = NXT_FILE_INVALID;
826         new_file->log_level = NXT_LOG_ALERT;
827 
828         ret = nxt_file_open(task, new_file, O_WRONLY | O_APPEND, O_CREAT,
829                             NXT_FILE_OWNER_ACCESS);
830 
831         if (ret != NXT_OK) {
832             goto fail;
833         }
834 
835     } nxt_list_loop;
836 
837     new_file = new_files->elts;
838 
839     ret = nxt_file_stderr(&new_file[0]);
840 
841     if (ret == NXT_OK) {
842         n = 0;
843 
844         nxt_list_each(file, rt->log_files) {
845 
846             nxt_port_change_log_file(task, rt, n, new_file[n].fd);
847             /*
848              * The old log file descriptor must be closed at the moment
849              * when no other threads use it.  dup2() allows to use the
850              * old file descriptor for new log file.  This change is
851              * performed atomically in the kernel.
852              */
853             (void) nxt_file_redirect(file, new_file[n].fd);
854 
855             n++;
856 
857         } nxt_list_loop;
858 
859         nxt_mp_destroy(mp);
860         return;
861     }
862 
863 fail:
864 
865     new_file = new_files->elts;
866     n = new_files->nelts;
867 
868     while (n != 0) {
869         if (new_file->fd != NXT_FILE_INVALID) {
870             nxt_file_close(task, new_file);
871         }
872 
873         new_file++;
874         n--;
875     }
876 
877     nxt_mp_destroy(mp);
878 }
879 
880 
881 static void
882 nxt_main_process_sigchld_handler(nxt_task_t *task, void *obj, void *data)
883 {
884     int                 status;
885     nxt_int_t           ret;
886     nxt_err_t           err;
887     nxt_pid_t           pid;
888     nxt_port_t          *port;
889     nxt_queue_t         children;
890     nxt_runtime_t       *rt;
891     nxt_process_t       *process, *child;
892     nxt_process_init_t  init;
893 
894     nxt_debug(task, "sigchld handler signo:%d (%s)",
895               (int) (uintptr_t) obj, data);
896 
897     rt = task->thread->runtime;
898 
899     for ( ;; ) {
900         pid = waitpid(-1, &status, WNOHANG);
901 
902         if (pid == -1) {
903 
904             switch (err = nxt_errno) {
905 
906             case NXT_ECHILD:
907                 return;
908 
909             case NXT_EINTR:
910                 continue;
911 
912             default:
913                 nxt_alert(task, "waitpid() failed: %E", err);
914                 return;
915             }
916         }
917 
918         nxt_debug(task, "waitpid(): %PI", pid);
919 
920         if (pid == 0) {
921             return;
922         }
923 
924         if (WTERMSIG(status)) {
925 #ifdef WCOREDUMP
926             nxt_alert(task, "process %PI exited on signal %d%s",
927                       pid, WTERMSIG(status),
928                       WCOREDUMP(status) ? " (core dumped)" : "");
929 #else
930             nxt_alert(task, "process %PI exited on signal %d",
931                       pid, WTERMSIG(status));
932 #endif
933 
934         } else {
935             nxt_trace(task, "process %PI exited with code %d",
936                       pid, WEXITSTATUS(status));
937         }
938 
939         process = nxt_runtime_process_find(rt, pid);
940 
941         if (process != NULL) {
942             nxt_main_process_cleanup(task, process);
943 
944             if (process->state == NXT_PROCESS_STATE_READY) {
945                 process->stream = 0;
946             }
947 
948             nxt_queue_init(&children);
949 
950             if (!nxt_queue_is_empty(&process->children)) {
951                 nxt_queue_add(&children, &process->children);
952 
953                 nxt_queue_init(&process->children);
954 
955                 nxt_queue_each(child, &children, nxt_process_t, link) {
956                     port = nxt_process_port_first(child);
957 
958                     (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_QUIT,
959                                                  -1, 0, 0, NULL);
960                 } nxt_queue_loop;
961             }
962 
963             if (nxt_exiting) {
964                 nxt_process_close_ports(task, process);
965 
966                 nxt_queue_each(child, &children, nxt_process_t, link) {
967                     nxt_queue_remove(&child->link);
968                     child->link.next = NULL;
969 
970                     nxt_process_close_ports(task, child);
971                 } nxt_queue_loop;
972 
973                 if (rt->nprocesses <= 1) {
974                     nxt_runtime_quit(task, 0);
975                 }
976 
977                 return;
978             }
979 
980             nxt_port_remove_notify_others(task, process);
981 
982             nxt_queue_each(child, &children, nxt_process_t, link) {
983                 nxt_port_remove_notify_others(task, child);
984 
985                 nxt_queue_remove(&child->link);
986                 child->link.next = NULL;
987 
988                 nxt_process_close_ports(task, child);
989             } nxt_queue_loop;
990 
991             init = *(nxt_process_init_t *) nxt_process_init(process);
992 
993             nxt_process_close_ports(task, process);
994 
995             if (init.restart) {
996                 ret = nxt_process_init_start(task, init);
997                 if (nxt_slow_path(ret == NXT_ERROR)) {
998                     nxt_alert(task, "failed to restart %s", init.name);
999                 }
1000             }
1001         }
1002     }
1003 }
1004 
1005 
1006 static void
1007 nxt_main_process_signal_handler(nxt_task_t *task, void *obj, void *data)
1008 {
1009     nxt_trace(task, "signal signo:%d (%s) recevied, ignored",
1010               (int) (uintptr_t) obj, data);
1011 }
1012 
1013 
1014 static void
1015 nxt_main_process_cleanup(nxt_task_t *task, nxt_process_t *process)
1016 {
1017     if (process->isolation.cleanup != NULL) {
1018         process->isolation.cleanup(task, process);
1019     }
1020 }
1021 
1022 
1023 static void
1024 nxt_main_port_socket_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
1025 {
1026     size_t                  size;
1027     nxt_int_t               ret;
1028     nxt_buf_t               *b, *out;
1029     nxt_port_t              *port;
1030     nxt_sockaddr_t          *sa;
1031     nxt_port_msg_type_t     type;
1032     nxt_listening_socket_t  ls;
1033     u_char                  message[2048];
1034 
1035     port = nxt_runtime_port_find(task->thread->runtime, msg->port_msg.pid,
1036                                  msg->port_msg.reply_port);
1037     if (nxt_slow_path(port == NULL)) {
1038         return;
1039     }
1040 
1041     if (nxt_slow_path(port->type != NXT_PROCESS_ROUTER)) {
1042         nxt_alert(task, "process %PI cannot create listener sockets",
1043                   msg->port_msg.pid);
1044 
1045         return;
1046     }
1047 
1048     b = msg->buf;
1049     sa = (nxt_sockaddr_t *) b->mem.pos;
1050 
1051     /* TODO check b size and make plain */
1052 
1053     ls.socket = -1;
1054     ls.error = NXT_SOCKET_ERROR_SYSTEM;
1055     ls.start = message;
1056     ls.end = message + sizeof(message);
1057 
1058     nxt_debug(task, "listening socket \"%*s\"",
1059               (size_t) sa->length, nxt_sockaddr_start(sa));
1060 
1061     ret = nxt_main_listening_socket(sa, &ls);
1062 
1063     if (ret == NXT_OK) {
1064         nxt_debug(task, "socket(\"%*s\"): %d",
1065                   (size_t) sa->length, nxt_sockaddr_start(sa), ls.socket);
1066 
1067         out = NULL;
1068 
1069         type = NXT_PORT_MSG_RPC_READY_LAST | NXT_PORT_MSG_CLOSE_FD;
1070 
1071     } else {
1072         size = ls.end - ls.start;
1073 
1074         nxt_alert(task, "%*s", size, ls.start);
1075 
1076         out = nxt_buf_mem_ts_alloc(task, task->thread->engine->mem_pool,
1077                                    size + 1);
1078         if (nxt_fast_path(out != NULL)) {
1079             *out->mem.free++ = (uint8_t) ls.error;
1080 
1081             out->mem.free = nxt_cpymem(out->mem.free, ls.start, size);
1082         }
1083 
1084         type = NXT_PORT_MSG_RPC_ERROR;
1085     }
1086 
1087     nxt_port_socket_write(task, port, type, ls.socket, msg->port_msg.stream,
1088                           0, out);
1089 }
1090 
1091 
1092 static nxt_int_t
1093 nxt_main_listening_socket(nxt_sockaddr_t *sa, nxt_listening_socket_t *ls)
1094 {
1095     nxt_err_t         err;
1096     nxt_socket_t      s;
1097 
1098     const socklen_t   length = sizeof(int);
1099     static const int  enable = 1;
1100 
1101     s = socket(sa->u.sockaddr.sa_family, sa->type, 0);
1102 
1103     if (nxt_slow_path(s == -1)) {
1104         err = nxt_errno;
1105 
1106 #if (NXT_INET6)
1107 
1108         if (err == EAFNOSUPPORT && sa->u.sockaddr.sa_family == AF_INET6) {
1109             ls->error = NXT_SOCKET_ERROR_NOINET6;
1110         }
1111 
1112 #endif
1113 
1114         ls->end = nxt_sprintf(ls->start, ls->end,
1115                               "socket(\\\"%*s\\\") failed %E",
1116                               (size_t) sa->length, nxt_sockaddr_start(sa), err);
1117 
1118         return NXT_ERROR;
1119     }
1120 
1121     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &enable, length) != 0) {
1122         ls->end = nxt_sprintf(ls->start, ls->end,
1123                               "setsockopt(\\\"%*s\\\", SO_REUSEADDR) failed %E",
1124                               (size_t) sa->length, nxt_sockaddr_start(sa),
1125                               nxt_errno);
1126         goto fail;
1127     }
1128 
1129 #if (NXT_INET6)
1130 
1131     if (sa->u.sockaddr.sa_family == AF_INET6) {
1132 
1133         if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &enable, length) != 0) {
1134             ls->end = nxt_sprintf(ls->start, ls->end,
1135                                "setsockopt(\\\"%*s\\\", IPV6_V6ONLY) failed %E",
1136                                (size_t) sa->length, nxt_sockaddr_start(sa),
1137                                nxt_errno);
1138             goto fail;
1139         }
1140     }
1141 
1142 #endif
1143 
1144     if (bind(s, &sa->u.sockaddr, sa->socklen) != 0) {
1145         err = nxt_errno;
1146 
1147 #if (NXT_HAVE_UNIX_DOMAIN)
1148 
1149         if (sa->u.sockaddr.sa_family == AF_UNIX) {
1150             switch (err) {
1151 
1152             case EACCES:
1153                 ls->error = NXT_SOCKET_ERROR_ACCESS;
1154                 break;
1155 
1156             case ENOENT:
1157             case ENOTDIR:
1158                 ls->error = NXT_SOCKET_ERROR_PATH;
1159                 break;
1160             }
1161 
1162         } else
1163 #endif
1164         {
1165             switch (err) {
1166 
1167             case EACCES:
1168                 ls->error = NXT_SOCKET_ERROR_PORT;
1169                 break;
1170 
1171             case EADDRINUSE:
1172                 ls->error = NXT_SOCKET_ERROR_INUSE;
1173                 break;
1174 
1175             case EADDRNOTAVAIL:
1176                 ls->error = NXT_SOCKET_ERROR_NOADDR;
1177                 break;
1178             }
1179         }
1180 
1181         ls->end = nxt_sprintf(ls->start, ls->end, "bind(\\\"%*s\\\") failed %E",
1182                               (size_t) sa->length, nxt_sockaddr_start(sa), err);
1183         goto fail;
1184     }
1185 
1186 #if (NXT_HAVE_UNIX_DOMAIN)
1187 
1188     if (sa->u.sockaddr.sa_family == AF_UNIX) {
1189         char     *filename;
1190         mode_t   access;
1191 
1192         filename = sa->u.sockaddr_un.sun_path;
1193         access = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
1194 
1195         if (chmod(filename, access) != 0) {
1196             ls->end = nxt_sprintf(ls->start, ls->end,
1197                                   "chmod(\\\"%s\\\") failed %E",
1198                                   filename, nxt_errno);
1199             goto fail;
1200         }
1201     }
1202 
1203 #endif
1204 
1205     ls->socket = s;
1206 
1207     return NXT_OK;
1208 
1209 fail:
1210 
1211     (void) close(s);
1212 
1213     return NXT_ERROR;
1214 }
1215 
1216 
1217 static nxt_conf_map_t  nxt_app_lang_module_map[] = {
1218     {
1219         nxt_string("type"),
1220         NXT_CONF_MAP_INT,
1221         offsetof(nxt_app_lang_module_t, type),
1222     },
1223 
1224     {
1225         nxt_string("version"),
1226         NXT_CONF_MAP_CSTRZ,
1227         offsetof(nxt_app_lang_module_t, version),
1228     },
1229 
1230     {
1231         nxt_string("file"),
1232         NXT_CONF_MAP_CSTRZ,
1233         offsetof(nxt_app_lang_module_t, file),
1234     },
1235 };
1236 
1237 
1238 static nxt_conf_map_t  nxt_app_lang_mounts_map[] = {
1239     {
1240         nxt_string("src"),
1241         NXT_CONF_MAP_CSTRZ,
1242         offsetof(nxt_fs_mount_t, src),
1243     },
1244     {
1245         nxt_string("dst"),
1246         NXT_CONF_MAP_CSTRZ,
1247         offsetof(nxt_fs_mount_t, dst),
1248     },
1249     {
1250         nxt_string("name"),
1251         NXT_CONF_MAP_CSTRZ,
1252         offsetof(nxt_fs_mount_t, name),
1253     },
1254     {
1255         nxt_string("type"),
1256         NXT_CONF_MAP_INT,
1257         offsetof(nxt_fs_mount_t, type),
1258     },
1259     {
1260         nxt_string("flags"),
1261         NXT_CONF_MAP_INT,
1262         offsetof(nxt_fs_mount_t, flags),
1263     },
1264     {
1265         nxt_string("data"),
1266         NXT_CONF_MAP_CSTRZ,
1267         offsetof(nxt_fs_mount_t, data),
1268     },
1269 };
1270 
1271 
1272 static void
1273 nxt_main_port_modules_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
1274 {
1275     uint32_t               index, jindex, nmounts;
1276     nxt_mp_t               *mp;
1277     nxt_int_t              ret;
1278     nxt_buf_t              *b;
1279     nxt_port_t             *port;
1280     nxt_runtime_t          *rt;
1281     nxt_fs_mount_t         *mnt;
1282     nxt_conf_value_t       *conf, *root, *value, *mounts;
1283     nxt_app_lang_module_t  *lang;
1284 
1285     static nxt_str_t root_path = nxt_string("/");
1286     static nxt_str_t mounts_name = nxt_string("mounts");
1287 
1288     rt = task->thread->runtime;
1289 
1290     if (msg->port_msg.pid != rt->port_by_type[NXT_PROCESS_DISCOVERY]->pid) {
1291         nxt_alert(task, "process %PI cannot send modules", msg->port_msg.pid);
1292         return;
1293     }
1294 
1295     if (nxt_exiting) {
1296         nxt_debug(task, "ignoring discovered modules, exiting");
1297         return;
1298     }
1299 
1300     port = nxt_runtime_port_find(task->thread->runtime, msg->port_msg.pid,
1301                                  msg->port_msg.reply_port);
1302 
1303     if (nxt_fast_path(port != NULL)) {
1304         (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_RPC_ERROR, -1,
1305                                      msg->port_msg.stream, 0, NULL);
1306     }
1307 
1308     b = msg->buf;
1309 
1310     if (b == NULL) {
1311         return;
1312     }
1313 
1314     mp = nxt_mp_create(1024, 128, 256, 32);
1315     if (mp == NULL) {
1316         return;
1317     }
1318 
1319     b = nxt_buf_chk_make_plain(mp, b, msg->size);
1320 
1321     if (b == NULL) {
1322         return;
1323     }
1324 
1325     nxt_debug(task, "application languages: \"%*s\"",
1326               b->mem.free - b->mem.pos, b->mem.pos);
1327 
1328     conf = nxt_conf_json_parse(mp, b->mem.pos, b->mem.free, NULL);
1329     if (conf == NULL) {
1330         goto fail;
1331     }
1332 
1333     root = nxt_conf_get_path(conf, &root_path);
1334     if (root == NULL) {
1335         goto fail;
1336     }
1337 
1338     for (index = 0; /* void */ ; index++) {
1339         value = nxt_conf_get_array_element(root, index);
1340         if (value == NULL) {
1341             break;
1342         }
1343 
1344         lang = nxt_array_zero_add(rt->languages);
1345         if (lang == NULL) {
1346             goto fail;
1347         }
1348 
1349         lang->module = NULL;
1350 
1351         ret = nxt_conf_map_object(rt->mem_pool, value, nxt_app_lang_module_map,
1352                                   nxt_nitems(nxt_app_lang_module_map), lang);
1353 
1354         if (ret != NXT_OK) {
1355             goto fail;
1356         }
1357 
1358         mounts = nxt_conf_get_object_member(value, &mounts_name, NULL);
1359         if (mounts == NULL) {
1360             nxt_alert(task, "missing mounts from discovery message.");
1361             goto fail;
1362         }
1363 
1364         if (nxt_conf_type(mounts) != NXT_CONF_ARRAY) {
1365             nxt_alert(task, "invalid mounts type from discovery message.");
1366             goto fail;
1367         }
1368 
1369         nmounts = nxt_conf_array_elements_count(mounts);
1370 
1371         lang->mounts = nxt_array_create(rt->mem_pool, nmounts,
1372                                         sizeof(nxt_fs_mount_t));
1373 
1374         if (lang->mounts == NULL) {
1375             goto fail;
1376         }
1377 
1378         for (jindex = 0; /* */; jindex++) {
1379             value = nxt_conf_get_array_element(mounts, jindex);
1380             if (value == NULL) {
1381                 break;
1382             }
1383 
1384             mnt = nxt_array_zero_add(lang->mounts);
1385             if (mnt == NULL) {
1386                 goto fail;
1387             }
1388 
1389             mnt->builtin = 1;
1390             mnt->deps = 1;
1391 
1392             ret = nxt_conf_map_object(rt->mem_pool, value,
1393                                       nxt_app_lang_mounts_map,
1394                                       nxt_nitems(nxt_app_lang_mounts_map), mnt);
1395 
1396             if (ret != NXT_OK) {
1397                 goto fail;
1398             }
1399         }
1400 
1401         nxt_debug(task, "lang %d %s \"%s\" (%d mounts)",
1402                   lang->type, lang->version, lang->file, lang->mounts->nelts);
1403     }
1404 
1405     qsort(rt->languages->elts, rt->languages->nelts,
1406           sizeof(nxt_app_lang_module_t), nxt_app_lang_compare);
1407 
1408 fail:
1409 
1410     nxt_mp_destroy(mp);
1411 
1412     ret = nxt_process_init_start(task, nxt_controller_process);
1413     if (ret == NXT_OK) {
1414         ret = nxt_process_init_start(task, nxt_router_process);
1415     }
1416 
1417     if (nxt_slow_path(ret == NXT_ERROR)) {
1418         nxt_exiting = 1;
1419 
1420         nxt_runtime_quit(task, 1);
1421     }
1422 }
1423 
1424 
1425 static int nxt_cdecl
1426 nxt_app_lang_compare(const void *v1, const void *v2)
1427 {
1428     int                          n;
1429     const nxt_app_lang_module_t  *lang1, *lang2;
1430 
1431     lang1 = v1;
1432     lang2 = v2;
1433 
1434     n = lang1->type - lang2->type;
1435 
1436     if (n != 0) {
1437         return n;
1438     }
1439 
1440     n = nxt_strverscmp(lang1->version, lang2->version);
1441 
1442     /* Negate result to move higher versions to the beginning. */
1443 
1444     return -n;
1445 }
1446 
1447 
1448 static void
1449 nxt_main_port_conf_store_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
1450 {
1451     void           *p;
1452     size_t         n, size;
1453     nxt_int_t      ret;
1454     nxt_port_t     *ctl_port;
1455     nxt_runtime_t  *rt;
1456     u_char         ver[NXT_INT_T_LEN];
1457 
1458     rt = task->thread->runtime;
1459 
1460     ctl_port = rt->port_by_type[NXT_PROCESS_CONTROLLER];
1461 
1462     if (nxt_slow_path(msg->port_msg.pid != ctl_port->pid)) {
1463         nxt_alert(task, "process %PI cannot store conf", msg->port_msg.pid);
1464         return;
1465     }
1466 
1467     p = MAP_FAILED;
1468 
1469     /*
1470      * Ancient compilers like gcc 4.8.5 on CentOS 7 wants 'size' to be
1471      * initialized in 'cleanup' section.
1472      */
1473     size = 0;
1474 
1475     if (nxt_slow_path(msg->fd[0] == -1)) {
1476         nxt_alert(task, "conf_store_handler: invalid shm fd");
1477         goto error;
1478     }
1479 
1480     if (nxt_buf_mem_used_size(&msg->buf->mem) != sizeof(size_t)) {
1481         nxt_alert(task, "conf_store_handler: unexpected buffer size (%d)",
1482                   (int) nxt_buf_mem_used_size(&msg->buf->mem));
1483         goto error;
1484     }
1485 
1486     nxt_memcpy(&size, msg->buf->mem.pos, sizeof(size_t));
1487 
1488     p = nxt_mem_mmap(NULL, size, PROT_READ, MAP_SHARED, msg->fd[0], 0);
1489 
1490     nxt_fd_close(msg->fd[0]);
1491     msg->fd[0] = -1;
1492 
1493     if (nxt_slow_path(p == MAP_FAILED)) {
1494         goto error;
1495     }
1496 
1497     nxt_debug(task, "conf_store_handler(%uz): %*s", size, size, p);
1498 
1499     if (nxt_conf_ver != NXT_VERNUM) {
1500         n = nxt_sprintf(ver, ver + NXT_INT_T_LEN, "%d", NXT_VERNUM) - ver;
1501 
1502         ret = nxt_main_file_store(task, rt->ver_tmp, rt->ver, ver, n);
1503         if (nxt_slow_path(ret != NXT_OK)) {
1504             goto error;
1505         }
1506 
1507         nxt_conf_ver = NXT_VERNUM;
1508     }
1509 
1510     ret = nxt_main_file_store(task, rt->conf_tmp, rt->conf, p, size);
1511 
1512     if (nxt_fast_path(ret == NXT_OK)) {
1513         goto cleanup;
1514     }
1515 
1516 error:
1517 
1518     nxt_alert(task, "failed to store current configuration");
1519 
1520 cleanup:
1521 
1522     if (p != MAP_FAILED) {
1523         nxt_mem_munmap(p, size);
1524     }
1525 
1526     if (msg->fd[0] != -1) {
1527         nxt_fd_close(msg->fd[0]);
1528         msg->fd[0] = -1;
1529     }
1530 }
1531 
1532 
1533 static nxt_int_t
1534 nxt_main_file_store(nxt_task_t *task, const char *tmp_name, const char *name,
1535     u_char *buf, size_t size)
1536 {
1537     ssize_t     n;
1538     nxt_int_t   ret;
1539     nxt_file_t  file;
1540 
1541     nxt_memzero(&file, sizeof(nxt_file_t));
1542 
1543     file.name = (nxt_file_name_t *) name;
1544 
1545     ret = nxt_file_open(task, &file, NXT_FILE_WRONLY, NXT_FILE_TRUNCATE,
1546                         NXT_FILE_OWNER_ACCESS);
1547     if (nxt_slow_path(ret != NXT_OK)) {
1548         return NXT_ERROR;
1549     }
1550 
1551     n = nxt_file_write(&file, buf, size, 0);
1552 
1553     nxt_file_close(task, &file);
1554 
1555     if (nxt_slow_path(n != (ssize_t) size)) {
1556         (void) nxt_file_delete(file.name);
1557         return NXT_ERROR;
1558     }
1559 
1560     return nxt_file_rename(file.name, (nxt_file_name_t *) name);
1561 }
1562 
1563 
1564 static void
1565 nxt_main_port_access_log_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
1566 {
1567     u_char               *path;
1568     nxt_int_t            ret;
1569     nxt_file_t           file;
1570     nxt_port_t           *port;
1571     nxt_port_msg_type_t  type;
1572 
1573     nxt_debug(task, "opening access log file");
1574 
1575     path = msg->buf->mem.pos;
1576 
1577     nxt_memzero(&file, sizeof(nxt_file_t));
1578 
1579     file.name = (nxt_file_name_t *) path;
1580     file.log_level = NXT_LOG_ERR;
1581 
1582     ret = nxt_file_open(task, &file, O_WRONLY | O_APPEND, O_CREAT,
1583                         NXT_FILE_OWNER_ACCESS);
1584 
1585     type = (ret == NXT_OK) ? NXT_PORT_MSG_RPC_READY_LAST | NXT_PORT_MSG_CLOSE_FD
1586                            : NXT_PORT_MSG_RPC_ERROR;
1587 
1588     port = nxt_runtime_port_find(task->thread->runtime, msg->port_msg.pid,
1589                                  msg->port_msg.reply_port);
1590 
1591     if (nxt_fast_path(port != NULL)) {
1592         (void) nxt_port_socket_write(task, port, type, file.fd,
1593                                      msg->port_msg.stream, 0, NULL);
1594     }
1595 }
1596