On Mon, Aug 06, 2018 at 06:23:28PM +0200, Matthieu Herrb wrote:
> Hi,
>
> fcntl(2) says:
>
> F_GETFD Get the close-on-exec flag associated with the file
> descriptor fd as FD_CLOEXEC. If the returned value
> ANDed with FD_CLOEXEC is 0, the file will remain open
> across exec(), otherwise the file will be closed upon
> execution of exec() (arg is ignored).
>
> This is wrong. The flag is returned in the low bit of the return
> value (see sys/kern/kern_descrip.c:443).
> Moreover the value O_CLOEXEC is not 0x1, so doing the AND described
> above is never going to provide the good answer.
>
> I've checked that Linux and FreeBSD also return 0x1 when the O_CLOEXEC
> is set, and none of them defines O_CLOEXEC as 0x1. The same
> documentation bug is present on FreenBSD.
>
> How to repeat:
>
> #include <err.h>
> #include <fcntl.h>
> #include <stdio.h>
>
> int
> main(int argc, char *argv[])
> {
> int fd, flags;
>
> fd = open("/dev/null", O_RDWR | O_CLOEXEC);
> if (fd < 0)
> err(2, "open");
> flags = fcntl(fd, F_GETFD, 0);
> if (flags < 0)
> err(2, "fcntl");
> printf("flags 0x%08x O_CLOEXEC 0x%08x\n", flags, O_CLOEXEC);
> return 0;
> }
There is a difference between FD_CLOEXEC and O_CLOEXEC. FD_CLOEXEC is
defined as 1 and so the man page is correct.
> Patch
>
> The patch below fixes the documentation.
>
> Index: fcntl.2
> ===================================================================
> RCS file: /cvs/OpenBSD/src/lib/libc/sys/fcntl.2,v
> retrieving revision 1.31
> diff -u -p -u -r1.31 fcntl.2
> --- fcntl.2 16 Dec 2014 00:06:49 -0000 1.31
> +++ fcntl.2 6 Aug 2018 16:10:53 -0000
> @@ -100,10 +100,7 @@ Get the close-on-exec flag associated wi
> .Fa fd
> as
> .Dv FD_CLOEXEC .
> -If the returned value ANDed with
> -.Dv FD_CLOEXEC
> -is 0,
> -the file will remain open across
> +If the returned value is 0, the file will remain open across
> .Fn exec ,
> otherwise the file will be closed upon execution of
> .Fn exec
>
>
--
:wq Claudio