#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static int getafile(void);
static int
getafile(void)
{
int fd;
char temp[] = "/tmp/dup2XXXXXXXXX";
if ((fd = mkstemp(temp)) < 0)
err(1, "mkstemp");
remove(temp);
if (ftruncate(fd, 1024) != 0)
err(1, "ftruncate");
return (fd);
}
int
main(int __unused argc, char __unused *argv[])
{
struct rlimit rlp;
int orgfd, fd1, fd2, test = 0;
orgfd = getafile();
printf("1..43\n");
if ((fd1 = dup(orgfd)) < 0)
err(1, "dup");
printf("ok %d - dup(2) works\n", ++test);
if (fcntl(fd1, F_SETFD, 1) != 0)
err(1, "fcntl(F_SETFD)");
if ((fd2 = dup2(fd1, fd1 + 1)) < 0)
err(1, "dup2");
printf("ok %d - dup2(2) works\n", ++test);
++test;
if (fd2 != fd1 + 1)
printf("no ok %d - dup2(2) didn't give us the right fd\n",
test);
else
printf("ok %d - dup2(2) returned a correct fd\n", test);
++test;
if (fcntl(fd2, F_GETFD) != 0)
printf("not ok %d - dup2(2) didn't clear close-on-exec\n",
test);
else
printf("ok %d - dup2(2) cleared close-on-exec\n", test);
if ((fd2 = dup2(fd1, fd1)) < 0)
err(1, "dup2");
printf("ok %d - dup2(2) to itself works\n", ++test);
++test;
if (fd2 != fd1)
printf("not ok %d - dup2(2) didn't give us the right fd\n",
test);
else
printf("ok %d - dup2(2) to itself returned a correct fd\n",
test);
++test;
if (fcntl(fd2, F_GETFD) == 0)
printf("not ok %d - dup2(2) cleared close-on-exec\n", test);
else
printf("ok %d - dup2(2) didn't clear close-on-exec\n", test);
if ((fd2 = fcntl(fd1, F_DUPFD, 10)) < 0)
err(1, "fcntl(F_DUPFD)");
if (fd2 < 10)
printf("not ok %d - fcntl(F_DUPFD) returned wrong fd %d\n",
++test, fd2);
else
printf("ok %d - fcntl(F_DUPFD) works\n", ++test);
++test;
if (fcntl(fd2, F_GETFD) != 0)
printf(
"not ok %d - fcntl(F_DUPFD) didn't clear close-on-exec\n",
test);
else
printf("ok %d - fcntl(F_DUPFD) cleared close-on-exec\n", test);
++test;
if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
err(1, "getrlimit");
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 ((fd2 = fcntl(fd1, F_DUP2FD, fd1 + 1)) < 0)
err(1, "fcntl(F_DUP2FD)");
printf("ok %d - fcntl(F_DUP2FD) works\n", ++test);
++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);
++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);
if ((fd2 = fcntl(fd1, F_DUP2FD, fd1)) < 0)
err(1, "fcntl(F_DUP2FD)");
printf("ok %d - fcntl(F_DUP2FD) to itself works\n", ++test);
++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);
++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);
if ((fd2 = fcntl(fd1, F_DUPFD_CLOEXEC, 10)) < 0)
err(1, "fcntl(F_DUPFD_CLOEXEC)");
if (fd2 < 10)
printf("not ok %d - fcntl(F_DUPFD_CLOEXEC) returned wrong fd %d\n",
++test, fd2);
else
printf("ok %d - fcntl(F_DUPFD_CLOEXEC) works\n", ++test);
++test;
if (fcntl(fd2, F_GETFD) != 1)
printf(
"not ok %d - fcntl(F_DUPFD_CLOEXEC) didn't set close-on-exec\n",
test);
else
printf("ok %d - fcntl(F_DUPFD_CLOEXEC) set close-on-exec\n",
test);
if ((fd2 = fcntl(fd1, F_DUP2FD_CLOEXEC, fd1 + 1)) < 0)
err(1, "fcntl(F_DUP2FD_CLOEXEC)");
printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) works\n", ++test);
++test;
if (fd2 != fd1 + 1)
printf(
"no ok %d - fcntl(F_DUP2FD_CLOEXEC) didn't give us the right fd\n",
test);
else
printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) returned a correct fd\n",
test);
++test;
if (fcntl(fd2, F_GETFD) != FD_CLOEXEC)
printf(
"not ok %d - fcntl(F_DUP2FD_CLOEXEC) didn't set close-on-exec\n",
test);
else
printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) set close-on-exec\n",
test);
++test;
if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
err(1, "getrlimit");
if ((fd2 = fcntl(fd1, F_DUP2FD_CLOEXEC, rlp.rlim_cur + 1)) >= 0)
printf("not ok %d - fcntl(F_DUP2FD_CLOEXEC) bypassed NOFILE limit\n",
test);
else
printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) didn't bypass NOFILE limit\n",
test);
if ((fd2 = dup3(fd1, fd1 + 1, O_CLOEXEC)) < 0)
err(1, "dup3(O_CLOEXEC)");
printf("ok %d - dup3(O_CLOEXEC) works\n", ++test);
++test;
if (fd2 != fd1 + 1)
printf(
"no ok %d - dup3(O_CLOEXEC) didn't give us the right fd\n",
test);
else
printf("ok %d - dup3(O_CLOEXEC) returned a correct fd\n",
test);
++test;
if (fcntl(fd2, F_GETFD) != FD_CLOEXEC)
printf(
"not ok %d - dup3(O_CLOEXEC) didn't set close-on-exec\n",
test);
else
printf("ok %d - dup3(O_CLOEXEC) set close-on-exec\n",
test);
if ((fd2 = dup3(fd1, fd1 + 1, 0)) < 0)
err(1, "dup3(0)");
printf("ok %d - dup3(0) works\n", ++test);
++test;
if (fd2 != fd1 + 1)
printf(
"no ok %d - dup3(0) didn't give us the right fd\n",
test);
else
printf("ok %d - dup3(0) returned a correct fd\n",
test);
++test;
if (fcntl(fd2, F_GETFD) != 0)
printf(
"not ok %d - dup3(0) didn't clear close-on-exec\n",
test);
else
printf("ok %d - dup3(0) cleared close-on-exec\n",
test);
++test;
if (dup3(fd1, fd1, O_CLOEXEC) != -1)
printf(
"not ok %d - dup3(fd1, fd1, O_CLOEXEC) succeeded\n", test);
else
printf("ok %d - dup3(fd1, fd1, O_CLOEXEC) failed\n", test);
++test;
if (dup3(fd1, fd1, 0) != -1)
printf(
"not ok %d - dup3(fd1, fd1, 0) succeeded\n", test);
else
printf("ok %d - dup3(fd1, fd1, 0) failed\n", test);
++test;
if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
err(1, "getrlimit");
if ((fd2 = dup3(fd1, rlp.rlim_cur + 1, O_CLOEXEC)) >= 0)
printf("not ok %d - dup3(O_CLOEXEC) bypassed NOFILE limit\n",
test);
else
printf("ok %d - dup3(O_CLOEXEC) didn't bypass NOFILE limit\n",
test);
if ((fd2 = fcntl(fd1, F_DUPFD_CLOFORK, 10)) < 0)
err(1, "fcntl(F_DUPFD_CLOFORK)");
if (fd2 < 10)
printf("not ok %d - fcntl(F_DUPFD_CLOFORK) returned wrong fd %d\n",
++test, fd2);
else
printf("ok %d - fcntl(F_DUPFD_CLOFORK) works\n", ++test);
++test;
if (fcntl(fd2, F_GETFD) != FD_CLOFORK)
printf(
"not ok %d - fcntl(F_DUPFD_CLOFORK) didn't set close-on-fork\n",
test);
else
printf("ok %d - fcntl(F_DUPFD_CLOFORK) set close-on-fork\n",
test);
if ((fd2 = dup3(fd1, fd1 + 1, O_CLOFORK)) < 0)
err(1, "dup3(O_CLOFORK)");
printf("ok %d - dup3(O_CLOFORK) works\n", ++test);
++test;
if (fd2 != fd1 + 1)
printf(
"no ok %d - dup3(O_CLOFORK) didn't give us the right fd\n",
test);
else
printf("ok %d - dup3(O_CLOFORK) returned a correct fd\n",
test);
++test;
if (fcntl(fd2, F_GETFD) != FD_CLOFORK)
printf(
"not ok %d - dup3(O_CLOFORK) didn't set close-on-fork\n",
test);
else
printf("ok %d - dup3(O_CLOFORK) set close-on-fork\n",
test);
if ((fd2 = dup3(fd1, fd1 + 1, 0)) < 0)
err(1, "dup3(0)");
printf("ok %d - dup3(0) works\n", ++test);
++test;
if (fd2 != fd1 + 1)
printf(
"no ok %d - dup3(0) didn't give us the right fd\n",
test);
else
printf("ok %d - dup3(0) returned a correct fd\n",
test);
++test;
if (fcntl(fd2, F_GETFD) != 0)
printf(
"not ok %d - dup3(0) didn't clear close-on-fork\n",
test);
else
printf("ok %d - dup3(0) cleared close-on-fork\n",
test);
++test;
if (dup3(fd1, fd1, O_CLOFORK) != -1)
printf(
"not ok %d - dup3(fd1, fd1, O_CLOFORK) succeeded\n", test);
else
printf("ok %d - dup3(fd1, fd1, O_CLOFORK) failed\n", test);
++test;
if (dup3(fd1, fd1, 0) != -1)
printf(
"not ok %d - dup3(fd1, fd1, 0) succeeded\n", test);
else
printf("ok %d - dup3(fd1, fd1, 0) failed\n", test);
++test;
if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
err(1, "getrlimit");
if ((fd2 = dup3(fd1, rlp.rlim_cur + 1, O_CLOFORK)) >= 0)
printf("not ok %d - dup3(O_CLOFORK) bypassed NOFILE limit\n",
test);
else
printf("ok %d - dup3(O_CLOFORK) didn't bypass NOFILE limit\n",
test);
return (0);
}