From 37820b8746512493229b7bc13a38f403ee608d04 Mon Sep 17 00:00:00 2001 From: Ian Lepore Date: Sun, 15 Apr 2018 21:46:08 +0000 Subject: [PATCH] Add an option to daemon(8) to specify a delay between restarts of a supervised program. The existing -r option has a hard-coded delay of one second. This change adds a -R option which takes a delay in seconds. This can be used to prevent log spam and rapid restarts, similar to init(8)'s behavior of adding a delay between rapid restarts when it's supervising a program. --- usr.sbin/daemon/daemon.8 | 9 +++++++-- usr.sbin/daemon/daemon.c | 11 ++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/usr.sbin/daemon/daemon.8 b/usr.sbin/daemon/daemon.8 index 345d81607b64..2540df0d1b46 100644 --- a/usr.sbin/daemon/daemon.8 +++ b/usr.sbin/daemon/daemon.8 @@ -26,7 +26,7 @@ .\" .\" $FreeBSD$ .\" -.Dd October 22, 2016 +.Dd April 14, 2018 .Dt DAEMON 8 .Os .Sh NAME @@ -44,6 +44,7 @@ .Op Fl s Ar syslog_priority .Op Fl T Ar syslog_tag .Op Fl l Ar syslog_facility +.Op Fl T Ar restart_delay_seconds .Ar command arguments ... .Sh DESCRIPTION The @@ -114,7 +115,11 @@ regardless of whether the .Fl u option is used or not. .It Fl r -Supervise and restart the program if it has been terminated. +Supervise and restart the program after a one-second delay if it has +been terminated. +.It Fl R restart_delay_seconds +Supervise and restart the program after the specified delay +if it has been terminated. .It Fl t Ar title Set the title for the daemon process. The default is the daemonized invocation. diff --git a/usr.sbin/daemon/daemon.c b/usr.sbin/daemon/daemon.c index 9018d8a53602..ad3d9fd34f14 100644 --- a/usr.sbin/daemon/daemon.c +++ b/usr.sbin/daemon/daemon.c @@ -99,7 +99,7 @@ main(int argc, char *argv[]) dosyslog = 0; outfn = NULL; title = NULL; - while ((ch = getopt(argc, argv, "cfSp:P:ru:o:s:l:t:l:m:T:")) != -1) { + while ((ch = getopt(argc, argv, "cfSp:P:ru:o:s:l:t:l:m:R:T:")) != -1) { switch (ch) { case 'c': nochdir = 0; @@ -130,6 +130,11 @@ main(int argc, char *argv[]) case 'r': restart = 1; break; + case 'R': + restart = strtol(optarg, &p, 0); + if (p == optarg || restart < 1) + errx(6, "invalid restart delay"); + break; case 's': logpri = get_log_mapping(optarg, prioritynames); if (logpri == -1) @@ -359,7 +364,7 @@ main(int argc, char *argv[]) goto exit; } if (restart && !terminate) { - daemon_sleep(1, 0); + daemon_sleep(restart, 0); close(pfd[0]); pfd[0] = -1; goto restart; @@ -558,7 +563,7 @@ usage(void) "usage: daemon [-cfrS] [-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]\n" + " [-T syslog_tag] [-m output_mask] [-R restart_delay_secs]\n" "command arguments ...\n"); exit(1); }