dup_test.c (164192) | dup_test.c (176957) |
---|---|
1/* 2 * $OpenBSD: dup2test.c,v 1.3 2003/07/31 21:48:08 deraadt Exp $ 3 * $OpenBSD: dup2_self.c,v 1.3 2003/07/31 21:48:08 deraadt Exp $ 4 * $OpenBSD: fcntl_dup.c,v 1.2 2003/07/31 21:48:08 deraadt Exp $ 5 * 6 * Written by Artur Grabowski <art@openbsd.org> 2002 Public Domain. 7 * | 1/* 2 * $OpenBSD: dup2test.c,v 1.3 2003/07/31 21:48:08 deraadt Exp $ 3 * $OpenBSD: dup2_self.c,v 1.3 2003/07/31 21:48:08 deraadt Exp $ 4 * $OpenBSD: fcntl_dup.c,v 1.2 2003/07/31 21:48:08 deraadt Exp $ 5 * 6 * Written by Artur Grabowski <art@openbsd.org> 2002 Public Domain. 7 * |
8 * $FreeBSD: head/tools/regression/file/dup/dup.c 164192 2006-11-11 18:45:20Z maxim $ | 8 * $FreeBSD: head/tools/regression/file/dup/dup.c 176957 2008-03-08 22:02:21Z antoine $ |
9 */ 10 11/* 12 * Test #1: check if dup(2) works. 13 * Test #2: check if dup2(2) works. 14 * Test #3: check if dup2(2) returned a fd we asked for. 15 * Test #4: check if dup2(2) cleared close-on-exec flag for duped fd. 16 * Test #5: check if dup2(2) allows to dup fd to itself. 17 * Test #6: check if dup2(2) returned a fd we asked for. 18 * Test #7: check if dup2(2) did not clear close-on-exec flag for duped fd. 19 * Test #8: check if fcntl(F_DUPFD) works. 20 * Test #9: check if fcntl(F_DUPFD) cleared close-on-exec flag for duped fd. 21 * Test #10: check if dup2() to a fd > current maximum number of open files 22 * limit work. | 9 */ 10 11/* 12 * Test #1: check if dup(2) works. 13 * Test #2: check if dup2(2) works. 14 * Test #3: check if dup2(2) returned a fd we asked for. 15 * Test #4: check if dup2(2) cleared close-on-exec flag for duped fd. 16 * Test #5: check if dup2(2) allows to dup fd to itself. 17 * Test #6: check if dup2(2) returned a fd we asked for. 18 * Test #7: check if dup2(2) did not clear close-on-exec flag for duped fd. 19 * Test #8: check if fcntl(F_DUPFD) works. 20 * Test #9: check if fcntl(F_DUPFD) cleared close-on-exec flag for duped fd. 21 * Test #10: check if dup2() to a fd > current maximum number of open files 22 * limit work. |
23 * Test #11: check if fcntl(F_DUP2FD) works. 24 * Test #12: check if fcntl(F_DUP2FD) returned a fd we asked for. 25 * Test #13: check if fcntl(F_DUP2FD) cleared close-on-exec flag for duped fd. 26 * Test #14: check if fcntl(F_DUP2FD) allows to dup fd to itself. 27 * Test #15: check if fcntl(F_DUP2FD) returned a fd we asked for. 28 * Test #16: check if fcntl(F_DUP2FD) did not clear close-on-exec flag for 29 * duped fd. 30 * Test #17: check if fcntl(F_DUP2FD) to a fd > current maximum number of open 31 * files limit work. |
|
23 */ 24 25#include <sys/types.h> 26#include <sys/time.h> 27#include <sys/resource.h> 28 29#include <err.h> 30#include <fcntl.h> --- 20 unchanged lines hidden (view full) --- 51int 52main(int __unused argc, char __unused *argv[]) 53{ 54 struct rlimit rlp; 55 int orgfd, fd1, fd2, test = 0; 56 57 orgfd = getafile(); 58 | 32 */ 33 34#include <sys/types.h> 35#include <sys/time.h> 36#include <sys/resource.h> 37 38#include <err.h> 39#include <fcntl.h> --- 20 unchanged lines hidden (view full) --- 60int 61main(int __unused argc, char __unused *argv[]) 62{ 63 struct rlimit rlp; 64 int orgfd, fd1, fd2, test = 0; 65 66 orgfd = getafile(); 67 |
59 printf("1..10\n"); | 68 printf("1..17\n"); |
60 61 /* If dup(2) ever work? */ 62 if ((fd1 = dup(orgfd)) < 0) 63 err(1, "dup"); 64 printf("ok %d - dup(2) works\n", ++test); 65 66 /* Set close-on-exec */ 67 if (fcntl(fd1, F_SETFD, 1) != 0) --- 78 unchanged lines hidden (view full) --- 146 "not ok %d - fcntl(F_DUPFD) didn't clear close-on-exec\n", 147 test); 148 else 149 printf("ok %d - fcntl(F_DUPFD) cleared close-on-exec\n", test); 150 151 ++test; 152 if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) 153 err(1, "getrlimit"); | 69 70 /* If dup(2) ever work? */ 71 if ((fd1 = dup(orgfd)) < 0) 72 err(1, "dup"); 73 printf("ok %d - dup(2) works\n", ++test); 74 75 /* Set close-on-exec */ 76 if (fcntl(fd1, F_SETFD, 1) != 0) --- 78 unchanged lines hidden (view full) --- 155 "not ok %d - fcntl(F_DUPFD) didn't clear close-on-exec\n", 156 test); 157 else 158 printf("ok %d - fcntl(F_DUPFD) cleared close-on-exec\n", test); 159 160 ++test; 161 if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) 162 err(1, "getrlimit"); |
154 if ((fd2 = dup2(fd1, rlp.rlim_cur + 1)) == 0) | 163 if ((fd2 = dup2(fd1, rlp.rlim_cur + 1)) >= 0) |
155 printf("not ok %d - dup2(2) bypassed NOFILE limit\n", test); 156 else 157 printf("ok %d - dup2(2) didn't bypass NOFILE limit\n", test); 158 | 164 printf("not ok %d - dup2(2) bypassed NOFILE limit\n", test); 165 else 166 printf("ok %d - dup2(2) didn't bypass NOFILE limit\n", test); 167 |
168 /* If fcntl(F_DUP2FD) ever work? */ 169 if ((fd2 = fcntl(fd1, F_DUP2FD, fd1 + 1)) < 0) 170 err(1, "fcntl(F_DUP2FD)"); 171 printf("ok %d - fcntl(F_DUP2FD) works\n", ++test); 172 173 /* Do we get the right fd? */ 174 ++test; 175 if (fd2 != fd1 + 1) 176 printf( 177 "no ok %d - fcntl(F_DUP2FD) didn't give us the right fd\n", 178 test); 179 else 180 printf("ok %d - fcntl(F_DUP2FD) returned a correct fd\n", 181 test); 182 183 /* Was close-on-exec cleared? */ 184 ++test; 185 if (fcntl(fd2, F_GETFD) != 0) 186 printf( 187 "not ok %d - fcntl(F_DUP2FD) didn't clear close-on-exec\n", 188 test); 189 else 190 printf("ok %d - fcntl(F_DUP2FD) cleared close-on-exec\n", 191 test); 192 193 /* Dup to itself */ 194 if ((fd2 = fcntl(fd1, F_DUP2FD, fd1)) < 0) 195 err(1, "fcntl(F_DUP2FD)"); 196 printf("ok %d - fcntl(F_DUP2FD) to itself works\n", ++test); 197 198 /* Do we get the right fd? */ 199 ++test; 200 if (fd2 != fd1) 201 printf( 202 "not ok %d - fcntl(F_DUP2FD) didn't give us the right fd\n", 203 test); 204 else 205 printf( 206 "ok %d - fcntl(F_DUP2FD) to itself returned a correct fd\n", 207 test); 208 209 /* Was close-on-exec cleared? */ 210 ++test; 211 if (fcntl(fd2, F_GETFD) == 0) 212 printf("not ok %d - fcntl(F_DUP2FD) cleared close-on-exec\n", 213 test); 214 else 215 printf("ok %d - fcntl(F_DUP2FD) didn't clear close-on-exec\n", 216 test); 217 218 ++test; 219 if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) 220 err(1, "getrlimit"); 221 if ((fd2 = fcntl(fd1, F_DUP2FD, rlp.rlim_cur + 1)) >= 0) 222 printf("not ok %d - fcntl(F_DUP2FD) bypassed NOFILE limit\n", 223 test); 224 else 225 printf("ok %d - fcntl(F_DUP2FD) didn't bypass NOFILE limit\n", 226 test); 227 |
|
159 return (0); 160} | 228 return (0); 229} |