1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2014 Fujitsu Ltd.
3*49cdfc7eSAndroid Build Coastguard Worker * Author: Zeng Linggang <[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 * Test Description:
19*49cdfc7eSAndroid Build Coastguard Worker * Verify that,
20*49cdfc7eSAndroid Build Coastguard Worker * 1. lchown() fails with -1 return value and sets errno to ELOOP
21*49cdfc7eSAndroid Build Coastguard Worker * if too many symbolic links were encountered in resolving path.
22*49cdfc7eSAndroid Build Coastguard Worker * 2. lchown() fails with -1 return value and sets errno to EROFS
23*49cdfc7eSAndroid Build Coastguard Worker * if the file is on a read-only file system.
24*49cdfc7eSAndroid Build Coastguard Worker */
25*49cdfc7eSAndroid Build Coastguard Worker
26*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <grp.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mount.h>
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
41*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
42*49cdfc7eSAndroid Build Coastguard Worker
43*49cdfc7eSAndroid Build Coastguard Worker /*
44*49cdfc7eSAndroid Build Coastguard Worker * Don't forget to remove USE_LEGACY_COMPAT_16_H from Makefile after
45*49cdfc7eSAndroid Build Coastguard Worker * rewriting all tests to the new API.
46*49cdfc7eSAndroid Build Coastguard Worker */
47*49cdfc7eSAndroid Build Coastguard Worker #include "compat_16.h"
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker #define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
50*49cdfc7eSAndroid Build Coastguard Worker S_IXGRP|S_IROTH|S_IXOTH)
51*49cdfc7eSAndroid Build Coastguard Worker #define TEST_EROFS "mntpoint"
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker static char test_eloop[PATH_MAX] = ".";
54*49cdfc7eSAndroid Build Coastguard Worker static const char *device;
55*49cdfc7eSAndroid Build Coastguard Worker static int mount_flag;
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker static struct test_case_t {
58*49cdfc7eSAndroid Build Coastguard Worker char *pathname;
59*49cdfc7eSAndroid Build Coastguard Worker int exp_errno;
60*49cdfc7eSAndroid Build Coastguard Worker } test_cases[] = {
61*49cdfc7eSAndroid Build Coastguard Worker {test_eloop, ELOOP},
62*49cdfc7eSAndroid Build Coastguard Worker {TEST_EROFS, EROFS},
63*49cdfc7eSAndroid Build Coastguard Worker };
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker TCID_DEFINE(lchown03);
66*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = ARRAY_SIZE(test_cases);
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
69*49cdfc7eSAndroid Build Coastguard Worker static void lchown_verify(const struct test_case_t *);
70*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
71*49cdfc7eSAndroid Build Coastguard Worker
main(int argc,char * argv[])72*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
73*49cdfc7eSAndroid Build Coastguard Worker {
74*49cdfc7eSAndroid Build Coastguard Worker int lc;
75*49cdfc7eSAndroid Build Coastguard Worker int i;
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(argc, argv, NULL, NULL);
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker setup();
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker for (lc = 0; TEST_LOOPING(lc); lc++) {
82*49cdfc7eSAndroid Build Coastguard Worker tst_count = 0;
83*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < TST_TOTAL; i++)
84*49cdfc7eSAndroid Build Coastguard Worker lchown_verify(&test_cases[i]);
85*49cdfc7eSAndroid Build Coastguard Worker }
86*49cdfc7eSAndroid Build Coastguard Worker
87*49cdfc7eSAndroid Build Coastguard Worker cleanup();
88*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
89*49cdfc7eSAndroid Build Coastguard Worker }
90*49cdfc7eSAndroid Build Coastguard Worker
setup(void)91*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
92*49cdfc7eSAndroid Build Coastguard Worker {
93*49cdfc7eSAndroid Build Coastguard Worker int i;
94*49cdfc7eSAndroid Build Coastguard Worker const char *fs_type;
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker tst_require_root();
97*49cdfc7eSAndroid Build Coastguard Worker
98*49cdfc7eSAndroid Build Coastguard Worker tst_sig(NOFORK, DEF_HANDLER, cleanup);
99*49cdfc7eSAndroid Build Coastguard Worker
100*49cdfc7eSAndroid Build Coastguard Worker TEST_PAUSE;
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Worker tst_tmpdir();
103*49cdfc7eSAndroid Build Coastguard Worker
104*49cdfc7eSAndroid Build Coastguard Worker fs_type = tst_dev_fs_type();
105*49cdfc7eSAndroid Build Coastguard Worker device = tst_acquire_device(cleanup);
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker if (!device)
108*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TCONF, cleanup, "Failed to acquire device");
109*49cdfc7eSAndroid Build Coastguard Worker
110*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(cleanup, "test_eloop", DIR_MODE);
111*49cdfc7eSAndroid Build Coastguard Worker SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop");
112*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 43; i++)
113*49cdfc7eSAndroid Build Coastguard Worker strcat(test_eloop, "/test_eloop");
114*49cdfc7eSAndroid Build Coastguard Worker
115*49cdfc7eSAndroid Build Coastguard Worker tst_mkfs(cleanup, device, fs_type, NULL, NULL);
116*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(cleanup, TEST_EROFS, DIR_MODE);
117*49cdfc7eSAndroid Build Coastguard Worker SAFE_MOUNT(cleanup, device, TEST_EROFS, fs_type, MS_RDONLY, NULL);
118*49cdfc7eSAndroid Build Coastguard Worker mount_flag = 1;
119*49cdfc7eSAndroid Build Coastguard Worker }
120*49cdfc7eSAndroid Build Coastguard Worker
lchown_verify(const struct test_case_t * test)121*49cdfc7eSAndroid Build Coastguard Worker static void lchown_verify(const struct test_case_t *test)
122*49cdfc7eSAndroid Build Coastguard Worker {
123*49cdfc7eSAndroid Build Coastguard Worker UID16_CHECK(geteuid(), "lchown", cleanup)
124*49cdfc7eSAndroid Build Coastguard Worker GID16_CHECK(getegid(), "lchown", cleanup)
125*49cdfc7eSAndroid Build Coastguard Worker
126*49cdfc7eSAndroid Build Coastguard Worker TEST(LCHOWN(cleanup, test->pathname, geteuid(), getegid()));
127*49cdfc7eSAndroid Build Coastguard Worker
128*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN != -1) {
129*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "lchown() returned %ld, expected -1, errno=%d",
130*49cdfc7eSAndroid Build Coastguard Worker TEST_RETURN, test->exp_errno);
131*49cdfc7eSAndroid Build Coastguard Worker return;
132*49cdfc7eSAndroid Build Coastguard Worker }
133*49cdfc7eSAndroid Build Coastguard Worker
134*49cdfc7eSAndroid Build Coastguard Worker if (TEST_ERRNO == test->exp_errno) {
135*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS | TTERRNO, "lchown() failed as expected");
136*49cdfc7eSAndroid Build Coastguard Worker } else {
137*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL | TTERRNO,
138*49cdfc7eSAndroid Build Coastguard Worker "lchown() failed unexpectedly; expected: %d - %s",
139*49cdfc7eSAndroid Build Coastguard Worker test->exp_errno,
140*49cdfc7eSAndroid Build Coastguard Worker strerror(test->exp_errno));
141*49cdfc7eSAndroid Build Coastguard Worker }
142*49cdfc7eSAndroid Build Coastguard Worker }
143*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)144*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
145*49cdfc7eSAndroid Build Coastguard Worker {
146*49cdfc7eSAndroid Build Coastguard Worker if (mount_flag && tst_umount(TEST_EROFS) < 0)
147*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TWARN | TERRNO, "umount device:%s failed", device);
148*49cdfc7eSAndroid Build Coastguard Worker
149*49cdfc7eSAndroid Build Coastguard Worker if (device)
150*49cdfc7eSAndroid Build Coastguard Worker tst_release_device(device);
151*49cdfc7eSAndroid Build Coastguard Worker
152*49cdfc7eSAndroid Build Coastguard Worker tst_rmdir();
153*49cdfc7eSAndroid Build Coastguard Worker }
154