#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
if (argc < 2) {
exit(1);
}
int result = lseek(atoi(argv[1]), 0, SEEK_SET);
printf("checkfd result = %d\n", result);
}
To test:
# cc fdcloexec.c -o fdcloexec
# cc checkfd.c -o checkfd
# ./fdcloexec
mydupfd = 4
checkfd result = -1
The first program opens a file and passes that file number as an argument to the checkfd program which it executes with system.
If F_DUPFD_CLOEXEC is used to duplicate the file number then the checkfd result should be -1. This indicates the file number passed to checkfd was closed when the program checkfd was executed.
If a plain F_DUPFD is used the checkfd result will be 0.
A PR is forthcoming which implements the F_DUPFD_CLOEXEC option.
Please support F_DUPFD_CLOEXEC for fcntl.
It is used by musl-1.1.24 library (which is used by live-bootstrap) to implement popen: https://git.musl-libc.org/cgit/musl/tree/src/stdio/popen.c?h=v1.1.24&id=ea9525c8bcf6170df59364c4bcd616de1acf8703
Here is a test program fdcloexec.c:
Here is the second test program checkfd.c:
To test:
The first program opens a file and passes that file number as an argument to the checkfd program which it executes with
system
.If F_DUPFD_CLOEXEC is used to duplicate the file number then the
checkfd
result should be-1
. This indicates the file number passed tocheckfd
was closed when the programcheckfd
was executed.If a plain F_DUPFD is used the checkfd result will be
0
.A PR is forthcoming which implements the F_DUPFD_CLOEXEC option.