[LTP] [PATCH] syscalls/fork02: new test for fork()

Zhao Gongyi zhaogongyi@huawei.com
Fri Oct 21 08:50:32 CEST 2022


Verify that current workdir correctly inherited by child.

Signed-off-by: Zhao Gongyi <zhaogongyi@huawei.com>
---
 runtest/syscalls                          |  1 +
 testcases/kernel/syscalls/fork/.gitignore |  1 +
 testcases/kernel/syscalls/fork/fork02.c   | 51 +++++++++++++++++++++++
 3 files changed, 53 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fork/fork02.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 7a425439a..39b7f21f6 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -364,6 +364,7 @@ flock06 flock06
 fmtmsg01 fmtmsg01

 fork01 fork01
+fork02 fork02
 fork03 fork03
 fork04 fork04
 fork05 fork05
diff --git a/testcases/kernel/syscalls/fork/.gitignore b/testcases/kernel/syscalls/fork/.gitignore
index b817e9c05..8a08c586b 100644
--- a/testcases/kernel/syscalls/fork/.gitignore
+++ b/testcases/kernel/syscalls/fork/.gitignore
@@ -1,4 +1,5 @@
 /fork01
+/fork02
 /fork03
 /fork04
 /fork05
diff --git a/testcases/kernel/syscalls/fork/fork02.c b/testcases/kernel/syscalls/fork/fork02.c
new file mode 100644
index 000000000..54ad50712
--- /dev/null
+++ b/testcases/kernel/syscalls/fork/fork02.c
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright(c) 2022 Huawei Technologies Co., Ltd
+ * Author: Zhao Gongyi <zhaogongyi@huawei.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that current workdir correctly inherited by child.
+ */
+
+#include "tst_test.h"
+
+#define NEW_DIR "new_dir"
+
+static char parent_cwd[PATH_MAX];
+static char child_cwd[PATH_MAX];
+
+static void run(void)
+{
+	pid_t pid = SAFE_FORK();
+
+	if (!pid) {
+		SAFE_GETCWD(child_cwd, sizeof(child_cwd));
+		if (strcmp(child_cwd, parent_cwd)) {
+			tst_res(TFAIL, "checking of cwd failed: "
+					"child_cwd %s parent_cwd %s ",
+					child_cwd, parent_cwd);
+		} else
+			tst_res(TPASS, "Checking of cwd passed");
+		return;
+	}
+
+	SAFE_WAITPID(pid, NULL, 0);
+}
+
+static void setup(void)
+{
+	SAFE_MKDIR(NEW_DIR, 00770);
+	SAFE_CHDIR(NEW_DIR);
+	SAFE_GETCWD(parent_cwd, sizeof(parent_cwd));
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = run,
+	.needs_tmpdir = 1,
+	.forks_child = 1,
+};
+
--
2.17.1



More information about the ltp mailing list