Skip to content
Merged
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 @@ -532,6 +532,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
20 changes: 18 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 @@ -621,6 +625,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 @@ -1103,6 +1111,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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be checked.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those don't really matter at this point. if they fail, the daemon can't write the \n, so the supervisor will already be waiting on ready until openrc-run times out and kill it

the best we can do is check and log, if we exit same thing happens, supervisor will start waiting until timeout and s-s-d won't be doing healthchecks

i'd argue for s-s-d it's better to let the exec go through, since then we have a chance of the daemon writing it's pid file so the timeout can kill it cleanly. for s-d that doesn't matter

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if they fail, the daemon can't write the \n

Not necessarily. Let's say the ready fd is supposed to be 3 and the dup2 fails. And in the daemon there's code like this:

somefd = open("somefile", ...); // this gets fd 3 since it's available
// ...
write(3, "ready\n", 6); // this now writes into "somefile"

Now you end up with the deamon potentially writing nonsense elsewhere.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, i'll add the checks then

}

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

if (start_wait > 0) {
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 @@ -1227,6 +1243,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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.


cmdline = make_cmdline(argv);
syslog(LOG_INFO, "Child command line: %s", cmdline);
free(cmdline);
Expand Down Expand Up @@ -1070,6 +1076,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 @@ -1207,19 +1217,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