xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/lchown/lchown02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2001
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software;  you can redistribute it and/or modify
5*49cdfc7eSAndroid Build Coastguard Worker  * it under the terms of the GNU General Public License as published by
6*49cdfc7eSAndroid Build Coastguard Worker  * the Free Software Foundation; either version 2 of the License, or
7*49cdfc7eSAndroid Build Coastguard Worker  * (at your option) any later version.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it will be useful,
10*49cdfc7eSAndroid Build Coastguard Worker  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12*49cdfc7eSAndroid Build Coastguard Worker  * the GNU General Public License for more details.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License
15*49cdfc7eSAndroid Build Coastguard Worker  * along with this program;  if not, write to the Free Software
16*49cdfc7eSAndroid Build Coastguard Worker  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker /*
20*49cdfc7eSAndroid Build Coastguard Worker  * Test Name: lchown02
21*49cdfc7eSAndroid Build Coastguard Worker  *
22*49cdfc7eSAndroid Build Coastguard Worker  * Test Description:
23*49cdfc7eSAndroid Build Coastguard Worker  *   Verify that,
24*49cdfc7eSAndroid Build Coastguard Worker  *   1) lchown(2) returns -1 and sets errno to EPERM if the effective user id
25*49cdfc7eSAndroid Build Coastguard Worker  *	of process does not match the owner of the file and the process is
26*49cdfc7eSAndroid Build Coastguard Worker  *	not super user.
27*49cdfc7eSAndroid Build Coastguard Worker  *   2) lchown(2) returns -1 and sets errno to EACCES if search permission is
28*49cdfc7eSAndroid Build Coastguard Worker  *	denied on a component of the path prefix.
29*49cdfc7eSAndroid Build Coastguard Worker  *   3) lchown(2) returns -1 and sets errno to EFAULT if pathname points
30*49cdfc7eSAndroid Build Coastguard Worker  *	outside user's accessible address space.
31*49cdfc7eSAndroid Build Coastguard Worker  *   4) lchown(2) returns -1 and sets errno to ENAMETOOLONG if the pathname
32*49cdfc7eSAndroid Build Coastguard Worker  *	component is too long.
33*49cdfc7eSAndroid Build Coastguard Worker  *   5) lchown(2) returns -1 and sets errno to ENOTDIR if the directory
34*49cdfc7eSAndroid Build Coastguard Worker  *	component in pathname is not a directory.
35*49cdfc7eSAndroid Build Coastguard Worker  *   6) lchown(2) returns -1 and sets errno to ENOENT if the specified file
36*49cdfc7eSAndroid Build Coastguard Worker  *	does not exists.
37*49cdfc7eSAndroid Build Coastguard Worker  *
38*49cdfc7eSAndroid Build Coastguard Worker  * Expected Result:
39*49cdfc7eSAndroid Build Coastguard Worker  *  lchown() should fail with return value -1 and set expected errno.
40*49cdfc7eSAndroid Build Coastguard Worker  *
41*49cdfc7eSAndroid Build Coastguard Worker  * HISTORY
42*49cdfc7eSAndroid Build Coastguard Worker  *	07/2001 Ported by Wayne Boyer
43*49cdfc7eSAndroid Build Coastguard Worker  *      11/2010 Rewritten by Cyril Hrubis [email protected]
44*49cdfc7eSAndroid Build Coastguard Worker  */
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
47*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
48*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
49*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
50*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
51*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
52*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
53*49cdfc7eSAndroid Build Coastguard Worker #include <grp.h>
54*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
55*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
56*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
57*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
60*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker /*
63*49cdfc7eSAndroid Build Coastguard Worker  * Don't forget to remove USE_LEGACY_COMPAT_16_H from Makefile after
64*49cdfc7eSAndroid Build Coastguard Worker  * rewriting all tests to the new API.
65*49cdfc7eSAndroid Build Coastguard Worker  */
66*49cdfc7eSAndroid Build Coastguard Worker #include "compat_16.h"
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker #define TEST_USER       "nobody"
69*49cdfc7eSAndroid Build Coastguard Worker #define MODE_RWX	S_IRWXU | S_IRWXG | S_IRWXO
70*49cdfc7eSAndroid Build Coastguard Worker #define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
71*49cdfc7eSAndroid Build Coastguard Worker #define DIR_TEMP	"testdir_1"
72*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE1	"tfile_1"
73*49cdfc7eSAndroid Build Coastguard Worker #define SFILE1		"sfile_1"
74*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE2	"testdir_1/tfile_2"
75*49cdfc7eSAndroid Build Coastguard Worker #define SFILE2		"testdir_1/sfile_2"
76*49cdfc7eSAndroid Build Coastguard Worker #define TFILE3          "t_file"
77*49cdfc7eSAndroid Build Coastguard Worker #define SFILE3		"t_file/sfile"
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker TCID_DEFINE(lchown02);
80*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 7;
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
83*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
84*49cdfc7eSAndroid Build Coastguard Worker static void setup_eperm(int pos);
85*49cdfc7eSAndroid Build Coastguard Worker static void setup_eacces(int pos);
86*49cdfc7eSAndroid Build Coastguard Worker static void setup_enotdir(int pos);
87*49cdfc7eSAndroid Build Coastguard Worker static void setup_longpath(int pos);
88*49cdfc7eSAndroid Build Coastguard Worker static void setup_efault(int pos);
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker static char path[PATH_MAX + 2];
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker struct test_case_t {
93*49cdfc7eSAndroid Build Coastguard Worker 	char *pathname;
94*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
95*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
96*49cdfc7eSAndroid Build Coastguard Worker 	void (*setup) (int pos);
97*49cdfc7eSAndroid Build Coastguard Worker };
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker static struct test_case_t test_cases[] = {
100*49cdfc7eSAndroid Build Coastguard Worker 	{SFILE1, "Process is not owner/root", EPERM, setup_eperm},
101*49cdfc7eSAndroid Build Coastguard Worker 	{SFILE2, "Search permission denied", EACCES, setup_eacces},
102*49cdfc7eSAndroid Build Coastguard Worker 	{NULL, "Unaccessible address space", EFAULT, setup_efault},
103*49cdfc7eSAndroid Build Coastguard Worker 	{path, "Pathname too long", ENAMETOOLONG, setup_longpath},
104*49cdfc7eSAndroid Build Coastguard Worker 	{SFILE3, "Path contains regular file", ENOTDIR, setup_enotdir},
105*49cdfc7eSAndroid Build Coastguard Worker 	{"", "Pathname is empty", ENOENT, NULL},
106*49cdfc7eSAndroid Build Coastguard Worker 	{NULL, NULL, 0, NULL}
107*49cdfc7eSAndroid Build Coastguard Worker };
108*49cdfc7eSAndroid Build Coastguard Worker 
109*49cdfc7eSAndroid Build Coastguard Worker static struct passwd *ltpuser;
110*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char * argv[])111*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
112*49cdfc7eSAndroid Build Coastguard Worker {
113*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
114*49cdfc7eSAndroid Build Coastguard Worker 	uid_t user_id;
115*49cdfc7eSAndroid Build Coastguard Worker 	gid_t group_id;
116*49cdfc7eSAndroid Build Coastguard Worker 	int i;
117*49cdfc7eSAndroid Build Coastguard Worker 
118*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(argc, argv, NULL, NULL);
119*49cdfc7eSAndroid Build Coastguard Worker 
120*49cdfc7eSAndroid Build Coastguard Worker 	setup();
121*49cdfc7eSAndroid Build Coastguard Worker 
122*49cdfc7eSAndroid Build Coastguard Worker 	user_id = geteuid();
123*49cdfc7eSAndroid Build Coastguard Worker 	UID16_CHECK(user_id, lchown, cleanup);
124*49cdfc7eSAndroid Build Coastguard Worker 	group_id = getegid();
125*49cdfc7eSAndroid Build Coastguard Worker 	GID16_CHECK(group_id, lchown, cleanup);
126*49cdfc7eSAndroid Build Coastguard Worker 
127*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
128*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
129*49cdfc7eSAndroid Build Coastguard Worker 
130*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; test_cases[i].desc != NULL; i++) {
131*49cdfc7eSAndroid Build Coastguard Worker 			char *file_name = test_cases[i].pathname;
132*49cdfc7eSAndroid Build Coastguard Worker 			char *test_desc = test_cases[i].desc;
133*49cdfc7eSAndroid Build Coastguard Worker 
134*49cdfc7eSAndroid Build Coastguard Worker 			/*
135*49cdfc7eSAndroid Build Coastguard Worker 			 * Call lchown(2) to test different test conditions.
136*49cdfc7eSAndroid Build Coastguard Worker 			 * verify that it fails with -1 return value and
137*49cdfc7eSAndroid Build Coastguard Worker 			 * sets appropriate errno.
138*49cdfc7eSAndroid Build Coastguard Worker 			 */
139*49cdfc7eSAndroid Build Coastguard Worker 			TEST(LCHOWN(cleanup, file_name, user_id, group_id));
140*49cdfc7eSAndroid Build Coastguard Worker 
141*49cdfc7eSAndroid Build Coastguard Worker 			/* Check return code from lchown(2) */
142*49cdfc7eSAndroid Build Coastguard Worker 			if (TEST_RETURN == -1) {
143*49cdfc7eSAndroid Build Coastguard Worker 				if (TEST_ERRNO == test_cases[i].exp_errno) {
144*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TPASS,
145*49cdfc7eSAndroid Build Coastguard Worker 						 "lchown(2) fails, %s, errno:%d",
146*49cdfc7eSAndroid Build Coastguard Worker 						 test_desc, TEST_ERRNO);
147*49cdfc7eSAndroid Build Coastguard Worker 				} else {
148*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TFAIL, "lchown(2) fails, %s, "
149*49cdfc7eSAndroid Build Coastguard Worker 						 "errno:%d, expected errno:%d",
150*49cdfc7eSAndroid Build Coastguard Worker 						 test_desc, TEST_ERRNO,
151*49cdfc7eSAndroid Build Coastguard Worker 						 test_cases[i].exp_errno);
152*49cdfc7eSAndroid Build Coastguard Worker 				}
153*49cdfc7eSAndroid Build Coastguard Worker 			} else {
154*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "lchown(2) returned %ld, "
155*49cdfc7eSAndroid Build Coastguard Worker 					 "expected -1, errno:%d", TEST_RETURN,
156*49cdfc7eSAndroid Build Coastguard Worker 					 test_cases[i].exp_errno);
157*49cdfc7eSAndroid Build Coastguard Worker 			}
158*49cdfc7eSAndroid Build Coastguard Worker 		}
159*49cdfc7eSAndroid Build Coastguard Worker 	}
160*49cdfc7eSAndroid Build Coastguard Worker 
161*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
162*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
163*49cdfc7eSAndroid Build Coastguard Worker }
164*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)165*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
166*49cdfc7eSAndroid Build Coastguard Worker {
167*49cdfc7eSAndroid Build Coastguard Worker 	int i;
168*49cdfc7eSAndroid Build Coastguard Worker 
169*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(FORK, DEF_HANDLER, cleanup);
170*49cdfc7eSAndroid Build Coastguard Worker 
171*49cdfc7eSAndroid Build Coastguard Worker 	tst_require_root();
172*49cdfc7eSAndroid Build Coastguard Worker 
173*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
174*49cdfc7eSAndroid Build Coastguard Worker 
175*49cdfc7eSAndroid Build Coastguard Worker 	/* change euid and gid to nobody */
176*49cdfc7eSAndroid Build Coastguard Worker 	ltpuser = getpwnam(TEST_USER);
177*49cdfc7eSAndroid Build Coastguard Worker 
178*49cdfc7eSAndroid Build Coastguard Worker 	if (ltpuser == NULL)
179*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK, cleanup, "getpwnam failed");
180*49cdfc7eSAndroid Build Coastguard Worker 
181*49cdfc7eSAndroid Build Coastguard Worker 	if (setgid(ltpuser->pw_uid) == -1)
182*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TBROK | TERRNO, "setgid failed");
183*49cdfc7eSAndroid Build Coastguard Worker 
184*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
185*49cdfc7eSAndroid Build Coastguard Worker 
186*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; test_cases[i].desc != NULL; i++)
187*49cdfc7eSAndroid Build Coastguard Worker 		if (test_cases[i].setup != NULL)
188*49cdfc7eSAndroid Build Coastguard Worker 			test_cases[i].setup(i);
189*49cdfc7eSAndroid Build Coastguard Worker }
190*49cdfc7eSAndroid Build Coastguard Worker 
191*49cdfc7eSAndroid Build Coastguard Worker /*
192*49cdfc7eSAndroid Build Coastguard Worker  * setup_eperm() - setup function for a test condition for which lchown(2)
193*49cdfc7eSAndroid Build Coastguard Worker  *	           returns -1 and sets errno to EPERM.
194*49cdfc7eSAndroid Build Coastguard Worker  *
195*49cdfc7eSAndroid Build Coastguard Worker  * Create test file and symlink with uid 0.
196*49cdfc7eSAndroid Build Coastguard Worker  */
setup_eperm(int pos LTP_ATTRIBUTE_UNUSED)197*49cdfc7eSAndroid Build Coastguard Worker static void setup_eperm(int pos LTP_ATTRIBUTE_UNUSED)
198*49cdfc7eSAndroid Build Coastguard Worker {
199*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
200*49cdfc7eSAndroid Build Coastguard Worker 
201*49cdfc7eSAndroid Build Coastguard Worker 	/* create a testfile */
202*49cdfc7eSAndroid Build Coastguard Worker 	if ((fd = open(TEST_FILE1, O_RDWR | O_CREAT, 0666)) == -1)
203*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "open failed");
204*49cdfc7eSAndroid Build Coastguard Worker 
205*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(cleanup, fd);
206*49cdfc7eSAndroid Build Coastguard Worker 
207*49cdfc7eSAndroid Build Coastguard Worker 	/* become root once more */
208*49cdfc7eSAndroid Build Coastguard Worker 	if (seteuid(0) == -1)
209*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TBROK | TERRNO, "setuid(0) failed");
210*49cdfc7eSAndroid Build Coastguard Worker 
211*49cdfc7eSAndroid Build Coastguard Worker 	/* create symling to testfile */
212*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK(cleanup, TEST_FILE1, SFILE1);
213*49cdfc7eSAndroid Build Coastguard Worker 
214*49cdfc7eSAndroid Build Coastguard Worker 	/* back to the user nobody */
215*49cdfc7eSAndroid Build Coastguard Worker 	if (seteuid(ltpuser->pw_uid) == -1)
216*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TBROK | TERRNO, "seteuid(%d) failed", ltpuser->pw_uid);
217*49cdfc7eSAndroid Build Coastguard Worker }
218*49cdfc7eSAndroid Build Coastguard Worker 
219*49cdfc7eSAndroid Build Coastguard Worker /*
220*49cdfc7eSAndroid Build Coastguard Worker  * setup_eaccess() - setup function for a test condition for which lchown(2)
221*49cdfc7eSAndroid Build Coastguard Worker  *	             returns -1 and sets errno to EACCES.
222*49cdfc7eSAndroid Build Coastguard Worker  *
223*49cdfc7eSAndroid Build Coastguard Worker  *  Create a test directory under temporary directory and create a test file
224*49cdfc7eSAndroid Build Coastguard Worker  *  under this directory with mode "0666" permissions.
225*49cdfc7eSAndroid Build Coastguard Worker  *  Modify the mode permissions on test directory such that process will not
226*49cdfc7eSAndroid Build Coastguard Worker  *  have search permissions on test directory.
227*49cdfc7eSAndroid Build Coastguard Worker  */
setup_eacces(int pos LTP_ATTRIBUTE_UNUSED)228*49cdfc7eSAndroid Build Coastguard Worker static void setup_eacces(int pos LTP_ATTRIBUTE_UNUSED)
229*49cdfc7eSAndroid Build Coastguard Worker {
230*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
231*49cdfc7eSAndroid Build Coastguard Worker 
232*49cdfc7eSAndroid Build Coastguard Worker 	/* create a test directory */
233*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
234*49cdfc7eSAndroid Build Coastguard Worker 
235*49cdfc7eSAndroid Build Coastguard Worker 	/* create a file under test directory */
236*49cdfc7eSAndroid Build Coastguard Worker 	if ((fd = open(TEST_FILE2, O_RDWR | O_CREAT, 0666)) == -1)
237*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "open failed");
238*49cdfc7eSAndroid Build Coastguard Worker 
239*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(cleanup, fd);
240*49cdfc7eSAndroid Build Coastguard Worker 
241*49cdfc7eSAndroid Build Coastguard Worker 	/* create a symlink of testfile */
242*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK(cleanup, TEST_FILE2, SFILE2);
243*49cdfc7eSAndroid Build Coastguard Worker 
244*49cdfc7eSAndroid Build Coastguard Worker 	/* modify mode permissions on test directory */
245*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHMOD(cleanup, DIR_TEMP, FILE_MODE);
246*49cdfc7eSAndroid Build Coastguard Worker }
247*49cdfc7eSAndroid Build Coastguard Worker 
248*49cdfc7eSAndroid Build Coastguard Worker /*
249*49cdfc7eSAndroid Build Coastguard Worker  * setup_efault() -- setup for a test condition where lchown(2) returns -1 and
250*49cdfc7eSAndroid Build Coastguard Worker  *                   sets errno to EFAULT.
251*49cdfc7eSAndroid Build Coastguard Worker  *
252*49cdfc7eSAndroid Build Coastguard Worker  * Create "bad address" by explicitly mmaping anonymous page that may not be
253*49cdfc7eSAndroid Build Coastguard Worker  * accesed (see PROT_NONE).
254*49cdfc7eSAndroid Build Coastguard Worker  */
setup_efault(int pos)255*49cdfc7eSAndroid Build Coastguard Worker static void setup_efault(int pos)
256*49cdfc7eSAndroid Build Coastguard Worker {
257*49cdfc7eSAndroid Build Coastguard Worker 	test_cases[pos].pathname = tst_get_bad_addr(cleanup);
258*49cdfc7eSAndroid Build Coastguard Worker }
259*49cdfc7eSAndroid Build Coastguard Worker 
260*49cdfc7eSAndroid Build Coastguard Worker /*
261*49cdfc7eSAndroid Build Coastguard Worker  * setup_enotdir() - setup function for a test condition for which chown(2)
262*49cdfc7eSAndroid Build Coastguard Worker  *	             returns -1 and sets errno to ENOTDIR.
263*49cdfc7eSAndroid Build Coastguard Worker  *
264*49cdfc7eSAndroid Build Coastguard Worker  * Create a regular file "t_file" to call lchown(2) on "t_file/sfile" later.
265*49cdfc7eSAndroid Build Coastguard Worker  */
setup_enotdir(int pos LTP_ATTRIBUTE_UNUSED)266*49cdfc7eSAndroid Build Coastguard Worker static void setup_enotdir(int pos LTP_ATTRIBUTE_UNUSED)
267*49cdfc7eSAndroid Build Coastguard Worker {
268*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
269*49cdfc7eSAndroid Build Coastguard Worker 
270*49cdfc7eSAndroid Build Coastguard Worker 	/* create a testfile under temporary directory */
271*49cdfc7eSAndroid Build Coastguard Worker 	if ((fd = open(TFILE3, O_RDWR | O_CREAT, MODE_RWX)) == -1) {
272*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "open(2) %s failed", TFILE3);
273*49cdfc7eSAndroid Build Coastguard Worker 	}
274*49cdfc7eSAndroid Build Coastguard Worker 
275*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(cleanup, fd);
276*49cdfc7eSAndroid Build Coastguard Worker }
277*49cdfc7eSAndroid Build Coastguard Worker 
278*49cdfc7eSAndroid Build Coastguard Worker /*
279*49cdfc7eSAndroid Build Coastguard Worker  * longpath_setup() - setup to create a node with a name length exceeding
280*49cdfc7eSAndroid Build Coastguard Worker  *                    the length of PATH_MAX.
281*49cdfc7eSAndroid Build Coastguard Worker  */
setup_longpath(int pos)282*49cdfc7eSAndroid Build Coastguard Worker static void setup_longpath(int pos)
283*49cdfc7eSAndroid Build Coastguard Worker {
284*49cdfc7eSAndroid Build Coastguard Worker 	memset(test_cases[pos].pathname, 'a', PATH_MAX + 1);
285*49cdfc7eSAndroid Build Coastguard Worker 	test_cases[pos].pathname[PATH_MAX + 1] = '\0';
286*49cdfc7eSAndroid Build Coastguard Worker }
287*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)288*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
289*49cdfc7eSAndroid Build Coastguard Worker {
290*49cdfc7eSAndroid Build Coastguard Worker 	if (seteuid(0) == -1) {
291*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO | TERRNO,
292*49cdfc7eSAndroid Build Coastguard Worker 			 "seteuid(2) failed to set the effective uid to 0");
293*49cdfc7eSAndroid Build Coastguard Worker 	}
294*49cdfc7eSAndroid Build Coastguard Worker 
295*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
296*49cdfc7eSAndroid Build Coastguard Worker }
297