1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2014 Fujitsu Ltd.
3*49cdfc7eSAndroid Build Coastguard Worker * Author: Xiaoguang Wang <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify it
6*49cdfc7eSAndroid Build Coastguard Worker * under the terms of version 2 of the GNU General Public License as
7*49cdfc7eSAndroid Build Coastguard Worker * published by the Free Software Foundation.
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it would be useful, but
10*49cdfc7eSAndroid Build Coastguard Worker * WITHOUT ANY WARRANTY; without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License along
14*49cdfc7eSAndroid Build Coastguard Worker * with this program; if not, write the Free Software Foundation, Inc.,
15*49cdfc7eSAndroid Build Coastguard Worker * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16*49cdfc7eSAndroid Build Coastguard Worker */
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Worker /*
19*49cdfc7eSAndroid Build Coastguard Worker * Description:
20*49cdfc7eSAndroid Build Coastguard Worker * Verify that,
21*49cdfc7eSAndroid Build Coastguard Worker * 1) mknod(2) returns -1 and sets errno to EROFS if pathname refers to
22*49cdfc7eSAndroid Build Coastguard Worker * a file on a read-only file system.
23*49cdfc7eSAndroid Build Coastguard Worker * 2) mknod(2) returns -1 and sets errno to ELOOP if Too many symbolic
24*49cdfc7eSAndroid Build Coastguard Worker * links were encountered in resolving pathname.
25*49cdfc7eSAndroid Build Coastguard Worker */
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
28*49cdfc7eSAndroid Build Coastguard Worker
29*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mount.h>
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
38*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
39*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
40*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
43*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
44*49cdfc7eSAndroid Build Coastguard Worker
45*49cdfc7eSAndroid Build Coastguard Worker #define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
46*49cdfc7eSAndroid Build Coastguard Worker S_IXGRP|S_IROTH|S_IXOTH)
47*49cdfc7eSAndroid Build Coastguard Worker #define MNT_POINT "mntpoint"
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker #define FIFOMODE (S_IFIFO | S_IRUSR | S_IRGRP | S_IROTH)
50*49cdfc7eSAndroid Build Coastguard Worker #define FREGMODE (S_IFREG | S_IRUSR | S_IRGRP | S_IROTH)
51*49cdfc7eSAndroid Build Coastguard Worker #define SOCKMODE (S_IFSOCK | S_IRUSR | S_IRGRP | S_IROTH)
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker static const char *device;
54*49cdfc7eSAndroid Build Coastguard Worker static int mount_flag;
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker static int dir_fd;
57*49cdfc7eSAndroid Build Coastguard Worker static int curfd = AT_FDCWD;
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker #define ELOPFILE "/test_eloop"
60*49cdfc7eSAndroid Build Coastguard Worker static char elooppathname[sizeof(ELOPFILE) * 43] = ".";
61*49cdfc7eSAndroid Build Coastguard Worker
62*49cdfc7eSAndroid Build Coastguard Worker static struct test_case_t {
63*49cdfc7eSAndroid Build Coastguard Worker int *dir_fd;
64*49cdfc7eSAndroid Build Coastguard Worker char *pathname;
65*49cdfc7eSAndroid Build Coastguard Worker mode_t mode;
66*49cdfc7eSAndroid Build Coastguard Worker int exp_errno;
67*49cdfc7eSAndroid Build Coastguard Worker } test_cases[] = {
68*49cdfc7eSAndroid Build Coastguard Worker { &curfd, "tnode1", FIFOMODE, 0 },
69*49cdfc7eSAndroid Build Coastguard Worker { &curfd, "tnode2", FREGMODE, 0 },
70*49cdfc7eSAndroid Build Coastguard Worker { &curfd, "tnode3", SOCKMODE, 0 },
71*49cdfc7eSAndroid Build Coastguard Worker { &dir_fd, "tnode4", FIFOMODE, EROFS },
72*49cdfc7eSAndroid Build Coastguard Worker { &dir_fd, "tnode5", FREGMODE, EROFS },
73*49cdfc7eSAndroid Build Coastguard Worker { &dir_fd, "tnode6", SOCKMODE, EROFS },
74*49cdfc7eSAndroid Build Coastguard Worker { &curfd, elooppathname, FIFOMODE, ELOOP },
75*49cdfc7eSAndroid Build Coastguard Worker { &curfd, elooppathname, FREGMODE, ELOOP },
76*49cdfc7eSAndroid Build Coastguard Worker { &curfd, elooppathname, SOCKMODE, ELOOP },
77*49cdfc7eSAndroid Build Coastguard Worker };
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker static void mknodat_verify(struct test_case_t *tc);
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "mknodat";
82*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = ARRAY_SIZE(test_cases);
83*49cdfc7eSAndroid Build Coastguard Worker
main(int ac,char ** av)84*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
85*49cdfc7eSAndroid Build Coastguard Worker {
86*49cdfc7eSAndroid Build Coastguard Worker int lc, i;
87*49cdfc7eSAndroid Build Coastguard Worker
88*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(ac, av, NULL, NULL);
89*49cdfc7eSAndroid Build Coastguard Worker
90*49cdfc7eSAndroid Build Coastguard Worker setup();
91*49cdfc7eSAndroid Build Coastguard Worker
92*49cdfc7eSAndroid Build Coastguard Worker for (lc = 0; TEST_LOOPING(lc); lc++) {
93*49cdfc7eSAndroid Build Coastguard Worker tst_count = 0;
94*49cdfc7eSAndroid Build Coastguard Worker
95*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < TST_TOTAL; i++)
96*49cdfc7eSAndroid Build Coastguard Worker mknodat_verify(&test_cases[i]);
97*49cdfc7eSAndroid Build Coastguard Worker }
98*49cdfc7eSAndroid Build Coastguard Worker
99*49cdfc7eSAndroid Build Coastguard Worker cleanup();
100*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
101*49cdfc7eSAndroid Build Coastguard Worker }
102*49cdfc7eSAndroid Build Coastguard Worker
setup(void)103*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
104*49cdfc7eSAndroid Build Coastguard Worker {
105*49cdfc7eSAndroid Build Coastguard Worker int i;
106*49cdfc7eSAndroid Build Coastguard Worker const char *fs_type;
107*49cdfc7eSAndroid Build Coastguard Worker
108*49cdfc7eSAndroid Build Coastguard Worker tst_require_root();
109*49cdfc7eSAndroid Build Coastguard Worker
110*49cdfc7eSAndroid Build Coastguard Worker tst_sig(NOFORK, DEF_HANDLER, cleanup);
111*49cdfc7eSAndroid Build Coastguard Worker
112*49cdfc7eSAndroid Build Coastguard Worker tst_tmpdir();
113*49cdfc7eSAndroid Build Coastguard Worker
114*49cdfc7eSAndroid Build Coastguard Worker fs_type = tst_dev_fs_type();
115*49cdfc7eSAndroid Build Coastguard Worker device = tst_acquire_device(cleanup);
116*49cdfc7eSAndroid Build Coastguard Worker
117*49cdfc7eSAndroid Build Coastguard Worker if (!device)
118*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TCONF, cleanup, "Failed to acquire device");
119*49cdfc7eSAndroid Build Coastguard Worker
120*49cdfc7eSAndroid Build Coastguard Worker tst_mkfs(cleanup, device, fs_type, NULL, NULL);
121*49cdfc7eSAndroid Build Coastguard Worker
122*49cdfc7eSAndroid Build Coastguard Worker TEST_PAUSE;
123*49cdfc7eSAndroid Build Coastguard Worker
124*49cdfc7eSAndroid Build Coastguard Worker /*
125*49cdfc7eSAndroid Build Coastguard Worker * mount a read-only file system for EROFS test
126*49cdfc7eSAndroid Build Coastguard Worker */
127*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(cleanup, MNT_POINT, DIR_MODE);
128*49cdfc7eSAndroid Build Coastguard Worker SAFE_MOUNT(cleanup, device, MNT_POINT, fs_type, MS_RDONLY, NULL);
129*49cdfc7eSAndroid Build Coastguard Worker mount_flag = 1;
130*49cdfc7eSAndroid Build Coastguard Worker dir_fd = SAFE_OPEN(cleanup, MNT_POINT, O_DIRECTORY);
131*49cdfc7eSAndroid Build Coastguard Worker
132*49cdfc7eSAndroid Build Coastguard Worker /*
133*49cdfc7eSAndroid Build Coastguard Worker * NOTE: the ELOOP test is written based on that the consecutive
134*49cdfc7eSAndroid Build Coastguard Worker * symlinks limits in kernel is hardwired to 40.
135*49cdfc7eSAndroid Build Coastguard Worker */
136*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(cleanup, "test_eloop", DIR_MODE);
137*49cdfc7eSAndroid Build Coastguard Worker SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop");
138*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 43; i++)
139*49cdfc7eSAndroid Build Coastguard Worker strcat(elooppathname, ELOPFILE);
140*49cdfc7eSAndroid Build Coastguard Worker }
141*49cdfc7eSAndroid Build Coastguard Worker
mknodat_verify(struct test_case_t * tc)142*49cdfc7eSAndroid Build Coastguard Worker static void mknodat_verify(struct test_case_t *tc)
143*49cdfc7eSAndroid Build Coastguard Worker {
144*49cdfc7eSAndroid Build Coastguard Worker int fd = *(tc->dir_fd);
145*49cdfc7eSAndroid Build Coastguard Worker char *pathname = tc->pathname;
146*49cdfc7eSAndroid Build Coastguard Worker mode_t mode = tc->mode;
147*49cdfc7eSAndroid Build Coastguard Worker
148*49cdfc7eSAndroid Build Coastguard Worker TEST(mknodat(fd, pathname, mode, 0));
149*49cdfc7eSAndroid Build Coastguard Worker
150*49cdfc7eSAndroid Build Coastguard Worker if (TEST_ERRNO == tc->exp_errno) {
151*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS | TTERRNO,
152*49cdfc7eSAndroid Build Coastguard Worker "mknodat() returned the expected value");
153*49cdfc7eSAndroid Build Coastguard Worker } else {
154*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL | TTERRNO,
155*49cdfc7eSAndroid Build Coastguard Worker "mknodat() got unexpected return value; expected: "
156*49cdfc7eSAndroid Build Coastguard Worker "%d - %s", tc->exp_errno,
157*49cdfc7eSAndroid Build Coastguard Worker strerror(tc->exp_errno));
158*49cdfc7eSAndroid Build Coastguard Worker }
159*49cdfc7eSAndroid Build Coastguard Worker
160*49cdfc7eSAndroid Build Coastguard Worker if (TEST_ERRNO == 0 &&
161*49cdfc7eSAndroid Build Coastguard Worker tst_syscall(__NR_unlinkat, fd, pathname, 0) < 0) {
162*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "unlinkat(%d, %s) "
163*49cdfc7eSAndroid Build Coastguard Worker "failed.", fd, pathname);
164*49cdfc7eSAndroid Build Coastguard Worker }
165*49cdfc7eSAndroid Build Coastguard Worker }
166*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)167*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
168*49cdfc7eSAndroid Build Coastguard Worker {
169*49cdfc7eSAndroid Build Coastguard Worker if (dir_fd > 0 && close(dir_fd) < 0)
170*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TWARN | TERRNO, "close(%d) failed", dir_fd);
171*49cdfc7eSAndroid Build Coastguard Worker if (mount_flag && tst_umount(MNT_POINT) < 0)
172*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TWARN | TERRNO, "umount device:%s failed", device);
173*49cdfc7eSAndroid Build Coastguard Worker
174*49cdfc7eSAndroid Build Coastguard Worker if (device)
175*49cdfc7eSAndroid Build Coastguard Worker tst_release_device(device);
176*49cdfc7eSAndroid Build Coastguard Worker
177*49cdfc7eSAndroid Build Coastguard Worker tst_rmdir();
178*49cdfc7eSAndroid Build Coastguard Worker }
179