1 /* test29: dup() dup2() Author: Jan-Mark Wams (jms@cs.vu.nl) */ 2 3 /* The definition of ``dup2()'' is realy a big mess! For: 4 ** 5 ** (1) if fildes2 is less than zero or greater than {OPEN_MAX} 6 ** errno has to set to [EBADF]. But if fildes2 equals {OPEN_MAX} 7 ** errno has to be set to [EINVAL]. And ``fcntl(F_DUPFD...)'' always 8 ** returns [EINVAL] if fildes2 is out of range! 9 ** 10 ** (2) if the number of file descriptors would exceed {OPEN_MAX}, or no 11 ** file descriptors above fildes2 are available, errno has to be set 12 ** to [EMFILE]. But this can never occur! 13 */ 14 15 #include <sys/types.h> 16 #include <sys/stat.h> 17 #include <sys/wait.h> 18 #include <stdlib.h> 19 #include <unistd.h> 20 #include <string.h> 21 #include <fcntl.h> 22 #include <limits.h> 23 #include <errno.h> 24 #include <time.h> 25 #include <stdio.h> 26 27 int max_error = 4; 28 #include "common.h" 29 30 #define ITERATIONS 10 31 32 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd) 33 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir) 34 #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a) 35 36 #define IS_CLOEXEC(fd) ((fcntl(fd, F_GETFD) & FD_CLOEXEC) == FD_CLOEXEC) 37 #define SET_CLOEXEC(fd) fcntl(fd, F_SETFD, FD_CLOEXEC) 38 39 40 int superuser; 41 42 void test29a(void); 43 void test29b(void); 44 void test29c(void); 45 46 int main(int argc, char *argv[]) 47 { 48 int i, m = 0xFFFF; 49 50 sync(); 51 if (argc == 2) m = atoi(argv[1]); 52 53 start(29); 54 superuser = (geteuid() == 0); 55 56 for (i = 0; i < ITERATIONS; i++) { 57 if (m & 0001) test29a(); 58 if (m & 0002) test29b(); 59 if (m & 0004) test29c(); 60 } 61 quit(); 62 63 return(-1); /* Unreachable */ 64 } 65 66 void test29a() 67 { 68 int fd1, fd2, fd3, fd4, fd5; 69 struct flock flock; 70 71 subtest = 1; 72 73 /* Basic checking. */ 74 if ((fd1 = dup(0)) != 3) e(1); 75 if ((fd2 = dup(0)) != 4) e(2); 76 if ((fd3 = dup(0)) != 5) e(3); 77 if ((fd4 = dup(0)) != 6) e(4); 78 if ((fd5 = dup(0)) != 7) e(5); 79 if (close(fd2) != 0) e(6); 80 if (close(fd4) != 0) e(7); 81 if ((fd2 = dup(0)) != 4) e(8); 82 if ((fd4 = dup(0)) != 6) e(9); 83 if (close(fd1) != 0) e(10); 84 if (close(fd3) != 0) e(11); 85 if (close(fd5) != 0) e(12); 86 if ((fd1 = dup(0)) != 3) e(13); 87 if ((fd3 = dup(0)) != 5) e(14); 88 if ((fd5 = dup(0)) != 7) e(15); 89 if (close(fd1) != 0) e(16); 90 if (close(fd2) != 0) e(17); 91 if (close(fd3) != 0) e(18); 92 if (close(fd4) != 0) e(19); 93 if (close(fd5) != 0) e(20); 94 95 /* FD_CLOEXEC should be cleared. */ 96 if ((fd1 = dup(0)) != 3) e(21); 97 if (SET_CLOEXEC(fd1) == -1) e(22); 98 if (!IS_CLOEXEC(fd1)) e(23); 99 if ((fd2 = dup(fd1)) != 4) e(24); 100 if ((fd3 = dup(fd2)) != 5) e(25); 101 if (IS_CLOEXEC(fd2)) e(26); 102 if (IS_CLOEXEC(fd3)) e(27); 103 if (SET_CLOEXEC(fd2) == -1) e(28); 104 if (!IS_CLOEXEC(fd2)) e(29); 105 if (IS_CLOEXEC(fd3)) e(30); 106 if (close(fd1) != 0) e(31); 107 if (close(fd2) != 0) e(32); 108 if (close(fd3) != 0) e(33); 109 110 /* Locks should be shared, so we can lock again. */ 111 System("echo 'Hallo' > file"); 112 if ((fd1 = open("file", O_RDWR)) != 3) e(34); 113 flock.l_whence = SEEK_SET; 114 flock.l_start = 0; 115 flock.l_len = 10; 116 flock.l_type = F_WRLCK; 117 if (fcntl(fd1, F_SETLK, &flock) == -1) e(35); 118 if (fcntl(fd1, F_SETLK, &flock) == -1) e(36); 119 if ((fd2 = dup(fd1)) != 4) e(37); 120 if (fcntl(fd1, F_SETLK, &flock) == -1) e(38); 121 if (fcntl(fd1, F_GETLK, &flock) == -1) e(39); 122 #if 0 /* XXX - see test7.c */ 123 if (flock.l_type != F_WRLCK) e(40); 124 if (flock.l_pid != getpid()) e(41); 125 #endif /* 0 */ 126 flock.l_type = F_WRLCK; 127 if (fcntl(fd2, F_GETLK, &flock) == -1) e(42); 128 #if 0 /* XXX - see test7.c */ 129 if (flock.l_type != F_WRLCK) e(43); 130 if (flock.l_pid != getpid()) e(44); 131 #endif /* 0 */ 132 if (close(fd1) != 0) e(45); 133 if (close(fd2) != 0) e(46); 134 135 System("rm -rf ../DIR_29/*"); 136 } 137 138 void test29b() 139 { 140 int fd; 141 char buf[32]; 142 143 subtest = 2; 144 145 /* Test file called ``file''. */ 146 System("echo 'Hallo!' > file"); 147 148 /* Check dup2() call with the same fds. Should have no effect. */ 149 if ((fd = open("file", O_RDONLY)) != 3) e(1); 150 if (read(fd, buf, 2) != 2) e(2); 151 if (strncmp(buf, "Ha", 2) != 0) e(3); 152 if (dup2(fd, fd) != fd) e(4); 153 if (read(fd, buf, 2) != 2) e(5); 154 if (strncmp(buf, "ll", 2) != 0) e(6); 155 if (dup2(fd, fd) != fd) e(7); 156 if (read(fd, buf, 2) != 2) e(8); 157 if (strncmp(buf, "o!", 2) != 0) e(9); 158 if (close(fd) != 0) e(10); 159 160 /* If dup2() call fails, the fildes2 argument has to stay open. */ 161 if ((fd = open("file", O_RDONLY)) != 3) e(11); 162 if (read(fd, buf, 2) != 2) e(12); 163 if (strncmp(buf, "Ha", 2) != 0) e(13); 164 if (dup2(OPEN_MAX + 3, fd) != -1) e(14); 165 if (errno != EBADF) e(15); 166 if (read(fd, buf, 2) != 2) e(16); 167 if (strncmp(buf, "ll", 2) != 0) e(17); 168 if (dup2(-4, fd) != -1) e(18); 169 if (errno != EBADF) e(19); 170 if (read(fd, buf, 2) != 2) e(20); 171 if (strncmp(buf, "o!", 2) != 0) e(21); 172 if (close(fd) != 0) e(22); 173 174 System("rm -rf ../DIR_29/*"); 175 } 176 177 void test29c() 178 { 179 int i; 180 181 subtest = 3; 182 183 /* Check bad arguments to dup() and dup2(). */ 184 for (i = -OPEN_MAX; i < OPEN_MAX * 2; i++) { 185 186 /* ``i'' is a valid and open fd. */ 187 if (i >= 0 && i < 3) continue; 188 189 /* If ``i'' is a valid fd it is not open. */ 190 if (dup(i) != -1) e(1); 191 if (errno != EBADF) e(2); 192 193 /* ``i'' Is OPEN_MAX. */ 194 if (i == OPEN_MAX) { 195 if (dup2(0, i) != -1) e(3); 196 if (errno != EINVAL) e(4); 197 } 198 199 /* ``i'' Is out of range. */ 200 if (i < 0 || i > OPEN_MAX) { 201 if (dup2(0, i) != -1) e(5); 202 if (errno != EBADF) e(6); 203 } 204 } 205 206 System("rm -rf ../DIR_29/*"); 207 } 208