1 /* test29: dup() dup2() Author: Jan-Mark Wams (jms@cs.vu.nl) */
3 /* The definition of ``dup2()'' is realy a big mess! For:
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!
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>
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)
40 char MaxName
[NAME_MAX
+ 1]; /* Name of maximum length */
41 char MaxPath
[PATH_MAX
]; /* Same for path */
42 char ToLongName
[NAME_MAX
+ 2]; /* Name of maximum +1 length */
43 char ToLongPath
[PATH_MAX
+ 1]; /* Same for path, both too long */
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));
59 if (argc
== 2) m
= atoi(argv
[1]);
62 System("rm -rf DIR_29; mkdir DIR_29");
64 superuser
= (geteuid() == 0);
66 for (i
= 0; i
< ITERATIONS
; i
++) {
67 if (m
& 0001) test29a();
68 if (m
& 0002) test29b();
69 if (m
& 0004) test29c();
76 int fd1
, fd2
, fd3
, fd4
, fd5
;
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);
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);
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
;
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);
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);
140 if (close(fd1
) != 0) e(45);
141 if (close(fd2
) != 0) e(46);
143 System("rm -rf ../DIR_29/*");
153 /* Test file called ``file''. */
154 System("echo 'Hallo!' > file");
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);
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);
182 System("rm -rf ../DIR_29/*");
191 /* Check bad arguments to dup() and dup2(). */
192 for (i
= -OPEN_MAX
; i
< OPEN_MAX
* 2; i
++) {
194 /* ``i'' is a valid and open fd. */
195 if (i
>= 0 && i
< 3) continue;
197 /* If ``i'' is a valid fd it is not open. */
198 if (dup(i
) != -1) e(1);
199 if (errno
!= EBADF
) e(2);
201 /* ``i'' Is OPEN_MAX. */
203 if (dup2(0, i
) != -1) e(3);
204 if (errno
!= EINVAL
) e(4);
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);
214 System("rm -rf ../DIR_29/*");
220 int err_num
= errno
; /* Save in case printf clobbers it. */
222 printf("Subtest %d, error %d errno=%d: ", subtest
, n
, errno
);
225 if (errct
++ > MAX_ERROR
) {
226 printf("Too many errors; test aborted\n");
228 system("rm -rf DIR*");
237 System("rm -rf DIR_29");
243 printf("%d errors\n", errct
);