diff options
author | antoine <antoine@FreeBSD.org> | 2008-03-08 22:02:21 +0000 |
---|---|---|
committer | antoine <antoine@FreeBSD.org> | 2008-03-08 22:02:21 +0000 |
commit | 514f31f40ed28fea8fdc190c743417debb0d03b3 (patch) | |
tree | a5c6d546b6b95056f3d9779f9c1afae2cec004f6 /tools/regression | |
parent | 587a314ce88f943ab58c211714655de88ddbdd25 (diff) | |
download | FreeBSD-src-514f31f40ed28fea8fdc190c743417debb0d03b3.zip FreeBSD-src-514f31f40ed28fea8fdc190c743417debb0d03b3.tar.gz |
Introduce a new F_DUP2FD command to fcntl(2), for compatibility with
Solaris and AIX.
fcntl(fd, F_DUP2FD, arg) and dup2(fd, arg) are functionnaly equivalent.
Document it.
Add some regression tests (identical to the dup2(2) regression tests).
PR: 120233
Submitted by: Jukka Ukkonen
Approved by: rwaston (mentor)
MFC after: 1 month
Diffstat (limited to 'tools/regression')
-rw-r--r-- | tools/regression/file/dup/dup.c | 73 |
1 files changed, 71 insertions, 2 deletions
diff --git a/tools/regression/file/dup/dup.c b/tools/regression/file/dup/dup.c index ad1f6df..d6dd0ff 100644 --- a/tools/regression/file/dup/dup.c +++ b/tools/regression/file/dup/dup.c @@ -20,6 +20,15 @@ * Test #9: check if fcntl(F_DUPFD) cleared close-on-exec flag for duped fd. * Test #10: check if dup2() to a fd > current maximum number of open files * limit work. + * Test #11: check if fcntl(F_DUP2FD) works. + * Test #12: check if fcntl(F_DUP2FD) returned a fd we asked for. + * Test #13: check if fcntl(F_DUP2FD) cleared close-on-exec flag for duped fd. + * Test #14: check if fcntl(F_DUP2FD) allows to dup fd to itself. + * Test #15: check if fcntl(F_DUP2FD) returned a fd we asked for. + * Test #16: check if fcntl(F_DUP2FD) did not clear close-on-exec flag for + * duped fd. + * Test #17: check if fcntl(F_DUP2FD) to a fd > current maximum number of open + * files limit work. */ #include <sys/types.h> @@ -56,7 +65,7 @@ main(int __unused argc, char __unused *argv[]) orgfd = getafile(); - printf("1..10\n"); + printf("1..17\n"); /* If dup(2) ever work? */ if ((fd1 = dup(orgfd)) < 0) @@ -151,10 +160,70 @@ main(int __unused argc, char __unused *argv[]) ++test; if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) err(1, "getrlimit"); - if ((fd2 = dup2(fd1, rlp.rlim_cur + 1)) == 0) + if ((fd2 = dup2(fd1, rlp.rlim_cur + 1)) >= 0) printf("not ok %d - dup2(2) bypassed NOFILE limit\n", test); else printf("ok %d - dup2(2) didn't bypass NOFILE limit\n", test); + /* If fcntl(F_DUP2FD) ever work? */ + if ((fd2 = fcntl(fd1, F_DUP2FD, fd1 + 1)) < 0) + err(1, "fcntl(F_DUP2FD)"); + printf("ok %d - fcntl(F_DUP2FD) works\n", ++test); + + /* Do we get the right fd? */ + ++test; + if (fd2 != fd1 + 1) + printf( + "no ok %d - fcntl(F_DUP2FD) didn't give us the right fd\n", + test); + else + printf("ok %d - fcntl(F_DUP2FD) returned a correct fd\n", + test); + + /* Was close-on-exec cleared? */ + ++test; + if (fcntl(fd2, F_GETFD) != 0) + printf( + "not ok %d - fcntl(F_DUP2FD) didn't clear close-on-exec\n", + test); + else + printf("ok %d - fcntl(F_DUP2FD) cleared close-on-exec\n", + test); + + /* Dup to itself */ + if ((fd2 = fcntl(fd1, F_DUP2FD, fd1)) < 0) + err(1, "fcntl(F_DUP2FD)"); + printf("ok %d - fcntl(F_DUP2FD) to itself works\n", ++test); + + /* Do we get the right fd? */ + ++test; + if (fd2 != fd1) + printf( + "not ok %d - fcntl(F_DUP2FD) didn't give us the right fd\n", + test); + else + printf( + "ok %d - fcntl(F_DUP2FD) to itself returned a correct fd\n", + test); + + /* Was close-on-exec cleared? */ + ++test; + if (fcntl(fd2, F_GETFD) == 0) + printf("not ok %d - fcntl(F_DUP2FD) cleared close-on-exec\n", + test); + else + printf("ok %d - fcntl(F_DUP2FD) didn't clear close-on-exec\n", + test); + + ++test; + if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) + err(1, "getrlimit"); + if ((fd2 = fcntl(fd1, F_DUP2FD, rlp.rlim_cur + 1)) >= 0) + printf("not ok %d - fcntl(F_DUP2FD) bypassed NOFILE limit\n", + test); + else + printf("ok %d - fcntl(F_DUP2FD) didn't bypass NOFILE limit\n", + test); + return (0); } |