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