Skip to content

Commit e321c60

Browse files
committed
ccon: Use original stderr for logging after set_terminal
By dup()ing the original stderr to some other file descriptor and closing that descriptor just before executing another program. This avoids polluting the terminal output with ccon logging even if ccon is called with --verbose. I've also set an explicit initial value for 'verbose'. The lack of one didn't seem to be causing problems, but it doesn't hurt to be explicit ;).
1 parent 38115cb commit e321c60

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

ccon.c

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ static pid_t child_pid;
7777
static pid_t hook_pid;
7878

7979
/* logging */
80-
static int verbose;
81-
#define LOG(...) do {if (verbose) {fprintf(stderr, __VA_ARGS__);}} while(0)
82-
#define PERROR(...) do {if (verbose) {perror(__VA_ARGS__);}} while(0)
80+
static int verbose = 0;
81+
static int log_fd = STDERR_FILENO;
82+
#define LOG(...) do {if (verbose && log_fd >= 0) {dprintf(log_fd, __VA_ARGS__);}} while(0)
83+
#define PERROR(s) do {LOG("%s: %s\n", s, strerror(errno));} while(0)
8384

8485
static int parse_args(int argc, char **argv, const char **config_path,
8586
const char **config_string);
@@ -888,6 +889,16 @@ static int set_terminal(json_t * process, int console, int dup_stdin,
888889
goto cleanup;
889890
}
890891

892+
if (log_fd == STDERR_FILENO) {
893+
log_fd = dup(STDERR_FILENO);
894+
if (log_fd == -1) {
895+
log_fd = STDERR_FILENO;
896+
PERROR("dup");
897+
err = 1;
898+
goto cleanup;
899+
}
900+
}
901+
891902
if (dup2(slave, STDERR_FILENO) == -1) {
892903
PERROR("dup2");
893904
err = 1;
@@ -909,6 +920,11 @@ static int set_terminal(json_t * process, int console, int dup_stdin,
909920
err = 1;
910921
}
911922
}
923+
if (err != 0 && log_fd >= 0 && log_fd != STDERR_FILENO) {
924+
if (close(log_fd)) {
925+
PERROR("close log file descriptor");
926+
}
927+
}
912928
return err;
913929
}
914930

@@ -1140,6 +1156,13 @@ static void exec_process(json_t * process, int console, int dup_stdin,
11401156
LOG(" %s", argv[i]);
11411157
}
11421158
LOG("\n");
1159+
if (log_fd != STDERR_FILENO) {
1160+
if (close(log_fd) == -1) {
1161+
PERROR("close log file descriptor");
1162+
goto cleanup;
1163+
}
1164+
log_fd = -1;
1165+
}
11431166
execveat(*exec_fd, "", argv, env, AT_EMPTY_PATH);
11441167
PERROR("execveat");
11451168
goto cleanup;
@@ -1154,22 +1177,24 @@ static void exec_process(json_t * process, int console, int dup_stdin,
11541177
}
11551178

11561179
LOG("execute [%s]:", path);
1157-
for (i = 0; argv[i]; i++) {
1158-
LOG(" %s", argv[i]);
1159-
}
1160-
LOG("\n");
1161-
execvpe(path, argv, env);
1162-
PERROR("execvpe");
11631180
} else {
1164-
1181+
path = argv[0];
11651182
LOG("execute:");
1166-
for (i = 0; argv[i]; i++) {
1167-
LOG(" %s", argv[i]);
1183+
}
1184+
for (i = 0; argv[i]; i++) {
1185+
LOG(" %s", argv[i]);
1186+
}
1187+
LOG("\n");
1188+
1189+
if (log_fd != STDERR_FILENO) {
1190+
if (close(log_fd) == -1) {
1191+
PERROR("close log file descriptor");
1192+
goto cleanup;
11681193
}
1169-
LOG("\n");
1170-
execvpe(argv[0], argv, env);
1171-
PERROR("execvpe");
1194+
log_fd = -1;
11721195
}
1196+
execvpe(path, argv, env);
1197+
PERROR("execvpe");
11731198

11741199
cleanup:
11751200
if (argv) {

test/t1008-console.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ test_expect_success PRINTF,SHELL 'Test console' "
5757
test_cmp expected actual
5858
"
5959

60-
test_expect_success GREP,PRINTF,SHELL 'Test console and terminal' "
60+
test_expect_success PRINTF,SHELL 'Test console and terminal' "
6161
ccon --verbose --config-string '{
6262
\"version\": \"0.4.0\",
6363
\"namespaces\": {
@@ -69,7 +69,7 @@ test_expect_success GREP,PRINTF,SHELL 'Test console and terminal' "
6969
\"terminal\": true,
7070
\"args\": [\"sh\", \"-c\", \"echo hello >>/dev/console && echo goodbye\"]
7171
}
72-
}' | grep -v /dev/console >actual &&
72+
}' >actual &&
7373
printf 'hello\r\ngoodbye\r\n' >expected &&
7474
test_cmp expected actual
7575
"

0 commit comments

Comments
 (0)