xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/getcwd/getcwd04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2014 Fujitsu Ltd.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Xiaoguang Wang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker  * Note: this test has already been in xfstests generic/028 test case,
9*49cdfc7eSAndroid Build Coastguard Worker  * I just port it to LTP.
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Kernel commit '232d2d60aa5469bb097f55728f65146bd49c1d25' introduced a race
12*49cdfc7eSAndroid Build Coastguard Worker  * condition that causes getcwd(2) to return "/" instead of correct path.
13*49cdfc7eSAndroid Build Coastguard Worker  *     232d2d6 dcache: Translating dentry into pathname without
14*49cdfc7eSAndroid Build Coastguard Worker  *             taking rename_lock
15*49cdfc7eSAndroid Build Coastguard Worker  *
16*49cdfc7eSAndroid Build Coastguard Worker  * And these two kernel commits fixed the bug:
17*49cdfc7eSAndroid Build Coastguard Worker  *   ede4cebce16f5643c61aedd6d88d9070a1d23a68
18*49cdfc7eSAndroid Build Coastguard Worker  *	prepend_path() needs to reinitialize dentry/vfsmount/mnt on restarts
19*49cdfc7eSAndroid Build Coastguard Worker  *   f6500801522c61782d4990fa1ad96154cb397cd4
20*49cdfc7eSAndroid Build Coastguard Worker  *	f650080 __dentry_path() fixes
21*49cdfc7eSAndroid Build Coastguard Worker  *
22*49cdfc7eSAndroid Build Coastguard Worker  * This test is to check whether this bug exists in the running kernel,
23*49cdfc7eSAndroid Build Coastguard Worker  * or whether this bug has been fixed.
24*49cdfc7eSAndroid Build Coastguard Worker  */
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker #define TIMEOUT	5
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker static void do_child(void);
36*49cdfc7eSAndroid Build Coastguard Worker static void sigproc(int sig);
37*49cdfc7eSAndroid Build Coastguard Worker static volatile sig_atomic_t end;
38*49cdfc7eSAndroid Build Coastguard Worker static char init_cwd[PATH_MAX];
39*49cdfc7eSAndroid Build Coastguard Worker 
verify_getcwd(void)40*49cdfc7eSAndroid Build Coastguard Worker static void verify_getcwd(void)
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker 	int status;
43*49cdfc7eSAndroid Build Coastguard Worker 	char cur_cwd[PATH_MAX];
44*49cdfc7eSAndroid Build Coastguard Worker 	pid_t child;
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 	child = SAFE_FORK();
47*49cdfc7eSAndroid Build Coastguard Worker 	if (child == 0)
48*49cdfc7eSAndroid Build Coastguard Worker 		do_child();
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	 while (1) {
51*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_GETCWD(cur_cwd, PATH_MAX);
52*49cdfc7eSAndroid Build Coastguard Worker 		if (strncmp(init_cwd, cur_cwd, PATH_MAX)) {
53*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "initial current work directory is "
54*49cdfc7eSAndroid Build Coastguard Worker 				 "%s, now is %s. Bug is reproduced!",
55*49cdfc7eSAndroid Build Coastguard Worker 				 init_cwd, cur_cwd);
56*49cdfc7eSAndroid Build Coastguard Worker 			break;
57*49cdfc7eSAndroid Build Coastguard Worker 		}
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 		if (end) {
60*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TPASS, "Bug is not reproduced!");
61*49cdfc7eSAndroid Build Coastguard Worker 			break;
62*49cdfc7eSAndroid Build Coastguard Worker 		}
63*49cdfc7eSAndroid Build Coastguard Worker 	}
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_KILL(child, SIGKILL);
66*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WAITPID(child, &status, 0);
67*49cdfc7eSAndroid Build Coastguard Worker }
68*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)69*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
70*49cdfc7eSAndroid Build Coastguard Worker {
71*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SIGNAL(SIGALRM, sigproc);
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	alarm(TIMEOUT);
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_GETCWD(init_cwd, PATH_MAX);
76*49cdfc7eSAndroid Build Coastguard Worker }
77*49cdfc7eSAndroid Build Coastguard Worker 
sigproc(int sig)78*49cdfc7eSAndroid Build Coastguard Worker static void sigproc(int sig)
79*49cdfc7eSAndroid Build Coastguard Worker {
80*49cdfc7eSAndroid Build Coastguard Worker 	end = sig;
81*49cdfc7eSAndroid Build Coastguard Worker }
82*49cdfc7eSAndroid Build Coastguard Worker 
do_child(void)83*49cdfc7eSAndroid Build Coastguard Worker static void do_child(void)
84*49cdfc7eSAndroid Build Coastguard Worker {
85*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i = 0;
86*49cdfc7eSAndroid Build Coastguard Worker 	char c_name[PATH_MAX] = "testfile", n_name[PATH_MAX];
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(c_name, 0644, NULL);
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 	while (1) {
91*49cdfc7eSAndroid Build Coastguard Worker 		snprintf(n_name, PATH_MAX, "testfile%u", i++);
92*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_RENAME(c_name, n_name);
93*49cdfc7eSAndroid Build Coastguard Worker 		strncpy(c_name, n_name, PATH_MAX);
94*49cdfc7eSAndroid Build Coastguard Worker 	}
95*49cdfc7eSAndroid Build Coastguard Worker }
96*49cdfc7eSAndroid Build Coastguard Worker 
97*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
98*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
99*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_getcwd,
100*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
101*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
102*49cdfc7eSAndroid Build Coastguard Worker 	.min_cpus = 2
103*49cdfc7eSAndroid Build Coastguard Worker };
104