From nobody Fri Mar 17 21:01:21 2023 X-Original-To: dev-commits-src-branches@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 4Pdc4L1Rywz3yqbV; Fri, 17 Mar 2023 21:01:22 +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 4Pdc4L0R7cz3pfK; Fri, 17 Mar 2023 21:01:22 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1679086882; 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=j+nl4e/8llDu9T3WtywmcKfNWysLbSH0vwQzDPbAR9Q=; b=Ldjf2/UzhXzkR2o1BtPH0lm5BaDS4W+xiJVzIoX3eex0yAAjn+QRF18MqAVxU4g/SsOupY n+vlG5NWya90ZWXk3owHwNuK/2WpjIitdf6m2Y3Vk5NPJkgijLhxW8/yaqAShdj6QVXmxV OWqUh/jQZwQnI5FYYTQsEdCCkQBU0AC/Oqfo6XFSkhgeWRhjlISDny/H7tR8odd9VBhWGx ZacsClJAUtDl3GgId/e219eHsd2SvTqR/Ui3AlqYrmaDMDBUOF023LtOPaE3GVX5/7BCkf UZeLPXPEfSGZ5TiE5aIBSBEL5YZuj7MvGF7nAWGXt6J3UKpABRidazQe4Z/nnA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1679086882; 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=j+nl4e/8llDu9T3WtywmcKfNWysLbSH0vwQzDPbAR9Q=; b=YsU7gIdnmuw/trB8FeHBHs165e2VKsIeDUBhvPJltu/IZLEc1iNyfbPudVLVRxKC95EGVs VT2EWL5TExoRXf87/WIC1bWQQ555DTNeqV1cuv+9PxY/kf930lsxOanVNPgOfbzYVwqupA ew0EBz2BV9Ov7ndwtzLh4yT2pozeUCFszdWtTr5c//SOUC/kxNVyCMJI/+gh/HBbu+pJ3G rsPPZjTqJ3ryUpBdOGyV+8A8tO9AT2tMBLhq42KgaczDH8hp6dAWpLuYMQvSCmpSBnKV9a WZX24xlXyRnZyYDtMiQljYZ8Z9RE+mfW1Ko4idf64YAoj/M6z0j2v2+2ize39Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1679086882; a=rsa-sha256; cv=none; b=vnIW5Rp3auxsZyFfLNm5vQa4e7fRnwqXwKbQDkBiMhuCS32h92Wx5POSR8ExVAqDT2/yDf n4+FVmHCkh4NAnzi3OIbtbCW8ZEfpISzr6jo7g7NE3wBoAdoHhh5/t7eGGhqw36yr/R8im 4K5TK4eqQn3Uhvq3nlhE9m82j9UQm0vRbAgAw7rx5IVQdaK3WSIB8ZiBvgWrVFEEBYBJO9 47ta3wgbMHe2m1oK2SoOPs6v5LDi3hm3JOC4o5f5VF/W9+xRIFShHNBaCJDfD+mNxLmR5v FrxgI+X8JV2iP+idthD6Mn+C8jcMQrkpP98Cz1BeDghIXysAOHGnjq+Epy31aA== 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 4Pdc4K6bXWzgjq; Fri, 17 Mar 2023 21:01:21 +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 32HL1Lpe014933; Fri, 17 Mar 2023 21:01:21 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 32HL1LHI014932; Fri, 17 Mar 2023 21:01:21 GMT (envelope-from git) Date: Fri, 17 Mar 2023 21:01:21 GMT Message-Id: <202303172101.32HL1LHI014932@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kyle Evans Subject: git: 282e4cf32d4e - stable/13 - daemon: add long_opts List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@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/stable/13 X-Git-Reftype: branch X-Git-Commit: 282e4cf32d4eaf53b746358b83515a333e8b6c72 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=282e4cf32d4eaf53b746358b83515a333e8b6c72 commit 282e4cf32d4eaf53b746358b83515a333e8b6c72 Author: Ihor Antonov AuthorDate: 2023-02-10 04:36:07 +0000 Commit: Kyle Evans CommitDate: 2023-03-17 21:01:01 +0000 daemon: add long_opts Long options improve readability of scripts, makes code comprehension easier. This patch adds long options while preserving the existing CLI interface. Also --help/-h option is added. Reviewed by: allanjude, pauamma (both earlier versions), kevans Differential Revision: https://reviews.freebsd.org/D38244 (cherry picked from commit 0a402ad2e6d6014ed8c622a90153f53b99c1b482) --- usr.sbin/daemon/daemon.8 | 71 +++++++++++++++++++++---------------------- usr.sbin/daemon/daemon.c | 78 ++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 98 insertions(+), 51 deletions(-) diff --git a/usr.sbin/daemon/daemon.8 b/usr.sbin/daemon/daemon.8 index c4f20548d4a2..3b91b9b20f3d 100644 --- a/usr.sbin/daemon/daemon.8 +++ b/usr.sbin/daemon/daemon.8 @@ -57,10 +57,10 @@ log file. .Pp The options are as follows: .Bl -tag -width indent -.It Fl c +.It Fl c , Fl -change-dir Change the current working directory to the root .Pq Dq Pa / . -.It Fl f +.It Fl f , Fl -close-fds Redirect standard input, standard output and standard error to .Pa /dev/null . When this option is used together with any of the options related to file @@ -68,35 +68,35 @@ or syslog output, the standard file descriptors are first redirected to .Pa /dev/null , then stdout and/or stderr is redirected to a file or to syslog as specified by the other options. -.It Fl H +.It Fl H , Fl -sighup Close .Pa output_file and re-open it when signal SIGHUP is received, for interoperability with .Xr newsyslog 1 and similar log rotation / archival mechanisms. If -.Fl o +.Fl -output-file is not specified, this flag is ignored. -.It Fl S +.It Fl S , Fl -syslog Enable syslog output. This is implicitly applied if other syslog parameters are provided. The default values are daemon, notice, and daemon for facility, priority, and tag, respectively. -.It Fl o Ar output_file +.It Fl o , Fl -output-file Ar output_file Append output from the daemonized process to .Pa output_file . If the file does not exist, it is created with permissions 0600. When this option is used together with options -.Fl c +.Fl -change-dir and -.Fl H +.Fl -sighup the absolute path needs to be provided to ensure .Nm can re-open the file after a SIGHUP. -.It Fl m Ar output_mask +.It Fl m , Fl -output-mask Ar output_mask Redirect output from the child process stdout (1), stderr (2), or both (3). This value specifies what is sent to syslog and the log file. The default is 3. -.It Fl p Ar child_pidfile +.It Fl p , Fl -child-pidfile Ar child_pidfile Write the ID of the created process into the .Ar child_pidfile using the @@ -112,9 +112,9 @@ The owner is the user who runs the .Nm regardless of whether the -.Fl u +.Fl -user option is used or not. -.It Fl P Ar supervisor_pidfile +.It Fl P , Fl -supervisor-pidfile Ar supervisor_pidfile Write the ID of the .Nm process into the @@ -132,45 +132,46 @@ The owner is the user who runs the .Nm regardless of whether the -.Fl u +.Fl -user option is used or not. -.It Fl r +.It Fl r , Fl -restart Supervise and restart the program after a one-second delay if it has been terminated. -.It Fl R Ar restart_delay_seconds +.It Fl R , Fl -restart-delay Ar restart_delay_seconds Supervise and restart the program after the specified delay if it has been terminated. -.It Fl t Ar title +.It Fl t , Fl -title Ar title Set the title for the daemon process. The default is the daemonized invocation. -.It Fl u Ar user +.It Fl u , Fl -user Ar user Login name of the user to execute the program under. Requires adequate superuser privileges. -.It Fl s Ar syslog_priority +.It Fl s , Fl -syslog-priority Ar syslog_priority These priorities are accepted: emerg, alert, crit, err, warning, notice, info, and debug. The default is notice. -.It Fl l Ar syslog_facility +.It Fl l , Fl -syslog-facility Ar syslog_facility These facilities are accepted: auth, authpriv, console, cron, daemon, ftp, kern, lpr, mail, news, ntp, security, syslog, user, uucp, and local0, ..., local7. The default is daemon. -.It Fl T Ar syslog_tag +.It Fl T , Fl -syslog-tag Ar syslog_tag Set the tag which is appended to all syslog messages. The default is daemon. .El .Pp If any of the options -.Fl p , -.Fl P , -.Fl r , -.Fl o , -.Fl s , -.Fl T , -.Fl m , -.Fl S , +.Fl -child-pidfile , +.Fl -output-mask , +.Fl -restart , +.Fl -restart-delay , +.Fl -supervisor-pidfile , +.Fl -syslog , +.Fl -syslog-facility +.Fl -syslog-priority , +.Fl -syslog-tag , or -.Fl l +.Fl -output , are specified, the program is executed in a spawned child process. The .Nm @@ -187,21 +188,21 @@ If neither file or syslog output are selected, all output is redirected to the .Nm process and written to stdout. The -.Fl f +.Fl -close-fds option may be used to suppress the stdout output completely. .Pp The -.Fl P +.Fl -supervisor-pidfile option is useful combined with the -.Fl r +.Fl -restart option as .Ar supervisor_pidfile contains the ID of the supervisor not the child. This is especially important if you use -.Fl r +.Fl -restart in an rc script as the -.Fl p +.Fl -child-pidfile option will give you the child's ID to signal when you attempt to stop the service, causing .Nm @@ -229,7 +230,7 @@ cannot be opened for appending, and otherwise 0. If the command cannot be executed, an error message is printed to standard error. The exact behavior depends on the logging parameters and the -.Fl f +.Fl -close-fds flag. .Sh SEE ALSO .Xr nohup 1 , diff --git a/usr.sbin/daemon/daemon.c b/usr.sbin/daemon/daemon.c index e8cdaa230caa..edcdf3141fcc 100644 --- a/usr.sbin/daemon/daemon.c +++ b/usr.sbin/daemon/daemon.c @@ -40,6 +40,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -77,11 +78,64 @@ static void open_pid_files(const char *, const char *, struct pidfh **, struct pidfh **); static void do_output(const unsigned char *, size_t, struct log_params *); static void daemon_sleep(time_t, long); -static void usage(void); static volatile sig_atomic_t terminate = 0, child_gone = 0, pid = 0, do_log_reopen = 0; +static const char shortopts[] = "+cfHSp:P:ru:o:s:l:t:m:R:T:h"; + +static const struct option longopts[] = { + { "change-dir", no_argument, NULL, 'c' }, + { "close-fds", no_argument, NULL, 'f' }, + { "sighup", no_argument, NULL, 'H' }, + { "syslog", no_argument, NULL, 'S' }, + { "output-file", required_argument, NULL, 'o' }, + { "output-mask", required_argument, NULL, 'm' }, + { "child-pidfile", required_argument, NULL, 'p' }, + { "supervisor-pidfile", required_argument, NULL, 'P' }, + { "restart", no_argument, NULL, 'r' }, + { "restart-delay", required_argument, NULL, 'R' }, + { "title", required_argument, NULL, 't' }, + { "user", required_argument, NULL, 'u' }, + { "syslog-priority", required_argument, NULL, 's' }, + { "syslog-facility", required_argument, NULL, 'l' }, + { "syslog-tag", required_argument, NULL, 'T' }, + { "help", no_argument, NULL, 'h' }, + { NULL, 0, NULL, 0 } +}; + +static _Noreturn void +usage(int exitcode) +{ + (void)fprintf(stderr, + "usage: daemon [-cfHrS] [-p child_pidfile] [-P supervisor_pidfile]\n" + " [-u user] [-o output_file] [-t title]\n" + " [-l syslog_facility] [-s syslog_priority]\n" + " [-T syslog_tag] [-m output_mask] [-R restart_delay_secs]\n" + "command arguments ...\n"); + + (void)fprintf(stderr, + " --change-dir -c Change the current working directory to root\n" + " --close-fds -f Set stdin, stdout, stderr to /dev/null\n" + " --sighup -H Close and re-open output file on SIGHUP\n" + " --syslog -S Send output to syslog\n" + " --output-file -o Append output of the child process to file\n" + " --output-mask -m What to send to syslog/file\n" + " 1=stdout, 2=stderr, 3=both\n" + " --child-pidfile -p Write PID of the child process to file\n" + " --supervisor-pidfile -P Write PID of the supervisor process to file\n" + " --restart -r Restart child if it terminates (1 sec delay)\n" + " --restart-delay -R Restart child if it terminates after N sec\n" + " --title -t Set the title of the supervisor process\n" + " --user -u <user> Drop privileges, run as given user\n" + " --syslog-priority -s <prio> Set syslog priority\n" + " --syslog-facility -l <flty> Set syslog facility\n" + " --syslog-tag -T <tag> Set syslog tag\n" + " --help -h Show this help\n"); + + exit(exitcode); +} + int main(int argc, char *argv[]) { @@ -106,7 +160,7 @@ main(int argc, char *argv[]) log_reopen = 0; outfn = NULL; title = NULL; - while ((ch = getopt(argc, argv, "cfHSp:P:ru:o:s:l:t:l:m:R:T:")) != -1) { + while ((ch = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { switch (ch) { case 'c': nochdir = 0; @@ -164,15 +218,18 @@ main(int argc, char *argv[]) case 'u': user = optarg; break; + case 'h': + usage(0); + __builtin_unreachable(); default: - usage(); + usage(1); } } argc -= optind; argv += optind; if (argc == 0) - usage(); + usage(1); if (!title) title = argv[0]; @@ -182,7 +239,7 @@ main(int argc, char *argv[]) if (outfd == -1) err(7, "open"); } - + if (dosyslog) openlog(logtag, LOG_PID | LOG_NDELAY, logfac); @@ -610,14 +667,3 @@ reopen_log(struct log_params *lpp) lpp->outfd = outfd; } -static void -usage(void) -{ - (void)fprintf(stderr, - "usage: daemon [-cfHrS] [-p child_pidfile] [-P supervisor_pidfile]\n" - " [-u user] [-o output_file] [-t title]\n" - " [-l syslog_facility] [-s syslog_priority]\n" - " [-T syslog_tag] [-m output_mask] [-R restart_delay_secs]\n" - "command arguments ...\n"); - exit(1); -}