From nobody Thu Mar 02 03:01:07 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 4PRwpr0gmhz3vk4c; Thu, 2 Mar 2023 03:01:08 +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 4PRwpq64jTz44F7; Thu, 2 Mar 2023 03:01:07 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1677726067; 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=7vqXO9bAJX7LKDf5GlSUH9Z/XWeF7lr5bhf/xoAmecQ=; b=BSH2czMpbeq9ZvggOcJncv/4OepxOYD7DaqbGbmMghtZlL0wpqSswqdAubJD4IFDMNk/uC oFdOF9tNc9Tm+bJPZaD8QO92HlFU2vrWlwJwC70cLw1ZtYRt5oFIUYG8mUxRmmfrkZ0Yh8 gWKpk7XPwQzwW1kO+zDs1ATmvDukjJ9bk5Ra0oKjbk3Yn7uWUayhVHgkJrInF4keveml0T /aCf7v3yp3LjucaM+fS0EOJKmc7+jnYsHiZwbwFrYj9b5GGfScG+THLAeYXgiD2wyVmiHl xiJoLHAjwutORnWrP9mKztZVpl0pAJDKX2fyDu7hxKiYEgeWAxfzhGWpV9Jtaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1677726067; 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=7vqXO9bAJX7LKDf5GlSUH9Z/XWeF7lr5bhf/xoAmecQ=; b=C3+h9VzhvKNJvG/aAYeCzgY6MiVBy1fiu42Te1pk6YplyCNua+etJyUYX1XeN5cGBi7ade zKy7hOxtRQuEL4Cz+sLhYUhfVkhfvYmHdAQCmzqjTXvxB3uBCHklCIgGndxknRc2vxlddr IDXvaQxiss+TcRQQgPhnEshv3iqAblrsfL2z7l+98zlzKRlNA16jO166Noxv9Pp8UIWFY6 xfRe+kRz4dBYQJoil5sBa4suhvsvz7saQHdvAzKsCmk1wlZeTJdyEL6koCfuh3sNnRkEPu a65LRIXNOaOIX3YxlaRFEGpBXvazFOHJxbW1pKtJRN0e3x+3JF1SEPN2rcNHIg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1677726067; a=rsa-sha256; cv=none; b=uWHlC8BGneYzs5CoiyBrrm0pw7/cLlWUfz2lJ09wRYrFrqzszDPkMutPZvJx8WrRV4NgXi RawprWdEnOxZji55nPSLNSW0+lz/JUJB24s59FpjCKDXLtkuH2QRm2WtyrHwl/JGeRfu0c V5qv2bhpH4lQkawOjkPnlFKe8XpLtCCwM8D9aVpaHcL1XPimcEe1zjzjsSMg4rqi7kyyl9 ZPEKp8nVHk4cZc4Vd/6j2n77Hnv3bz1ugnH3GTZYX3/55pW+Zin6Kp29Thkwr29wcXNGmJ uwsevhn8Spx1V3PSNuoUDFD82otbqZ1ZDcbZpZs6bph/Pw1vOg4SDTAgDfFCFQ== 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 4PRwpq57sQzyvT; Thu, 2 Mar 2023 03:01:07 +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 322317kj034373; Thu, 2 Mar 2023 03:01:07 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 322317VG034372; Thu, 2 Mar 2023 03:01:07 GMT (envelope-from git) Date: Thu, 2 Mar 2023 03:01:07 GMT Message-Id: <202303020301.322317VG034372@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: e70444c622a9 - main - daemon: deduplicate log_params varaibles 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: e70444c622a97e02fb5a76c06f9a48b4a07aa963 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=e70444c622a97e02fb5a76c06f9a48b4a07aa963 commit e70444c622a97e02fb5a76c06f9a48b4a07aa963 Author: Ihor Antonov AuthorDate: 2023-03-02 03:00:42 +0000 Commit: Kyle Evans CommitDate: 2023-03-02 03:00:42 +0000 daemon: deduplicate log_params varaibles While we're here, sort log_params by size. Reviewed by: kevans Pull Request: https://github.com/freebsd/freebsd-src/pull/669 --- usr.sbin/daemon/daemon.c | 92 +++++++++++++++++++++++------------------------- 1 file changed, 44 insertions(+), 48 deletions(-) diff --git a/usr.sbin/daemon/daemon.c b/usr.sbin/daemon/daemon.c index 2db2594041e7..379c31a0a221 100644 --- a/usr.sbin/daemon/daemon.c +++ b/usr.sbin/daemon/daemon.c @@ -60,10 +60,10 @@ __FBSDID("$FreeBSD$"); #define LBUF_SIZE 4096 struct log_params { - int logpri; + const char *output_filename; + int syslog_priority; int noclose; - int outfd; - const char *outfn; + int output_fd; bool syslog_enabled; }; @@ -143,26 +143,27 @@ int main(int argc, char *argv[]) { bool supervision_enabled = false; - bool syslog_enabled = false; bool log_reopen = false; char *p = NULL; const char *pidfile = NULL; - const char *logtag = "daemon"; - const char *outfn = NULL; + const char *syslog_tag = "daemon"; const char *ppidfile = NULL; const char *title = NULL; const char *user = NULL; int ch = 0; int child_eof = 0; - int logfac = LOG_DAEMON; - int logpri = LOG_NOTICE; + int syslog_facility = LOG_DAEMON; int nochdir = 1; - int noclose = 1; - int outfd = -1; int pfd[2] = { -1, -1 }; int restart = 0; int stdmask = STDOUT_FILENO | STDERR_FILENO; - struct log_params logpar = { 0 }; + struct log_params logpar = { + .syslog_enabled = false, + .syslog_priority = LOG_NOTICE, + .noclose = 1, + .output_fd = -1, + .output_filename = NULL + }; struct pidfh *ppfh = NULL; struct pidfh *pfh = NULL; sigset_t mask_orig; @@ -181,17 +182,17 @@ main(int argc, char *argv[]) nochdir = 0; break; case 'f': - noclose = 0; + logpar.noclose = 0; break; case 'H': log_reopen = true; break; case 'l': - logfac = get_log_mapping(optarg, facilitynames); - if (logfac == -1) { + syslog_facility = get_log_mapping(optarg, facilitynames); + if (syslog_facility == -1) { errx(5, "unrecognized syslog facility"); } - syslog_enabled = true; + logpar.syslog_enabled = true; break; case 'm': stdmask = strtol(optarg, &p, 10); @@ -200,7 +201,7 @@ main(int argc, char *argv[]) } break; case 'o': - outfn = optarg; + logpar.output_filename = optarg; break; case 'p': pidfile = optarg; @@ -218,21 +219,21 @@ main(int argc, char *argv[]) } break; case 's': - logpri = get_log_mapping(optarg, prioritynames); - if (logpri == -1) { + logpar.syslog_priority = get_log_mapping(optarg, prioritynames); + if (logpar.syslog_priority == -1) { errx(4, "unrecognized syslog priority"); } - syslog_enabled = true; + logpar.syslog_enabled = true; break; case 'S': - syslog_enabled = true; + logpar.syslog_enabled = true; break; case 't': title = optarg; break; case 'T': - logtag = optarg; - syslog_enabled = true; + syslog_tag = optarg; + logpar.syslog_enabled = true; break; case 'u': user = optarg; @@ -255,15 +256,15 @@ main(int argc, char *argv[]) title = argv[0]; } - if (outfn) { - outfd = open_log(outfn); - if (outfd == -1) { + if (logpar.output_filename) { + logpar.output_fd = open_log(logpar.output_filename); + if (logpar.output_fd == -1) { err(7, "open"); } } - if (syslog_enabled) { - openlog(logtag, LOG_PID | LOG_NDELAY, logfac); + if (logpar.syslog_enabled) { + openlog(syslog_tag, LOG_PID | LOG_NDELAY, syslog_facility); } /* @@ -271,7 +272,7 @@ main(int argc, char *argv[]) * to be able to report the error intelligently */ open_pid_files(pidfile, ppidfile, &pfh, &ppfh); - if (daemon(nochdir, noclose) == -1) { + if (daemon(nochdir, logpar.noclose) == -1) { warn("daemon"); goto exit; } @@ -295,11 +296,11 @@ main(int argc, char *argv[]) * To achieve this daemon catches SIGTERM and * forwards it to the child, expecting to get SIGCHLD eventually. */ - supervision_enabled = pidfile != NULL || + supervision_enabled = pidfile != NULL || ppidfile != NULL || - restart != 0 || - outfd != -1 || - syslog_enabled == true; + restart != 0 || + logpar.output_fd != -1 || + logpar.syslog_enabled == true; if (supervision_enabled) { struct sigaction act_term = { 0 }; @@ -348,12 +349,7 @@ main(int argc, char *argv[]) * not have superuser privileges. */ (void)madvise(NULL, 0, MADV_PROTECT); - logpar.outfd = outfd; - logpar.syslog_enabled = syslog_enabled; - logpar.logpri = logpri; - logpar.noclose = noclose; - logpar.outfn = outfn; - if (log_reopen && outfd >= 0 && + if (log_reopen && logpar.output_fd >= 0 && sigaction(SIGHUP, &act_hup, NULL) == -1) { warn("sigaction"); goto exit; @@ -484,10 +480,10 @@ restart: goto restart; } exit: - close(outfd); + close(logpar.output_fd); close(pfd[0]); close(pfd[1]); - if (syslog_enabled) { + if (logpar.syslog_enabled) { closelog(); } pidfile_remove(pfh); @@ -648,13 +644,13 @@ do_output(const unsigned char *buf, size_t len, struct log_params *logpar) return; } if (logpar->syslog_enabled) { - syslog(logpar->logpri, "%.*s", (int)len, buf); + syslog(logpar->syslog_priority, "%.*s", (int)len, buf); } - if (logpar->outfd != -1) { - if (write(logpar->outfd, buf, len) == -1) + if (logpar->output_fd != -1) { + if (write(logpar->output_fd, buf, len) == -1) warn("write"); } - if (logpar->noclose && !logpar->syslog_enabled && logpar->outfd == -1) { + if (logpar->noclose && !logpar->syslog_enabled && logpar->output_fd == -1) { printf("%.*s", (int)len, buf); } } @@ -708,10 +704,10 @@ reopen_log(struct log_params *lpp) int outfd; do_log_reopen = 0; - outfd = open_log(lpp->outfn); - if (lpp->outfd >= 0) { - close(lpp->outfd); + outfd = open_log(lpp->output_filename); + if (lpp->output_fd >= 0) { + close(lpp->output_fd); } - lpp->outfd = outfd; + lpp->output_fd = outfd; }