2 * work_fork.c - fork implementation for blocking worker child.
5 #include "ntp_workimpl.h"
14 #include "ntp_stdlib.h"
15 #include "ntp_malloc.h"
16 #include "ntp_syslog.h"
19 #include "ntp_assert.h"
20 #include "ntp_unixtime.h"
21 #include "ntp_worker.h"
23 /* === variables === */
25 addremove_io_fd_func addremove_io_fd;
26 static volatile int worker_sighup_received;
30 /* === function prototypes === */
31 static void fork_blocking_child(blocking_child *);
32 static RETSIGTYPE worker_sighup(int);
33 static void send_worker_home_atexit(void);
34 static void cleanup_after_child(blocking_child *);
36 /* === I/O helpers === */
37 /* Since we have signals enabled, there's a good chance that blocking IO
38 * via pipe suffers from EINTR -- and this goes for both directions.
39 * The next two wrappers will loop until either all the data is written
40 * or read, plus handling the EOF condition on read. They may return
41 * zero if no data was transferred at all, and effectively every return
42 * value that differs from the given transfer length signifies an error
61 } else if (r == 0 || errno != EINTR) {
65 return (size_t)(b - (char *)vb);
84 } else if (errno != EINTR) {
88 return (size_t)(b - (const char *)vb);
92 /* === functions === */
96 * On some systems _exit() is preferred to exit() for forked children.
97 * For example, http://netbsd.gw.com/cgi-bin/man-cgi?fork++NetBSD-5.0
98 * recommends _exit() to avoid double-flushing C runtime stream buffers
99 * and also to avoid calling the parent's atexit() routines in the
100 * child. On those systems WORKER_CHILD_EXIT is _exit. Since _exit
101 * bypasses CRT cleanup, fflush() files we know might have output
109 if (syslog_file != NULL)
113 WORKER_CHILD_EXIT (exitcode); /* space before ( required */
123 worker_sighup_received = 1;
135 sleep_remain = (u_int)seconds;
137 if (!worker_sighup_received)
138 sleep_remain = sleep(sleep_remain);
139 if (worker_sighup_received) {
140 TRACE(1, ("worker SIGHUP with %us left to sleep",
142 worker_sighup_received = 0;
145 } while (sleep_remain);
152 interrupt_worker_sleep(void)
158 for (idx = 0; idx < blocking_children_alloc; idx++) {
159 c = blocking_children[idx];
161 if (NULL == c || c->reusable == TRUE)
164 rc = kill(c->pid, SIGHUP);
167 "Unable to signal HUP to wake child pid %d: %m",
174 * harvest_child_status() runs in the parent.
176 * Note the error handling -- this is an interaction with SIGCHLD.
177 * SIG_IGN on SIGCHLD on some OSes means do not wait but reap
178 * automatically. Since we're not really interested in the result code,
179 * we simply ignore the error.
182 harvest_child_status(
187 /* Wait on the child so it can finish terminating */
188 if (waitpid(c->pid, NULL, 0) == c->pid)
189 TRACE(4, ("harvested child %d\n", c->pid));
190 else if (errno != ECHILD)
191 msyslog(LOG_ERR, "error waiting on child %d: %m", c->pid);
197 * req_child_exit() runs in the parent.
204 if (-1 != c->req_write_pipe) {
205 close(c->req_write_pipe);
206 c->req_write_pipe = -1;
209 /* Closing the pipe forces the child to exit */
210 harvest_child_status(c);
216 * cleanup_after_child() runs in parent.
223 harvest_child_status(c);
224 if (-1 != c->resp_read_pipe) {
225 (*addremove_io_fd)(c->resp_read_pipe, c->ispipe, TRUE);
226 close(c->resp_read_pipe);
227 c->resp_read_pipe = -1;
229 c->resp_read_ctx = NULL;
230 DEBUG_INSIST(-1 == c->req_read_pipe);
231 DEBUG_INSIST(-1 == c->resp_write_pipe);
237 send_worker_home_atexit(void)
245 for (idx = 0; idx < blocking_children_alloc; idx++) {
246 c = blocking_children[idx];
255 send_blocking_req_internal(
257 blocking_pipe_header * hdr,
264 DEBUG_REQUIRE(hdr != NULL);
265 DEBUG_REQUIRE(data != NULL);
266 DEBUG_REQUIRE(BLOCKING_REQ_MAGIC == hdr->magic_sig);
268 if (-1 == c->req_write_pipe) {
269 fork_blocking_child(c);
270 DEBUG_INSIST(-1 != c->req_write_pipe);
273 octets = sizeof(*hdr);
274 rc = netwrite(c->req_write_pipe, hdr, octets);
277 octets = hdr->octets - sizeof(*hdr);
278 rc = netwrite(c->req_write_pipe, data, octets);
284 "send_blocking_req_internal: short write (%zu of %zu), %m",
287 /* Fatal error. Clean up the child process. */
289 exit(1); /* otherwise would be return -1 */
293 blocking_pipe_header *
294 receive_blocking_req_internal(
298 blocking_pipe_header hdr;
299 blocking_pipe_header * req;
303 DEBUG_REQUIRE(-1 != c->req_read_pipe);
306 rc = netread(c->req_read_pipe, &hdr, sizeof(hdr));
309 TRACE(4, ("parent closed request pipe, child %d terminating\n",
311 } else if (rc != sizeof(hdr)) {
313 "receive_blocking_req_internal: short header read (%zu of %zu), %m",
316 INSIST(sizeof(hdr) < hdr.octets && hdr.octets < 4 * 1024);
317 req = emalloc(hdr.octets);
318 memcpy(req, &hdr, sizeof(*req));
319 octets = hdr.octets - sizeof(hdr);
320 rc = netread(c->req_read_pipe, (char *)(req + 1),
325 "receive_blocking_req_internal: short read (%zu of %zu), %m",
327 else if (BLOCKING_REQ_MAGIC != req->magic_sig)
329 "receive_blocking_req_internal: packet header mismatch (0x%x)",
343 send_blocking_resp_internal(
345 blocking_pipe_header * resp
351 DEBUG_REQUIRE(-1 != c->resp_write_pipe);
353 octets = resp->octets;
354 rc = netwrite(c->resp_write_pipe, resp, octets);
360 TRACE(1, ("send_blocking_resp_internal: short write (%zu of %zu), %m\n",
366 blocking_pipe_header *
367 receive_blocking_resp_internal(
371 blocking_pipe_header hdr;
372 blocking_pipe_header * resp;
376 DEBUG_REQUIRE(c->resp_read_pipe != -1);
379 rc = netread(c->resp_read_pipe, &hdr, sizeof(hdr));
382 /* this is the normal child exited indication */
383 } else if (rc != sizeof(hdr)) {
384 TRACE(1, ("receive_blocking_resp_internal: short header read (%zu of %zu), %m\n",
386 } else if (BLOCKING_RESP_MAGIC != hdr.magic_sig) {
387 TRACE(1, ("receive_blocking_resp_internal: header mismatch (0x%x)\n",
390 INSIST(sizeof(hdr) < hdr.octets &&
391 hdr.octets < 16 * 1024);
392 resp = emalloc(hdr.octets);
393 memcpy(resp, &hdr, sizeof(*resp));
394 octets = hdr.octets - sizeof(hdr);
395 rc = netread(c->resp_read_pipe, (char *)(resp + 1),
399 TRACE(1, ("receive_blocking_resp_internal: short read (%zu of %zu), %m\n",
405 cleanup_after_child(c);
414 #if defined(HAVE_DROPROOT) && defined(WORK_FORK)
416 fork_deferred_worker(void)
421 REQUIRE(droproot && root_dropped);
423 for (idx = 0; idx < blocking_children_alloc; idx++) {
424 c = blocking_children[idx];
427 if (-1 != c->req_write_pipe && 0 == c->pid)
428 fork_blocking_child(c);
439 static int atexit_installed;
440 static int blocking_pipes[4] = { -1, -1, -1, -1 };
450 * parent and child communicate via a pair of pipes.
452 * 0 child read request
453 * 1 parent write request
454 * 2 parent read response
455 * 3 child write response
457 if (-1 == c->req_write_pipe) {
458 rc = pipe_socketpair(&blocking_pipes[0], &was_pipe);
462 rc = pipe_socketpair(&blocking_pipes[2], &is_pipe);
465 close(blocking_pipes[0]);
466 close(blocking_pipes[1]);
468 INSIST(was_pipe == is_pipe);
473 msyslog(LOG_ERR, "unable to create worker pipes: %m");
478 * Move the descriptors the parent will keep open out of the
479 * low descriptors preferred by C runtime buffered FILE *.
481 c->req_write_pipe = move_fd(blocking_pipes[1]);
482 c->resp_read_pipe = move_fd(blocking_pipes[2]);
484 * wake any worker child on orderly shutdown of the
485 * daemon so that it can notice the broken pipes and
488 if (!atexit_installed) {
489 atexit(&send_worker_home_atexit);
490 atexit_installed = TRUE;
494 #if defined(HAVE_DROPROOT) && !defined(NEED_EARLY_FORK)
495 /* defer the fork until after root is dropped */
496 if (droproot && !root_dropped)
499 if (syslog_file != NULL)
504 /* [BUG 3050] setting SIGCHLD to SIG_IGN likely causes unwanted
505 * or undefined effects. We don't do it and leave SIGCHLD alone.
507 /* signal_no_reset(SIGCHLD, SIG_IGN); */
510 if (-1 == childpid) {
511 msyslog(LOG_ERR, "unable to fork worker: %m");
516 /* this is the parent */
517 TRACE(1, ("forked worker child (pid %d)\n", childpid));
521 /* close the child's pipe descriptors. */
522 close(blocking_pipes[0]);
523 close(blocking_pipes[3]);
525 memset(blocking_pipes, -1, sizeof(blocking_pipes));
527 /* wire into I/O loop */
528 (*addremove_io_fd)(c->resp_read_pipe, is_pipe, FALSE);
530 return; /* parent returns */
534 * The parent gets the child pid as the return value of fork().
535 * The child must work for it.
538 worker_process = TRUE;
541 * Change the process name of the child to avoid confusion
542 * about ntpd trunning twice.
544 if (saved_argc != 0) {
548 for (argcc = 0; argcc < saved_argc; argcc++) {
549 int l = strlen(saved_argv[argcc]);
551 memset(saved_argv[argcc], 0, l);
553 strlcpy(saved_argv[0], "ntpd: asynchronous dns resolver", argvlen);
557 * In the child, close all files except stdin, stdout, stderr,
558 * and the two child ends of the pipes.
560 DEBUG_INSIST(-1 == c->req_read_pipe);
561 DEBUG_INSIST(-1 == c->resp_write_pipe);
562 c->req_read_pipe = blocking_pipes[0];
563 c->resp_write_pipe = blocking_pipes[3];
567 if (syslog_file != NULL) {
572 keep_fd = max(c->req_read_pipe, c->resp_write_pipe);
573 for (fd = 3; fd < keep_fd; fd++)
574 if (fd != c->req_read_pipe &&
575 fd != c->resp_write_pipe)
577 close_all_beyond(keep_fd);
579 * We get signals from refclock serial I/O on NetBSD in the
580 * worker if we do not reset SIGIO's handler to the default.
581 * It is not conditionalized for NetBSD alone because on
582 * systems where it is not needed, it is harmless, and that
583 * allows us to handle unknown others with NetBSD behavior.
586 #if defined(USE_SIGIO)
587 signal_no_reset(SIGIO, SIG_DFL);
588 #elif defined(USE_SIGPOLL)
589 signal_no_reset(SIGPOLL, SIG_DFL);
591 signal_no_reset(SIGHUP, worker_sighup);
592 init_logging("ntp_intres", 0, FALSE);
596 * And now back to the portable code
598 exit_worker(blocking_child_common(c));
602 void worker_global_lock(int inOrOut)
607 #else /* !WORK_FORK follows */
608 char work_fork_nonempty_compilation_unit;