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