diff --git a/usr.sbin/daemon/daemon.8 b/usr.sbin/daemon/daemon.8 --- a/usr.sbin/daemon/daemon.8 +++ b/usr.sbin/daemon/daemon.8 @@ -24,7 +24,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd December 27, 2023 +.Dd April 25, 2024 .Dt DAEMON 8 .Os .Sh NAME @@ -43,6 +43,7 @@ .Op Fl T Ar syslog_tag .Op Fl l Ar syslog_facility .Op Fl R Ar restart_delay_seconds +.Op Fl C Ar restart_count .Ar command arguments ... .Sh DESCRIPTION The @@ -55,6 +56,17 @@ .Pp The options are as follows: .Bl -tag -width indent +.It Fl C , Fl -restart-count Ar restart_count +Restart the process at most +.Ar restart_count +times. +When zero is specified, +.Nm +will exit. +The maximum restart count is +.Cm 128 . +This option is used together with option +.Fl -restart . .It Fl c , Fl -change-dir Change the current working directory to the root .Pq Dq Pa / . @@ -72,8 +84,9 @@ and re-open it when signal .Dv SIGHUP is received, for interoperability with -.Xr newsyslog 1 -and similar log rotation / archival mechanisms. If +.Xr newsyslog 8 +and similar log rotation / archival mechanisms. +If .Fl -output-file is not specified, this flag is ignored. .It Fl l , Fl -syslog-facility Ar syslog_facility diff --git a/usr.sbin/daemon/daemon.c b/usr.sbin/daemon/daemon.c --- a/usr.sbin/daemon/daemon.c +++ b/usr.sbin/daemon/daemon.c @@ -56,6 +56,9 @@ /* 1 year in seconds */ #define MAX_RESTART_DELAY 60*60*24*365 +/* Maximum number of restarts */ +#define MAX_RESTART_COUNT 128 + #define LBUF_SIZE 4096 enum daemon_mode { @@ -92,6 +95,8 @@ bool restart_enabled; bool syslog_enabled; bool log_reopen; + int restart_count; + int restarted_count; }; static void restrict_process(const char *); @@ -109,7 +114,7 @@ static bool daemon_is_child_dead(struct daemon_state *); static void daemon_set_child_pipe(struct daemon_state *); -static const char shortopts[] = "+cfHSp:P:ru:o:s:l:t:m:R:T:h"; +static const char shortopts[] = "+cfHSp:P:ru:o:s:l:t:m:R:T:C:h"; static const struct option longopts[] = { { "change-dir", no_argument, NULL, 'c' }, @@ -121,6 +126,7 @@ { "child-pidfile", required_argument, NULL, 'p' }, { "supervisor-pidfile", required_argument, NULL, 'P' }, { "restart", no_argument, NULL, 'r' }, + { "restart-count", required_argument, NULL, 'C' }, { "restart-delay", required_argument, NULL, 'R' }, { "title", required_argument, NULL, 't' }, { "user", required_argument, NULL, 'u' }, @@ -139,6 +145,7 @@ " [-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" + " [-C restart_count]\n" "command arguments ...\n"); (void)fprintf(stderr, @@ -152,6 +159,7 @@ " --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-count -C Restart child at most N times, then exit\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" @@ -198,6 +206,13 @@ case 'c': state.keep_cur_workdir = 0; break; + case 'C': + state.restart_count = (int)strtonum(optarg, 0, + MAX_RESTART_COUNT, &e); + if (e != NULL) { + errx(6, "invalid restart count: %s", e); + } + break; case 'f': state.keep_fds_open = 0; break; @@ -331,6 +346,12 @@ state.mode = MODE_SUPERVISE; daemon_eventloop(&state); daemon_sleep(&state); + if (state.restart_enabled && state.restart_count > -1) { + if (state.restarted_count >= state.restart_count) { + state.restart_enabled = false; + } + state.restarted_count++; + } } while (state.restart_enabled); daemon_terminate(&state); @@ -723,6 +744,8 @@ .keep_fds_open = 1, .output_fd = -1, .output_filename = NULL, + .restart_count = -1, + .restarted_count = 0 }; }