conmon: Add (and use) get_pipe_fd_from_env helper

This avoids duplicating this code in two places.

Signed-off-by: Alexander Larsson <alexl@redhat.com>
This commit is contained in:
Alexander Larsson 2017-06-22 10:57:27 +02:00
parent 4838d6eb80
commit 6aa1075ab6

View file

@ -440,6 +440,24 @@ static char *escape_json_string(const char *str)
return g_string_free (escaped, FALSE); return g_string_free (escaped, FALSE);
} }
static int get_pipe_fd_from_env(const char *envname)
{
char *pipe_str, *endptr;
int pipe_fd;
pipe_str = getenv(envname);
if (pipe_str == NULL)
return -1;
errno = 0;
pipe_fd = strtol(pipe_str, &endptr, 10);
if (errno != 0 || *endptr != '\0')
pexit("unable to parse %s", envname);
if (fcntl(pipe_fd, F_SETFD, FD_CLOEXEC) == -1)
pexit("unable to make %s CLOEXEC", envname);
return pipe_fd;
}
/* Global state */ /* Global state */
@ -783,7 +801,6 @@ int main(int argc, char *argv[])
int num_read; int num_read;
int sync_pipe_fd = -1; int sync_pipe_fd = -1;
int start_pipe_fd = -1; int start_pipe_fd = -1;
char *start_pipe, *sync_pipe, *endptr;
int len; int len;
GError *error = NULL; GError *error = NULL;
GOptionContext *context; GOptionContext *context;
@ -844,12 +861,8 @@ int main(int argc, char *argv[])
if (opt_log_path == NULL) if (opt_log_path == NULL)
nexit("Log file path not provided. Use --log-path"); nexit("Log file path not provided. Use --log-path");
start_pipe = getenv("_OCI_STARTPIPE"); start_pipe_fd = get_pipe_fd_from_env("_OCI_STARTPIPE");
if (start_pipe) { if (start_pipe_fd >= 0) {
errno = 0;
start_pipe_fd = strtol(start_pipe, &endptr, 10);
if (errno != 0 || *endptr != '\0')
pexit("unable to parse _OCI_STARTPIPE");
/* Block for an initial write to the start pipe before /* Block for an initial write to the start pipe before
spawning any childred or exiting, to ensure the spawning any childred or exiting, to ensure the
parent can put us in the right cgroup. */ parent can put us in the right cgroup. */
@ -881,15 +894,7 @@ int main(int argc, char *argv[])
setsid(); setsid();
/* Environment variables */ /* Environment variables */
sync_pipe = getenv("_OCI_SYNCPIPE"); sync_pipe_fd = get_pipe_fd_from_env("_OCI_SYNCPIPE");
if (sync_pipe) {
errno = 0;
sync_pipe_fd = strtol(sync_pipe, &endptr, 10);
if (errno != 0 || *endptr != '\0')
pexit("unable to parse _OCI_SYNCPIPE");
if (fcntl(sync_pipe_fd, F_SETFD, FD_CLOEXEC) == -1)
pexit("unable to make _OCI_SYNCPIPE CLOEXEC");
}
/* Open the log path file. */ /* Open the log path file. */
logfd = open(opt_log_path, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0600); logfd = open(opt_log_path, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0600);