Skip to contentSkip to conversation

generic: new case to test getcwd(2)

PATCH Superseded Eryu Guan 3 participants 3 messages Jun 9, 2014 – Jun 10, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Eryu Guan

author
#0 lore raw

PATCH 1/1 generic: new case to test getcwd(2)

getcwd(2) may return "/" instead of the correct cwd on buggy kernel.

Regression test for:
ede4ceb prepend_path() needs to reinitialize dentry/vfsmount/mnt on restarts f650080 __dentry_path() fixes

Cc Artem Savkov <[email protected]>
Cc Jan Stancek <[email protected]>

---

This case was originally written by Artem Savkov and I made minor updates and ported it to xfstests.

 .gitignore            |  1 +
 src/Makefile          |  2 +-
 src/t_getcwd.c        | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/generic/028     | 54 +++++++++++++++++++++++++++++++
 tests/generic/028.out |  2 ++
 tests/generic/group   |  1 +
 6 files changed, 149 insertions(+), 1 deletion(-)
 create mode 100644 src/t_getcwd.c
 create mode 100755 tests/generic/028
 create mode 100644 tests/generic/028.out
.gitignore +1 -0
index 66e6ee8..5353439 100644
@@ -83,6 +83,7 @@
 /src/t_dir_offset
 /src/t_dir_offset2
 /src/t_futimens
+/src/t_getcwd
 /src/t_holes
 /src/t_immutable
 /src/t_mmap_writev
src/Makefile +1 -1
index d754048..7a7984a 100644
@@ -19,7 +19,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
 	bulkstat_unlink_test_modified t_dir_offset t_futimens t_immutable \
 	stale_handle pwrite_mmap_blocked t_dir_offset2 seek_sanity_test \
 	seek_copy_test t_readdir_1 t_readdir_2 fsync-tester nsexec cloner \
-	renameat2
+	renameat2 t_getcwd
 
 SUBDIRS =
 
src/t_getcwd.c new file +90 -0

90 lines not on this page · raw message

tests/generic/028 new file +54 -0

54 lines not on this page · raw message

tests/generic/028.out new file +2 -0
new file mode 100644
index 0000000..2615f73
@@ -0,0 +1,2 @@
+QA output created by 028
+Silence is golden
tests/generic/group +1 -0
index e851c62..8f4b491 100644
@@ -30,6 +30,7 @@
 025 auto quick
 026 acl quick auto
 027 auto enospc
+028 auto
 053 acl repair auto quick
 062 attr udf auto quick
 068 other auto freeze dangerous stress
Signature · 1 line
1.9.3
#1 lore raw
Quoted 1 line · Eryu Guan

getcwd(2) may return "/" instead of the correct cwd on buggy kernel.

What buggy kernel are we talking about here? In what conditions may that bug happen?

Quoted 3 lines

Regression test for:
ede4ceb prepend_path() needs to reinitialize dentry/vfsmount/mnt on restarts f650080 __dentry_path() fixes

Al's commits don't tell you anything about the context of the bug being fixed...

....

Quoted 7 lines
src/t_getcwd.c new file +1 -0
new file mode 100644
index 0000000..891de75
@@ -0,0 +1,90 @@
+#include <sys/types.h>

src/t_getcwd.c line 11

Quoted 10 lines
src/t_getcwd.c new file +10 -0
+#include <sys/wait.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define TIMEOUT 60

Does it need to run for 60 seconds? If we keep adding tests that run one timeouts measured in minutes, we're going to blow out the runtime of the test suite to being unmanageable pretty quickly.

Quoted 7 lines
src/t_getcwd.c new file +7 -0
+#define BUF_SIZE 256
+
+static sig_atomic_t end;
+
+void test_getcwd(char *init_cwd)
+{
+	int i = 0;

src/t_getcwd.c line 28

Quoted 10 lines
src/t_getcwd.c new file +10 -0
+	char cur_cwd[BUF_SIZE];
+	while (!end) {
+		getcwd(cur_cwd, BUF_SIZE);
+		if (strncmp(init_cwd, cur_cwd, BUF_SIZE)) {
+			printf("[%u] %s != %s\n", i, init_cwd, cur_cwd);
+			break;
+		}
+		i++;
+	}
+}

So this runs in a tight loop checking if it's working dir changes? This should return an error if the $CWD changes...

