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;
28 /* === function prototypes === */
29 static void fork_blocking_child(blocking_child *);
30 static RETSIGTYPE worker_sighup(int);
31 static void send_worker_home_atexit(void);
32 static void cleanup_after_child(blocking_child *);
34 /* === functions === */
38 * On some systems _exit() is preferred to exit() for forked children.
39 * For example, http://netbsd.gw.com/cgi-bin/man-cgi?fork++NetBSD-5.0
40 * recommends _exit() to avoid double-flushing C runtime stream buffers
41 * and also to avoid calling the parent's atexit() routines in the
42 * child. On those systems WORKER_CHILD_EXIT is _exit. Since _exit
43 * bypasses CRT cleanup, fflush() files we know might have output
51 if (syslog_file != NULL)
55 WORKER_CHILD_EXIT (exitcode); /* space before ( required */
65 worker_sighup_received = 1;
77 sleep_remain = (u_int)seconds;
79 if (!worker_sighup_received)
80 sleep_remain = sleep(sleep_remain);
81 if (worker_sighup_received) {
82 TRACE(1, ("worker SIGHUP with %us left to sleep",
84 worker_sighup_received = 0;
87 } while (sleep_remain);
94 interrupt_worker_sleep(void)
100 for (idx = 0; idx < blocking_children_alloc; idx++) {
101 c = blocking_children[idx];
103 if (NULL == c || c->reusable == TRUE)
106 rc = kill(c->pid, SIGHUP);
109 "Unable to signal HUP to wake child pid %d: %m",
116 * harvest_child_status() runs in the parent.
119 harvest_child_status(
125 /* Wait on the child so it can finish terminating */
126 if (waitpid(c->pid, NULL, 0) == c->pid)
127 TRACE(4, ("harvested child %d\n", c->pid));
128 else msyslog(LOG_ERR, "error waiting on child %d: %m", c->pid);
133 * req_child_exit() runs in the parent.
140 if (-1 != c->req_write_pipe) {
141 close(c->req_write_pipe);
142 c->req_write_pipe = -1;
145 /* Closing the pipe forces the child to exit */
146 harvest_child_status(c);
152 * cleanup_after_child() runs in parent.
159 harvest_child_status(c);
160 if (-1 != c->resp_read_pipe) {
161 (*addremove_io_fd)(c->resp_read_pipe, c->ispipe, TRUE);
162 close(c->resp_read_pipe);
163 c->resp_read_pipe = -1;
166 c->resp_read_ctx = NULL;
167 DEBUG_INSIST(-1 == c->req_read_pipe);
168 DEBUG_INSIST(-1 == c->resp_write_pipe);
174 send_worker_home_atexit(void)
182 for (idx = 0; idx < blocking_children_alloc; idx++) {
183 c = blocking_children[idx];
192 send_blocking_req_internal(
194 blocking_pipe_header * hdr,
201 DEBUG_REQUIRE(hdr != NULL);
202 DEBUG_REQUIRE(data != NULL);
203 DEBUG_REQUIRE(BLOCKING_REQ_MAGIC == hdr->magic_sig);
205 if (-1 == c->req_write_pipe) {
206 fork_blocking_child(c);
207 DEBUG_INSIST(-1 != c->req_write_pipe);
210 octets = sizeof(*hdr);
211 rc = write(c->req_write_pipe, hdr, octets);
214 octets = hdr->octets - sizeof(*hdr);
215 rc = write(c->req_write_pipe, data, octets);
223 "send_blocking_req_internal: pipe write: %m");
226 "send_blocking_req_internal: short write %d of %d",
229 /* Fatal error. Clean up the child process. */
231 exit(1); /* otherwise would be return -1 */
235 blocking_pipe_header *
236 receive_blocking_req_internal(
240 blocking_pipe_header hdr;
241 blocking_pipe_header * req;
245 DEBUG_REQUIRE(-1 != c->req_read_pipe);
250 rc = read(c->req_read_pipe, &hdr, sizeof(hdr));
251 } while (rc < 0 && EINTR == errno);
255 "receive_blocking_req_internal: pipe read %m");
256 } else if (0 == rc) {
257 TRACE(4, ("parent closed request pipe, child %d terminating\n",
259 } else if (rc != sizeof(hdr)) {
261 "receive_blocking_req_internal: short header read %d of %lu",
262 rc, (u_long)sizeof(hdr));
264 INSIST(sizeof(hdr) < hdr.octets && hdr.octets < 4 * 1024);
265 req = emalloc(hdr.octets);
266 memcpy(req, &hdr, sizeof(*req));
267 octets = hdr.octets - sizeof(hdr);
268 rc = read(c->req_read_pipe, (char *)req + sizeof(*req),
273 "receive_blocking_req_internal: pipe data read %m");
274 else if (rc != octets)
276 "receive_blocking_req_internal: short read %d of %ld",
278 else if (BLOCKING_REQ_MAGIC != req->magic_sig)
280 "receive_blocking_req_internal: packet header mismatch (0x%x)",
294 send_blocking_resp_internal(
296 blocking_pipe_header * resp
302 DEBUG_REQUIRE(-1 != c->resp_write_pipe);
304 octets = resp->octets;
305 rc = write(c->resp_write_pipe, resp, octets);
312 TRACE(1, ("send_blocking_resp_internal: pipe write %m\n"));
314 TRACE(1, ("send_blocking_resp_internal: short write %d of %ld\n",
321 blocking_pipe_header *
322 receive_blocking_resp_internal(
326 blocking_pipe_header hdr;
327 blocking_pipe_header * resp;
331 DEBUG_REQUIRE(c->resp_read_pipe != -1);
334 rc = read(c->resp_read_pipe, &hdr, sizeof(hdr));
337 TRACE(1, ("receive_blocking_resp_internal: pipe read %m\n"));
338 } else if (0 == rc) {
339 /* this is the normal child exited indication */
340 } else if (rc != sizeof(hdr)) {
341 TRACE(1, ("receive_blocking_resp_internal: short header read %d of %lu\n",
342 rc, (u_long)sizeof(hdr)));
343 } else if (BLOCKING_RESP_MAGIC != hdr.magic_sig) {
344 TRACE(1, ("receive_blocking_resp_internal: header mismatch (0x%x)\n",
347 INSIST(sizeof(hdr) < hdr.octets &&
348 hdr.octets < 16 * 1024);
349 resp = emalloc(hdr.octets);
350 memcpy(resp, &hdr, sizeof(*resp));
351 octets = hdr.octets - sizeof(hdr);
352 rc = read(c->resp_read_pipe,
353 (char *)resp + sizeof(*resp),
357 TRACE(1, ("receive_blocking_resp_internal: pipe data read %m\n"));
358 else if (rc < octets)
359 TRACE(1, ("receive_blocking_resp_internal: short read %d of %ld\n",
365 cleanup_after_child(c);
374 #if defined(HAVE_DROPROOT) && defined(WORK_FORK)
376 fork_deferred_worker(void)
381 REQUIRE(droproot && root_dropped);
383 for (idx = 0; idx < blocking_children_alloc; idx++) {
384 c = blocking_children[idx];
387 if (-1 != c->req_write_pipe && 0 == c->pid)
388 fork_blocking_child(c);
399 static int atexit_installed;
400 static int blocking_pipes[4] = { -1, -1, -1, -1 };
410 * parent and child communicate via a pair of pipes.
412 * 0 child read request
413 * 1 parent write request
414 * 2 parent read response
415 * 3 child write response
417 if (-1 == c->req_write_pipe) {
418 rc = pipe_socketpair(&blocking_pipes[0], &was_pipe);
422 rc = pipe_socketpair(&blocking_pipes[2], &is_pipe);
425 close(blocking_pipes[0]);
426 close(blocking_pipes[1]);
428 INSIST(was_pipe == is_pipe);
433 msyslog(LOG_ERR, "unable to create worker pipes: %m");
438 * Move the descriptors the parent will keep open out of the
439 * low descriptors preferred by C runtime buffered FILE *.
441 c->req_write_pipe = move_fd(blocking_pipes[1]);
442 c->resp_read_pipe = move_fd(blocking_pipes[2]);
444 * wake any worker child on orderly shutdown of the
445 * daemon so that it can notice the broken pipes and
448 if (!atexit_installed) {
449 atexit(&send_worker_home_atexit);
450 atexit_installed = TRUE;
455 /* defer the fork until after root is dropped */
456 if (droproot && !root_dropped)
459 if (syslog_file != NULL)
464 signal_no_reset(SIGCHLD, SIG_IGN);
467 if (-1 == childpid) {
468 msyslog(LOG_ERR, "unable to fork worker: %m");
473 /* this is the parent */
474 TRACE(1, ("forked worker child (pid %d)\n", childpid));
478 /* close the child's pipe descriptors. */
479 close(blocking_pipes[0]);
480 close(blocking_pipes[3]);
482 memset(blocking_pipes, -1, sizeof(blocking_pipes));
484 /* wire into I/O loop */
485 (*addremove_io_fd)(c->resp_read_pipe, is_pipe, FALSE);
487 return; /* parent returns */
491 * The parent gets the child pid as the return value of fork().
492 * The child must work for it.
495 worker_process = TRUE;
498 * In the child, close all files except stdin, stdout, stderr,
499 * and the two child ends of the pipes.
501 DEBUG_INSIST(-1 == c->req_read_pipe);
502 DEBUG_INSIST(-1 == c->resp_write_pipe);
503 c->req_read_pipe = blocking_pipes[0];
504 c->resp_write_pipe = blocking_pipes[3];
508 if (syslog_file != NULL) {
513 keep_fd = max(c->req_read_pipe, c->resp_write_pipe);
514 for (fd = 3; fd < keep_fd; fd++)
515 if (fd != c->req_read_pipe &&
516 fd != c->resp_write_pipe)
518 close_all_beyond(keep_fd);
520 * We get signals from refclock serial I/O on NetBSD in the
521 * worker if we do not reset SIGIO's handler to the default.
522 * It is not conditionalized for NetBSD alone because on
523 * systems where it is not needed, it is harmless, and that
524 * allows us to handle unknown others with NetBSD behavior.
527 #if defined(USE_SIGIO)
528 signal_no_reset(SIGIO, SIG_DFL);
529 #elif defined(USE_SIGPOLL)
530 signal_no_reset(SIGPOLL, SIG_DFL);
532 signal_no_reset(SIGHUP, worker_sighup);
533 init_logging("ntp_intres", 0, FALSE);
537 * And now back to the portable code
539 exit_worker(blocking_child_common(c));
543 #else /* !WORK_FORK follows */
544 char work_fork_nonempty_compilation_unit;