1 /* test31: mkfifo() Author: Jan-Mark Wams (jms@cs.vu.nl) */
18 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
19 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
20 #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
25 char MaxName
[NAME_MAX
+ 1]; /* Name of maximum length */
26 char MaxPath
[PATH_MAX
]; /* Same for path */
27 char ToLongName
[NAME_MAX
+ 2]; /* Name of maximum +1 length */
28 char ToLongPath
[PATH_MAX
+ 1]; /* Same for path, both too long */
30 _PROTOTYPE(void main
, (int argc
, char *argv
[]));
31 _PROTOTYPE(void test31a
, (void));
32 _PROTOTYPE(void test31b
, (void));
33 _PROTOTYPE(void test31c
, (void));
34 _PROTOTYPE(void makelongnames
, (void));
35 _PROTOTYPE(void e
, (int number
));
36 _PROTOTYPE(void quit
, (void));
45 if (argc
== 2) m
= atoi(argv
[1]);
48 System("rm -rf DIR_31; mkdir DIR_31");
51 superuser
= (geteuid() == 0);
55 for (i
= 0; i
< ITERATIONS
; i
++) {
56 if (m
& 0001) test31a();
57 if (m
& 0002) test31b();
58 if (m
& 0004) test31c();
64 { /* Test normal operation. */
70 struct stat st
, dirst
;
76 System("rm -rf ../DIR_31/*");
78 /* Check if the file status information is updated correctly */
82 while (time1
== time((time_t *)0))
88 if (mkfifo("fifo", 0644) != 0) e(1);
91 } while (time1
!= time2
&& cnt
++ < 100);
94 if (st
.st_uid
!= geteuid()) e(3); /* Uid should be set. */
95 #if defined(NGROUPS_MAX) && NGROUPS_MAX == 0
96 if (st
.st_gid
!= getegid()) e(4);
97 #endif /* defined(NGROUPS_MAX) && NGROUPS_MAX == 0 */
98 if (!S_ISFIFO(st
.st_mode
)) e(5);
99 if (st
.st_mode
& 0777 != 0644) e(6);
100 if (st
.st_nlink
!= 1) e(7);
101 if (st
.st_ctime
!= time1
) e(8);
102 if (st
.st_atime
!= time1
) e(9);
103 if (st
.st_mtime
!= time1
) e(10);
104 if (st
.st_size
!= 0) e(11); /* File should be empty. */
106 /* Check if status for "." is updated. */
108 if (st
.st_ctime
<= dirst
.st_ctime
) e(12);
109 if (st
.st_mtime
<= dirst
.st_mtime
) e(13);
111 /* Basic checking if a fifo file created with mkfifo() is a pipe. */
112 alarm(10); /* in case fifo hangs */
114 case -1: printf("Can't fork\n"); break;
116 if ((fd
= open("fifo", O_RDONLY
)) != 3) e(14);
117 if (read(fd
, buf
, BUF_SIZE
) != 7) e(15);
118 if (strcmp(buf
, "banana") != 0) e(16);
119 if (close(fd
) != 0) e(17);
120 if ((fd
= open("fifo", O_WRONLY
)) != 3) e(18);
121 if (write(fd
, "thanks", 7) != 7) e(19);
122 if (close(fd
) != 0) e(20);
126 if ((fd
= open("fifo", O_WRONLY
)) != 3) e(21);
127 if (write(fd
, "banana", 7) != 7) e(22);
128 if (close(fd
) != 0) e(23);
129 if ((fd
= open("fifo", O_RDONLY
)) != 3) e(24);
130 if (read(fd
, buf
, BUF_SIZE
) != 7) e(25);
131 if (strcmp(buf
, "thanks") != 0) e(26);
132 if (close(fd
) != 0) e(27);
134 if (stat_loc
!= 0) e(28); /* Alarm? */
143 System("rm -rf ../DIR_31/*");
145 /* Test maximal file name length. */
146 if (mkfifo(MaxName
, 0777) != 0) e(1);
147 if (unlink(MaxName
) != 0) e(2);
148 MaxPath
[strlen(MaxPath
) - 2] = '/';
149 MaxPath
[strlen(MaxPath
) - 1] = 'a'; /* make ././.../a */
150 if (mkfifo(MaxPath
, 0777) != 0) e(3);
151 if (unlink(MaxPath
) != 0) e(4);
152 MaxPath
[strlen(MaxPath
) - 1] = '/'; /* make ././.../a */
159 System("rm -rf ../DIR_31/*");
161 /* Check if mkfifo() removes, files, fifos, dirs. */
162 if (mkfifo("fifo", 0777) != 0) e(1);
163 System("mkdir dir; > file");
164 if (mkfifo("fifo", 0777) != -1) e(2);
165 if (errno
!= EEXIST
) e(3);
166 if (mkfifo("dir", 0777) != -1) e(4);
167 if (errno
!= EEXIST
) e(5);
168 if (mkfifo("file", 0777) != -1) e(6);
169 if (errno
!= EEXIST
) e(7);
171 /* Test empty path. */
172 if (mkfifo("", 0777) != -1) e(8);
173 if (errno
!= ENOENT
) e(9);
174 if (mkfifo("/tmp/noway/out", 0777) != -1) e(10);
175 if (errno
!= ENOENT
) e(11);
177 /* Test if path prefix is a directory. */
178 if (mkfifo("/etc/passwd/nono", 0777) != -1) e(12);
179 if (errno
!= ENOTDIR
) e(13);
181 mkdir("bar", 0777); /* make bar */
183 /* Check if no access on part of path generates the correct error. */
184 System("chmod 577 bar"); /* r-xrwxrwx */
186 if (mkfifo("bar/nono", 0666) != -1) e(14);
187 if (errno
!= EACCES
) e(15);
190 if (mkfifo("bar/nono", 0666) != 0) e(14);
191 if (unlink("bar/nono") != 0) e(666);
193 System("chmod 677 bar"); /* rw-rwxrwx */
195 if (mkfifo("bar/../nono", 0666) != -1) e(16);
196 if (errno
!= EACCES
) e(17);
198 if (unlink("nono") != -1) e(18);
201 System("rm -rf bar");
203 /* Test ToLongName and ToLongPath */
204 #ifdef _POSIX_NO_TRUNC
205 # if _POSIX_NO_TRUNC - 0 != -1
206 if (mkfifo(ToLongName
, 0777) != -1) e(19);
207 if (errno
!= ENAMETOOLONG
) e(20);
209 if (mkfifo(ToLongName
, 0777) != 0) e(21);
212 # include "error, this case requires dynamic checks and is not handled"
214 ToLongPath
[PATH_MAX
- 2] = '/';
215 ToLongPath
[PATH_MAX
- 1] = 'a';
216 if (mkfifo(ToLongPath
, 0777) != -1) e(22);
217 if (errno
!= ENAMETOOLONG
) e(23);
218 ToLongPath
[PATH_MAX
- 1] = '/';
225 memset(MaxName
, 'a', NAME_MAX
);
226 MaxName
[NAME_MAX
] = '\0';
227 for (i
= 0; i
< PATH_MAX
- 1; i
++) { /* idem path */
231 MaxPath
[PATH_MAX
- 1] = '\0';
233 strcpy(ToLongName
, MaxName
); /* copy them Max to ToLong */
234 strcpy(ToLongPath
, MaxPath
);
236 ToLongName
[NAME_MAX
] = 'a';
237 ToLongName
[NAME_MAX
+ 1] = '\0'; /* extend ToLongName by one too many */
238 ToLongPath
[PATH_MAX
- 1] = '/';
239 ToLongPath
[PATH_MAX
] = '\0'; /* inc ToLongPath by one */
245 int err_num
= errno
; /* Save in case printf clobbers it. */
247 printf("Subtest %d, error %d errno=%d: ", subtest
, n
, errno
);
250 if (errct
++ > MAX_ERROR
) {
251 printf("Too many errors; test aborted\n");
253 system("rm -rf DIR*");
262 System("rm -rf DIR_31");
268 printf("%d errors\n", errct
);