Quoted 14 lines
src/t_getcwd.c new file +14 -0
+void do_rename(char *prefix)
+{
+	int i = 0;
+	int fd;
+	char c_name[BUF_SIZE];
+	char n_name[BUF_SIZE];
+
+	strncpy(c_name, prefix, BUF_SIZE);
+
+	fd = open(c_name, O_CREAT | O_RDWR);
+	if (fd < 0) {
+		fprintf(stderr, "failed to create file %s: %s\n",
+			c_name, strerror(errno));
+		exit(1);

src/t_getcwd.c line 53

Quoted 10 lines
src/t_getcwd.c new file +10 -0
+	}
+	close(fd);
+
+	while (1) {
+		i++;
+		snprintf(n_name, BUF_SIZE, "%s%u", prefix, i);
+		rename(c_name, n_name);
+		strncpy(c_name, n_name, BUF_SIZE);
+	}
+}

And the child process sits in a tight loop renaming a file in $CWD until it is killed?

Quoted 20 lines
src/t_getcwd.c new file +20 -0
+void sigproc(int sig)
+{
+	end = 1;
+}
+
+int main(int argc, char *argv[])
+{
+	char init_cwd[BUF_SIZE];
+	pid_t pid;
+	int status;
+	int ret = 1;
+
+	getcwd(init_cwd, BUF_SIZE);
+
+	if (signal(SIGALRM, sigproc) == SIG_ERR) {
+		perror("signal failed");
+		exit(1);
+	}
+
+	alarm(TIMEOUT);

src/t_getcwd.c line 84

Quoted 10 lines
src/t_getcwd.c new file +10 -0
+
+	pid = fork();
+	if (pid < 0) {
+		perror("fork failed");
+		exit(1);
+	} else if (pid == 0) {
+		do_rename("testfile");
+	} else {
+		test_getcwd(init_cwd);
+		ret = !end;
		ret = test_getcwd(init_cwd);
Quoted 12 lines
src/t_getcwd.c new file +6 -0
+		kill(pid, SIGTERM);
+		waitpid(pid, &status, 0);
+	}
+
+	exit(ret);
+}
tests/generic/028 new file +0 -0
new file mode 100755
index 0000000..12216df

tests/generic/028 line 6

Quoted 6 lines
tests/generic/028 new file +6 -0
@@ -0,0 +1,54 @@
+#! /bin/bash
+# FS QA Test No. generic/028
+#
+# Regression test for
+# ede4ceb prepend_path() needs to reinitialize dentry/vfsmount/mnt on restarts
+# f650080 __dentry_path() fixes

Needs a better description.

tests/generic/028 line 8

Quoted 2 lines
tests/generic/028 new file +2 -0
+
+rm -f $seqres.full

$seqres.full is not used by the test, so no need to remove it.

tests/generic/group line 33

Quoted 7 lines
tests/generic/group +1 -0
@@ -30,6 +30,7 @@
 025 auto quick
 026 acl quick auto
 027 auto enospc
+028 auto

If this test doesn't need to run for 60s, then it could also be a quick test...

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#2 lore raw
Quoted 5 lines · Dave Chinner
Quoted 1 line · Eryu Guan

getcwd(2) may return "/" instead of the correct cwd on buggy kernel.

What buggy kernel are we talking about here? In what conditions may that bug happen?

I tested it on RHEL7 Beta kernel and test failed. It returns "/" randomly, Jan Stancek hit it once when building ltp and Mikulas Patocka could hit it by running lvm2 test suite. Please refer to this thread

https://www.mail-archive.com/[email protected]/msg17896.html

I'll put more details in commit log.

Quoted 23 lines
Quoted 5 lines

Regression test for:
ede4ceb prepend_path() needs to reinitialize dentry/vfsmount/mnt on restarts f650080 __dentry_path() fixes

Al's commits don't tell you anything about the context of the bug being fixed...

....

Quoted 13 lines
src/t_getcwd.c new file +7 -0
new file mode 100644
index 0000000..891de75
@@ -0,0 +1,90 @@
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
Quoted 8 lines

src/t_getcwd.c line 11

Quoted 4 lines
src/t_getcwd.c new file +4 -0
+#include <string.h>
+#include <unistd.h>
+
+#define TIMEOUT 60

Does it need to run for 60 seconds? If we keep adding tests that run one timeouts measured in minutes, we're going to blow out the runtime of the test suite to being unmanageable pretty quickly.

