dup_test.c revision 250513
1164191Smaxim/* 2164191Smaxim * $OpenBSD: dup2test.c,v 1.3 2003/07/31 21:48:08 deraadt Exp $ 3164191Smaxim * $OpenBSD: dup2_self.c,v 1.3 2003/07/31 21:48:08 deraadt Exp $ 4164191Smaxim * $OpenBSD: fcntl_dup.c,v 1.2 2003/07/31 21:48:08 deraadt Exp $ 5164191Smaxim * 6164191Smaxim * Written by Artur Grabowski <art@openbsd.org> 2002 Public Domain. 7164191Smaxim * 8164191Smaxim * $FreeBSD: head/tools/regression/file/dup/dup.c 250513 2013-05-11 16:31:41Z jilles $ 9164191Smaxim */ 10164191Smaxim 11164191Smaxim/* 12164191Smaxim * Test #1: check if dup(2) works. 13164191Smaxim * Test #2: check if dup2(2) works. 14164191Smaxim * Test #3: check if dup2(2) returned a fd we asked for. 15164191Smaxim * Test #4: check if dup2(2) cleared close-on-exec flag for duped fd. 16164191Smaxim * Test #5: check if dup2(2) allows to dup fd to itself. 17164191Smaxim * Test #6: check if dup2(2) returned a fd we asked for. 18164191Smaxim * Test #7: check if dup2(2) did not clear close-on-exec flag for duped fd. 19164191Smaxim * Test #8: check if fcntl(F_DUPFD) works. 20164191Smaxim * Test #9: check if fcntl(F_DUPFD) cleared close-on-exec flag for duped fd. 21164191Smaxim * Test #10: check if dup2() to a fd > current maximum number of open files 22164191Smaxim * limit work. 23176957Santoine * Test #11: check if fcntl(F_DUP2FD) works. 24176957Santoine * Test #12: check if fcntl(F_DUP2FD) returned a fd we asked for. 25176957Santoine * Test #13: check if fcntl(F_DUP2FD) cleared close-on-exec flag for duped fd. 26176957Santoine * Test #14: check if fcntl(F_DUP2FD) allows to dup fd to itself. 27176957Santoine * Test #15: check if fcntl(F_DUP2FD) returned a fd we asked for. 28176957Santoine * Test #16: check if fcntl(F_DUP2FD) did not clear close-on-exec flag for 29176957Santoine * duped fd. 30176957Santoine * Test #17: check if fcntl(F_DUP2FD) to a fd > current maximum number of open 31176957Santoine * files limit work. 32250513Sjilles * Test #18: check if fcntl(F_DUPFD_CLOEXEC) works. 33250513Sjilles * Test #19: check if fcntl(F_DUPFD_CLOEXEC) set close-on-exec flag for duped 34250513Sjilles * fd. 35164191Smaxim */ 36164191Smaxim 37164191Smaxim#include <sys/types.h> 38164191Smaxim#include <sys/time.h> 39164191Smaxim#include <sys/resource.h> 40164191Smaxim 41164191Smaxim#include <err.h> 42164191Smaxim#include <fcntl.h> 43164191Smaxim#include <stdio.h> 44164191Smaxim#include <stdlib.h> 45164191Smaxim#include <unistd.h> 46164191Smaxim 47164191Smaximstatic int getafile(void); 48164191Smaxim 49164191Smaximstatic int 50164191Smaximgetafile(void) 51164191Smaxim{ 52164191Smaxim int fd; 53164191Smaxim 54164191Smaxim char temp[] = "/tmp/dup2XXXXXXXXX"; 55164191Smaxim if ((fd = mkstemp(temp)) < 0) 56164191Smaxim err(1, "mkstemp"); 57164191Smaxim remove(temp); 58164191Smaxim if (ftruncate(fd, 1024) != 0) 59164191Smaxim err(1, "ftruncate"); 60164191Smaxim return (fd); 61164191Smaxim} 62164191Smaxim 63164191Smaximint 64164191Smaximmain(int __unused argc, char __unused *argv[]) 65164191Smaxim{ 66164191Smaxim struct rlimit rlp; 67164191Smaxim int orgfd, fd1, fd2, test = 0; 68164191Smaxim 69164191Smaxim orgfd = getafile(); 70164191Smaxim 71250513Sjilles printf("1..19\n"); 72164191Smaxim 73164191Smaxim /* If dup(2) ever work? */ 74164191Smaxim if ((fd1 = dup(orgfd)) < 0) 75164191Smaxim err(1, "dup"); 76164191Smaxim printf("ok %d - dup(2) works\n", ++test); 77164191Smaxim 78164191Smaxim /* Set close-on-exec */ 79164191Smaxim if (fcntl(fd1, F_SETFD, 1) != 0) 80164191Smaxim err(1, "fcntl(F_SETFD)"); 81164191Smaxim 82164191Smaxim /* If dup2(2) ever work? */ 83164191Smaxim if ((fd2 = dup2(fd1, fd1 + 1)) < 0) 84164191Smaxim err(1, "dup2"); 85164191Smaxim printf("ok %d - dup2(2) works\n", ++test); 86164191Smaxim 87164191Smaxim /* Do we get the right fd? */ 88164191Smaxim ++test; 89164191Smaxim if (fd2 != fd1 + 1) 90164191Smaxim printf("no ok %d - dup2(2) didn't give us the right fd\n", 91164191Smaxim test); 92164191Smaxim else 93164191Smaxim printf("ok %d - dup2(2) returned a correct fd\n", test); 94164191Smaxim 95164191Smaxim /* Was close-on-exec cleared? */ 96164191Smaxim ++test; 97164191Smaxim if (fcntl(fd2, F_GETFD) != 0) 98164191Smaxim printf("not ok %d - dup2(2) didn't clear close-on-exec\n", 99164191Smaxim test); 100164191Smaxim else 101164191Smaxim printf("ok %d - dup2(2) cleared close-on-exec\n", test); 102164191Smaxim 103164191Smaxim /* 104164191Smaxim * Dup to itself. 105164191Smaxim * 106164191Smaxim * We're testing a small tweak in dup2 semantics. 107164191Smaxim * Normally dup and dup2 will clear the close-on-exec 108164191Smaxim * flag on the new fd (which appears to be an implementation 109164191Smaxim * mistake from start and not some planned behavior). 110228975Suqs * In today's implementations of dup and dup2 we have to make 111164191Smaxim * an effort to really clear that flag. But all tested 112164191Smaxim * implementations of dup2 have another tweak. If we 113164191Smaxim * dup2(old, new) when old == new, the syscall short-circuits 114164191Smaxim * and returns early (because there is no need to do all the 115164191Smaxim * work (and there is a risk for serious mistakes)). 116164191Smaxim * So although the docs say that dup2 should "take 'old', 117164191Smaxim * close 'new' perform a dup(2) of 'old' into 'new'" 118164191Smaxim * the docs are not really followed because close-on-exec 119164191Smaxim * is not cleared on 'new'. 120164191Smaxim * 121164191Smaxim * Since everyone has this bug, we pretend that this is 122164191Smaxim * the way it is supposed to be and test here that it really 123164191Smaxim * works that way. 124164191Smaxim * 125164191Smaxim * This is a fine example on where two separate implementation 126164191Smaxim * fuckups take out each other and make the end-result the way 127164191Smaxim * it was meant to be. 128164191Smaxim */ 129164192Smaxim if ((fd2 = dup2(fd1, fd1)) < 0) 130164191Smaxim err(1, "dup2"); 131164191Smaxim printf("ok %d - dup2(2) to itself works\n", ++test); 132164191Smaxim 133164191Smaxim /* Do we get the right fd? */ 134164191Smaxim ++test; 135164191Smaxim if (fd2 != fd1) 136164191Smaxim printf("not ok %d - dup2(2) didn't give us the right fd\n", 137164191Smaxim test); 138164191Smaxim else 139164191Smaxim printf("ok %d - dup2(2) to itself returned a correct fd\n", 140164191Smaxim test); 141164191Smaxim 142164191Smaxim /* Was close-on-exec cleared? */ 143164191Smaxim ++test; 144164191Smaxim if (fcntl(fd2, F_GETFD) == 0) 145164191Smaxim printf("not ok %d - dup2(2) cleared close-on-exec\n", test); 146164191Smaxim else 147164191Smaxim printf("ok %d - dup2(2) didn't clear close-on-exec\n", test); 148164191Smaxim 149164191Smaxim /* Does fcntl(F_DUPFD) work? */ 150250512Sjilles if ((fd2 = fcntl(fd1, F_DUPFD, 10)) < 0) 151164191Smaxim err(1, "fcntl(F_DUPFD)"); 152250512Sjilles if (fd2 < 10) 153250512Sjilles printf("not ok %d - fcntl(F_DUPFD) returned wrong fd %d\n", 154250512Sjilles ++test, fd2); 155250512Sjilles else 156250512Sjilles printf("ok %d - fcntl(F_DUPFD) works\n", ++test); 157164191Smaxim 158164191Smaxim /* Was close-on-exec cleared? */ 159164191Smaxim ++test; 160164191Smaxim if (fcntl(fd2, F_GETFD) != 0) 161164191Smaxim printf( 162164191Smaxim "not ok %d - fcntl(F_DUPFD) didn't clear close-on-exec\n", 163164191Smaxim test); 164164191Smaxim else 165164191Smaxim printf("ok %d - fcntl(F_DUPFD) cleared close-on-exec\n", test); 166164191Smaxim 167164191Smaxim ++test; 168164191Smaxim if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) 169164191Smaxim err(1, "getrlimit"); 170176957Santoine if ((fd2 = dup2(fd1, rlp.rlim_cur + 1)) >= 0) 171164191Smaxim printf("not ok %d - dup2(2) bypassed NOFILE limit\n", test); 172164191Smaxim else 173164191Smaxim printf("ok %d - dup2(2) didn't bypass NOFILE limit\n", test); 174164191Smaxim 175176957Santoine /* If fcntl(F_DUP2FD) ever work? */ 176176957Santoine if ((fd2 = fcntl(fd1, F_DUP2FD, fd1 + 1)) < 0) 177176957Santoine err(1, "fcntl(F_DUP2FD)"); 178176957Santoine printf("ok %d - fcntl(F_DUP2FD) works\n", ++test); 179176957Santoine 180176957Santoine /* Do we get the right fd? */ 181176957Santoine ++test; 182176957Santoine if (fd2 != fd1 + 1) 183176957Santoine printf( 184176957Santoine "no ok %d - fcntl(F_DUP2FD) didn't give us the right fd\n", 185176957Santoine test); 186176957Santoine else 187176957Santoine printf("ok %d - fcntl(F_DUP2FD) returned a correct fd\n", 188176957Santoine test); 189176957Santoine 190176957Santoine /* Was close-on-exec cleared? */ 191176957Santoine ++test; 192176957Santoine if (fcntl(fd2, F_GETFD) != 0) 193176957Santoine printf( 194176957Santoine "not ok %d - fcntl(F_DUP2FD) didn't clear close-on-exec\n", 195176957Santoine test); 196176957Santoine else 197176957Santoine printf("ok %d - fcntl(F_DUP2FD) cleared close-on-exec\n", 198176957Santoine test); 199176957Santoine 200176957Santoine /* Dup to itself */ 201176957Santoine if ((fd2 = fcntl(fd1, F_DUP2FD, fd1)) < 0) 202176957Santoine err(1, "fcntl(F_DUP2FD)"); 203176957Santoine printf("ok %d - fcntl(F_DUP2FD) to itself works\n", ++test); 204176957Santoine 205176957Santoine /* Do we get the right fd? */ 206176957Santoine ++test; 207176957Santoine if (fd2 != fd1) 208176957Santoine printf( 209176957Santoine "not ok %d - fcntl(F_DUP2FD) didn't give us the right fd\n", 210176957Santoine test); 211176957Santoine else 212176957Santoine printf( 213176957Santoine "ok %d - fcntl(F_DUP2FD) to itself returned a correct fd\n", 214176957Santoine test); 215176957Santoine 216176957Santoine /* Was close-on-exec cleared? */ 217176957Santoine ++test; 218176957Santoine if (fcntl(fd2, F_GETFD) == 0) 219176957Santoine printf("not ok %d - fcntl(F_DUP2FD) cleared close-on-exec\n", 220176957Santoine test); 221176957Santoine else 222176957Santoine printf("ok %d - fcntl(F_DUP2FD) didn't clear close-on-exec\n", 223176957Santoine test); 224176957Santoine 225176957Santoine ++test; 226176957Santoine if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) 227176957Santoine err(1, "getrlimit"); 228176957Santoine if ((fd2 = fcntl(fd1, F_DUP2FD, rlp.rlim_cur + 1)) >= 0) 229176957Santoine printf("not ok %d - fcntl(F_DUP2FD) bypassed NOFILE limit\n", 230176957Santoine test); 231176957Santoine else 232176957Santoine printf("ok %d - fcntl(F_DUP2FD) didn't bypass NOFILE limit\n", 233176957Santoine test); 234176957Santoine 235250513Sjilles /* Does fcntl(F_DUPFD_CLOEXEC) work? */ 236250513Sjilles if ((fd2 = fcntl(fd1, F_DUPFD_CLOEXEC, 10)) < 0) 237250513Sjilles err(1, "fcntl(F_DUPFD_CLOEXEC)"); 238250513Sjilles if (fd2 < 10) 239250513Sjilles printf("not ok %d - fcntl(F_DUPFD_CLOEXEC) returned wrong fd %d\n", 240250513Sjilles ++test, fd2); 241250513Sjilles else 242250513Sjilles printf("ok %d - fcntl(F_DUPFD_CLOEXEC) works\n", ++test); 243250513Sjilles 244250513Sjilles /* Was close-on-exec cleared? */ 245250513Sjilles ++test; 246250513Sjilles if (fcntl(fd2, F_GETFD) != 1) 247250513Sjilles printf( 248250513Sjilles "not ok %d - fcntl(F_DUPFD_CLOEXEC) didn't set close-on-exec\n", 249250513Sjilles test); 250250513Sjilles else 251250513Sjilles printf("ok %d - fcntl(F_DUPFD_CLOEXEC) set close-on-exec\n", 252250513Sjilles test); 253250513Sjilles 254164191Smaxim return (0); 255164191Smaxim} 256