Hello: On systems with support for open(O_CLOEXEC): $ zsh -c 'zmodload zsh/system; sysopen -w -o cloexec -u 3 /dev/null; ls -l /proc/self/fd' total 0 lrwx------ 1 chazelas chazelas 64 May 23 16:35 0 -> /dev/pts/1 lrwx------ 1 chazelas chazelas 64 May 23 16:35 1 -> /dev/pts/1 lrwx------ 1 chazelas chazelas 64 May 23 16:35 2 -> /dev/pts/1 lr-x------ 1 chazelas chazelas 64 May 23 16:35 3 -> /proc/21668/fd (OK) $ zsh -c 'zmodload zsh/system; sysopen -w -o cloexec -u 4 /dev/null; ls -l /proc/self/fd' total 0 lrwx------ 1 chazelas chazelas 64 May 23 16:35 0 -> /dev/pts/1 lrwx------ 1 chazelas chazelas 64 May 23 16:35 1 -> /dev/pts/1 lrwx------ 1 chazelas chazelas 64 May 23 16:35 2 -> /dev/pts/1 lr-x------ 1 chazelas chazelas 64 May 23 16:35 3 -> /proc/21669/fd l-wx------ 1 chazelas chazelas 64 May 23 16:35 4 -> /dev/null (not OK: fd 4 passed to ls) $ zsh -c 'zmodload zsh/system; sysopen -w -o cloexec -u fd /dev/null; ls -l /proc/self/fd' total 0 lrwx------ 1 chazelas chazelas 64 May 23 16:35 0 -> /dev/pts/1 lrwx------ 1 chazelas chazelas 64 May 23 16:35 1 -> /dev/pts/1 l-wx------ 1 chazelas chazelas 64 May 23 16:35 12 -> /dev/null lrwx------ 1 chazelas chazelas 64 May 23 16:35 2 -> /dev/pts/1 lr-x------ 1 chazelas chazelas 64 May 23 16:35 3 -> /proc/21674/fd (not OK: fd 3 passed to ls). The reason is that in the last two cases, dup()/dup2() was called on the fd returned by open(). In the first case to make it the requested "3", and in the second case so it's a fd above 10. The cloexec flag is one that is attached to the fd, not the open file description, so it doesn't survive a dup(). So one needs to reapply the CLOEXEC flag again after the dup. See attached for a suggested fix. Linux has a dup3() which you can pass the CLOEXEC flag to, but I don't expect that to be very portable. The code could be simplified so that fcntl() is always called as it's likely it's going to be called anyway. -- Stephane