pkgin_all: script to auto-install all packages
[minix.git] / test / test29.c
blobccbd4d1af5473d593ef79510c89311ccd5b08b68
1 /* test29: dup() dup2() Author: Jan-Mark Wams (jms@cs.vu.nl) */
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!
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>
27 #define MAX_ERROR 4
28 #define ITERATIONS 10
30 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
31 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
32 #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
34 #define IS_CLOEXEC(fd) ((fcntl(fd, F_GETFD) & FD_CLOEXEC) == FD_CLOEXEC)
35 #define SET_CLOEXEC(fd) fcntl(fd, F_SETFD, FD_CLOEXEC)
37 #include "common.c"
39 int superuser;
41 void test29a(void);
42 void test29b(void);
43 void test29c(void);
45 int main(int argc, char *argv[])
47 int i, m = 0xFFFF;
49 sync();
50 if (argc == 2) m = atoi(argv[1]);
52 start(29);
53 superuser = (geteuid() == 0);
55 for (i = 0; i < ITERATIONS; i++) {
56 if (m & 0001) test29a();
57 if (m & 0002) test29b();
58 if (m & 0004) test29c();
60 quit();
62 return(-1); /* Unreachable */
65 void test29a()
67 int fd1, fd2, fd3, fd4, fd5;
68 struct flock flock;
70 subtest = 1;
72 /* Basic checking. */
73 if ((fd1 = dup(0)) != 3) e(1);
74 if ((fd2 = dup(0)) != 4) e(2);
75 if ((fd3 = dup(0)) != 5) e(3);
76 if ((fd4 = dup(0)) != 6) e(4);
77 if ((fd5 = dup(0)) != 7) e(5);
78 if (close(fd2) != 0) e(6);
79 if (close(fd4) != 0) e(7);
80 if ((fd2 = dup(0)) != 4) e(8);
81 if ((fd4 = dup(0)) != 6) e(9);
82 if (close(fd1) != 0) e(10);
83 if (close(fd3) != 0) e(11);
84 if (close(fd5) != 0) e(12);
85 if ((fd1 = dup(0)) != 3) e(13);
86 if ((fd3 = dup(0)) != 5) e(14);
87 if ((fd5 = dup(0)) != 7) e(15);
88 if (close(fd1) != 0) e(16);
89 if (close(fd2) != 0) e(17);
90 if (close(fd3) != 0) e(18);
91 if (close(fd4) != 0) e(19);
92 if (close(fd5) != 0) e(20);
94 /* FD_CLOEXEC should be cleared. */
95 if ((fd1 = dup(0)) != 3) e(21);
96 if (SET_CLOEXEC(fd1) == -1) e(22);
97 if (!IS_CLOEXEC(fd1)) e(23);
98 if ((fd2 = dup(fd1)) != 4) e(24);
99 if ((fd3 = dup(fd2)) != 5) e(25);
100 if (IS_CLOEXEC(fd2)) e(26);
101 if (IS_CLOEXEC(fd3)) e(27);
102 if (SET_CLOEXEC(fd2) == -1) e(28);
103 if (!IS_CLOEXEC(fd2)) e(29);
104 if (IS_CLOEXEC(fd3)) e(30);
105 if (close(fd1) != 0) e(31);
106 if (close(fd2) != 0) e(32);
107 if (close(fd3) != 0) e(33);
109 /* Locks should be shared, so we can lock again. */
110 System("echo 'Hallo' > file");
111 if ((fd1 = open("file", O_RDWR)) != 3) e(34);
112 flock.l_whence = SEEK_SET;
113 flock.l_start = 0;
114 flock.l_len = 10;
115 flock.l_type = F_WRLCK;
116 if (fcntl(fd1, F_SETLK, &flock) == -1) e(35);
117 if (fcntl(fd1, F_SETLK, &flock) == -1) e(36);
118 if ((fd2 = dup(fd1)) != 4) e(37);
119 if (fcntl(fd1, F_SETLK, &flock) == -1) e(38);
120 if (fcntl(fd1, F_GETLK, &flock) == -1) e(39);
121 #if 0 /* XXX - see test7.c */
122 if (flock.l_type != F_WRLCK) e(40);
123 if (flock.l_pid != getpid()) e(41);
124 #endif /* 0 */
125 flock.l_type = F_WRLCK;
126 if (fcntl(fd2, F_GETLK, &flock) == -1) e(42);
127 #if 0 /* XXX - see test7.c */
128 if (flock.l_type != F_WRLCK) e(43);
129 if (flock.l_pid != getpid()) e(44);
130 #endif /* 0 */
131 if (close(fd1) != 0) e(45);
132 if (close(fd2) != 0) e(46);
134 System("rm -rf ../DIR_29/*");
137 void test29b()
139 int fd;
140 char buf[32];
142 subtest = 2;
144 /* Test file called ``file''. */
145 System("echo 'Hallo!' > file");
147 /* Check dup2() call with the same fds. Should have no effect. */
148 if ((fd = open("file", O_RDONLY)) != 3) e(1);
149 if (read(fd, buf, 2) != 2) e(2);
150 if (strncmp(buf, "Ha", 2) != 0) e(3);
151 if (dup2(fd, fd) != fd) e(4);
152 if (read(fd, buf, 2) != 2) e(5);
153 if (strncmp(buf, "ll", 2) != 0) e(6);
154 if (dup2(fd, fd) != fd) e(7);
155 if (read(fd, buf, 2) != 2) e(8);
156 if (strncmp(buf, "o!", 2) != 0) e(9);
157 if (close(fd) != 0) e(10);
159 /* If dup2() call fails, the fildes2 argument has to stay open. */
160 if ((fd = open("file", O_RDONLY)) != 3) e(11);
161 if (read(fd, buf, 2) != 2) e(12);
162 if (strncmp(buf, "Ha", 2) != 0) e(13);
163 if (dup2(OPEN_MAX + 3, fd) != -1) e(14);
164 if (errno != EBADF) e(15);
165 if (read(fd, buf, 2) != 2) e(16);
166 if (strncmp(buf, "ll", 2) != 0) e(17);
167 if (dup2(-4, fd) != -1) e(18);
168 if (errno != EBADF) e(19);
169 if (read(fd, buf, 2) != 2) e(20);
170 if (strncmp(buf, "o!", 2) != 0) e(21);
171 if (close(fd) != 0) e(22);
173 System("rm -rf ../DIR_29/*");
176 void test29c()
178 int i;
180 subtest = 3;
182 /* Check bad arguments to dup() and dup2(). */
183 for (i = -OPEN_MAX; i < OPEN_MAX * 2; i++) {
185 /* ``i'' is a valid and open fd. */
186 if (i >= 0 && i < 3) continue;
188 /* If ``i'' is a valid fd it is not open. */
189 if (dup(i) != -1) e(1);
190 if (errno != EBADF) e(2);
192 /* ``i'' Is OPEN_MAX. */
193 if (i == OPEN_MAX) {
194 if (dup2(0, i) != -1) e(3);
195 if (errno != EINVAL) e(4);
198 /* ``i'' Is out of range. */
199 if (i < 0 || i > OPEN_MAX) {
200 if (dup2(0, i) != -1) e(5);
201 if (errno != EBADF) e(6);
205 System("rm -rf ../DIR_29/*");