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