From nobody Sat Mar 18 05:53:41 2023 X-Original-To: dev-commits-src-main@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 4PdqtY32Zbz3y8H7; Sat, 18 Mar 2023 05:53:41 +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 4PdqtY2H7Jz3FJ3; Sat, 18 Mar 2023 05:53:41 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1679118821; 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=Yg1uegF9aXREBa9w1tPDQyLXnDItO09oFq81aKwWgaI=; b=fPnciP7OXBPXecfgpK/Ig4KpvwSBYO5RI1apLGeQ0vVptxlwQKMbJ6ElesNZv071kHhcg8 VftWY+r+Zr8lE8L6Kzj0uaRf5NAsiGUIiWNoLIOISVLWK30LzpiEaPOtP246K2Gq4efNIy Qw+YT6D1DFPxULCH6wvZZe/FM04QNUY9MVlZfyhlo97q8DNI2cHsWdGpJ+o6kJ5e08/pXw IGL16DluibEFGjmw2DF+7Kq/wgrrJPFD+klK94YocoqgmjcYNJhkPQmsdXkPSPKFPf2BB3 2ONs2VwJV7ysVSHHZkvDcEZUrAVusIygY4YqnXMkreAukrt8En8sfFZ03OJQnA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1679118821; 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=Yg1uegF9aXREBa9w1tPDQyLXnDItO09oFq81aKwWgaI=; b=AMZqh/mtnkbDvloxYmL3GVdm7r40lQLLKGuXG+k+WWOF6Yfxx6QszEZodDsLG+k/DRWe5f lzIUxKiR0mBqI+71XxUHhevZg3ggufKA6BstlDCbSbPWZlyCx2i7X5iSxS8NJW1M5vi05N TfP7tjmGPK/7br9h+cHh6dOaVKiz9gI87OvBN+JlqKlRvtXH+1EyntFWgB4mJqkQuzyREl 73JhZiuIzB05Emy+T6UcOQfU9K/D91SJ6BgTETwKaAPlAjKtl6EfIWgaswCg485n54lWRj C6g2Rxeq9W6Zsg0AvSbBCBSdWuQNqhGZ04HI0deoSAZGLg2D8VkuKFSp7becjA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1679118821; a=rsa-sha256; cv=none; b=f/VmXdWa6ABjZc4bM/D73eCuxbPJMjH5kTsNf9NE9mXHFN7KiMa/e00fkWfFvfoFDYBk8V 9RxMlBuI2baHqJB5Bn5z7WXBMNA31qVEQ3pQn2z9dCuVaPQbcitUSSXCEo9mwFjrQKINy8 hDBhiv1QNgpV8DAwjS2d5TKtJ4fIq52EuHmGIauijSoN28feshvalvzMyIs5/SLTe+BMMA JjLsqmRtg5Nx2oJRARYhfkrxJJ9gTvWUm4muJ+X8N8TSSAUmbEqSZ2H548Cf9P+JDKS+2c scHE4WetPVTxQEfb62xRMoH/ll9laLo8ZWAUk9PPf95pvv45yiuRgRX62JYD9w== 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 4PdqtY1LNVzwVR; Sat, 18 Mar 2023 05:53:41 +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 32I5rfCj088925; Sat, 18 Mar 2023 05:53:41 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 32I5rfRu088924; Sat, 18 Mar 2023 05:53:41 GMT (envelope-from git) Date: Sat, 18 Mar 2023 05:53:41 GMT Message-Id: <202303180553.32I5rfRu088924@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: cf6356fd471c - main - daemon: repace goto exit with daemon_terminate() List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@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: cf6356fd471cdf4e5c52550f63599881d722962c Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=cf6356fd471cdf4e5c52550f63599881d722962c commit cf6356fd471cdf4e5c52550f63599881d722962c Author: Ihor Antonov AuthorDate: 2023-03-18 05:31:12 +0000 Commit: Kyle Evans CommitDate: 2023-03-18 05:52:59 +0000 daemon: repace goto exit with daemon_terminate() Start breaking down big main() Remove goto exit label and replace it with a function that does cleanup. Comment re-worded by kevans@. Pull Request: https://github.com/freebsd/freebsd-src/pull/694 --- usr.sbin/daemon/daemon.c | 65 +++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/usr.sbin/daemon/daemon.c b/usr.sbin/daemon/daemon.c index b215803cf3c6..2a57d021258e 100644 --- a/usr.sbin/daemon/daemon.c +++ b/usr.sbin/daemon/daemon.c @@ -95,6 +95,8 @@ 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_terminate(struct daemon_state *); + static volatile sig_atomic_t terminate = 0; static volatile sig_atomic_t child_gone = 0; @@ -305,7 +307,7 @@ main(int argc, char *argv[]) open_pid_files(&state); if (daemon(state.keep_cur_workdir, state.keep_fds_open) == -1) { warn("daemon"); - goto exit; + daemon_terminate(&state); } /* Write out parent pidfile if needed. */ pidfile_write(state.parent_pidfh); @@ -341,15 +343,15 @@ main(int argc, char *argv[]) /* Block SIGTERM to avoid racing until we have forked. */ if (sigprocmask(SIG_BLOCK, &mask_term, &mask_orig)) { warn("sigprocmask"); - goto exit; + daemon_terminate(&state); } if (sigaction(SIGTERM, &act_term, NULL) == -1) { warn("sigaction"); - goto exit; + daemon_terminate(&state); } if (sigaction(SIGCHLD, &act_chld, NULL) == -1) { warn("sigaction"); - goto exit; + daemon_terminate(&state); } /* * Try to protect against pageout kill. Ignore the @@ -360,7 +362,7 @@ main(int argc, char *argv[]) if (state.log_reopen && state.output_fd >= 0 && sigaction(SIGHUP, &act_hup, NULL) == -1) { warn("sigaction"); - goto exit; + daemon_terminate(&state); } restart: if (pipe(state.pipe_fd)) { @@ -376,7 +378,7 @@ restart: /* fork failed, this can only happen when supervision is enabled */ if (pid == -1) { warn("fork"); - goto exit; + daemon_terminate(&state); } @@ -425,7 +427,7 @@ restart: */ if (sigprocmask(SIG_UNBLOCK, &mask_term, NULL)) { warn("sigprocmask"); - goto exit; + daemon_terminate(&state); } close(state.pipe_fd[1]); state.pipe_fd[1] = -1; @@ -458,34 +460,34 @@ restart: } if (terminate) { - goto exit; + daemon_terminate(&state); } if (state.child_eof) { if (sigprocmask(SIG_BLOCK, &mask_susp, NULL)) { warn("sigprocmask"); - goto exit; + daemon_terminate(&state); } while (!terminate && !child_gone) { sigsuspend(&mask_orig); } if (sigprocmask(SIG_UNBLOCK, &mask_susp, NULL)) { warn("sigprocmask"); - goto exit; + daemon_terminate(&state); } continue; } if (sigprocmask(SIG_BLOCK, &mask_read, NULL)) { warn("sigprocmask"); - goto exit; + daemon_terminate(&state); } state.child_eof = !listen_child(state.pipe_fd[0], &state); if (sigprocmask(SIG_UNBLOCK, &mask_read, NULL)) { warn("sigprocmask"); - goto exit; + daemon_terminate(&state); } } @@ -494,23 +496,14 @@ restart: } if (sigprocmask(SIG_BLOCK, &mask_term, NULL)) { warn("sigprocmask"); - goto exit; + daemon_terminate(&state); } if (state.restart_enabled && !terminate) { close(state.pipe_fd[0]); state.pipe_fd[0] = -1; goto restart; } -exit: - close(state.output_fd); - close(state.pipe_fd[0]); - close(state.pipe_fd[1]); - if (state.syslog_enabled) { - closelog(); - } - pidfile_remove(state.child_pidfh); - pidfile_remove(state.parent_pidfh); - exit(1); /* If daemon(3) succeeded exit status does not matter. */ + daemon_terminate(&state); } static void @@ -601,7 +594,7 @@ listen_child(int fd, struct daemon_state *state) static size_t bytes_read = 0; int rv; - assert(state); + assert(state != NULL); assert(bytes_read < LBUF_SIZE - 1); if (do_log_reopen) { @@ -659,7 +652,7 @@ static void do_output(const unsigned char *buf, size_t len, struct daemon_state *state) { assert(len <= LBUF_SIZE); - assert(state); + assert(state != NULL); if (len < 1) { return; @@ -762,3 +755,25 @@ daemon_state_init(struct daemon_state *state) .output_filename = NULL, }; } + + +static _Noreturn void +daemon_terminate(struct daemon_state *state) +{ + assert(state != NULL); + close(state->output_fd); + close(state->pipe_fd[0]); + close(state->pipe_fd[1]); + if (state->syslog_enabled) { + closelog(); + } + pidfile_remove(state->child_pidfh); + pidfile_remove(state->parent_pidfh); + + /* + * Note that the exit value here doesn't matter in the case of a clean + * exit; daemon(3) already detached us from the caller, nothing is left + * to care about this one. + */ + exit(1); +}