posix_spawn(): dup2() again this time on file_action_dup2.

* fixes #14322.
This commit is contained in:
Jérôme Duval 2018-08-06 19:13:32 +02:00
parent 5d0fd0e422
commit c90a0ee947
5 changed files with 74 additions and 1 deletions

View File

@ -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;
}
}

View File

@ -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 ;

View File

@ -0,0 +1,10 @@
#include "posix_spawn_pipe_test.h"
#include <stdio.h>
#include <string.h>
int main() {
puts(testOut);
fputs(testErr, stderr);
return 0;
}

View File

@ -0,0 +1,56 @@
#include "posix_spawn_pipe_test.h"
#include <errno.h>
#include <unistd.h>
#include <spawn.h>
#include <stdio.h>
#include <string.h>
#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;
}

View File

@ -0,0 +1,5 @@
#ifndef t_h
#define t_h
#define testOut "test out\n"
#define testErr "test err\n"
#endif