Skip to content

Commit

Permalink
misc, *-daemon: Implement s6-style readiness notification.
Browse files Browse the repository at this point in the history
Adds two helper functions, and allows scripts to wait until services are
ready before proceeding with it's dependees by setting `ready=fd:<num>`,
where <num> is the file descriptor the daemon should write a new line to.
  • Loading branch information
navi-desu committed Jan 19, 2025
1 parent da04d1c commit 4194148
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
40 changes: 40 additions & 0 deletions src/shared/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,46 @@ pid_t get_pid(const char *applet,const char *pidfile)
return pid;
}

struct ready ready_parse(const char *applet, const char *ready_string)
{
struct ready ready = {0};
if (sscanf(ready_string, "fd:%d", &ready.fd) != 1)
eerrorx("%s: invalid ready '%s'.", applet, optarg);

ready.type = READY_FD;

if (pipe(ready.pipe) == -1)
eerrorx("%s: pipe failed: %s", applet, strerror(errno));

return ready;
}

bool ready_wait(const char *applet, struct ready ready)
{
if (ready.type == READY_NONE)
return true;

close(ready.pipe[1]);
ready.fd = ready.pipe[0];

for (;;) {
char buf[BUFSIZ];
ssize_t bytes = read(ready.fd, buf, BUFSIZ);
if (bytes == -1) {
if (errno != EINTR) {
eerror("%s: read failed '%s'\n", applet, strerror(errno));
return false;
}
continue;
}

if (memchr(buf, '\n', bytes))
break;
}

return true;
}

#ifndef HAVE_CLOSE_RANGE
static inline int close_range(int first RC_UNUSED,
int last RC_UNUSED,
Expand Down
13 changes: 13 additions & 0 deletions src/shared/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,17 @@ pid_t get_pid(const char *applet, const char *pidfile);

void cloexec_fds_from(int);

struct ready {
enum {
READY_NONE = 0,
READY_FD,
} type;

int pipe[2];
int fd;
};

struct ready ready_parse(const char *applet, const char *ready_string);
bool ready_wait(const char *applet, struct ready ready);

#endif
21 changes: 19 additions & 2 deletions 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;
struct ready ready;
int ret = EXIT_SUCCESS;

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

case LONGOPT_READY:
ready = ready_parse(applet, optarg);
break;

case_RC_COMMON_GETOPT
}

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

cloexec_fds_from(3);

if (ready.type == READY_FD) {
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 +1197,11 @@ int main(int argc, char **argv)
start_wait = 0;
}

if (start_wait > 0) {
/* TODO: timeout on waiting readiness. */
if (ready.type != READY_NONE) {
if (!ready_wait(applet, ready))
ret = EXIT_FAILURE;
} else if (start_wait > 0) {
struct timespec ts;
bool alive = false;

Expand Down Expand Up @@ -1224,6 +1241,6 @@ int main(int argc, char **argv)
rc_service_daemon_set(svcname, exec,
(const char *const *)margv, pidfile, true);

exit(EXIT_SUCCESS);
exit(ret);
/* NOTREACHED */
}
20 changes: 18 additions & 2 deletions src/supervise-daemon/supervise-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ enum {
LONGOPT_SECBITS,
LONGOPT_STDERR_LOGGER,
LONGOPT_STDOUT_LOGGER,
LONGOPT_READY,
};

const char *applet = NULL;
Expand Down Expand Up @@ -116,6 +117,7 @@ const struct option longopts[] = {
{ "stdout-logger",1, NULL, LONGOPT_STDOUT_LOGGER},
{ "stderr-logger",1, NULL, LONGOPT_STDERR_LOGGER},
{ "reexec", 0, NULL, '3'},
{ "ready", 1, NULL, LONGOPT_READY},
longopts_COMMON
};
const char * const longopts_help[] = {
Expand Down Expand Up @@ -165,6 +167,7 @@ static int devnull_fd = -1;
static int stdin_fd;
static int stdout_fd;
static int stderr_fd;
static struct ready ready;
static char *redirect_stderr = NULL;
static char *redirect_stdout = NULL;
static char *stderr_process = NULL;
Expand Down Expand Up @@ -588,6 +591,9 @@ RC_NORETURN static void child_process(char *exec, char **argv)

cloexec_fds_from(3);

if (ready.type == READY_FD)
dup2(ready.pipe[1], ready.fd);

cmdline = make_cmdline(argv);
syslog(LOG_INFO, "Child command line: %s", cmdline);
free(cmdline);
Expand Down Expand Up @@ -1068,6 +1074,10 @@ int main(int argc, char **argv)
stderr_process = optarg;
break;

case LONGOPT_READY:
ready = ready_parse(applet, optarg);
break;

case_RC_COMMON_GETOPT
}

Expand Down Expand Up @@ -1205,19 +1215,25 @@ int main(int argc, char **argv)
if (child_pid == -1)
eerrorx("%s: fork: %s", applet, strerror(errno));
if (child_pid != 0)
/* first parent process, do nothing. */
exit(EXIT_SUCCESS);
exit(ready_wait(applet, ready) ? EXIT_SUCCESS : EXIT_FAILURE);

#ifdef TIOCNOTTY
tty_fd = open("/dev/tty", O_RDWR);
#endif
devnull_fd = open("/dev/null", O_RDWR);
dup2(devnull_fd, STDIN_FILENO);
dup2(devnull_fd, STDOUT_FILENO);
dup2(devnull_fd, STDERR_FILENO);

if (ready.type == READY_FD)
close(ready.pipe[0]);

child_pid = fork();
if (child_pid == -1)
eerrorx("%s: fork: %s", applet, strerror(errno));
else if (child_pid != 0) {
if (ready.type == READY_FD)
close(ready.pipe[1]);
c = argv;
x = 0;
while (c && *c) {
Expand Down

0 comments on commit 4194148

Please sign in to comment.