1 /* test31: mkfifo() Author: Jan-Mark Wams (jms@cs.vu.nl) */
20 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
21 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
22 #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
25 char *MaxName
; /* Name of maximum length */
26 char MaxPath
[PATH_MAX
]; /* Same for path */
27 char *ToLongName
; /* Name of maximum +1 length */
28 char ToLongPath
[PATH_MAX
+ 1]; /* Same for path, both too long */
33 void makelongnames(void);
35 int main(int argc
, char *argv
[])
40 if (argc
== 2) m
= atoi(argv
[1]);
44 superuser
= (geteuid() == 0);
47 for (i
= 0; i
< ITERATIONS
; i
++) {
48 if (m
& 0001) test31a();
49 if (m
& 0002) test31b();
50 if (m
& 0004) test31c();
57 { /* Test normal operation. */
63 struct stat st
, dirst
;
69 System("rm -rf ../DIR_31/*");
71 /* Check if the file status information is updated correctly */
75 while (time1
== time((time_t *)0))
81 if (mkfifo("fifo", 0644) != 0) e(1);
84 } while (time1
!= time2
&& cnt
++ < 100);
87 if (st
.st_uid
!= geteuid()) e(3); /* Uid should be set. */
88 #if defined(NGROUPS_MAX) && NGROUPS_MAX == 0
89 if (st
.st_gid
!= getegid()) e(4);
90 #endif /* defined(NGROUPS_MAX) && NGROUPS_MAX == 0 */
91 if (!S_ISFIFO(st
.st_mode
)) e(5);
92 if ((st
.st_mode
& 0777) != 0644) e(6);
93 if (st
.st_nlink
!= 1) e(7);
94 if (st
.st_ctime
!= time1
) e(8);
95 if (st
.st_atime
!= time1
) e(9);
96 if (st
.st_mtime
!= time1
) e(10);
97 if (st
.st_size
!= 0) e(11); /* File should be empty. */
99 /* Check if status for "." is updated. */
101 if (st
.st_ctime
<= dirst
.st_ctime
) e(12);
102 if (st
.st_mtime
<= dirst
.st_mtime
) e(13);
104 /* Basic checking if a fifo file created with mkfifo() is a pipe. */
105 alarm(10); /* in case fifo hangs */
107 case -1: printf("Can't fork\n"); break;
109 if ((fd
= open("fifo", O_RDONLY
)) != 3) e(14);
110 if (read(fd
, buf
, BUF_SIZE
) != 7) e(15);
111 if (strcmp(buf
, "banana") != 0) e(16);
112 if (close(fd
) != 0) e(17);
113 if ((fd
= open("fifo", O_WRONLY
)) != 3) e(18);
114 if (write(fd
, "thanks", 7) != 7) e(19);
115 if (close(fd
) != 0) e(20);
119 if ((fd
= open("fifo", O_WRONLY
)) != 3) e(21);
120 if (write(fd
, "banana", 7) != 7) e(22);
121 if (close(fd
) != 0) e(23);
122 if ((fd
= open("fifo", O_RDONLY
)) != 3) e(24);
123 if (read(fd
, buf
, BUF_SIZE
) != 7) e(25);
124 if (strcmp(buf
, "thanks") != 0) e(26);
125 if (close(fd
) != 0) e(27);
127 if (stat_loc
!= 0) e(28); /* Alarm? */
136 System("rm -rf ../DIR_31/*");
138 /* Test maximal file name length. */
139 if (mkfifo(MaxName
, 0777) != 0) e(1);
140 if (unlink(MaxName
) != 0) e(2);
141 MaxPath
[strlen(MaxPath
) - 2] = '/';
142 MaxPath
[strlen(MaxPath
) - 1] = 'a'; /* make ././.../a */
143 if (mkfifo(MaxPath
, 0777) != 0) e(3);
144 if (unlink(MaxPath
) != 0) e(4);
145 MaxPath
[strlen(MaxPath
) - 1] = '/'; /* make ././.../a */
153 System("rm -rf ../DIR_31/*");
155 /* Check if mkfifo() removes, files, fifos, dirs. */
156 if (mkfifo("fifo", 0777) != 0) e(1);
157 System("mkdir dir; > file");
158 if (mkfifo("fifo", 0777) != -1) e(2);
159 if (errno
!= EEXIST
) e(3);
160 if (mkfifo("dir", 0777) != -1) e(4);
161 if (errno
!= EEXIST
) e(5);
162 if (mkfifo("file", 0777) != -1) e(6);
163 if (errno
!= EEXIST
) e(7);
165 /* Test empty path. */
166 if (mkfifo("", 0777) != -1) e(8);
167 if (errno
!= ENOENT
) e(9);
168 if (mkfifo("/tmp/noway/out", 0777) != -1) e(10);
169 if (errno
!= ENOENT
) e(11);
171 /* Test if path prefix is a directory. */
172 if (mkfifo("/etc/passwd/nono", 0777) != -1) e(12);
173 if (errno
!= ENOTDIR
) e(13);
175 mkdir("bar", 0777); /* make bar */
177 /* Check if no access on part of path generates the correct error. */
178 System("chmod 577 bar"); /* r-xrwxrwx */
180 if (mkfifo("bar/nono", 0666) != -1) e(14);
181 if (errno
!= EACCES
) e(15);
184 if (mkfifo("bar/nono", 0666) != 0) e(14);
185 if (unlink("bar/nono") != 0) e(666);
187 System("chmod 677 bar"); /* rw-rwxrwx */
189 if (mkfifo("bar/../nono", 0666) != -1) e(16);
190 if (errno
!= EACCES
) e(17);
192 if (unlink("nono") != -1) e(18);
195 System("rm -rf bar");
197 /* Test ToLongName and ToLongPath */
198 does_truncate
= does_fs_truncate();
200 if (mkfifo(ToLongName
, 0777) != 0) e(19);
202 if (mkfifo(ToLongName
, 0777) != -1) e(20);
203 if (errno
!= ENAMETOOLONG
) e(21);
206 ToLongPath
[PATH_MAX
- 2] = '/';
207 ToLongPath
[PATH_MAX
- 1] = 'a';
208 if (mkfifo(ToLongPath
, 0777) != -1) e(22);
209 if (errno
!= ENAMETOOLONG
) e(23);
210 ToLongPath
[PATH_MAX
- 1] = '/';
218 max_name_length
= name_max("."); /* Aka NAME_MAX, but not every FS supports
219 * the same length, hence runtime check */
220 MaxName
= malloc(max_name_length
+ 1);
221 ToLongName
= malloc(max_name_length
+ 1 + 1); /* Name of maximum +1 length */
222 memset(MaxName
, 'a', max_name_length
);
223 MaxName
[max_name_length
] = '\0';
225 for (i
= 0; i
< PATH_MAX
- 1; i
++) { /* idem path */
229 MaxPath
[PATH_MAX
- 1] = '\0';
231 strcpy(ToLongName
, MaxName
); /* copy them Max to ToLong */
232 strcpy(ToLongPath
, MaxPath
);
234 ToLongName
[max_name_length
] = 'a';
235 ToLongName
[max_name_length
+1] = '\0';/* extend ToLongName by one too many */
236 ToLongPath
[PATH_MAX
- 1] = '/';
237 ToLongPath
[PATH_MAX
] = '\0'; /* inc ToLongPath by one */