Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement readiness notification #767

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading