From nobody Thu Mar 23 03:13:39 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Phr5b2qyHz40Rwf; Thu, 23 Mar 2023 03:13:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Phr5b2KWxz4LlM; Thu, 23 Mar 2023 03:13:39 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1679541219; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=cagzUigZkRrrp8zSMNsdpGhUTXK/OA2LYmlIfCw0LQs=; b=w31qtybr/qsuhclX3/k59+jXXs0K7XvinZ65oFkyTouo5ilp4YX6G0UsJidGh5g8ZxRMGk OT5xZi+Dc6KnwpKQF6S6SLVDOWoVBQ8jCaO1y0OyoxfZvgec4ABHfSyhrBFs96RFYosYxZ XNHDBy5fM+OXMzw9zxPw2kwT0bq8ReGCaMhWRFf/8REluWxF9hXe7OOpX+ZuTTxpAmL1Um xTnsSlPeawRPjttoTLQzWdQbYD3nTpBG6s1IiMFLHVAJ89QReL+CSCQFAxgjkgitiwkDs3 mEsB//XTenYJitT/34zjzb8rBxYN2Ko5h1E0qO0HFcKc4s84Q41JRCYP0FsQmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1679541219; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=cagzUigZkRrrp8zSMNsdpGhUTXK/OA2LYmlIfCw0LQs=; b=dHNFd3omo1ybddxjYPkRZwupU1d2NitTCszmegAOopxK8Vu+JaggY7LXFwFgFDepBT6c3p 8urwPJ1r10KujFzkJroGXHRm0B3SuwCWbnpmjCf6/g+mE4G4fB4fl3LNpzVe9Ub/Aa8Nyi z7sSFPa/ik/8rxRTYo6NKyivqstEeEpr3hDKW9GkFd+rfNgFuIjZxxt6axm5+PdLFAOOeQ Cr1KUTL+98qPv9eIaplV9LY8DMXTphF5aAI6N1RI3+UvEzbynlsIGJM7jnAphlrfHYPXeS JwsPT1K2nSYdu3p0iJdZEvMlm3mo3IV+YlTE0CrbKKNSHMHSwrbEMSmiQ96aeQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1679541219; a=rsa-sha256; cv=none; b=Do5/DteSDjpDjzjPlAtDxNamIqepwjKtGsygjP5XeRSUYn1QYIdtu0VCWjAaGBp8hLyDag EqBD8I32Xqjg96iWzaTsWtzrhMP+vGDNmaHSkHO/PN3U+HwtiVR2ZZJ+IPlqq48EQ2hZNz m6G8HGSuK7U80kIRUeDvOErtm8CmsIHmVS+GVVCKwL1Ifq14xWsyVjUpZuBvxJmAxX3xzD XHVuAuURbAETdlfiDQOzKF+mu06phMwQASIujg9uNPRfRXqvG6vUunHGN+G3GIOkYfh4qx Y7oEVQjhF2uBv8FkgPlDC2sdurqsGbw9bqqFpnlQu5003eeoyeaNcZNTrszksQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Phr5b1N87zKkS; Thu, 23 Mar 2023 03:13:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 32N3DdWw077792; Thu, 23 Mar 2023 03:13:39 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 32N3DdUF077791; Thu, 23 Mar 2023 03:13:39 GMT (envelope-from git) Date: Thu, 23 Mar 2023 03:13:39 GMT Message-Id: <202303230313.32N3DdUF077791@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kyle Evans Subject: git: 4c41f4a0d67f - main - daemon: decouple init logic from main loop List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 4c41f4a0d67fc93cfb07ad5287f02d024d19ef5a Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=4c41f4a0d67fc93cfb07ad5287f02d024d19ef5a commit 4c41f4a0d67fc93cfb07ad5287f02d024d19ef5a Author: Ihor Antonov AuthorDate: 2023-03-23 02:37:12 +0000 Commit: Kyle Evans CommitDate: 2023-03-23 02:37:12 +0000 daemon: decouple init logic from main loop main() func contained both initialization and main loop logic. This made certain operations like restarting problematic and required dirty hacks in form of goto jumps. This commit moves the main loop logic into daemon_eventloop(), cleans up main, and makes restart logic clear: daemon_mainloop() is run in a loop with a restart condition checked at the end. Reviewed by: kevans Pull Request: https://github.com/freebsd/freebsd-src/pull/699 --- usr.sbin/daemon/daemon.c | 195 ++++++++++++++++++++++++++--------------------- 1 file changed, 109 insertions(+), 86 deletions(-) diff --git a/usr.sbin/daemon/daemon.c b/usr.sbin/daemon/daemon.c index 983dbdc7ed8c..4b218c2b53c5 100644 --- a/usr.sbin/daemon/daemon.c +++ b/usr.sbin/daemon/daemon.c @@ -60,7 +60,12 @@ __FBSDID("$FreeBSD$"); #define LBUF_SIZE 4096 struct daemon_state { + sigset_t mask_orig; + sigset_t mask_read; + sigset_t mask_term; + sigset_t mask_susp; int pipe_fd[2]; + char **argv; const char *child_pidfile; const char *parent_pidfile; const char *output_filename; @@ -96,6 +101,7 @@ static void open_pid_files(struct daemon_state *); static void do_output(const unsigned char *, size_t, struct daemon_state *); static void daemon_sleep(time_t, long); static void daemon_state_init(struct daemon_state *); +static void daemon_eventloop(struct daemon_state *); static void daemon_terminate(struct daemon_state *); static volatile sig_atomic_t terminate = 0; @@ -163,47 +169,9 @@ main(int argc, char *argv[]) char *p = NULL; int ch = 0; struct daemon_state state; - sigset_t mask_orig; - sigset_t mask_read; - sigset_t mask_term; - sigset_t mask_susp; daemon_state_init(&state); - /* - * Signal handling logic: - * - * - SIGTERM is masked while there is no child. - * - * - SIGCHLD is masked while reading from the pipe. SIGTERM has to be - * caught, to avoid indefinite blocking on read(). - * - * - Both SIGCHLD and SIGTERM are masked before calling sigsuspend() - * to avoid racing. - * - * - After SIGTERM is recieved and propagated to the child there are - * several options on what to do next: - * - read until EOF - * - read until EOF but only for a while - * - bail immediately - * Currently the third option is used, because otherwise there is no - * guarantee that read() won't block indefinitely if the child refuses - * to depart. To handle the second option, a different approach - * would be needed (procctl()?). - * - * - Child's exit might be detected by receiveing EOF from the pipe. - * But the child might have closed its stdout and stderr, so deamon - * must wait for the SIGCHLD to ensure that the child is actually gone. - */ - sigemptyset(&mask_susp); - sigemptyset(&mask_read); - sigemptyset(&mask_term); - sigemptyset(&mask_orig); - sigaddset(&mask_susp, SIGTERM); - sigaddset(&mask_susp, SIGCHLD); - sigaddset(&mask_term, SIGTERM); - sigaddset(&mask_read, SIGCHLD); - /* * Supervision mode is enabled if one of the following options are used: * --child-pidfile -p @@ -309,6 +277,7 @@ main(int argc, char *argv[]) } argc -= optind; argv += optind; + state.argv = argv; if (argc == 0) { usage(1); @@ -343,8 +312,8 @@ main(int argc, char *argv[]) pidfile_write(state.parent_pidfh); if (state.supervision_enabled) { - /* Block SIGTERM to avoid racing until we have forked. */ - if (sigprocmask(SIG_BLOCK, &mask_term, &mask_orig)) { + /* Block SIGTERM to avoid racing until the child is spawned. */ + if (sigprocmask(SIG_BLOCK, &state.mask_term, &state.mask_orig)) { warn("sigprocmask"); daemon_terminate(&state); } @@ -357,8 +326,50 @@ main(int argc, char *argv[]) * not have superuser privileges. */ (void)madvise(NULL, 0, MADV_PROTECT); -restart: - if (pipe(state.pipe_fd)) { + } + do { + daemon_eventloop(&state); + close(state.pipe_fd[0]); + state.pipe_fd[0] = -1; + } while (state.restart_enabled && !terminate); + + daemon_terminate(&state); +} + + +/* + * Main event loop: fork the child and watch for events. + * In legacy mode simply execve into the target process. + * + * Signal handling logic: + * + * - SIGTERM is masked while there is no child. + * + * - SIGCHLD is masked while reading from the pipe. SIGTERM has to be + * caught, to avoid indefinite blocking on read(). + * + * - Both SIGCHLD and SIGTERM are masked before calling sigsuspend() + * to avoid racing. + * + * - After SIGTERM is recieved and propagated to the child there are + * several options on what to do next: + * - read until EOF + * - read until EOF but only for a while + * - bail immediately + * Currently the third option is used, because otherwise there is no + * guarantee that read() won't block indefinitely if the child refuses + * to depart. To handle the second option, a different approach + * would be needed (procctl()?). + * + * - Child's exit might be detected by receiveing EOF from the pipe. + * But the child might have closed its stdout and stderr, so deamon + * must wait for the SIGCHLD to ensure that the child is actually gone. + */ +static void +daemon_eventloop(struct daemon_state *state) +{ + if (state->supervision_enabled) { + if (pipe(state->pipe_fd)) { err(1, "pipe"); } /* @@ -371,43 +382,43 @@ restart: /* fork failed, this can only happen when supervision is enabled */ if (pid == -1) { warn("fork"); - daemon_terminate(&state); + daemon_terminate(state); } /* fork succeeded, this is child's branch or supervision is disabled */ if (pid == 0) { - pidfile_write(state.child_pidfh); + pidfile_write(state->child_pidfh); - if (state.user != NULL) { - restrict_process(state.user); + if (state->user != NULL) { + restrict_process(state->user); } /* * In supervision mode, the child gets the original sigmask, * and dup'd pipes. */ - if (state.supervision_enabled) { - close(state.pipe_fd[0]); - if (sigprocmask(SIG_SETMASK, &mask_orig, NULL)) { + if (state->supervision_enabled) { + close(state->pipe_fd[0]); + if (sigprocmask(SIG_SETMASK, &state->mask_orig, NULL)) { err(1, "sigprogmask"); } - if (state.stdmask & STDERR_FILENO) { - if (dup2(state.pipe_fd[1], STDERR_FILENO) == -1) { + if (state->stdmask & STDERR_FILENO) { + if (dup2(state->pipe_fd[1], STDERR_FILENO) == -1) { err(1, "dup2"); } } - if (state.stdmask & STDOUT_FILENO) { - if (dup2(state.pipe_fd[1], STDOUT_FILENO) == -1) { + if (state->stdmask & STDOUT_FILENO) { + if (dup2(state->pipe_fd[1], STDOUT_FILENO) == -1) { err(1, "dup2"); } } - if (state.pipe_fd[1] != STDERR_FILENO && - state.pipe_fd[1] != STDOUT_FILENO) { - close(state.pipe_fd[1]); + if (state->pipe_fd[1] != STDERR_FILENO && + state->pipe_fd[1] != STDOUT_FILENO) { + close(state->pipe_fd[1]); } } - execvp(argv[0], argv); + execvp(state->argv[0], state->argv); /* execvp() failed - report error and exit this process */ - err(1, "%s", argv[0]); + err(1, "%s", state->argv[0]); } /* @@ -417,64 +428,65 @@ restart: * * Unblock SIGTERM - now there is a valid child PID to signal to. */ - if (sigprocmask(SIG_UNBLOCK, &mask_term, NULL)) { + if (sigprocmask(SIG_UNBLOCK, &state->mask_term, NULL)) { warn("sigprocmask"); - daemon_terminate(&state); + daemon_terminate(state); } - close(state.pipe_fd[1]); - state.pipe_fd[1] = -1; + close(state->pipe_fd[1]); + state->pipe_fd[1] = -1; - setproctitle("%s[%d]", state.title, (int)pid); + setproctitle("%s[%d]", state->title, (int)pid); for (;;) { - if (child_gone && state.child_eof) { + if (child_gone && state->child_eof) { break; } if (terminate) { - daemon_terminate(&state); + daemon_terminate(state); } - if (state.child_eof) { - if (sigprocmask(SIG_BLOCK, &mask_susp, NULL)) { + if (state->child_eof) { + if (sigprocmask(SIG_BLOCK, &state->mask_susp, NULL)) { warn("sigprocmask"); - daemon_terminate(&state); + daemon_terminate(state); } while (!terminate && !child_gone) { - sigsuspend(&mask_orig); + sigsuspend(&state->mask_orig); } - if (sigprocmask(SIG_UNBLOCK, &mask_susp, NULL)) { + if (sigprocmask(SIG_UNBLOCK, &state->mask_susp, NULL)) { warn("sigprocmask"); - daemon_terminate(&state); + daemon_terminate(state); } continue; } - if (sigprocmask(SIG_BLOCK, &mask_read, NULL)) { + if (sigprocmask(SIG_BLOCK, &state->mask_read, NULL)) { warn("sigprocmask"); - daemon_terminate(&state); + daemon_terminate(state); } - state.child_eof = !listen_child(state.pipe_fd[0], &state); + state->child_eof = !listen_child(state->pipe_fd[0], state); - if (sigprocmask(SIG_UNBLOCK, &mask_read, NULL)) { + if (sigprocmask(SIG_UNBLOCK, &state->mask_read, NULL)) { warn("sigprocmask"); - daemon_terminate(&state); + daemon_terminate(state); } } - if (state.restart_enabled && !terminate) { - daemon_sleep(state.restart_delay, 0); - } - if (sigprocmask(SIG_BLOCK, &mask_term, NULL)) { + + /* + * At the end of the loop the the child is already gone. + * Block SIGTERM to avoid racing until the child is spawned. + */ + if (sigprocmask(SIG_BLOCK, &state->mask_term, NULL)) { warn("sigprocmask"); - daemon_terminate(&state); + daemon_terminate(state); } - if (state.restart_enabled && !terminate) { - close(state.pipe_fd[0]); - state.pipe_fd[0] = -1; - goto restart; + + /* sleep before exiting mainloop if restart is enabled */ + if (state->restart_enabled && !terminate) { + daemon_sleep(state->restart_delay, 0); } - daemon_terminate(&state); } static void @@ -746,6 +758,7 @@ daemon_state_init(struct daemon_state *state) { *state = (struct daemon_state) { .pipe_fd = { -1, -1 }, + .argv = NULL, .parent_pidfh = NULL, .child_pidfh = NULL, .child_pidfile = NULL, @@ -767,6 +780,16 @@ daemon_state_init(struct daemon_state *state) .output_fd = -1, .output_filename = NULL, }; + + sigemptyset(&state->mask_susp); + sigemptyset(&state->mask_read); + sigemptyset(&state->mask_term); + sigemptyset(&state->mask_orig); + sigaddset(&state->mask_susp, SIGTERM); + sigaddset(&state->mask_susp, SIGCHLD); + sigaddset(&state->mask_term, SIGTERM); + sigaddset(&state->mask_read, SIGCHLD); + } static _Noreturn void