1 /* test31: mkfifo() Author: Jan-Mark Wams (jms@cs.vu.nl) */
21 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
22 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
23 #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
26 char *MaxName
; /* Name of maximum length */
27 char MaxPath
[PATH_MAX
]; /* Same for path */
28 char *ToLongName
; /* Name of maximum +1 length */
29 char ToLongPath
[PATH_MAX
+ 1]; /* Same for path, both too long */
34 void makelongnames(void);
36 int main(int argc
, char *argv
[])
41 if (argc
== 2) m
= atoi(argv
[1]);
45 superuser
= (geteuid() == 0);
48 for (i
= 0; i
< ITERATIONS
; i
++) {
49 if (m
& 0001) test31a();
50 if (m
& 0002) test31b();
51 if (m
& 0004) test31c();
58 { /* Test normal operation. */
64 struct stat st
, dirst
;
70 System("rm -rf ../DIR_31/*");
72 /* Check if the file status information is updated correctly */
76 while (time1
== time((time_t *)0))
82 if (mkfifo("fifo", 0644) != 0) e(1);
85 } while (time1
!= time2
&& cnt
++ < 100);
88 if (st
.st_uid
!= geteuid()) e(3); /* Uid should be set. */
89 #if defined(NGROUPS_MAX) && NGROUPS_MAX == 0
90 if (st
.st_gid
!= getegid()) e(4);
91 #endif /* defined(NGROUPS_MAX) && NGROUPS_MAX == 0 */
92 if (!S_ISFIFO(st
.st_mode
)) e(5);
93 if ((st
.st_mode
& 0777) != 0644) e(6);
94 if (st
.st_nlink
!= 1) e(7);
95 if (st
.st_ctime
!= time1
) e(8);
96 if (st
.st_atime
!= time1
) e(9);
97 if (st
.st_mtime
!= time1
) e(10);
98 if (st
.st_size
!= 0) e(11); /* File should be empty. */
100 /* Check if status for "." is updated. */
102 if (st
.st_ctime
<= dirst
.st_ctime
) e(12);
103 if (st
.st_mtime
<= dirst
.st_mtime
) e(13);
105 /* Basic checking if a fifo file created with mkfifo() is a pipe. */
106 alarm(10); /* in case fifo hangs */
108 case -1: printf("Can't fork\n"); break;
110 if ((fd
= open("fifo", O_RDONLY
)) != 3) e(14);
111 if (read(fd
, buf
, BUF_SIZE
) != 7) e(15);
112 if (strcmp(buf
, "banana") != 0) e(16);
113 if (close(fd
) != 0) e(17);
114 if ((fd
= open("fifo", O_WRONLY
)) != 3) e(18);
115 if (write(fd
, "thanks", 7) != 7) e(19);
116 if (close(fd
) != 0) e(20);
120 if ((fd
= open("fifo", O_WRONLY
)) != 3) e(21);
121 if (write(fd
, "banana", 7) != 7) e(22);
122 if (close(fd
) != 0) e(23);
123 if ((fd
= open("fifo", O_RDONLY
)) != 3) e(24);
124 if (read(fd
, buf
, BUF_SIZE
) != 7) e(25);
125 if (strcmp(buf
, "thanks") != 0) e(26);
126 if (close(fd
) != 0) e(27);
128 if (stat_loc
!= 0) e(28); /* Alarm? */
137 System("rm -rf ../DIR_31/*");
139 /* Test maximal file name length. */
140 if (mkfifo(MaxName
, 0777) != 0) e(1);
141 if (unlink(MaxName
) != 0) e(2);
142 MaxPath
[strlen(MaxPath
) - 2] = '/';
143 MaxPath
[strlen(MaxPath
) - 1] = 'a'; /* make ././.../a */
144 if (mkfifo(MaxPath
, 0777) != 0) e(3);
145 if (unlink(MaxPath
) != 0) e(4);
146 MaxPath
[strlen(MaxPath
) - 1] = '/'; /* make ././.../a */
154 System("rm -rf ../DIR_31/*");
156 /* Check if mkfifo() removes, files, fifos, dirs. */
157 if (mkfifo("fifo", 0777) != 0) e(1);
158 System("mkdir dir; > file");
159 if (mkfifo("fifo", 0777) != -1) e(2);
160 if (errno
!= EEXIST
) e(3);
161 if (mkfifo("dir", 0777) != -1) e(4);
162 if (errno
!= EEXIST
) e(5);
163 if (mkfifo("file", 0777) != -1) e(6);
164 if (errno
!= EEXIST
) e(7);
166 /* Test empty path. */
167 if (mkfifo("", 0777) != -1) e(8);
168 if (errno
!= ENOENT
) e(9);
169 if (mkfifo("/tmp/noway/out", 0777) != -1) e(10);
170 if (errno
!= ENOENT
) e(11);
172 /* Test if path prefix is a directory. */
173 if (mkfifo("/etc/passwd/nono", 0777) != -1) e(12);
174 if (errno
!= ENOTDIR
) e(13);
176 mkdir("bar", 0777); /* make bar */
178 /* Check if no access on part of path generates the correct error. */
179 System("chmod 577 bar"); /* r-xrwxrwx */
181 if (mkfifo("bar/nono", 0666) != -1) e(14);
182 if (errno
!= EACCES
) e(15);
185 if (mkfifo("bar/nono", 0666) != 0) e(14);
186 if (unlink("bar/nono") != 0) e(666);
188 System("chmod 677 bar"); /* rw-rwxrwx */
190 if (mkfifo("bar/../nono", 0666) != -1) e(16);
191 if (errno
!= EACCES
) e(17);
193 if (unlink("nono") != -1) e(18);
196 System("rm -rf bar");
198 /* Test ToLongName and ToLongPath */
199 does_truncate
= does_fs_truncate();
201 if (mkfifo(ToLongName
, 0777) != 0) e(19);
203 if (mkfifo(ToLongName
, 0777) != -1) e(20);
204 if (errno
!= ENAMETOOLONG
) e(21);
207 ToLongPath
[PATH_MAX
- 2] = '/';
208 ToLongPath
[PATH_MAX
- 1] = 'a';
209 if (mkfifo(ToLongPath
, 0777) != -1) e(22);
210 if (errno
!= ENAMETOOLONG
) e(23);
211 ToLongPath
[PATH_MAX
- 1] = '/';
219 max_name_length
= name_max("."); /* Aka NAME_MAX, but not every FS supports
220 * the same length, hence runtime check */
221 MaxName
= malloc(max_name_length
+ 1);
222 ToLongName
= malloc(max_name_length
+ 1 + 1); /* Name of maximum +1 length */
223 memset(MaxName
, 'a', max_name_length
);
224 MaxName
[max_name_length
] = '\0';
226 for (i
= 0; i
< PATH_MAX
- 1; i
++) { /* idem path */
230 MaxPath
[PATH_MAX
- 1] = '\0';
232 strcpy(ToLongName
, MaxName
); /* copy them Max to ToLong */
233 strcpy(ToLongPath
, MaxPath
);
235 ToLongName
[max_name_length
] = 'a';
236 ToLongName
[max_name_length
+1] = '\0';/* extend ToLongName by one too many */
237 ToLongPath
[PATH_MAX
- 1] = '/';
238 ToLongPath
[PATH_MAX
] = '\0'; /* inc ToLongPath by one */