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$ 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. 32164191Smaxim */ 33164191Smaxim 34164191Smaxim#include <sys/types.h> 35164191Smaxim#include <sys/time.h> 36164191Smaxim#include <sys/resource.h> 37164191Smaxim 38164191Smaxim#include <err.h> 39164191Smaxim#include <fcntl.h> 40164191Smaxim#include <stdio.h> 41164191Smaxim#include <stdlib.h> 42164191Smaxim#include <unistd.h> 43164191Smaxim 44164191Smaximstatic int getafile(void); 45164191Smaxim 46164191Smaximstatic int 47164191Smaximgetafile(void) 48164191Smaxim{ 49164191Smaxim int fd; 50164191Smaxim 51164191Smaxim char temp[] = "/tmp/dup2XXXXXXXXX"; 52164191Smaxim if ((fd = mkstemp(temp)) < 0) 53164191Smaxim err(1, "mkstemp"); 54164191Smaxim remove(temp); 55164191Smaxim if (ftruncate(fd, 1024) != 0) 56164191Smaxim err(1, "ftruncate"); 57164191Smaxim return (fd); 58164191Smaxim} 59164191Smaxim 60164191Smaximint 61164191Smaximmain(int __unused argc, char __unused *argv[]) 62164191Smaxim{ 63164191Smaxim struct rlimit rlp; 64164191Smaxim int orgfd, fd1, fd2, test = 0; 65164191Smaxim 66164191Smaxim orgfd = getafile(); 67164191Smaxim 68176957Santoine printf("1..17\n"); 69164191Smaxim 70164191Smaxim /* If dup(2) ever work? */ 71164191Smaxim if ((fd1 = dup(orgfd)) < 0) 72164191Smaxim err(1, "dup"); 73164191Smaxim printf("ok %d - dup(2) works\n", ++test); 74164191Smaxim 75164191Smaxim /* Set close-on-exec */ 76164191Smaxim if (fcntl(fd1, F_SETFD, 1) != 0) 77164191Smaxim err(1, "fcntl(F_SETFD)"); 78164191Smaxim 79164191Smaxim /* If dup2(2) ever work? */ 80164191Smaxim if ((fd2 = dup2(fd1, fd1 + 1)) < 0) 81164191Smaxim err(1, "dup2"); 82164191Smaxim printf("ok %d - dup2(2) works\n", ++test); 83164191Smaxim 84164191Smaxim /* Do we get the right fd? */ 85164191Smaxim ++test; 86164191Smaxim if (fd2 != fd1 + 1) 87164191Smaxim printf("no ok %d - dup2(2) didn't give us the right fd\n", 88164191Smaxim test); 89164191Smaxim else 90164191Smaxim printf("ok %d - dup2(2) returned a correct fd\n", test); 91164191Smaxim 92164191Smaxim /* Was close-on-exec cleared? */ 93164191Smaxim ++test; 94164191Smaxim if (fcntl(fd2, F_GETFD) != 0) 95164191Smaxim printf("not ok %d - dup2(2) didn't clear close-on-exec\n", 96164191Smaxim test); 97164191Smaxim else 98164191Smaxim printf("ok %d - dup2(2) cleared close-on-exec\n", test); 99164191Smaxim 100164191Smaxim /* 101164191Smaxim * Dup to itself. 102164191Smaxim * 103164191Smaxim * We're testing a small tweak in dup2 semantics. 104164191Smaxim * Normally dup and dup2 will clear the close-on-exec 105164191Smaxim * flag on the new fd (which appears to be an implementation 106164191Smaxim * mistake from start and not some planned behavior). 107164191Smaxim * In todays implementations of dup and dup2 we have to make 108164191Smaxim * an effort to really clear that flag. But all tested 109164191Smaxim * implementations of dup2 have another tweak. If we 110164191Smaxim * dup2(old, new) when old == new, the syscall short-circuits 111164191Smaxim * and returns early (because there is no need to do all the 112164191Smaxim * work (and there is a risk for serious mistakes)). 113164191Smaxim * So although the docs say that dup2 should "take 'old', 114164191Smaxim * close 'new' perform a dup(2) of 'old' into 'new'" 115164191Smaxim * the docs are not really followed because close-on-exec 116164191Smaxim * is not cleared on 'new'. 117164191Smaxim * 118164191Smaxim * Since everyone has this bug, we pretend that this is 119164191Smaxim * the way it is supposed to be and test here that it really 120164191Smaxim * works that way. 121164191Smaxim * 122164191Smaxim * This is a fine example on where two separate implementation 123164191Smaxim * fuckups take out each other and make the end-result the way 124164191Smaxim * it was meant to be. 125164191Smaxim */ 126164192Smaxim if ((fd2 = dup2(fd1, fd1)) < 0) 127164191Smaxim err(1, "dup2"); 128164191Smaxim printf("ok %d - dup2(2) to itself works\n", ++test); 129164191Smaxim 130164191Smaxim /* Do we get the right fd? */ 131164191Smaxim ++test; 132164191Smaxim if (fd2 != fd1) 133164191Smaxim printf("not ok %d - dup2(2) didn't give us the right fd\n", 134164191Smaxim test); 135164191Smaxim else 136164191Smaxim printf("ok %d - dup2(2) to itself returned a correct fd\n", 137164191Smaxim test); 138164191Smaxim 139164191Smaxim /* Was close-on-exec cleared? */ 140164191Smaxim ++test; 141164191Smaxim if (fcntl(fd2, F_GETFD) == 0) 142164191Smaxim printf("not ok %d - dup2(2) cleared close-on-exec\n", test); 143164191Smaxim else 144164191Smaxim printf("ok %d - dup2(2) didn't clear close-on-exec\n", test); 145164191Smaxim 146164191Smaxim /* Does fcntl(F_DUPFD) work? */ 147164191Smaxim if ((fd2 = fcntl(fd1, F_DUPFD)) < 0) 148164191Smaxim err(1, "fcntl(F_DUPFD)"); 149164191Smaxim printf("ok %d - fcntl(F_DUPFD) works\n", ++test); 150164191Smaxim 151164191Smaxim /* Was close-on-exec cleared? */ 152164191Smaxim ++test; 153164191Smaxim if (fcntl(fd2, F_GETFD) != 0) 154164191Smaxim printf( 155164191Smaxim "not ok %d - fcntl(F_DUPFD) didn't clear close-on-exec\n", 156164191Smaxim test); 157164191Smaxim else 158164191Smaxim printf("ok %d - fcntl(F_DUPFD) cleared close-on-exec\n", test); 159164191Smaxim 160164191Smaxim ++test; 161164191Smaxim if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) 162164191Smaxim err(1, "getrlimit"); 163176957Santoine if ((fd2 = dup2(fd1, rlp.rlim_cur + 1)) >= 0) 164164191Smaxim printf("not ok %d - dup2(2) bypassed NOFILE limit\n", test); 165164191Smaxim else 166164191Smaxim printf("ok %d - dup2(2) didn't bypass NOFILE limit\n", test); 167164191Smaxim 168176957Santoine /* If fcntl(F_DUP2FD) ever work? */ 169176957Santoine if ((fd2 = fcntl(fd1, F_DUP2FD, fd1 + 1)) < 0) 170176957Santoine err(1, "fcntl(F_DUP2FD)"); 171176957Santoine printf("ok %d - fcntl(F_DUP2FD) works\n", ++test); 172176957Santoine 173176957Santoine /* Do we get the right fd? */ 174176957Santoine ++test; 175176957Santoine if (fd2 != fd1 + 1) 176176957Santoine printf( 177176957Santoine "no ok %d - fcntl(F_DUP2FD) didn't give us the right fd\n", 178176957Santoine test); 179176957Santoine else 180176957Santoine printf("ok %d - fcntl(F_DUP2FD) returned a correct fd\n", 181176957Santoine test); 182176957Santoine 183176957Santoine /* Was close-on-exec cleared? */ 184176957Santoine ++test; 185176957Santoine if (fcntl(fd2, F_GETFD) != 0) 186176957Santoine printf( 187176957Santoine "not ok %d - fcntl(F_DUP2FD) didn't clear close-on-exec\n", 188176957Santoine test); 189176957Santoine else 190176957Santoine printf("ok %d - fcntl(F_DUP2FD) cleared close-on-exec\n", 191176957Santoine test); 192176957Santoine 193176957Santoine /* Dup to itself */ 194176957Santoine if ((fd2 = fcntl(fd1, F_DUP2FD, fd1)) < 0) 195176957Santoine err(1, "fcntl(F_DUP2FD)"); 196176957Santoine printf("ok %d - fcntl(F_DUP2FD) to itself works\n", ++test); 197176957Santoine 198176957Santoine /* Do we get the right fd? */ 199176957Santoine ++test; 200176957Santoine if (fd2 != fd1) 201176957Santoine printf( 202176957Santoine "not ok %d - fcntl(F_DUP2FD) didn't give us the right fd\n", 203176957Santoine test); 204176957Santoine else 205176957Santoine printf( 206176957Santoine "ok %d - fcntl(F_DUP2FD) to itself returned a correct fd\n", 207176957Santoine test); 208176957Santoine 209176957Santoine /* Was close-on-exec cleared? */ 210176957Santoine ++test; 211176957Santoine if (fcntl(fd2, F_GETFD) == 0) 212176957Santoine printf("not ok %d - fcntl(F_DUP2FD) cleared close-on-exec\n", 213176957Santoine test); 214176957Santoine else 215176957Santoine printf("ok %d - fcntl(F_DUP2FD) didn't clear close-on-exec\n", 216176957Santoine test); 217176957Santoine 218176957Santoine ++test; 219176957Santoine if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) 220176957Santoine err(1, "getrlimit"); 221176957Santoine if ((fd2 = fcntl(fd1, F_DUP2FD, rlp.rlim_cur + 1)) >= 0) 222176957Santoine printf("not ok %d - fcntl(F_DUP2FD) bypassed NOFILE limit\n", 223176957Santoine test); 224176957Santoine else 225176957Santoine printf("ok %d - fcntl(F_DUP2FD) didn't bypass NOFILE limit\n", 226176957Santoine test); 227176957Santoine 228164191Smaxim return (0); 229164191Smaxim} 230