mirror of https://github.com/krallin/tini.git
Add FD swap / redirect stderr code
This commit is contained in:
parent
1f18862ed9
commit
0982ad2613
32
src/tini.c
32
src/tini.c
|
@ -136,8 +136,29 @@ int isolate_child() {
|
||||||
|
|
||||||
|
|
||||||
int spawn(const signal_configuration_t* const sigconf_ptr, char* const argv[], int* const child_pid_ptr) {
|
int spawn(const signal_configuration_t* const sigconf_ptr, char* const argv[], int* const child_pid_ptr) {
|
||||||
|
int new_stdout_fd = 1;
|
||||||
|
int new_stderr_fd = 2;
|
||||||
|
char *redir_path;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
|
||||||
|
redir_path = getenv("REDIR_STDOUT");
|
||||||
|
if (redir_path) {
|
||||||
|
new_stdout_fd = open(redir_path, O_WRONLY | O_CREAT | O_APPEND);
|
||||||
|
if (new_stdout_fd == -1) {
|
||||||
|
PRINT_FATAL("Failed to open stdout redirect path: %s", strerror(errno));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
redir_path = getenv("REDIR_STDERR");
|
||||||
|
if (redir_path) {
|
||||||
|
new_stderr_fd = open(redir_path, O_WRONLY | O_CREAT | O_APPEND);
|
||||||
|
if (new_stderr_fd == -1) {
|
||||||
|
PRINT_FATAL("Failed to open stderr redirect path: %s", strerror(errno));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: check if tini was a foreground process to begin with (it's not OK to "steal" the foreground!")
|
// TODO: check if tini was a foreground process to begin with (it's not OK to "steal" the foreground!")
|
||||||
|
|
||||||
pid = fork();
|
pid = fork();
|
||||||
|
@ -156,6 +177,17 @@ int spawn(const signal_configuration_t* const sigconf_ptr, char* const argv[], i
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Do the FD swap
|
||||||
|
// No need to set CLO_EXEC on existing stdout, stderr FDs, because we're closing them anyway
|
||||||
|
if (dup2(new_stdout_fd, 1) == -1) {
|
||||||
|
PRINT_FATAL("Failed to duplicate stdout FD: %s", strerror(errno));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (dup2(new_stderr_fd, 2) == -1) {
|
||||||
|
PRINT_FATAL("Failed to duplicate stdout FD: %s", strerror(errno));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
execvp(argv[0], argv);
|
execvp(argv[0], argv);
|
||||||
|
|
||||||
// execvp will only return on an error so make sure that we check the errno
|
// execvp will only return on an error so make sure that we check the errno
|
||||||
|
|
Loading…
Reference in New Issue