source: trunk/minix/test/test29.c@ 20

Last change on this file since 20 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 6.2 KB
Line 
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#define MAX_ERROR 4
28#define ITERATIONS 10
29
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)
33
34#define IS_CLOEXEC(fd) ((fcntl(fd, F_GETFD) & FD_CLOEXEC) == FD_CLOEXEC)
35#define SET_CLOEXEC(fd) fcntl(fd, F_SETFD, FD_CLOEXEC)
36
37int errct = 0;
38int subtest = 1;
39int superuser;
40char MaxName[NAME_MAX + 1]; /* Name of maximum length */
41char MaxPath[PATH_MAX]; /* Same for path */
42char ToLongName[NAME_MAX + 2]; /* Name of maximum +1 length */
43char ToLongPath[PATH_MAX + 1]; /* Same for path, both too long */
44
45_PROTOTYPE(void main, (int argc, char *argv[]));
46_PROTOTYPE(void test29a, (void));
47_PROTOTYPE(void test29b, (void));
48_PROTOTYPE(void test29c, (void));
49_PROTOTYPE(void e, (int number));
50_PROTOTYPE(void quit, (void));
51
52void main(argc, argv)
53int argc;
54char *argv[];
55{
56 int i, m = 0xFFFF;
57
58 sync();
59 if (argc == 2) m = atoi(argv[1]);
60 printf("Test 29 ");
61 fflush(stdout);
62 System("rm -rf DIR_29; mkdir DIR_29");
63 Chdir("DIR_29");
64 superuser = (geteuid() == 0);
65
66 for (i = 0; i < ITERATIONS; i++) {
67 if (m & 0001) test29a();
68 if (m & 0002) test29b();
69 if (m & 0004) test29c();
70 }
71 quit();
72}
73
74void test29a()
75{
76 int fd1, fd2, fd3, fd4, fd5;
77 struct flock flock;
78
79 subtest = 1;
80
81 /* Basic checking. */
82 if ((fd1 = dup(0)) != 3) e(1);
83 if ((fd2 = dup(0)) != 4) e(2);
84 if ((fd3 = dup(0)) != 5) e(3);
85 if ((fd4 = dup(0)) != 6) e(4);
86 if ((fd5 = dup(0)) != 7) e(5);
87 if (close(fd2) != 0) e(6);
88 if (close(fd4) != 0) e(7);
89 if ((fd2 = dup(0)) != 4) e(8);
90 if ((fd4 = dup(0)) != 6) e(9);
91 if (close(fd1) != 0) e(10);
92 if (close(fd3) != 0) e(11);
93 if (close(fd5) != 0) e(12);
94 if ((fd1 = dup(0)) != 3) e(13);
95 if ((fd3 = dup(0)) != 5) e(14);
96 if ((fd5 = dup(0)) != 7) e(15);
97 if (close(fd1) != 0) e(16);
98 if (close(fd2) != 0) e(17);
99 if (close(fd3) != 0) e(18);
100 if (close(fd4) != 0) e(19);
101 if (close(fd5) != 0) e(20);
102
103 /* FD_CLOEXEC should be cleared. */
104 if ((fd1 = dup(0)) != 3) e(21);
105 if (SET_CLOEXEC(fd1) == -1) e(22);
106 if (!IS_CLOEXEC(fd1)) e(23);
107 if ((fd2 = dup(fd1)) != 4) e(24);
108 if ((fd3 = dup(fd2)) != 5) e(25);
109 if (IS_CLOEXEC(fd2)) e(26);
110 if (IS_CLOEXEC(fd3)) e(27);
111 if (SET_CLOEXEC(fd2) == -1) e(28);
112 if (!IS_CLOEXEC(fd2)) e(29);
113 if (IS_CLOEXEC(fd3)) e(30);
114 if (close(fd1) != 0) e(31);
115 if (close(fd2) != 0) e(32);
116 if (close(fd3) != 0) e(33);
117
118 /* Locks should be shared, so we can lock again. */
119 System("echo 'Hallo' > file");
120 if ((fd1 = open("file", O_RDWR)) != 3) e(34);
121 flock.l_whence = SEEK_SET;
122 flock.l_start = 0;
123 flock.l_len = 10;
124 flock.l_type = F_WRLCK;
125 if (fcntl(fd1, F_SETLK, &flock) == -1) e(35);
126 if (fcntl(fd1, F_SETLK, &flock) == -1) e(36);
127 if ((fd2 = dup(fd1)) != 4) e(37);
128 if (fcntl(fd1, F_SETLK, &flock) == -1) e(38);
129 if (fcntl(fd1, F_GETLK, &flock) == -1) e(39);
130#if 0 /* XXX - see test7.c */
131 if (flock.l_type != F_WRLCK) e(40);
132 if (flock.l_pid != getpid()) e(41);
133#endif /* 0 */
134 flock.l_type = F_WRLCK;
135 if (fcntl(fd2, F_GETLK, &flock) == -1) e(42);
136#if 0 /* XXX - see test7.c */
137 if (flock.l_type != F_WRLCK) e(43);
138 if (flock.l_pid != getpid()) e(44);
139#endif /* 0 */
140 if (close(fd1) != 0) e(45);
141 if (close(fd2) != 0) e(46);
142
143 System("rm -rf ../DIR_29/*");
144}
145
146void test29b()
147{
148 int fd;
149 char buf[32];
150
151 subtest = 2;
152
153 /* Test file called ``file''. */
154 System("echo 'Hallo!' > file");
155
156 /* Check dup2() call with the same fds. Should have no effect. */
157 if ((fd = open("file", O_RDONLY)) != 3) e(1);
158 if (read(fd, buf, 2) != 2) e(2);
159 if (strncmp(buf, "Ha", 2) != 0) e(3);
160 if (dup2(fd, fd) != fd) e(4);
161 if (read(fd, buf, 2) != 2) e(5);
162 if (strncmp(buf, "ll", 2) != 0) e(6);
163 if (dup2(fd, fd) != fd) e(7);
164 if (read(fd, buf, 2) != 2) e(8);
165 if (strncmp(buf, "o!", 2) != 0) e(9);
166 if (close(fd) != 0) e(10);
167
168 /* If dup2() call fails, the fildes2 argument has to stay open. */
169 if ((fd = open("file", O_RDONLY)) != 3) e(11);
170 if (read(fd, buf, 2) != 2) e(12);
171 if (strncmp(buf, "Ha", 2) != 0) e(13);
172 if (dup2(OPEN_MAX + 3, fd) != -1) e(14);
173 if (errno != EBADF) e(15);
174 if (read(fd, buf, 2) != 2) e(16);
175 if (strncmp(buf, "ll", 2) != 0) e(17);
176 if (dup2(-4, fd) != -1) e(18);
177 if (errno != EBADF) e(19);
178 if (read(fd, buf, 2) != 2) e(20);
179 if (strncmp(buf, "o!", 2) != 0) e(21);
180 if (close(fd) != 0) e(22);
181
182 System("rm -rf ../DIR_29/*");
183}
184
185void test29c()
186{
187 int i;
188
189 subtest = 3;
190
191 /* Check bad arguments to dup() and dup2(). */
192 for (i = -OPEN_MAX; i < OPEN_MAX * 2; i++) {
193
194 /* ``i'' is a valid and open fd. */
195 if (i >= 0 && i < 3) continue;
196
197 /* If ``i'' is a valid fd it is not open. */
198 if (dup(i) != -1) e(1);
199 if (errno != EBADF) e(2);
200
201 /* ``i'' Is OPEN_MAX. */
202 if (i == OPEN_MAX) {
203 if (dup2(0, i) != -1) e(3);
204 if (errno != EINVAL) e(4);
205 }
206
207 /* ``i'' Is out of range. */
208 if (i < 0 || i > OPEN_MAX) {
209 if (dup2(0, i) != -1) e(5);
210 if (errno != EBADF) e(6);
211 }
212 }
213
214 System("rm -rf ../DIR_29/*");
215}
216
217void e(n)
218int n;
219{
220 int err_num = errno; /* Save in case printf clobbers it. */
221
222 printf("Subtest %d, error %d errno=%d: ", subtest, n, errno);
223 errno = err_num;
224 perror("");
225 if (errct++ > MAX_ERROR) {
226 printf("Too many errors; test aborted\n");
227 chdir("..");
228 system("rm -rf DIR*");
229 exit(1);
230 }
231 errno = 0;
232}
233
234void quit()
235{
236 Chdir("..");
237 System("rm -rf DIR_29");
238
239 if (errct == 0) {
240 printf("ok\n");
241 exit(0);
242 } else {
243 printf("%d errors\n", errct);
244 exit(1);
245 }
246}
Note: See TracBrowser for help on using the repository browser.