blob: 8408636699999be1156abc48f45b08619f008ae9 [file] [log] [blame]
Xiaoguang Wang31c39f72014-07-27 17:00:54 +08001/*
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
36#include <stdio.h>
37#include <errno.h>
38#include <fcntl.h>
39#include <sys/types.h>
40#include <unistd.h>
Xiaoguang Wang31c39f72014-07-27 17:00:54 +080041#include "test.h"
42#include "safe_macros.h"
43
44#define TIMEOUT 5
45
46static void setup(void);
47static void cleanup(void);
48static void do_child(void);
49static void sigproc(int sig);
50static volatile sig_atomic_t end;
51static char init_cwd[PATH_MAX];
52
53char *TCID = "getcwd04";
54int TST_TOTAL = 1;
55
56int main(int ac, char **av)
57{
58 int status;
59 const char *msg;
60 char cur_cwd[PATH_MAX];
61 pid_t child;
62
63 msg = parse_opts(ac, av, NULL, NULL);
64 if (msg != NULL)
65 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
66
67 setup();
68
69 child = tst_fork();
70 if (child < 0)
71 tst_brkm(TBROK | TERRNO, cleanup, "fork failed");
72
73 if (child == 0)
74 do_child();
75
76 while (1) {
77 SAFE_GETCWD(cleanup, cur_cwd, PATH_MAX);
78 if (strncmp(init_cwd, cur_cwd, PATH_MAX)) {
79 tst_resm(TFAIL, "initial current work directory is "
80 "%s, now is %s. Bug is reproduced!",
81 init_cwd, cur_cwd);
82 break;
83 }
84
85 if (end) {
86 tst_resm(TPASS, "Bug is not reproduced!");
87 break;
88 }
89 }
90
91 SAFE_KILL(cleanup, child, SIGKILL);
92 SAFE_WAITPID(cleanup, child, &status, 0);
93
94 cleanup();
95 tst_exit();
96}
97
98static void setup(void)
99{
100 tst_sig(FORK, DEF_HANDLER, cleanup);
101
102 TEST_PAUSE;
103
104 if (tst_ncpus() == 1)
105 tst_brkm(TCONF, NULL, "This test needs two cpus at least");
106
107 tst_tmpdir();
108
109 if (signal(SIGALRM, sigproc) == SIG_ERR)
110 tst_brkm(TBROK | TERRNO, cleanup, "signal(SIGALRM) failed");
111
112 alarm(TIMEOUT);
113
114 SAFE_GETCWD(cleanup, init_cwd, PATH_MAX);
115}
116
117static void sigproc(int sig)
118{
119 end = sig;
120}
121
122static void do_child(void)
123{
124 unsigned int i = 0;
125 char c_name[PATH_MAX] = "testfile", n_name[PATH_MAX];
126
127 SAFE_TOUCH(NULL, c_name, 0644, NULL);
128
129 while (1) {
130 snprintf(n_name, PATH_MAX, "testfile%u", i++);
131 SAFE_RENAME(NULL, c_name, n_name);
132 strncpy(c_name, n_name, PATH_MAX);
133 }
134}
135
136static void cleanup(void)
137{
Xiaoguang Wang31c39f72014-07-27 17:00:54 +0800138 tst_rmdir();
139}