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 int set_user_group_ids(void);
94 /* === functions === */
98 * On some systems _exit() is preferred to exit() for forked children.
99 * For example, http://netbsd.gw.com/cgi-bin/man-cgi?fork++NetBSD-5.0
100 * recommends _exit() to avoid double-flushing C runtime stream buffers
101 * and also to avoid calling the parent's atexit() routines in the
102 * child. On those systems WORKER_CHILD_EXIT is _exit. Since _exit
103 * bypasses CRT cleanup, fflush() files we know might have output
111 if (syslog_file != NULL)
115 WORKER_CHILD_EXIT (exitcode); /* space before ( required */
125 worker_sighup_received = 1;
137 sleep_remain = (u_int)seconds;
139 if (!worker_sighup_received)
140 sleep_remain = sleep(sleep_remain);
141 if (worker_sighup_received) {
142 TRACE(1, ("worker SIGHUP with %us left to sleep",
144 worker_sighup_received = 0;
147 } while (sleep_remain);
154 interrupt_worker_sleep(void)
160 for (idx = 0; idx < blocking_children_alloc; idx++) {
161 c = blocking_children[idx];
163 if (NULL == c || c->reusable == TRUE)
166 rc = kill(c->pid, SIGHUP);
169 "Unable to signal HUP to wake child pid %d: %m",
176 * harvest_child_status() runs in the parent.
178 * Note the error handling -- this is an interaction with SIGCHLD.
179 * SIG_IGN on SIGCHLD on some OSes means do not wait but reap
180 * automatically. Since we're not really interested in the result code,
181 * we simply ignore the error.
184 harvest_child_status(
189 /* Wait on the child so it can finish terminating */
190 if (waitpid(c->pid, NULL, 0) == c->pid)
191 TRACE(4, ("harvested child %d\n", c->pid));
192 else if (errno != ECHILD)
193 msyslog(LOG_ERR, "error waiting on child %d: %m", c->pid);
199 * req_child_exit() runs in the parent.
206 if (-1 != c->req_write_pipe) {
207 close(c->req_write_pipe);
208 c->req_write_pipe = -1;
211 /* Closing the pipe forces the child to exit */
212 harvest_child_status(c);
218 * cleanup_after_child() runs in parent.
225 harvest_child_status(c);
226 if (-1 != c->resp_read_pipe) {
227 (*addremove_io_fd)(c->resp_read_pipe, c->ispipe, TRUE);
228 close(c->resp_read_pipe);
229 c->resp_read_pipe = -1;
231 c->resp_read_ctx = NULL;
232 DEBUG_INSIST(-1 == c->req_read_pipe);
233 DEBUG_INSIST(-1 == c->resp_write_pipe);
239 send_worker_home_atexit(void)
247 for (idx = 0; idx < blocking_children_alloc; idx++) {
248 c = blocking_children[idx];
257 send_blocking_req_internal(
259 blocking_pipe_header * hdr,
266 DEBUG_REQUIRE(hdr != NULL);
267 DEBUG_REQUIRE(data != NULL);
268 DEBUG_REQUIRE(BLOCKING_REQ_MAGIC == hdr->magic_sig);
270 if (-1 == c->req_write_pipe) {
271 fork_blocking_child(c);
272 DEBUG_INSIST(-1 != c->req_write_pipe);
275 octets = sizeof(*hdr);
276 rc = netwrite(c->req_write_pipe, hdr, octets);
279 octets = hdr->octets - sizeof(*hdr);
280 rc = netwrite(c->req_write_pipe, data, octets);
286 "send_blocking_req_internal: short write (%zu of %zu), %m",
289 /* Fatal error. Clean up the child process. */
291 exit(1); /* otherwise would be return -1 */
295 blocking_pipe_header *
296 receive_blocking_req_internal(
300 blocking_pipe_header hdr;
301 blocking_pipe_header * req;
305 DEBUG_REQUIRE(-1 != c->req_read_pipe);
308 rc = netread(c->req_read_pipe, &hdr, sizeof(hdr));
311 TRACE(4, ("parent closed request pipe, child %d terminating\n",
313 } else if (rc != sizeof(hdr)) {
315 "receive_blocking_req_internal: short header read (%zu of %zu), %m",
318 INSIST(sizeof(hdr) < hdr.octets && hdr.octets < 4 * 1024);
319 req = emalloc(hdr.octets);
320 memcpy(req, &hdr, sizeof(*req));
321 octets = hdr.octets - sizeof(hdr);
322 rc = netread(c->req_read_pipe, (char *)(req + 1),
327 "receive_blocking_req_internal: short read (%zu of %zu), %m",
329 else if (BLOCKING_REQ_MAGIC != req->magic_sig)
331 "receive_blocking_req_internal: packet header mismatch (0x%x)",
345 send_blocking_resp_internal(
347 blocking_pipe_header * resp
353 DEBUG_REQUIRE(-1 != c->resp_write_pipe);
355 octets = resp->octets;
356 rc = netwrite(c->resp_write_pipe, resp, octets);
362 TRACE(1, ("send_blocking_resp_internal: short write (%zu of %zu), %m\n",
368 blocking_pipe_header *
369 receive_blocking_resp_internal(
373 blocking_pipe_header hdr;
374 blocking_pipe_header * resp;
378 DEBUG_REQUIRE(c->resp_read_pipe != -1);
381 rc = netread(c->resp_read_pipe, &hdr, sizeof(hdr));
384 /* this is the normal child exited indication */
385 } else if (rc != sizeof(hdr)) {
386 TRACE(1, ("receive_blocking_resp_internal: short header read (%zu of %zu), %m\n",
388 } else if (BLOCKING_RESP_MAGIC != hdr.magic_sig) {
389 TRACE(1, ("receive_blocking_resp_internal: header mismatch (0x%x)\n",
392 INSIST(sizeof(hdr) < hdr.octets &&
393 hdr.octets < 16 * 1024);
394 resp = emalloc(hdr.octets);
395 memcpy(resp, &hdr, sizeof(*resp));
396 octets = hdr.octets - sizeof(hdr);
397 rc = netread(c->resp_read_pipe, (char *)(resp + 1),
401 TRACE(1, ("receive_blocking_resp_internal: short read (%zu of %zu), %m\n",
407 cleanup_after_child(c);
416 #if defined(HAVE_DROPROOT) && defined(WORK_FORK)
418 fork_deferred_worker(void)
423 REQUIRE(droproot && root_dropped);
425 for (idx = 0; idx < blocking_children_alloc; idx++) {
426 c = blocking_children[idx];
429 if (-1 != c->req_write_pipe && 0 == c->pid)
430 fork_blocking_child(c);
441 static int atexit_installed;
442 static int blocking_pipes[4] = { -1, -1, -1, -1 };
452 * parent and child communicate via a pair of pipes.
454 * 0 child read request
455 * 1 parent write request
456 * 2 parent read response
457 * 3 child write response
459 if (-1 == c->req_write_pipe) {
460 rc = pipe_socketpair(&blocking_pipes[0], &was_pipe);
464 rc = pipe_socketpair(&blocking_pipes[2], &is_pipe);
467 close(blocking_pipes[0]);
468 close(blocking_pipes[1]);
470 INSIST(was_pipe == is_pipe);
475 msyslog(LOG_ERR, "unable to create worker pipes: %m");
480 * Move the descriptors the parent will keep open out of the
481 * low descriptors preferred by C runtime buffered FILE *.
483 c->req_write_pipe = move_fd(blocking_pipes[1]);
484 c->resp_read_pipe = move_fd(blocking_pipes[2]);
486 * wake any worker child on orderly shutdown of the
487 * daemon so that it can notice the broken pipes and
490 if (!atexit_installed) {
491 atexit(&send_worker_home_atexit);
492 atexit_installed = TRUE;
496 #if defined(HAVE_DROPROOT) && !defined(NEED_EARLY_FORK)
497 /* defer the fork until after root is dropped */
498 if (droproot && !root_dropped)
501 if (syslog_file != NULL)
506 /* [BUG 3050] setting SIGCHLD to SIG_IGN likely causes unwanted
507 * or undefined effects. We don't do it and leave SIGCHLD alone.
509 /* signal_no_reset(SIGCHLD, SIG_IGN); */
512 if (-1 == childpid) {
513 msyslog(LOG_ERR, "unable to fork worker: %m");
518 /* this is the parent */
519 TRACE(1, ("forked worker child (pid %d)\n", childpid));
523 /* close the child's pipe descriptors. */
524 close(blocking_pipes[0]);
525 close(blocking_pipes[3]);
527 memset(blocking_pipes, -1, sizeof(blocking_pipes));
529 /* wire into I/O loop */
530 (*addremove_io_fd)(c->resp_read_pipe, is_pipe, FALSE);
532 return; /* parent returns */
536 * The parent gets the child pid as the return value of fork().
537 * The child must work for it.
540 worker_process = TRUE;
543 * Change the process name of the child to avoid confusion
544 * about ntpd trunning twice.
546 if (saved_argc != 0) {
550 for (argcc = 0; argcc < saved_argc; argcc++) {
551 int l = strlen(saved_argv[argcc]);
553 memset(saved_argv[argcc], 0, l);
555 strlcpy(saved_argv[0], "ntpd: asynchronous dns resolver", argvlen);
559 * In the child, close all files except stdin, stdout, stderr,
560 * and the two child ends of the pipes.
562 DEBUG_INSIST(-1 == c->req_read_pipe);
563 DEBUG_INSIST(-1 == c->resp_write_pipe);
564 c->req_read_pipe = blocking_pipes[0];
565 c->resp_write_pipe = blocking_pipes[3];
569 if (syslog_file != NULL) {
574 keep_fd = max(c->req_read_pipe, c->resp_write_pipe);
575 for (fd = 3; fd < keep_fd; fd++)
576 if (fd != c->req_read_pipe &&
577 fd != c->resp_write_pipe)
579 close_all_beyond(keep_fd);
581 * We get signals from refclock serial I/O on NetBSD in the
582 * worker if we do not reset SIGIO's handler to the default.
583 * It is not conditionalized for NetBSD alone because on
584 * systems where it is not needed, it is harmless, and that
585 * allows us to handle unknown others with NetBSD behavior.
588 #if defined(USE_SIGIO)
589 signal_no_reset(SIGIO, SIG_DFL);
590 #elif defined(USE_SIGPOLL)
591 signal_no_reset(SIGPOLL, SIG_DFL);
593 signal_no_reset(SIGHUP, worker_sighup);
594 init_logging("ntp_intres", 0, FALSE);
597 (void) set_user_group_ids();
600 * And now back to the portable code
602 exit_worker(blocking_child_common(c));
606 void worker_global_lock(int inOrOut)
611 #else /* !WORK_FORK follows */
612 char work_fork_nonempty_compilation_unit;