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