1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) Linux Test Project, 2002-2024
5*49cdfc7eSAndroid Build Coastguard Worker * Ported to LTP: Wayne Boyer
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * Check mkdir() with various error conditions that should produce
12*49cdfc7eSAndroid Build Coastguard Worker * EFAULT, ENAMETOOLONG, EEXIST, ENOENT, ENOTDIR, ELOOP and EROFS.
13*49cdfc7eSAndroid Build Coastguard Worker *
14*49cdfc7eSAndroid Build Coastguard Worker * Testing on various types of files (symlinks, directories, pipes, devices, etc).
15*49cdfc7eSAndroid Build Coastguard Worker */
16*49cdfc7eSAndroid Build Coastguard Worker
17*49cdfc7eSAndroid Build Coastguard Worker #include <paths.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mount.h>
25*49cdfc7eSAndroid Build Coastguard Worker
26*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker #define TST_EEXIST "tst_eexist"
29*49cdfc7eSAndroid Build Coastguard Worker #define TST_PIPE "tst_pipe"
30*49cdfc7eSAndroid Build Coastguard Worker #define TST_FOLDER "tst_folder"
31*49cdfc7eSAndroid Build Coastguard Worker #define TST_SYMLINK "tst_symlink"
32*49cdfc7eSAndroid Build Coastguard Worker #define TST_NULLDEV _PATH_DEVNULL
33*49cdfc7eSAndroid Build Coastguard Worker #define TST_ENOENT "tst_enoent/tst"
34*49cdfc7eSAndroid Build Coastguard Worker #define TST_ENOTDIR_FILE "tst_enotdir"
35*49cdfc7eSAndroid Build Coastguard Worker #define TST_ENOTDIR_DIR "tst_enotdir/tst"
36*49cdfc7eSAndroid Build Coastguard Worker #define MODE 0777
37*49cdfc7eSAndroid Build Coastguard Worker
38*49cdfc7eSAndroid Build Coastguard Worker #define MNT_POINT "mntpoint"
39*49cdfc7eSAndroid Build Coastguard Worker #define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
40*49cdfc7eSAndroid Build Coastguard Worker S_IXGRP|S_IROTH|S_IXOTH)
41*49cdfc7eSAndroid Build Coastguard Worker #define TST_EROFS "mntpoint/tst_erofs"
42*49cdfc7eSAndroid Build Coastguard Worker
43*49cdfc7eSAndroid Build Coastguard Worker static char long_dir[PATH_MAX + 2] = {[0 ... PATH_MAX + 1] = 'a'};
44*49cdfc7eSAndroid Build Coastguard Worker static char loop_dir[PATH_MAX] = ".";
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker struct tcase;
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
49*49cdfc7eSAndroid Build Coastguard Worker char *pathname;
50*49cdfc7eSAndroid Build Coastguard Worker int exp_errno;
51*49cdfc7eSAndroid Build Coastguard Worker } TC[] = {
52*49cdfc7eSAndroid Build Coastguard Worker {NULL, EFAULT},
53*49cdfc7eSAndroid Build Coastguard Worker {long_dir, ENAMETOOLONG},
54*49cdfc7eSAndroid Build Coastguard Worker {TST_EEXIST, EEXIST},
55*49cdfc7eSAndroid Build Coastguard Worker {TST_FOLDER, EEXIST},
56*49cdfc7eSAndroid Build Coastguard Worker {TST_PIPE, EEXIST},
57*49cdfc7eSAndroid Build Coastguard Worker {TST_SYMLINK, EEXIST},
58*49cdfc7eSAndroid Build Coastguard Worker {TST_NULLDEV, EEXIST},
59*49cdfc7eSAndroid Build Coastguard Worker {TST_ENOENT, ENOENT},
60*49cdfc7eSAndroid Build Coastguard Worker {TST_ENOTDIR_DIR, ENOTDIR},
61*49cdfc7eSAndroid Build Coastguard Worker {loop_dir, ELOOP},
62*49cdfc7eSAndroid Build Coastguard Worker {TST_EROFS, EROFS},
63*49cdfc7eSAndroid Build Coastguard Worker };
64*49cdfc7eSAndroid Build Coastguard Worker
verify_mkdir(unsigned int n)65*49cdfc7eSAndroid Build Coastguard Worker static void verify_mkdir(unsigned int n)
66*49cdfc7eSAndroid Build Coastguard Worker {
67*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = TC + n;
68*49cdfc7eSAndroid Build Coastguard Worker
69*49cdfc7eSAndroid Build Coastguard Worker TEST(mkdir(tc->pathname, MODE));
70*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != -1) {
71*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "mkdir() returned %ld, expected -1, errno=%d",
72*49cdfc7eSAndroid Build Coastguard Worker TST_RET, tc->exp_errno);
73*49cdfc7eSAndroid Build Coastguard Worker return;
74*49cdfc7eSAndroid Build Coastguard Worker }
75*49cdfc7eSAndroid Build Coastguard Worker
76*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR == tc->exp_errno) {
77*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO, "mkdir() failed as expected");
78*49cdfc7eSAndroid Build Coastguard Worker } else {
79*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO,
80*49cdfc7eSAndroid Build Coastguard Worker "mkdir() failed unexpectedly; expected: %d - %s",
81*49cdfc7eSAndroid Build Coastguard Worker tc->exp_errno, strerror(tc->exp_errno));
82*49cdfc7eSAndroid Build Coastguard Worker }
83*49cdfc7eSAndroid Build Coastguard Worker }
84*49cdfc7eSAndroid Build Coastguard Worker
setup(void)85*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
86*49cdfc7eSAndroid Build Coastguard Worker {
87*49cdfc7eSAndroid Build Coastguard Worker unsigned int i;
88*49cdfc7eSAndroid Build Coastguard Worker char *tmpdir = tst_get_tmpdir();
89*49cdfc7eSAndroid Build Coastguard Worker
90*49cdfc7eSAndroid Build Coastguard Worker SAFE_SYMLINK(tmpdir, TST_SYMLINK);
91*49cdfc7eSAndroid Build Coastguard Worker free(tmpdir);
92*49cdfc7eSAndroid Build Coastguard Worker
93*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKFIFO(TST_PIPE, 0777);
94*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(TST_FOLDER, 0777);
95*49cdfc7eSAndroid Build Coastguard Worker SAFE_TOUCH(TST_EEXIST, MODE, NULL);
96*49cdfc7eSAndroid Build Coastguard Worker SAFE_TOUCH(TST_ENOTDIR_FILE, MODE, NULL);
97*49cdfc7eSAndroid Build Coastguard Worker
98*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < ARRAY_SIZE(TC); i++) {
99*49cdfc7eSAndroid Build Coastguard Worker if (TC[i].exp_errno == EFAULT)
100*49cdfc7eSAndroid Build Coastguard Worker TC[i].pathname = tst_get_bad_addr(NULL);
101*49cdfc7eSAndroid Build Coastguard Worker }
102*49cdfc7eSAndroid Build Coastguard Worker
103*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR("test_eloop", DIR_MODE);
104*49cdfc7eSAndroid Build Coastguard Worker SAFE_SYMLINK("../test_eloop", "test_eloop/test_eloop");
105*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 43; i++)
106*49cdfc7eSAndroid Build Coastguard Worker strcat(loop_dir, "/test_eloop");
107*49cdfc7eSAndroid Build Coastguard Worker }
108*49cdfc7eSAndroid Build Coastguard Worker
109*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
110*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(TC),
111*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
112*49cdfc7eSAndroid Build Coastguard Worker .needs_rofs = 1,
113*49cdfc7eSAndroid Build Coastguard Worker .mntpoint = MNT_POINT,
114*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
115*49cdfc7eSAndroid Build Coastguard Worker .test = verify_mkdir,
116*49cdfc7eSAndroid Build Coastguard Worker };
117