1*433d6423SLionel Sambuc /* test30: creat() (p) Jan-Mark Wams. email: jms@cs.vu.nl */
2*433d6423SLionel Sambuc
3*433d6423SLionel Sambuc /*
4*433d6423SLionel Sambuc ** Creat() should be equivalent to:
5*433d6423SLionel Sambuc ** open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
6*433d6423SLionel Sambuc ** Since we can not look in the source, we can not assume creat() is
7*433d6423SLionel Sambuc ** a mere sysnonym (= a systemcall synonym).
8*433d6423SLionel Sambuc */
9*433d6423SLionel Sambuc
10*433d6423SLionel Sambuc #include <sys/types.h>
11*433d6423SLionel Sambuc #include <sys/stat.h>
12*433d6423SLionel Sambuc #include <sys/wait.h>
13*433d6423SLionel Sambuc #include <stdlib.h>
14*433d6423SLionel Sambuc #include <unistd.h>
15*433d6423SLionel Sambuc #include <string.h>
16*433d6423SLionel Sambuc #include <fcntl.h>
17*433d6423SLionel Sambuc #include <limits.h>
18*433d6423SLionel Sambuc #include <errno.h>
19*433d6423SLionel Sambuc #include <time.h>
20*433d6423SLionel Sambuc #include <stdio.h>
21*433d6423SLionel Sambuc
22*433d6423SLionel Sambuc int max_error = 4;
23*433d6423SLionel Sambuc #include "common.h"
24*433d6423SLionel Sambuc
25*433d6423SLionel Sambuc #define ITERATIONS 10
26*433d6423SLionel Sambuc
27*433d6423SLionel Sambuc
28*433d6423SLionel Sambuc #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
29*433d6423SLionel Sambuc #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
30*433d6423SLionel Sambuc #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
31*433d6423SLionel Sambuc
32*433d6423SLionel Sambuc int superuser;
33*433d6423SLionel Sambuc char *MaxName; /* Name of maximum length */
34*433d6423SLionel Sambuc char MaxPath[PATH_MAX]; /* Same for path */
35*433d6423SLionel Sambuc char *ToLongName; /* Name of maximum +1 length */
36*433d6423SLionel Sambuc char ToLongPath[PATH_MAX + 1]; /* Same for path, both too long */
37*433d6423SLionel Sambuc
38*433d6423SLionel Sambuc void test30a(void);
39*433d6423SLionel Sambuc void test30b(void);
40*433d6423SLionel Sambuc void test30c(void);
41*433d6423SLionel Sambuc void makelongnames(void);
42*433d6423SLionel Sambuc
main(int argc,char * argv[])43*433d6423SLionel Sambuc int main(int argc, char *argv[])
44*433d6423SLionel Sambuc {
45*433d6423SLionel Sambuc int i, m = 0xFFFF;
46*433d6423SLionel Sambuc
47*433d6423SLionel Sambuc sync();
48*433d6423SLionel Sambuc if (argc == 2) m = atoi(argv[1]);
49*433d6423SLionel Sambuc umask(0000);
50*433d6423SLionel Sambuc start(30);
51*433d6423SLionel Sambuc makelongnames();
52*433d6423SLionel Sambuc superuser = (geteuid() == 0);
53*433d6423SLionel Sambuc
54*433d6423SLionel Sambuc
55*433d6423SLionel Sambuc for (i = 0; i < ITERATIONS; i++) {
56*433d6423SLionel Sambuc if (m & 0001) test30a();
57*433d6423SLionel Sambuc if (m & 0002) test30b();
58*433d6423SLionel Sambuc if (m & 0004) test30c();
59*433d6423SLionel Sambuc }
60*433d6423SLionel Sambuc quit();
61*433d6423SLionel Sambuc
62*433d6423SLionel Sambuc return(-1); /* Unreachable */
63*433d6423SLionel Sambuc }
64*433d6423SLionel Sambuc
test30a()65*433d6423SLionel Sambuc void test30a()
66*433d6423SLionel Sambuc { /* Test normal operation. */
67*433d6423SLionel Sambuc
68*433d6423SLionel Sambuc #define BUF_SIZE 1024
69*433d6423SLionel Sambuc
70*433d6423SLionel Sambuc int fd1, fd2;
71*433d6423SLionel Sambuc char buf[BUF_SIZE];
72*433d6423SLionel Sambuc struct stat st, dirst;
73*433d6423SLionel Sambuc time_t time1, time2;
74*433d6423SLionel Sambuc int stat_loc, cnt;
75*433d6423SLionel Sambuc
76*433d6423SLionel Sambuc subtest = 1;
77*433d6423SLionel Sambuc
78*433d6423SLionel Sambuc System("rm -rf ../DIR_30/*");
79*433d6423SLionel Sambuc
80*433d6423SLionel Sambuc /* Check if processes have independent new fds */
81*433d6423SLionel Sambuc switch (fork()) {
82*433d6423SLionel Sambuc case -1: printf("Can't fork\n"); break;
83*433d6423SLionel Sambuc case 0:
84*433d6423SLionel Sambuc alarm(20);
85*433d6423SLionel Sambuc if ((fd1 = creat("myfile", 0644)) != 3) e(1);
86*433d6423SLionel Sambuc if (close(fd1) != 0) e(2);
87*433d6423SLionel Sambuc exit(0);
88*433d6423SLionel Sambuc default:
89*433d6423SLionel Sambuc if ((fd1 = creat("myfile", 0644)) != 3) e(3);
90*433d6423SLionel Sambuc if (close(fd1) != 0) e(4);
91*433d6423SLionel Sambuc if (wait(&stat_loc) == -1) e(5);
92*433d6423SLionel Sambuc if (stat_loc != 0) e(6);
93*433d6423SLionel Sambuc }
94*433d6423SLionel Sambuc
95*433d6423SLionel Sambuc /* Save the dir status. */
96*433d6423SLionel Sambuc Stat(".", &dirst);
97*433d6423SLionel Sambuc time(&time1);
98*433d6423SLionel Sambuc while (time1 == time((time_t *)0))
99*433d6423SLionel Sambuc ;
100*433d6423SLionel Sambuc
101*433d6423SLionel Sambuc /* Check if the file status information is updated correctly */
102*433d6423SLionel Sambuc cnt = 0;
103*433d6423SLionel Sambuc System("rm -rf myfile");
104*433d6423SLionel Sambuc do {
105*433d6423SLionel Sambuc time(&time1);
106*433d6423SLionel Sambuc if ((fd1 = creat("myfile", 0644)) != 3) e(7);
107*433d6423SLionel Sambuc Stat("myfile", &st);
108*433d6423SLionel Sambuc time(&time2);
109*433d6423SLionel Sambuc } while (time1 != time2 && cnt++ < 100);
110*433d6423SLionel Sambuc if (cnt >= 100) e(8);
111*433d6423SLionel Sambuc if (st.st_uid != geteuid()) e(9); /* Uid should be set. */
112*433d6423SLionel Sambuc #if defined(NGROUPS_MAX) && NGROUPS_MAX == 0
113*433d6423SLionel Sambuc if (st.st_gid != getegid()) e(10);
114*433d6423SLionel Sambuc #endif /* defined(NGROUPS_MAX) && NGROUPS_MAX == 0 */
115*433d6423SLionel Sambuc if (!S_ISREG(st.st_mode)) e(11);
116*433d6423SLionel Sambuc if ((st.st_mode & 0777) != 0644) e(12);
117*433d6423SLionel Sambuc if (st.st_nlink != 1) e(13);
118*433d6423SLionel Sambuc if (st.st_ctime != time1) e(14); /* All time fields should be updated */
119*433d6423SLionel Sambuc if (st.st_atime != time1) e(15);
120*433d6423SLionel Sambuc if (st.st_mtime != time1) e(16);
121*433d6423SLionel Sambuc if (st.st_size != 0) e(17); /* File should be trunked. */
122*433d6423SLionel Sambuc
123*433d6423SLionel Sambuc /* Check if c and mtime for "." is updated. */
124*433d6423SLionel Sambuc Stat(".", &st);
125*433d6423SLionel Sambuc if (st.st_ctime <= dirst.st_ctime) e(18);
126*433d6423SLionel Sambuc if (st.st_mtime <= dirst.st_mtime) e(19);
127*433d6423SLionel Sambuc
128*433d6423SLionel Sambuc /* Let's see if cread fds are write only. */
129*433d6423SLionel Sambuc if (read(fd1, buf, 7) != -1) e(20); /* we can't read */
130*433d6423SLionel Sambuc if (errno != EBADF) e(21); /* a write only fd */
131*433d6423SLionel Sambuc if (write(fd1, "HELLO", 6) != 6) e(22); /* but we can write */
132*433d6423SLionel Sambuc
133*433d6423SLionel Sambuc /* No O_APPEND flag should have been used. */
134*433d6423SLionel Sambuc if (lseek(fd1, (off_t) 1, SEEK_SET) != 1) e(23);
135*433d6423SLionel Sambuc if (write(fd1, "ello", 5) != 5) e(24);
136*433d6423SLionel Sambuc Stat("myfile", &st);
137*433d6423SLionel Sambuc if (st.st_size != 6) e(25);
138*433d6423SLionel Sambuc if (st.st_size == 11) e(26); /* O_APPEND should make it 11. */
139*433d6423SLionel Sambuc if (close(fd1) != 0) e(27);
140*433d6423SLionel Sambuc
141*433d6423SLionel Sambuc /* A creat should set the file offset to 0. */
142*433d6423SLionel Sambuc if ((fd1 = creat("myfile", 0644)) != 3) e(28);
143*433d6423SLionel Sambuc if (lseek(fd1, (off_t) 0, SEEK_CUR) != 0) e(29);
144*433d6423SLionel Sambuc if (close(fd1) != 0) e(30);
145*433d6423SLionel Sambuc
146*433d6423SLionel Sambuc /* Test if file permission bits and the file ownership are unchanged. */
147*433d6423SLionel Sambuc /* So we will see if creat() is just an open() if the file exists. */
148*433d6423SLionel Sambuc if (superuser) {
149*433d6423SLionel Sambuc System("echo > bar; chmod 073 bar"); /* Make bar 073 */
150*433d6423SLionel Sambuc if (chown("bar", 1, 1) != 0) e(1137);
151*433d6423SLionel Sambuc fd1 = creat("bar", 0777); /* knock knock */
152*433d6423SLionel Sambuc if (fd1 == -1) e(31);
153*433d6423SLionel Sambuc Stat("bar", &st);
154*433d6423SLionel Sambuc if (st.st_size != (size_t) 0) e(32); /* empty file. */
155*433d6423SLionel Sambuc if (write(fd1, "foo", 3) != 3) e(33); /* rewrite bar */
156*433d6423SLionel Sambuc if (close(fd1) != 0) e(34);
157*433d6423SLionel Sambuc Stat("bar", &st);
158*433d6423SLionel Sambuc if (st.st_uid != 1) e(35);
159*433d6423SLionel Sambuc if (st.st_gid != 1) e(36);
160*433d6423SLionel Sambuc if ((st.st_mode & 0777) != 073) e(37); /* mode still is 077 */
161*433d6423SLionel Sambuc if (st.st_size != (size_t) 3) e(38);
162*433d6423SLionel Sambuc }
163*433d6423SLionel Sambuc
164*433d6423SLionel Sambuc /* Fifo's should be openable with creat(). */
165*433d6423SLionel Sambuc if (mkfifo("fifo", 0644) != 0) e(39);
166*433d6423SLionel Sambuc
167*433d6423SLionel Sambuc /* Creat() should have no effect on FIFO files. */
168*433d6423SLionel Sambuc switch (fork()) {
169*433d6423SLionel Sambuc case -1: printf("Can't fork\n"); break;
170*433d6423SLionel Sambuc case 0:
171*433d6423SLionel Sambuc alarm(20); /* Give child 20 seconds to live. */
172*433d6423SLionel Sambuc if ((fd1 = open("fifo", O_WRONLY)) != 3) e(40);
173*433d6423SLionel Sambuc if (write(fd1, "I did see Elvis.\n", 18) != 18) e(41);
174*433d6423SLionel Sambuc if ((fd2 = creat("fifo", 0644)) != 4) e(42);
175*433d6423SLionel Sambuc if (write(fd2, "I DID.\n", 8) != 8) e(43);
176*433d6423SLionel Sambuc if (close(fd2) != 0) e(44);
177*433d6423SLionel Sambuc if (close(fd1) != 0) e(45);
178*433d6423SLionel Sambuc exit(0);
179*433d6423SLionel Sambuc default:
180*433d6423SLionel Sambuc if ((fd1 = open("fifo", O_RDONLY)) != 3) e(46);
181*433d6423SLionel Sambuc if (read(fd1, buf, 18) != 18) e(47);
182*433d6423SLionel Sambuc if (strcmp(buf, "I did see Elvis.\n") != 0) e(48);
183*433d6423SLionel Sambuc if (strcmp(buf, "I DID.\n") == 0) e(49);
184*433d6423SLionel Sambuc if (read(fd1, buf, BUF_SIZE) != 8) e(50);
185*433d6423SLionel Sambuc if (strcmp(buf, "I DID.\n") != 0) e(51);
186*433d6423SLionel Sambuc if (close(fd1) != 0) e(52);
187*433d6423SLionel Sambuc if (wait(&stat_loc) == -1) e(53);
188*433d6423SLionel Sambuc if (stat_loc != 0) e(54); /* The alarm went off? */
189*433d6423SLionel Sambuc }
190*433d6423SLionel Sambuc
191*433d6423SLionel Sambuc /* Creat() should have no effect on directroys. */
192*433d6423SLionel Sambuc System("mkdir dir; touch dir/f1 dir/f2 dir/f3");
193*433d6423SLionel Sambuc if ((fd1 = creat("dir", 0644)) != -1) e(55);
194*433d6423SLionel Sambuc if (errno != EISDIR) e(56);
195*433d6423SLionel Sambuc close(fd1);
196*433d6423SLionel Sambuc
197*433d6423SLionel Sambuc /* The path should contain only dirs in the prefix. */
198*433d6423SLionel Sambuc if ((fd1 = creat("dir/f1/nono", 0644)) != -1) e(57);
199*433d6423SLionel Sambuc if (errno != ENOTDIR) e(58);
200*433d6423SLionel Sambuc close(fd1);
201*433d6423SLionel Sambuc
202*433d6423SLionel Sambuc /* The path should contain only dirs in the prefix. */
203*433d6423SLionel Sambuc if ((fd1 = creat("", 0644)) != -1) e(59);
204*433d6423SLionel Sambuc if (errno != ENOENT) e(60);
205*433d6423SLionel Sambuc close(fd1);
206*433d6423SLionel Sambuc if ((fd1 = creat("dir/noso/nono", 0644)) != -1) e(61);
207*433d6423SLionel Sambuc if (errno != ENOENT) e(62);
208*433d6423SLionel Sambuc close(fd1);
209*433d6423SLionel Sambuc
210*433d6423SLionel Sambuc }
211*433d6423SLionel Sambuc
test30b()212*433d6423SLionel Sambuc void test30b()
213*433d6423SLionel Sambuc {
214*433d6423SLionel Sambuc int fd;
215*433d6423SLionel Sambuc
216*433d6423SLionel Sambuc subtest = 2;
217*433d6423SLionel Sambuc
218*433d6423SLionel Sambuc System("rm -rf ../DIR_30/*");
219*433d6423SLionel Sambuc
220*433d6423SLionel Sambuc /* Test maximal file name length. */
221*433d6423SLionel Sambuc if ((fd = creat(MaxName, 0777)) != 3) e(1);
222*433d6423SLionel Sambuc if (close(fd) != 0) e(2);
223*433d6423SLionel Sambuc MaxPath[strlen(MaxPath) - 2] = '/';
224*433d6423SLionel Sambuc MaxPath[strlen(MaxPath) - 1] = 'a'; /* make ././.../a */
225*433d6423SLionel Sambuc if ((fd = creat(MaxPath, 0777)) != 3) e(3);
226*433d6423SLionel Sambuc if (close(fd) != 0) e(4);
227*433d6423SLionel Sambuc MaxPath[strlen(MaxPath) - 1] = '/'; /* make ././.../a */
228*433d6423SLionel Sambuc }
229*433d6423SLionel Sambuc
test30c()230*433d6423SLionel Sambuc void test30c()
231*433d6423SLionel Sambuc {
232*433d6423SLionel Sambuc int fd, does_truncate;
233*433d6423SLionel Sambuc
234*433d6423SLionel Sambuc subtest = 3;
235*433d6423SLionel Sambuc
236*433d6423SLionel Sambuc System("rm -rf ../DIR_30/*");
237*433d6423SLionel Sambuc
238*433d6423SLionel Sambuc if (!superuser) {
239*433d6423SLionel Sambuc /* Test if creat is not usable to open files with the wrong mode */
240*433d6423SLionel Sambuc System("> nono; chmod 177 nono");
241*433d6423SLionel Sambuc fd = creat("nono", 0777);
242*433d6423SLionel Sambuc if (fd != -1) e(1);
243*433d6423SLionel Sambuc if (errno != EACCES) e(2);
244*433d6423SLionel Sambuc }
245*433d6423SLionel Sambuc if (mkdir("bar", 0777) != 0) e(3); /* make bar */
246*433d6423SLionel Sambuc
247*433d6423SLionel Sambuc /* Check if no access on part of path generates the correct error. */
248*433d6423SLionel Sambuc System("chmod 577 bar"); /* r-xrwxrwx */
249*433d6423SLionel Sambuc if (!superuser) {
250*433d6423SLionel Sambuc /* Normal users can't creat without write permision. */
251*433d6423SLionel Sambuc if (creat("bar/nono", 0666) != -1) e(4);
252*433d6423SLionel Sambuc if (errno != EACCES) e(5);
253*433d6423SLionel Sambuc if (creat("bar/../nono", 0666) != -1) e(6);
254*433d6423SLionel Sambuc if (errno != EACCES) e(7);
255*433d6423SLionel Sambuc }
256*433d6423SLionel Sambuc if (superuser) {
257*433d6423SLionel Sambuc /* Super user can still creat stuff. */
258*433d6423SLionel Sambuc if ((fd = creat("bar/nono", 0666)) != 3) e(8);
259*433d6423SLionel Sambuc if (close(fd) != 0) e(9);
260*433d6423SLionel Sambuc if (unlink("bar/nono") != 0) e(10);
261*433d6423SLionel Sambuc }
262*433d6423SLionel Sambuc
263*433d6423SLionel Sambuc /* Clean up bar. */
264*433d6423SLionel Sambuc System("rm -rf bar");
265*433d6423SLionel Sambuc
266*433d6423SLionel Sambuc /* Test ToLongName and ToLongPath */
267*433d6423SLionel Sambuc does_truncate = does_fs_truncate();
268*433d6423SLionel Sambuc fd = creat(ToLongName, 0777);
269*433d6423SLionel Sambuc if (does_truncate) {
270*433d6423SLionel Sambuc if (fd == -1) e(11);
271*433d6423SLionel Sambuc if (close(fd) != 0) e(12);
272*433d6423SLionel Sambuc } else {
273*433d6423SLionel Sambuc if (fd != -1) e(13);
274*433d6423SLionel Sambuc if (errno != ENAMETOOLONG) e(14);
275*433d6423SLionel Sambuc (void) close(fd); /* Just in case. */
276*433d6423SLionel Sambuc }
277*433d6423SLionel Sambuc
278*433d6423SLionel Sambuc ToLongPath[PATH_MAX - 2] = '/';
279*433d6423SLionel Sambuc ToLongPath[PATH_MAX - 1] = 'a';
280*433d6423SLionel Sambuc if ((fd = creat(ToLongPath, 0777)) != -1) e(15);
281*433d6423SLionel Sambuc if (errno != ENAMETOOLONG) e(16);
282*433d6423SLionel Sambuc if (close(fd) != -1) e(17);
283*433d6423SLionel Sambuc ToLongPath[PATH_MAX - 1] = '/';
284*433d6423SLionel Sambuc }
285*433d6423SLionel Sambuc
makelongnames()286*433d6423SLionel Sambuc void makelongnames()
287*433d6423SLionel Sambuc {
288*433d6423SLionel Sambuc register int i;
289*433d6423SLionel Sambuc int max_name_length;
290*433d6423SLionel Sambuc
291*433d6423SLionel Sambuc max_name_length = name_max("."); /* Aka NAME_MAX, but not every FS supports
292*433d6423SLionel Sambuc * the same length, hence runtime check */
293*433d6423SLionel Sambuc MaxName = malloc(max_name_length + 1);
294*433d6423SLionel Sambuc ToLongName = malloc(max_name_length + 1 + 1); /* Name of maximum +1 length */
295*433d6423SLionel Sambuc memset(MaxName, 'a', max_name_length);
296*433d6423SLionel Sambuc MaxName[max_name_length] = '\0';
297*433d6423SLionel Sambuc
298*433d6423SLionel Sambuc for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
299*433d6423SLionel Sambuc MaxPath[i++] = '.';
300*433d6423SLionel Sambuc MaxPath[i] = '/';
301*433d6423SLionel Sambuc }
302*433d6423SLionel Sambuc MaxPath[PATH_MAX - 1] = '\0';
303*433d6423SLionel Sambuc
304*433d6423SLionel Sambuc strcpy(ToLongName, MaxName); /* copy them Max to ToLong */
305*433d6423SLionel Sambuc strcpy(ToLongPath, MaxPath);
306*433d6423SLionel Sambuc
307*433d6423SLionel Sambuc ToLongName[max_name_length] = 'a';
308*433d6423SLionel Sambuc ToLongName[max_name_length+1] = '\0';/* extend ToLongName by one too many */
309*433d6423SLionel Sambuc ToLongPath[PATH_MAX - 1] = '/';
310*433d6423SLionel Sambuc ToLongPath[PATH_MAX] = '\0'; /* inc ToLongPath by one */
311*433d6423SLionel Sambuc }
312