<sys/socket.h>: turn off MSG_NOSIGNAL
[minix3.git] / test / test29.c
blob966f31a27befc22ac9d2b6a4a763e2efd8a0a371
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 int max_error = 4;
28 #include "common.h"
30 #define ITERATIONS 10
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)
36 #define IS_CLOEXEC(fd) ((fcntl(fd, F_GETFD) & FD_CLOEXEC) == FD_CLOEXEC)
37 #define SET_CLOEXEC(fd) fcntl(fd, F_SETFD, FD_CLOEXEC)
40 int superuser;
42 void test29a(void);
43 void test29b(void);
44 void test29c(void);
46 int main(int argc, char *argv[])
48 int i, m = 0xFFFF;
50 sync();
51 if (argc == 2) m = atoi(argv[1]);
53 start(29);
54 superuser = (geteuid() == 0);
56 for (i = 0; i < ITERATIONS; i++) {
57 if (m & 0001) test29a();
58 if (m & 0002) test29b();
59 if (m & 0004) test29c();
61 quit();
63 return(-1); /* Unreachable */
66 void test29a()
68 int fd1, fd2, fd3, fd4, fd5;
69 struct flock flock;
71 subtest = 1;
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);
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);
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);
135 System("rm -rf ../DIR_29/*");
138 void test29b()
140 int fd;
141 char buf[32];
143 subtest = 2;
145 /* Test file called ``file''. */
146 System("echo 'Hallo!' > file");
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);
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);
174 System("rm -rf ../DIR_29/*");
177 void test29c()
179 int i;
181 subtest = 3;
183 /* Check bad arguments to dup() and dup2(). */
184 for (i = -OPEN_MAX; i < OPEN_MAX * 2; i++) {
186 /* ``i'' is a valid and open fd. */
187 if (i >= 0 && i < 3) continue;
189 /* If ``i'' is a valid fd it is not open. */
190 if (dup(i) != -1) e(1);
191 if (errno != EBADF) e(2);
193 /* ``i'' Is OPEN_MAX. */
194 if (i == OPEN_MAX) {
195 if (dup2(0, i) != -1) e(3);
196 if (errno != EINVAL) e(4);
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);
206 System("rm -rf ../DIR_29/*");