1 /*
2 * Copyright (c) 2014 Fujitsu Ltd.
3 * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17 /*
18 * Test Description:
19 * Verify that,
20 * 1. lchown() fails with -1 return value and sets errno to ELOOP
21 * if too many symbolic links were encountered in resolving path.
22 * 2. lchown() fails with -1 return value and sets errno to EROFS
23 * if the file is on a read-only file system.
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <signal.h>
33 #include <grp.h>
34 #include <pwd.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/mman.h>
38 #include <sys/mount.h>
39
40 #include "test.h"
41 #include "safe_macros.h"
42 #include "compat_16.h"
43
44 #define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
45 S_IXGRP|S_IROTH|S_IXOTH)
46 #define TEST_EROFS "mntpoint"
47
48 static char test_eloop[PATH_MAX] = ".";
49 static const char *device;
50 static int mount_flag;
51
52 static struct test_case_t {
53 char *pathname;
54 int exp_errno;
55 } test_cases[] = {
56 {test_eloop, ELOOP},
57 {TEST_EROFS, EROFS},
58 };
59
60 TCID_DEFINE(lchown03);
61 int TST_TOTAL = ARRAY_SIZE(test_cases);
62
63 static void setup(void);
64 static void lchown_verify(const struct test_case_t *);
65 static void cleanup(void);
66
main(int argc,char * argv[])67 int main(int argc, char *argv[])
68 {
69 int lc;
70 int i;
71
72 tst_parse_opts(argc, argv, NULL, NULL);
73
74 setup();
75
76 for (lc = 0; TEST_LOOPING(lc); lc++) {
77 tst_count = 0;
78 for (i = 0; i < TST_TOTAL; i++)
79 lchown_verify(&test_cases[i]);
80 }
81
82 cleanup();
83 tst_exit();
84 }
85
setup(void)86 static void setup(void)
87 {
88 int i;
89 const char *fs_type;
90
91 tst_require_root();
92
93 tst_sig(NOFORK, DEF_HANDLER, cleanup);
94
95 TEST_PAUSE;
96
97 tst_tmpdir();
98
99 fs_type = tst_dev_fs_type();
100 device = tst_acquire_device(cleanup);
101
102 if (!device)
103 tst_brkm(TCONF, cleanup, "Failed to acquire device");
104
105 SAFE_MKDIR(cleanup, "test_eloop", DIR_MODE);
106 SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop");
107 for (i = 0; i < 43; i++)
108 strcat(test_eloop, "/test_eloop");
109
110 tst_mkfs(cleanup, device, fs_type, NULL, NULL);
111 SAFE_MKDIR(cleanup, TEST_EROFS, DIR_MODE);
112 SAFE_MOUNT(cleanup, device, TEST_EROFS, fs_type, MS_RDONLY, NULL);
113 mount_flag = 1;
114 }
115
lchown_verify(const struct test_case_t * test)116 static void lchown_verify(const struct test_case_t *test)
117 {
118 UID16_CHECK(geteuid(), "lchown", cleanup)
119 GID16_CHECK(getegid(), "lchown", cleanup)
120
121 TEST(LCHOWN(cleanup, test->pathname, geteuid(), getegid()));
122
123 if (TEST_RETURN != -1) {
124 tst_resm(TFAIL, "lchown() returned %ld, expected -1, errno=%d",
125 TEST_RETURN, test->exp_errno);
126 return;
127 }
128
129 if (TEST_ERRNO == test->exp_errno) {
130 tst_resm(TPASS | TTERRNO, "lchown() failed as expected");
131 } else {
132 tst_resm(TFAIL | TTERRNO,
133 "lchown() failed unexpectedly; expected: %d - %s",
134 test->exp_errno,
135 strerror(test->exp_errno));
136 }
137 }
138
cleanup(void)139 static void cleanup(void)
140 {
141 if (mount_flag && tst_umount(TEST_EROFS) < 0)
142 tst_resm(TWARN | TERRNO, "umount device:%s failed", device);
143
144 if (device)
145 tst_release_device(device);
146
147 tst_rmdir();
148 }
149