Skip to content

Commit 6aa1075

Browse files
committed
conmon: Add (and use) get_pipe_fd_from_env helper
This avoids duplicating this code in two places. Signed-off-by: Alexander Larsson <[email protected]>
1 parent 4838d6e commit 6aa1075

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

conmon/conmon.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,24 @@ static char *escape_json_string(const char *str)
440440
return g_string_free (escaped, FALSE);
441441
}
442442

443+
static int get_pipe_fd_from_env(const char *envname)
444+
{
445+
char *pipe_str, *endptr;
446+
int pipe_fd;
447+
448+
pipe_str = getenv(envname);
449+
if (pipe_str == NULL)
450+
return -1;
451+
452+
errno = 0;
453+
pipe_fd = strtol(pipe_str, &endptr, 10);
454+
if (errno != 0 || *endptr != '\0')
455+
pexit("unable to parse %s", envname);
456+
if (fcntl(pipe_fd, F_SETFD, FD_CLOEXEC) == -1)
457+
pexit("unable to make %s CLOEXEC", envname);
458+
459+
return pipe_fd;
460+
}
443461

444462
/* Global state */
445463

@@ -783,7 +801,6 @@ int main(int argc, char *argv[])
783801
int num_read;
784802
int sync_pipe_fd = -1;
785803
int start_pipe_fd = -1;
786-
char *start_pipe, *sync_pipe, *endptr;
787804
int len;
788805
GError *error = NULL;
789806
GOptionContext *context;
@@ -844,12 +861,8 @@ int main(int argc, char *argv[])
844861
if (opt_log_path == NULL)
845862
nexit("Log file path not provided. Use --log-path");
846863

847-
start_pipe = getenv("_OCI_STARTPIPE");
848-
if (start_pipe) {
849-
errno = 0;
850-
start_pipe_fd = strtol(start_pipe, &endptr, 10);
851-
if (errno != 0 || *endptr != '\0')
852-
pexit("unable to parse _OCI_STARTPIPE");
864+
start_pipe_fd = get_pipe_fd_from_env("_OCI_STARTPIPE");
865+
if (start_pipe_fd >= 0) {
853866
/* Block for an initial write to the start pipe before
854867
spawning any childred or exiting, to ensure the
855868
parent can put us in the right cgroup. */
@@ -881,15 +894,7 @@ int main(int argc, char *argv[])
881894
setsid();
882895

883896
/* Environment variables */
884-
sync_pipe = getenv("_OCI_SYNCPIPE");
885-
if (sync_pipe) {
886-
errno = 0;
887-
sync_pipe_fd = strtol(sync_pipe, &endptr, 10);
888-
if (errno != 0 || *endptr != '\0')
889-
pexit("unable to parse _OCI_SYNCPIPE");
890-
if (fcntl(sync_pipe_fd, F_SETFD, FD_CLOEXEC) == -1)
891-
pexit("unable to make _OCI_SYNCPIPE CLOEXEC");
892-
}
897+
sync_pipe_fd = get_pipe_fd_from_env("_OCI_SYNCPIPE");
893898

894899
/* Open the log path file. */
895900
logfd = open(opt_log_path, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0600);

0 commit comments

Comments
 (0)