1#include <stdio.h>
2#include <fcntl.h>
3#include <spawn.h>
4#include <errno.h>
5
6#define panic(str) if (ret != 0) { errno = ret; perror(str); return 1; }
7
8int main() {
9        int ret;
10        pid_t child;
11        char* const av[] = { "posix_spawn_redir_err", NULL };
12        posix_spawn_file_actions_t child_fd_acts;
13        ret = posix_spawn_file_actions_init(&child_fd_acts);
14        panic("init");
15        ret = posix_spawn_file_actions_addopen(&child_fd_acts, 1, "errlog",
16                O_WRONLY | O_CREAT | O_TRUNC, 0644);
17        panic("addopen");
18        ret = posix_spawn_file_actions_adddup2(&child_fd_acts, 1, 2);
19        panic("adddup2");
20        ret = posix_spawn(&child, "./posix_spawn_redir_err", &child_fd_acts, NULL, av, NULL);
21        panic("spawn");
22        return 0;
23}
24
25