Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F142913730
D44944.id137659.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
4 KB
Referenced Files
None
Subscribers
None
D44944.id137659.diff
View Options
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
@@ -58,6 +59,16 @@
.It Fl c , Fl -change-dir
Change the current working directory to the root
.Pq Dq Pa / .
+.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.
+This option is used together with option
+.Fl -restart
+.
.It Fl f , Fl -close-fds
Redirect standard input, standard output and standard error to
.Pa /dev/null .
@@ -72,8 +83,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 <file> Write PID of the child process to file\n"
" --supervisor-pidfile -P <file> Write PID of the supervisor process to file\n"
" --restart -r Restart child if it terminates (1 sec delay)\n"
+ " --restart-count -C <N> Restart child at most N times, then exit\n"
" --restart-delay -R <N> Restart child if it terminates after N sec\n"
" --title -t <title> 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
};
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Jan 25, 4:40 PM (16 h, 49 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
27951427
Default Alt Text
D44944.id137659.diff (4 KB)
Attached To
Mode
D44944: daemon: Add -C (--restart-count) option
Attached
Detach File
Event Timeline
Log In to Comment