Skip to content

Commit

Permalink
start-stop-daemon: add s6-style readiness notification.
Browse files Browse the repository at this point in the history
  • Loading branch information
navi-desu committed Jan 5, 2025
1 parent a6d66bf commit 9ffcbd2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions sh/start-stop-daemon.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ ssd_start()
${pidfile:+--pidfile} $pidfile \
${command_user+--user} $command_user \
${umask+--umask} $umask \
${ready+--ready} $ready \
$_background $start_stop_daemon_args \
-- $command_args $command_args_background
if eend $? "Failed to start ${name:-$RC_SVCNAME}"; then
Expand Down
31 changes: 30 additions & 1 deletion src/start-stop-daemon/start-stop-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ enum {
LONGOPT_SCHEDULER,
LONGOPT_SCHEDULER_PRIO,
LONGOPT_SECBITS,
LONGOPT_READY,
};

const char *applet = NULL;
Expand Down Expand Up @@ -130,6 +131,7 @@ const struct option longopts[] = {
{ "progress", 0, NULL, 'P'},
{ "scheduler", 1, NULL, LONGOPT_SCHEDULER},
{ "scheduler-priority", 1, NULL, LONGOPT_SCHEDULER_PRIO},
{ "ready", 1, NULL, LONGOPT_READY},
longopts_COMMON
};
const char * const longopts_help[] = {
Expand Down Expand Up @@ -353,6 +355,8 @@ int main(int argc, char **argv)
int pipefd[2];
char readbuf[1];
ssize_t ss;
int ready_fd = -1;
int ready_pipe[2];

applet = basename_c(argv[0]);
atexit(cleanup);
Expand Down Expand Up @@ -618,6 +622,12 @@ int main(int argc, char **argv)
sscanf(optarg, "%d", &sched_prio);
break;

case LONGOPT_READY:
if (sscanf(optarg, "fd:%d", &ready_fd) != 1)
eerrorx("%s: invalid ready '%s'.", applet, optarg);
pipe(ready_pipe);
break;

case_RC_COMMON_GETOPT
}

Expand Down Expand Up @@ -1100,6 +1110,11 @@ int main(int argc, char **argv)

cloexec_fds_from(3);

if (ready_fd != -1) {
close(ready_pipe[0]);
dup2(ready_pipe[1], ready_fd);
}

if (scheduler != NULL) {
int scheduler_index;
struct sched_param sched = {.sched_priority = sched_prio};
Expand Down Expand Up @@ -1184,7 +1199,21 @@ int main(int argc, char **argv)
start_wait = 0;
}

if (start_wait > 0) {
/* TODO: timeout on waiting readiness. */
if (ready_fd != -1) {
close(ready_pipe[1]);
for (;;) {
char buf[BUFSIZ];
int bytes = read(ready_pipe[0], buf, BUFSIZ);
if (bytes == -1 && errno != EINTR) {
eerror("%s: read failed '%s'\n", applet, strerror(errno));
exit(EXIT_FAILURE);
}

if (memchr(buf, '\n', bytes))
break;
}
} else if (start_wait > 0) {
struct timespec ts;
bool alive = false;

Expand Down

0 comments on commit 9ffcbd2

Please sign in to comment.