#define _GNU_SOURCE

#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <err.h>

int
main(void)
{
    int fd1, fd2;
    pid_t pid;
    int status;

    fd1 = open("log", O_RDONLY);
    if (fd1 == -1)
        err(EXIT_FAILURE, "open");
    else
        printf("fd1=%d\n", fd1);

    //fd2 = dup3(fd1, 4, O_CLOEXEC);
    fd2 = fcntl(fd1, F_DUPFD_CLOEXEC, 20);

    if (fd2 == -1)
        err(EXIT_FAILURE, "dup3");
    else
        printf("fd2=%d\n", fd2);

    if (fork() == 0) {
        if (execl("child", "child", (char *) NULL) == -1)
            err(EXIT_FAILURE, "exec");
    }
    pid = wait(&status);
    printf("child pid=%d, status=%d\n", pid, status >> 8);
    exit(EXIT_SUCCESS);
}
