From c90a0ee947fa2beaf598f456b559c4c281c119cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Duval?= Date: Mon, 6 Aug 2018 19:13:32 +0200 Subject: [PATCH] posix_spawn(): dup2() again this time on file_action_dup2. * fixes #14322. --- src/system/libroot/posix/spawn.cpp | 2 +- src/tests/system/libroot/posix/Jamfile | 2 + .../libroot/posix/posix_spawn_pipe_err.c | 10 ++++ .../libroot/posix/posix_spawn_pipe_test.c | 56 +++++++++++++++++++ .../libroot/posix/posix_spawn_pipe_test.h | 5 ++ 5 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/tests/system/libroot/posix/posix_spawn_pipe_err.c create mode 100644 src/tests/system/libroot/posix/posix_spawn_pipe_test.c create mode 100644 src/tests/system/libroot/posix/posix_spawn_pipe_test.h diff --git a/src/system/libroot/posix/spawn.cpp b/src/system/libroot/posix/spawn.cpp index d849906ea5..5bbd454f50 100644 --- a/src/system/libroot/posix/spawn.cpp +++ b/src/system/libroot/posix/spawn.cpp @@ -408,7 +408,7 @@ process_file_actions(const posix_spawn_file_actions_t *_actions, int *errfd) return errno; } } else if (action->type == file_action_dup2) { - if (dup2(action->action.dup2_action.srcfd, action->fd) != 0) + if (dup2(action->action.dup2_action.srcfd, action->fd) == -1) return errno; } } diff --git a/src/tests/system/libroot/posix/Jamfile b/src/tests/system/libroot/posix/Jamfile index a1c690b5b6..4db77cf867 100644 --- a/src/tests/system/libroot/posix/Jamfile +++ b/src/tests/system/libroot/posix/Jamfile @@ -42,6 +42,8 @@ SimpleTest pthread_barrier_test : pthread_barrier_test.cpp ; SimpleTest posix_spawn_test : posix_spawn_test.cpp ; SimpleTest posix_spawn_redir_test : posix_spawn_redir_test.c ; SimpleTest posix_spawn_redir_err : posix_spawn_redir_err.c ; +SimpleTest posix_spawn_pipe_test : posix_spawn_pipe_test.c ; +SimpleTest posix_spawn_pipe_err : posix_spawn_pipe_err.c ; # XSI tests SimpleTest xsi_msg_queue_test1 : xsi_msg_queue_test1.cpp ; diff --git a/src/tests/system/libroot/posix/posix_spawn_pipe_err.c b/src/tests/system/libroot/posix/posix_spawn_pipe_err.c new file mode 100644 index 0000000000..762bea3af1 --- /dev/null +++ b/src/tests/system/libroot/posix/posix_spawn_pipe_err.c @@ -0,0 +1,10 @@ +#include "posix_spawn_pipe_test.h" + +#include +#include + + int main() { + puts(testOut); + fputs(testErr, stderr); + return 0; +} diff --git a/src/tests/system/libroot/posix/posix_spawn_pipe_test.c b/src/tests/system/libroot/posix/posix_spawn_pipe_test.c new file mode 100644 index 0000000000..62da03c07f --- /dev/null +++ b/src/tests/system/libroot/posix/posix_spawn_pipe_test.c @@ -0,0 +1,56 @@ +#include "posix_spawn_pipe_test.h" + +#include +#include +#include +#include +#include + +#define panic(n, str) if (n != 0) { perror(str); return 1; } +#define readIdx 0 +#define writeIdx 1 + +int main() { + int out[2], err[2]; + posix_spawn_file_actions_t fdops; + pid_t pid; + char* const argv[] = { "./posix_spawn_pipe_err", NULL }; + + panic(pipe(out), "pipe stdout"); + panic(pipe(err), "pipe stderr"); + + errno = posix_spawn_file_actions_init(&fdops); + panic(errno, "init"); + errno = posix_spawn_file_actions_addclose(&fdops, out[readIdx]); + panic(errno, "close stdout read"); + errno = posix_spawn_file_actions_adddup2(&fdops, out[writeIdx], 1); + panic(errno, "dup2 stdout write"); + errno = posix_spawn_file_actions_addclose(&fdops, err[readIdx]); + panic(errno, "close stderr read"); + errno = posix_spawn_file_actions_adddup2(&fdops, err[writeIdx], 2); + panic(errno, "dup2 stderr write"); + errno = posix_spawn(&pid, "./posix_spawn_pipe_err", &fdops, NULL, argv, NULL); + panic(errno, "spawn"); + + FILE *cOut = fdopen(out[readIdx], "r"); + if (cOut == NULL) panic(-1, "cOut"); + FILE *cErr = fdopen(err[readIdx], "r"); + if (cErr == NULL) panic(-1, "cErr"); + + char *buf = NULL; + size_t bufsize = 0; + getline(&buf, &bufsize, cOut); + panic(ferror(cOut), "getline cOut"); + if (strcmp(buf, testOut) != 0) { + printf("stdout got: %s", buf); + printf("stdout exp: %s", testOut); + } + getline(&buf, &bufsize, cErr); + panic(ferror(cErr), "getline cErr"); + if (strcmp(buf, testErr) != 0) { + printf("stderr got: %s", buf); + printf("stderr exp: %s", testErr); + } + + return 0; +} diff --git a/src/tests/system/libroot/posix/posix_spawn_pipe_test.h b/src/tests/system/libroot/posix/posix_spawn_pipe_test.h new file mode 100644 index 0000000000..396fe34f00 --- /dev/null +++ b/src/tests/system/libroot/posix/posix_spawn_pipe_test.h @@ -0,0 +1,5 @@ +#ifndef t_h +#define t_h +#define testOut "test out\n" +#define testErr "test err\n" +#endif