The problem shows up pretty quickly, I think 10s is long enough. I'll reduce the run time.

Quoted 13 lines
Quoted 13 lines
src/t_getcwd.c new file +12 -0
+#define BUF_SIZE 256
+
+static sig_atomic_t end;
+
+void test_getcwd(char *init_cwd)
+{
+	int i = 0;
+	char cur_cwd[BUF_SIZE];
+	while (!end) {
+		getcwd(cur_cwd, BUF_SIZE);
+		if (strncmp(init_cwd, cur_cwd, BUF_SIZE)) {
+			printf("[%u] %s != %s\n", i, init_cwd, cur_cwd);
Quoted 8 lines

src/t_getcwd.c line 28

Quoted 5 lines
src/t_getcwd.c new file +5 -0
+			break;
+		}
+		i++;
+	}
+}

So this runs in a tight loop checking if it's working dir changes? This should return an error if the $CWD changes...

OK

Quoted 74 lines
Quoted 15 lines
src/t_getcwd.c new file +14 -0
+void do_rename(char *prefix)
+{
+	int i = 0;
+	int fd;
+	char c_name[BUF_SIZE];
+	char n_name[BUF_SIZE];
+
+	strncpy(c_name, prefix, BUF_SIZE);
+
+	fd = open(c_name, O_CREAT | O_RDWR);
+	if (fd < 0) {
+		fprintf(stderr, "failed to create file %s: %s\n",
+			c_name, strerror(errno));
+		exit(1);

src/t_getcwd.c line 53

Quoted 10 lines
src/t_getcwd.c new file +10 -0
+	}
+	close(fd);
+
+	while (1) {
+		i++;
+		snprintf(n_name, BUF_SIZE, "%s%u", prefix, i);
+		rename(c_name, n_name);
+		strncpy(c_name, n_name, BUF_SIZE);
+	}
+}

And the child process sits in a tight loop renaming a file in $CWD until it is killed?

Quoted 20 lines
src/t_getcwd.c new file +20 -0
+void sigproc(int sig)
+{
+	end = 1;
+}
+
+int main(int argc, char *argv[])
+{
+	char init_cwd[BUF_SIZE];
+	pid_t pid;
+	int status;
+	int ret = 1;
+
+	getcwd(init_cwd, BUF_SIZE);
+
+	if (signal(SIGALRM, sigproc) == SIG_ERR) {
+		perror("signal failed");
+		exit(1);
+	}
+
+	alarm(TIMEOUT);

src/t_getcwd.c line 84

Quoted 10 lines
src/t_getcwd.c new file +10 -0
+
+	pid = fork();
+	if (pid < 0) {
+		perror("fork failed");
+		exit(1);
+	} else if (pid == 0) {
+		do_rename("testfile");
+	} else {
+		test_getcwd(init_cwd);
+		ret = !end;
		ret = test_getcwd(init_cwd);
Quoted 12 lines
src/t_getcwd.c new file +6 -0
+		kill(pid, SIGTERM);
+		waitpid(pid, &status, 0);
+	}
+
+	exit(ret);
+}
tests/generic/028 new file +0 -0
new file mode 100755
index 0000000..12216df
@@ -0,0 +1,54 @@
Quoted 8 lines

tests/generic/028 line 6

Quoted 6 lines
tests/generic/028 new file +6 -0
+#! /bin/bash
+# FS QA Test No. generic/028
+#
+# Regression test for
+# ede4ceb prepend_path() needs to reinitialize dentry/vfsmount/mnt on restarts
+# f650080 __dentry_path() fixes

Needs a better description.

OK.

Quoted 5 lines

tests/generic/028 line 8

Quoted 3 lines
tests/generic/028 new file +2 -0
+
+rm -f $seqres.full

$seqres.full is not used by the test, so no need to remove it.

OK.

Quoted 11 lines

tests/generic/group line 33

Quoted 8 lines
tests/generic/group +1 -0
@@ -30,6 +30,7 @@
 025 auto quick
 026 acl quick auto
 027 auto enospc
+028 auto

If this test doesn't need to run for 60s, then it could also be a quick test...

Sure, I'll make it 10s and add quick group.

Thanks for the review!

Eryu

Quoted 11 lines

Cheers,

Dave.

Signature · 6 lines
Dave Chinner
[email protected]
--
To unsubscribe from this list: send the line "unsubscribe fstests" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html