Below are steps to reproduce it in docker, logs from docker and strace log.
Copy the posix_spawn_sample.c code below (note it uses "ash" in 'char *argv[] = {"ash", ,,,}' for posix_spawn on Alpine Linux )
Note that "test.log" file is listed by "lsof -p PID" command.
> docker run --rm -it alpine:3.11.3 ash
/ # apk add libc-dev
(1/2) Installing musl-dev (1.1.24-r0)
(2/2) Installing libc-dev (0.7.2-r0)
OK: 111 MiB in 26 packages
/ # gcc -o posix_spawn_sample posix_spawn_sample.c
/ # ./posix_spawn_sample
Child pid: 17
PID=17
1 /bin/busybox /dev/pts/0
1 /bin/busybox /dev/pts/0
1 /bin/busybox /dev/pts/0
1 /bin/busybox /dev/tty
16 /posix_spawn_sample /dev/pts/0
16 /posix_spawn_sample /dev/pts/0
16 /posix_spawn_sample /dev/pts/0
16 /posix_spawn_sample /test.log
--------------------------------------
strace log
--------------------------------------
/ # strace -f ./posix_spawn_sample
execve("./posix_spawn_sample", ["./posix_spawn_sample"], 0x7ffd951eb318 /* 6 vars */) = 0
arch_prctl(ARCH_SET_FS, 0x7f0fa33d6d48) = 0
set_tid_address(0x7f0fa33d731c) = 31
mprotect(0x7f0fa33d3000, 4096, PROT_READ) = 0
mprotect(0x55e26cf7b000, 4096, PROT_READ) = 0
open("test.log", O_WRONLY|O_CREAT|O_APPEND|O_CLOEXEC, 0666) = 3
fcntl(3, F_SETFD, FD_CLOEXEC) = 0
fcntl(3, F_SETFD, FD_CLOEXEC) = 0
fcntl(3, F_GETFL) = 0x8401 (flags O_WRONLY|O_APPEND|O_LARGEFILE)
ioctl(3, TIOCGWINSZ, 0x7ffd87e838a8) = -1 ENOTTY (Not a tty)
pipe2([4, 5], O_CLOEXEC) = 0
rt_sigprocmask(SIG_BLOCK, ~[], [], 8) = 0
clone(child_stack=0x7ffd87e838b8, flags=CLONE_VM|CLONE_VFORK|SIGCHLDstrace: Process 32 attached
<unfinished ...>
[pid 32] close(4) = 0
[pid 32] fcntl(5, F_SETFD, FD_CLOEXEC) = 0
[pid 32] rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
[pid 32] execve("/bin/sh", ["ash", "-c", "echo PID=$$ && lsof -p $$"], 0x7ffd87e83998 /* 6 vars */ <unfinished ...>
[pid 31] <... clone resumed>) = 32
[pid 32] <... execve resumed>) = 0
[pid 31] close(5 <unfinished ...>
[pid 32] arch_prctl(ARCH_SET_FS, 0x7f1a19d9ed48 <unfinished ...>
[pid 31] <... close resumed>) = 0
[pid 31] read(4, <unfinished ...>
[pid 32] <... arch_prctl resumed>) = 0
[pid 31] <... read resumed>"", 4) = 0
[pid 32] set_tid_address(0x7f1a19d9f31c <unfinished ...>
[pid 31] close(4 <unfinished ...>
[pid 32] <... set_tid_address resumed>) = 32
[pid 31] <... close resumed>) = 0
[pid 32] mprotect(0x7f1a19d9b000, 4096, PROT_READ <unfinished ...>
[pid 31] rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
[pid 32] <... mprotect resumed>) = 0
[pid 31] ioctl(1, TIOCGWINSZ, {ws_row=40, ws_col=155, ws_xpixel=0, ws_ypixel=0}) = 0
[pid 32] mprotect(0x556eae13a000, 16384, PROT_READ <unfinished ...>
[pid 31] writev(1, [{iov_base="Child pid: 32", iov_len=13}, {iov_base="\n", iov_len=1}], 2 <unfinished ...>
[pid 32] <... mprotect resumed>) = 0Child pid: 32
[pid 31] <... writev resumed>) = 14
[pid 31] wait4(32, <unfinished ...>
[pid 32] getuid() = 0
[pid 32] getpid() = 32
[pid 32] rt_sigprocmask(SIG_UNBLOCK, [RT_1 RT_2], NULL, 8) = 0
[pid 32] rt_sigaction(SIGCHLD, {sa_handler=0x556eae0b9dfc, sa_mask=~[RTMIN RT_1 RT_2], sa_flags=SA_RESTORER, sa_restorer=0x7f1a19d500d4}, NULL, 8) = 0
[pid 32] rt_sigaction(SIGHUP, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f1a19d500d4}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
[pid 32] getppid() = 31
[pid 32] stat("/", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
[pid 32] stat(".", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
[pid 32] rt_sigaction(SIGINT, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
[pid 32] rt_sigaction(SIGINT, {sa_handler=0x556eae0b9dfc, sa_mask=~[RTMIN RT_1 RT_2], sa_flags=SA_RESTORER, sa_restorer=0x7f1a19d500d4}, NULL, 8) = 0
[pid 32] rt_sigaction(SIGQUIT, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
[pid 32] rt_sigaction(SIGQUIT, {sa_handler=SIG_IGN, sa_mask=~[RTMIN RT_1 RT_2], sa_flags=SA_RESTORER, sa_restorer=0x7f1a19d500d4}, NULL, 8) = 0
[pid 32] rt_sigaction(SIGTERM, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
[pid 32] wait4(-1, 0x7fff4e3ea22c, WNOHANG, NULL) = -1 ECHILD (No child process)
[pid 32] write(1, "PID=32\n", 7PID=32
) = 7
[pid 32] stat("/usr/local/sbin/lsof", 0x7fff4e3ea170) = -1 ENOENT (No such file or directory)
[pid 32] stat("/usr/local/bin/lsof", 0x7fff4e3ea170) = -1 ENOENT (No such file or directory)
[pid 32] stat("/usr/sbin/lsof", 0x7fff4e3ea170) = -1 ENOENT (No such file or directory)
[pid 32] stat("/usr/bin/lsof", {st_mode=S_IFREG|0755, st_size=841288, ...}) = 0
[pid 32] brk(NULL) = 0x556eaf7c5000
[pid 32] brk(0x556eaf7c6000) = 0x556eaf7c6000
[pid 32] execve("/usr/bin/lsof", ["lsof", "-p", "32"], 0x556eae13ed80 /* 6 vars */) = 0
[pid 32] arch_prctl(ARCH_SET_FS, 0x7fe42bc41d48) = 0
[pid 32] set_tid_address(0x7fe42bc4231c) = 32
[pid 32] mprotect(0x7fe42bc3e000, 4096, PROT_READ) = 0
[pid 32] mprotect(0x55d56c79b000, 16384, PROT_READ) = 0
[pid 32] getuid() = 0
[pid 32] open("/proc", O_RDONLY|O_CLOEXEC|O_DIRECTORY) = 3
[pid 32] fcntl(3, F_SETFD, FD_CLOEXEC) = 0
[pid 32] getdents64(3, /* 67 entries */, 2048) = 1984
[pid 32] readlink("/proc/1/exe", "/bin/busybox", 80) = 12
[pid 32] getpid() = 32
[pid 32] open("/proc/1/fd/", O_RDONLY|O_CLOEXEC|O_DIRECTORY) = 4
[pid 32] fcntl(4, F_SETFD, FD_CLOEXEC) = 0
[pid 32] brk(NULL) = 0x55d56dac0000
[pid 32] brk(0x55d56dac1000) = 0x55d56dac1000
[pid 32] getdents64(4, /* 6 entries */, 2048) = 144
[pid 32] readlink("/proc/1/fd/0", "/dev/pts/0", 80) = 10
[pid 32] ioctl(1, TIOCGWINSZ, {ws_row=40, ws_col=155, ws_xpixel=0, ws_ypixel=0}) = 0
[pid 32] writev(1, [{iov_base="1\t/bin/busybox\t/dev/pts/0", iov_len=25}, {iov_base="\n", iov_len=1}], 21 /bin/busybox /dev/pts/0
) = 26
[pid 32] readlink("/proc/1/fd/1", "/dev/pts/0", 80) = 10
[pid 32] writev(1, [{iov_base="1\t/bin/busybox\t/dev/pts/0", iov_len=25}, {iov_base="\n", iov_len=1}], 21 /bin/busybox /dev/pts/0
) = 26
[pid 32] readlink("/proc/1/fd/2", "/dev/pts/0", 80) = 10
[pid 32] writev(1, [{iov_base="1\t/bin/busybox\t/dev/pts/0", iov_len=25}, {iov_base="\n", iov_len=1}], 21 /bin/busybox /dev/pts/0
) = 26
[pid 32] readlink("/proc/1/fd/10", "/dev/tty", 80) = 8
[pid 32] writev(1, [{iov_base="1\t/bin/busybox\t/dev/tty", iov_len=23}, {iov_base="\n", iov_len=1}], 21 /bin/busybox /dev/tty
) = 24
[pid 32] getdents64(4, /* 0 entries */, 2048) = 0
[pid 32] close(4) = 0
[pid 32] readlink("/proc/28/exe", "/usr/bin/strace", 80) = 15
[pid 32] getpid() = 32
[pid 32] open("/proc/28/fd/", O_RDONLY|O_CLOEXEC|O_DIRECTORY) = 4
[pid 32] fcntl(4, F_SETFD, FD_CLOEXEC) = 0
[pid 32] getdents64(4, /* 5 entries */, 2048) = 120
[pid 32] readlink("/proc/28/fd/0", "pipe:[89317]", 80) = 12
[pid 32] writev(1, [{iov_base="28\t/usr/bin/strace\tpipe:[89317]", iov_len=31}, {iov_base="\n", iov_len=1}], 228 /usr/bin/strace pipe:[89317]
) = 32
[pid 32] readlink("/proc/28/fd/1", "pipe:[89318]", 80) = 12
[pid 32] writev(1, [{iov_base="28\t/usr/bin/strace\tpipe:[89318]", iov_len=31}, {iov_base="\n", iov_len=1}], 228 /usr/bin/strace pipe:[89318]
) = 32
[pid 32] readlink("/proc/28/fd/2", "/dev/pts/0", 80) = 10
[pid 32] writev(1, [{iov_base="28\t/usr/bin/strace\t/dev/pts/0", iov_len=29}, {iov_base="\n", iov_len=1}], 228 /usr/bin/strace /dev/pts/0
) = 30
[pid 32] getdents64(4, /* 0 entries */, 2048) = 0
[pid 32] close(4) = 0
[pid 32] readlink("/proc/31/exe", "/posix_spawn_sample", 80) = 19
[pid 32] getpid() = 32
[pid 32] open("/proc/31/fd/", O_RDONLY|O_CLOEXEC|O_DIRECTORY) = 4
[pid 32] fcntl(4, F_SETFD, FD_CLOEXEC) = 0
[pid 32] getdents64(4, /* 6 entries */, 2048) = 144
[pid 32] readlink("/proc/31/fd/0", "/dev/pts/0", 80) = 10
[pid 32] writev(1, [{iov_base="31\t/posix_spawn_sample\t/dev/pts/"..., iov_len=33}, {iov_base="\n", iov_len=1}], 231 /posix_spawn_sample /dev/pts/0
) = 34
[pid 32] readlink("/proc/31/fd/1", "/dev/pts/0", 80) = 10
[pid 32] writev(1, [{iov_base="31\t/posix_spawn_sample\t/dev/pts/"..., iov_len=33}, {iov_base="\n", iov_len=1}], 231 /posix_spawn_sample /dev/pts/0
) = 34
[pid 32] readlink("/proc/31/fd/2", "/dev/pts/0", 80) = 10
[pid 32] writev(1, [{iov_base="31\t/posix_spawn_sample\t/dev/pts/"..., iov_len=33}, {iov_base="\n", iov_len=1}], 231 /posix_spawn_sample /dev/pts/0
) = 34
[pid 32] readlink("/proc/31/fd/3", "/test.log", 80) = 9
[pid 32] writev(1, [{iov_base="31\t/posix_spawn_sample\t/test.log", iov_len=32}, {iov_base="\n", iov_len=1}], 231 /posix_spawn_sample /test.log
) = 33
[pid 32] getdents64(4, /* 0 entries */, 2048) = 0
[pid 32] close(4) = 0
[pid 32] readlink("/proc/32/exe", "/bin/busybox", 80) = 12
[pid 32] getpid() = 32
[pid 32] getdents64(3, /* 0 entries */, 2048) = 0
[pid 32] close(3) = 0
[pid 32] exit_group(0) = ?
[pid 32] +++ exited with 0 +++
<... wait4 resumed>[{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 32
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=32, si_uid=0, si_status=0, si_utime=0, si_stime=0} ---
writev(3, [{iov_base="test line\n", iov_len=10}, {iov_base=NULL, iov_len=0}], 2) = 10
close(3) = 0
exit_group(0) = ?
+++ exited with 0 +++
--------------------------------------
Thanks,
Alexander.