1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /* Copyright (c) International Business Machines Corp., 2001
3*49cdfc7eSAndroid Build Coastguard Worker * Ported to LTP: Wayne Boyer
4*49cdfc7eSAndroid Build Coastguard Worker */
5*49cdfc7eSAndroid Build Coastguard Worker /*
6*49cdfc7eSAndroid Build Coastguard Worker * Description:
7*49cdfc7eSAndroid Build Coastguard Worker * 1) attempt to rmdir() non-empty directory -> ENOTEMPTY
8*49cdfc7eSAndroid Build Coastguard Worker * 2) attempt to rmdir() directory with long path name -> ENAMETOOLONG
9*49cdfc7eSAndroid Build Coastguard Worker * 3) attempt to rmdir() non-existing directory -> ENOENT
10*49cdfc7eSAndroid Build Coastguard Worker * 4) attempt to rmdir() a file -> ENOTDIR
11*49cdfc7eSAndroid Build Coastguard Worker * 5) attempt to rmdir() invalid pointer -> EFAULT
12*49cdfc7eSAndroid Build Coastguard Worker * 6) attempt to rmdir() symlink loop -> ELOOP
13*49cdfc7eSAndroid Build Coastguard Worker * 7) attempt to rmdir() dir on RO mounted FS -> EROFS
14*49cdfc7eSAndroid Build Coastguard Worker * 8) attempt to rmdir() mount point -> EBUSY
15*49cdfc7eSAndroid Build Coastguard Worker * 9) attempt to rmdir() current directory "." -> EINVAL
16*49cdfc7eSAndroid Build Coastguard Worker */
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker
22*49cdfc7eSAndroid Build Coastguard Worker #define DIR_MODE (S_IRWXU | S_IRWXG | S_IRWXO)
23*49cdfc7eSAndroid Build Coastguard Worker #define FILE_MODE (S_IRWXU | S_IRWXG | S_IRWXO)
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker #define TESTDIR "testdir"
26*49cdfc7eSAndroid Build Coastguard Worker #define TESTDIR2 "nosuchdir/testdir2"
27*49cdfc7eSAndroid Build Coastguard Worker #define TESTDIR3 "testfile2/testdir3"
28*49cdfc7eSAndroid Build Coastguard Worker #define TESTDIR4 "/loopdir"
29*49cdfc7eSAndroid Build Coastguard Worker #define MNT_POINT "mntpoint"
30*49cdfc7eSAndroid Build Coastguard Worker #define TESTDIR5 "mntpoint/dir"
31*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE "testdir/testfile"
32*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE2 "testfile2"
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker static char longpathname[PATH_MAX + 2];
35*49cdfc7eSAndroid Build Coastguard Worker static char looppathname[sizeof(TESTDIR4) * 43] = ".";
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker static struct testcase {
38*49cdfc7eSAndroid Build Coastguard Worker char *dir;
39*49cdfc7eSAndroid Build Coastguard Worker int exp_errno;
40*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
41*49cdfc7eSAndroid Build Coastguard Worker {TESTDIR, ENOTEMPTY},
42*49cdfc7eSAndroid Build Coastguard Worker {longpathname, ENAMETOOLONG},
43*49cdfc7eSAndroid Build Coastguard Worker {TESTDIR2, ENOENT},
44*49cdfc7eSAndroid Build Coastguard Worker {TESTDIR3, ENOTDIR},
45*49cdfc7eSAndroid Build Coastguard Worker {NULL, EFAULT},
46*49cdfc7eSAndroid Build Coastguard Worker {looppathname, ELOOP},
47*49cdfc7eSAndroid Build Coastguard Worker {TESTDIR5, EROFS},
48*49cdfc7eSAndroid Build Coastguard Worker {MNT_POINT, EBUSY},
49*49cdfc7eSAndroid Build Coastguard Worker {".", EINVAL}
50*49cdfc7eSAndroid Build Coastguard Worker };
51*49cdfc7eSAndroid Build Coastguard Worker
setup(void)52*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
53*49cdfc7eSAndroid Build Coastguard Worker {
54*49cdfc7eSAndroid Build Coastguard Worker unsigned int i;
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(TESTDIR, DIR_MODE);
57*49cdfc7eSAndroid Build Coastguard Worker SAFE_TOUCH(TESTFILE, FILE_MODE, NULL);
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker memset(longpathname, 'a', PATH_MAX + 1);
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker SAFE_TOUCH(TESTFILE2, FILE_MODE, NULL);
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < ARRAY_SIZE(tcases); i++) {
64*49cdfc7eSAndroid Build Coastguard Worker if (!tcases[i].dir)
65*49cdfc7eSAndroid Build Coastguard Worker tcases[i].dir = tst_get_bad_addr(NULL);
66*49cdfc7eSAndroid Build Coastguard Worker }
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker /*
69*49cdfc7eSAndroid Build Coastguard Worker * NOTE: the ELOOP test is written based on that the
70*49cdfc7eSAndroid Build Coastguard Worker * consecutive symlinks limit in kernel is hardwired
71*49cdfc7eSAndroid Build Coastguard Worker * to 40.
72*49cdfc7eSAndroid Build Coastguard Worker */
73*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR("loopdir", DIR_MODE);
74*49cdfc7eSAndroid Build Coastguard Worker SAFE_SYMLINK("../loopdir", "loopdir/loopdir");
75*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 43; i++)
76*49cdfc7eSAndroid Build Coastguard Worker strcat(looppathname, TESTDIR4);
77*49cdfc7eSAndroid Build Coastguard Worker }
78*49cdfc7eSAndroid Build Coastguard Worker
verify_rmdir(unsigned int n)79*49cdfc7eSAndroid Build Coastguard Worker static void verify_rmdir(unsigned int n)
80*49cdfc7eSAndroid Build Coastguard Worker {
81*49cdfc7eSAndroid Build Coastguard Worker struct testcase *tc = &tcases[n];
82*49cdfc7eSAndroid Build Coastguard Worker
83*49cdfc7eSAndroid Build Coastguard Worker TEST(rmdir(tc->dir));
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != -1) {
86*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "rmdir() succeeded unexpectedly (%li)",
87*49cdfc7eSAndroid Build Coastguard Worker TST_RET);
88*49cdfc7eSAndroid Build Coastguard Worker return;
89*49cdfc7eSAndroid Build Coastguard Worker }
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR == tc->exp_errno) {
92*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO, "rmdir() failed as expected");
93*49cdfc7eSAndroid Build Coastguard Worker return;
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO,
97*49cdfc7eSAndroid Build Coastguard Worker "rmdir() failed unexpectedly; expected: %d - %s",
98*49cdfc7eSAndroid Build Coastguard Worker tc->exp_errno, tst_strerrno(tc->exp_errno));
99*49cdfc7eSAndroid Build Coastguard Worker }
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
102*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
103*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
104*49cdfc7eSAndroid Build Coastguard Worker .test = verify_rmdir,
105*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
106*49cdfc7eSAndroid Build Coastguard Worker .needs_rofs = 1,
107*49cdfc7eSAndroid Build Coastguard Worker .mntpoint = MNT_POINT,
108*49cdfc7eSAndroid Build Coastguard Worker };
109