1*433d6423SLionel Sambuc /* test31: mkfifo() Author: Jan-Mark Wams (jms@cs.vu.nl) */
2*433d6423SLionel Sambuc
3*433d6423SLionel Sambuc #include <sys/types.h>
4*433d6423SLionel Sambuc #include <sys/stat.h>
5*433d6423SLionel Sambuc #include <sys/wait.h>
6*433d6423SLionel Sambuc #include <stdlib.h>
7*433d6423SLionel Sambuc #include <unistd.h>
8*433d6423SLionel Sambuc #include <string.h>
9*433d6423SLionel Sambuc #include <fcntl.h>
10*433d6423SLionel Sambuc #include <limits.h>
11*433d6423SLionel Sambuc #include <errno.h>
12*433d6423SLionel Sambuc #include <time.h>
13*433d6423SLionel Sambuc #include <stdio.h>
14*433d6423SLionel Sambuc
15*433d6423SLionel Sambuc int max_error = 4;
16*433d6423SLionel Sambuc #include "common.h"
17*433d6423SLionel Sambuc
18*433d6423SLionel Sambuc #define ITERATIONS 10
19*433d6423SLionel Sambuc
20*433d6423SLionel Sambuc
21*433d6423SLionel Sambuc #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
22*433d6423SLionel Sambuc #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
23*433d6423SLionel Sambuc #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
24*433d6423SLionel Sambuc
25*433d6423SLionel Sambuc int superuser;
26*433d6423SLionel Sambuc char *MaxName; /* Name of maximum length */
27*433d6423SLionel Sambuc char MaxPath[PATH_MAX]; /* Same for path */
28*433d6423SLionel Sambuc char *ToLongName; /* Name of maximum +1 length */
29*433d6423SLionel Sambuc char ToLongPath[PATH_MAX + 1]; /* Same for path, both too long */
30*433d6423SLionel Sambuc
31*433d6423SLionel Sambuc void test31a(void);
32*433d6423SLionel Sambuc void test31b(void);
33*433d6423SLionel Sambuc void test31c(void);
34*433d6423SLionel Sambuc void makelongnames(void);
35*433d6423SLionel Sambuc
main(int argc,char * argv[])36*433d6423SLionel Sambuc int main(int argc, char *argv[])
37*433d6423SLionel Sambuc {
38*433d6423SLionel Sambuc int i, m = 0xFFFF;
39*433d6423SLionel Sambuc
40*433d6423SLionel Sambuc sync();
41*433d6423SLionel Sambuc if (argc == 2) m = atoi(argv[1]);
42*433d6423SLionel Sambuc umask(0000);
43*433d6423SLionel Sambuc start(31);
44*433d6423SLionel Sambuc makelongnames();
45*433d6423SLionel Sambuc superuser = (geteuid() == 0);
46*433d6423SLionel Sambuc
47*433d6423SLionel Sambuc
48*433d6423SLionel Sambuc for (i = 0; i < ITERATIONS; i++) {
49*433d6423SLionel Sambuc if (m & 0001) test31a();
50*433d6423SLionel Sambuc if (m & 0002) test31b();
51*433d6423SLionel Sambuc if (m & 0004) test31c();
52*433d6423SLionel Sambuc }
53*433d6423SLionel Sambuc quit();
54*433d6423SLionel Sambuc return 1;
55*433d6423SLionel Sambuc }
56*433d6423SLionel Sambuc
test31a()57*433d6423SLionel Sambuc void test31a()
58*433d6423SLionel Sambuc { /* Test normal operation. */
59*433d6423SLionel Sambuc
60*433d6423SLionel Sambuc #define BUF_SIZE 1024
61*433d6423SLionel Sambuc
62*433d6423SLionel Sambuc int fd;
63*433d6423SLionel Sambuc char buf[BUF_SIZE];
64*433d6423SLionel Sambuc struct stat st, dirst;
65*433d6423SLionel Sambuc time_t time1, time2;
66*433d6423SLionel Sambuc int stat_loc, cnt;
67*433d6423SLionel Sambuc
68*433d6423SLionel Sambuc subtest = 1;
69*433d6423SLionel Sambuc
70*433d6423SLionel Sambuc System("rm -rf ../DIR_31/*");
71*433d6423SLionel Sambuc
72*433d6423SLionel Sambuc /* Check if the file status information is updated correctly */
73*433d6423SLionel Sambuc cnt = 0;
74*433d6423SLionel Sambuc Stat(".", &dirst);
75*433d6423SLionel Sambuc time(&time1);
76*433d6423SLionel Sambuc while (time1 == time((time_t *)0))
77*433d6423SLionel Sambuc ;
78*433d6423SLionel Sambuc
79*433d6423SLionel Sambuc do {
80*433d6423SLionel Sambuc unlink("fifo");
81*433d6423SLionel Sambuc time(&time1);
82*433d6423SLionel Sambuc if (mkfifo("fifo", 0644) != 0) e(1);
83*433d6423SLionel Sambuc Stat("fifo", &st);
84*433d6423SLionel Sambuc time(&time2);
85*433d6423SLionel Sambuc } while (time1 != time2 && cnt++ < 100);
86*433d6423SLionel Sambuc
87*433d6423SLionel Sambuc if (cnt >= 100) e(2);
88*433d6423SLionel Sambuc if (st.st_uid != geteuid()) e(3); /* Uid should be set. */
89*433d6423SLionel Sambuc #if defined(NGROUPS_MAX) && NGROUPS_MAX == 0
90*433d6423SLionel Sambuc if (st.st_gid != getegid()) e(4);
91*433d6423SLionel Sambuc #endif /* defined(NGROUPS_MAX) && NGROUPS_MAX == 0 */
92*433d6423SLionel Sambuc if (!S_ISFIFO(st.st_mode)) e(5);
93*433d6423SLionel Sambuc if ((st.st_mode & 0777) != 0644) e(6);
94*433d6423SLionel Sambuc if (st.st_nlink != 1) e(7);
95*433d6423SLionel Sambuc if (st.st_ctime != time1) e(8);
96*433d6423SLionel Sambuc if (st.st_atime != time1) e(9);
97*433d6423SLionel Sambuc if (st.st_mtime != time1) e(10);
98*433d6423SLionel Sambuc if (st.st_size != 0) e(11); /* File should be empty. */
99*433d6423SLionel Sambuc
100*433d6423SLionel Sambuc /* Check if status for "." is updated. */
101*433d6423SLionel Sambuc Stat(".", &st);
102*433d6423SLionel Sambuc if (st.st_ctime <= dirst.st_ctime) e(12);
103*433d6423SLionel Sambuc if (st.st_mtime <= dirst.st_mtime) e(13);
104*433d6423SLionel Sambuc
105*433d6423SLionel Sambuc /* Basic checking if a fifo file created with mkfifo() is a pipe. */
106*433d6423SLionel Sambuc alarm(10); /* in case fifo hangs */
107*433d6423SLionel Sambuc switch (fork()) {
108*433d6423SLionel Sambuc case -1: printf("Can't fork\n"); break;
109*433d6423SLionel Sambuc case 0:
110*433d6423SLionel Sambuc if ((fd = open("fifo", O_RDONLY)) != 3) e(14);
111*433d6423SLionel Sambuc if (read(fd, buf, BUF_SIZE) != 7) e(15);
112*433d6423SLionel Sambuc if (strcmp(buf, "banana") != 0) e(16);
113*433d6423SLionel Sambuc if (close(fd) != 0) e(17);
114*433d6423SLionel Sambuc if ((fd = open("fifo", O_WRONLY)) != 3) e(18);
115*433d6423SLionel Sambuc if (write(fd, "thanks", 7) != 7) e(19);
116*433d6423SLionel Sambuc if (close(fd) != 0) e(20);
117*433d6423SLionel Sambuc exit(0);
118*433d6423SLionel Sambuc
119*433d6423SLionel Sambuc default:
120*433d6423SLionel Sambuc if ((fd = open("fifo", O_WRONLY)) != 3) e(21);
121*433d6423SLionel Sambuc if (write(fd, "banana", 7) != 7) e(22);
122*433d6423SLionel Sambuc if (close(fd) != 0) e(23);
123*433d6423SLionel Sambuc if ((fd = open("fifo", O_RDONLY)) != 3) e(24);
124*433d6423SLionel Sambuc if (read(fd, buf, BUF_SIZE) != 7) e(25);
125*433d6423SLionel Sambuc if (strcmp(buf, "thanks") != 0) e(26);
126*433d6423SLionel Sambuc if (close(fd) != 0) e(27);
127*433d6423SLionel Sambuc wait(&stat_loc);
128*433d6423SLionel Sambuc if (stat_loc != 0) e(28); /* Alarm? */
129*433d6423SLionel Sambuc }
130*433d6423SLionel Sambuc alarm(0);
131*433d6423SLionel Sambuc }
132*433d6423SLionel Sambuc
test31b()133*433d6423SLionel Sambuc void test31b()
134*433d6423SLionel Sambuc {
135*433d6423SLionel Sambuc subtest = 2;
136*433d6423SLionel Sambuc
137*433d6423SLionel Sambuc System("rm -rf ../DIR_31/*");
138*433d6423SLionel Sambuc
139*433d6423SLionel Sambuc /* Test maximal file name length. */
140*433d6423SLionel Sambuc if (mkfifo(MaxName, 0777) != 0) e(1);
141*433d6423SLionel Sambuc if (unlink(MaxName) != 0) e(2);
142*433d6423SLionel Sambuc MaxPath[strlen(MaxPath) - 2] = '/';
143*433d6423SLionel Sambuc MaxPath[strlen(MaxPath) - 1] = 'a'; /* make ././.../a */
144*433d6423SLionel Sambuc if (mkfifo(MaxPath, 0777) != 0) e(3);
145*433d6423SLionel Sambuc if (unlink(MaxPath) != 0) e(4);
146*433d6423SLionel Sambuc MaxPath[strlen(MaxPath) - 1] = '/'; /* make ././.../a */
147*433d6423SLionel Sambuc }
148*433d6423SLionel Sambuc
test31c()149*433d6423SLionel Sambuc void test31c()
150*433d6423SLionel Sambuc {
151*433d6423SLionel Sambuc int does_truncate;
152*433d6423SLionel Sambuc subtest = 3;
153*433d6423SLionel Sambuc
154*433d6423SLionel Sambuc System("rm -rf ../DIR_31/*");
155*433d6423SLionel Sambuc
156*433d6423SLionel Sambuc /* Check if mkfifo() removes, files, fifos, dirs. */
157*433d6423SLionel Sambuc if (mkfifo("fifo", 0777) != 0) e(1);
158*433d6423SLionel Sambuc System("mkdir dir; > file");
159*433d6423SLionel Sambuc if (mkfifo("fifo", 0777) != -1) e(2);
160*433d6423SLionel Sambuc if (errno != EEXIST) e(3);
161*433d6423SLionel Sambuc if (mkfifo("dir", 0777) != -1) e(4);
162*433d6423SLionel Sambuc if (errno != EEXIST) e(5);
163*433d6423SLionel Sambuc if (mkfifo("file", 0777) != -1) e(6);
164*433d6423SLionel Sambuc if (errno != EEXIST) e(7);
165*433d6423SLionel Sambuc
166*433d6423SLionel Sambuc /* Test empty path. */
167*433d6423SLionel Sambuc if (mkfifo("", 0777) != -1) e(8);
168*433d6423SLionel Sambuc if (errno != ENOENT) e(9);
169*433d6423SLionel Sambuc if (mkfifo("/tmp/noway/out", 0777) != -1) e(10);
170*433d6423SLionel Sambuc if (errno != ENOENT) e(11);
171*433d6423SLionel Sambuc
172*433d6423SLionel Sambuc /* Test if path prefix is a directory. */
173*433d6423SLionel Sambuc if (mkfifo("/etc/passwd/nono", 0777) != -1) e(12);
174*433d6423SLionel Sambuc if (errno != ENOTDIR) e(13);
175*433d6423SLionel Sambuc
176*433d6423SLionel Sambuc mkdir("bar", 0777); /* make bar */
177*433d6423SLionel Sambuc
178*433d6423SLionel Sambuc /* Check if no access on part of path generates the correct error. */
179*433d6423SLionel Sambuc System("chmod 577 bar"); /* r-xrwxrwx */
180*433d6423SLionel Sambuc if (!superuser) {
181*433d6423SLionel Sambuc if (mkfifo("bar/nono", 0666) != -1) e(14);
182*433d6423SLionel Sambuc if (errno != EACCES) e(15);
183*433d6423SLionel Sambuc }
184*433d6423SLionel Sambuc if (superuser) {
185*433d6423SLionel Sambuc if (mkfifo("bar/nono", 0666) != 0) e(14);
186*433d6423SLionel Sambuc if (unlink("bar/nono") != 0) e(666);
187*433d6423SLionel Sambuc }
188*433d6423SLionel Sambuc System("chmod 677 bar"); /* rw-rwxrwx */
189*433d6423SLionel Sambuc if (!superuser) {
190*433d6423SLionel Sambuc if (mkfifo("bar/../nono", 0666) != -1) e(16);
191*433d6423SLionel Sambuc if (errno != EACCES) e(17);
192*433d6423SLionel Sambuc }
193*433d6423SLionel Sambuc if (unlink("nono") != -1) e(18);
194*433d6423SLionel Sambuc
195*433d6423SLionel Sambuc /* Clean up bar. */
196*433d6423SLionel Sambuc System("rm -rf bar");
197*433d6423SLionel Sambuc
198*433d6423SLionel Sambuc /* Test ToLongName and ToLongPath */
199*433d6423SLionel Sambuc does_truncate = does_fs_truncate();
200*433d6423SLionel Sambuc if (does_truncate) {
201*433d6423SLionel Sambuc if (mkfifo(ToLongName, 0777) != 0) e(19);
202*433d6423SLionel Sambuc } else {
203*433d6423SLionel Sambuc if (mkfifo(ToLongName, 0777) != -1) e(20);
204*433d6423SLionel Sambuc if (errno != ENAMETOOLONG) e(21);
205*433d6423SLionel Sambuc }
206*433d6423SLionel Sambuc
207*433d6423SLionel Sambuc ToLongPath[PATH_MAX - 2] = '/';
208*433d6423SLionel Sambuc ToLongPath[PATH_MAX - 1] = 'a';
209*433d6423SLionel Sambuc if (mkfifo(ToLongPath, 0777) != -1) e(22);
210*433d6423SLionel Sambuc if (errno != ENAMETOOLONG) e(23);
211*433d6423SLionel Sambuc ToLongPath[PATH_MAX - 1] = '/';
212*433d6423SLionel Sambuc }
213*433d6423SLionel Sambuc
makelongnames()214*433d6423SLionel Sambuc void makelongnames()
215*433d6423SLionel Sambuc {
216*433d6423SLionel Sambuc register int i;
217*433d6423SLionel Sambuc int max_name_length;
218*433d6423SLionel Sambuc
219*433d6423SLionel Sambuc max_name_length = name_max("."); /* Aka NAME_MAX, but not every FS supports
220*433d6423SLionel Sambuc * the same length, hence runtime check */
221*433d6423SLionel Sambuc MaxName = malloc(max_name_length + 1);
222*433d6423SLionel Sambuc ToLongName = malloc(max_name_length + 1 + 1); /* Name of maximum +1 length */
223*433d6423SLionel Sambuc memset(MaxName, 'a', max_name_length);
224*433d6423SLionel Sambuc MaxName[max_name_length] = '\0';
225*433d6423SLionel Sambuc
226*433d6423SLionel Sambuc for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
227*433d6423SLionel Sambuc MaxPath[i++] = '.';
228*433d6423SLionel Sambuc MaxPath[i] = '/';
229*433d6423SLionel Sambuc }
230*433d6423SLionel Sambuc MaxPath[PATH_MAX - 1] = '\0';
231*433d6423SLionel Sambuc
232*433d6423SLionel Sambuc strcpy(ToLongName, MaxName); /* copy them Max to ToLong */
233*433d6423SLionel Sambuc strcpy(ToLongPath, MaxPath);
234*433d6423SLionel Sambuc
235*433d6423SLionel Sambuc ToLongName[max_name_length] = 'a';
236*433d6423SLionel Sambuc ToLongName[max_name_length+1] = '\0';/* extend ToLongName by one too many */
237*433d6423SLionel Sambuc ToLongPath[PATH_MAX - 1] = '/';
238*433d6423SLionel Sambuc ToLongPath[PATH_MAX] = '\0'; /* inc ToLongPath by one */
239*433d6423SLionel Sambuc }
240