From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from localhost.localdomain (unknown [182.253.88.132]) by gnuweeb.org (Postfix) with ESMTPSA id 7729A81A0D; Thu, 22 Dec 2022 03:52:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1671681158; bh=7CHYFJ/fWvU3HmqUeRZ+VXRA4YRaz7Sx/PWqqaM5V34=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OAr/7wyrio6E1/EaQ1M1rE1/PZVifyMfZI3ucHwy85zJYky5RY12Y+9aiE2RZw231 2feH9uuKuOWyZtteCOwapCVVLvuI4pfaxx2SkRugm8I2vXxfldLaoNWw+0XK7I1jR8 WGpOhumr90RujdFpnWDTg6xszQEaQaHGqRnSnAqZHgHESJJVTy609MpkLkvRcTjRaT 0WyC50LXJ7FYc1kIqOh+TvotowI7B1E/wMGdqKj5834q4SSpEfpOerJV+oL29Dx9f/ ZD1OA4+Qo5fMOmSUs9Uaq5Tkzt7AU8tzmBWfll7ppSBNLmC3pPG0vMs4oqmscV4dtN FRcqZfbRnochQ== From: Ammar Faizi To: Willy Tarreau , Shuah Khan , "Paul E. McKenney" Cc: Ammar Faizi , Gilang Fachrezy , VNLX Kernel Department , Alviro Iskandar Setiawan , Kanna Scarlet , Muhammad Rizki , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [RFC PATCH v1 5/8] selftests/nolibc: Add `fork(2)` selftest Date: Thu, 22 Dec 2022 10:51:31 +0700 Message-Id: <20221222035134.3467659-6-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221222035134.3467659-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: From: Ammar Faizi Ensure the fork() function can create a child process. Also, when the child exits, the parent must be able to get the child's exit code via waitpid(). Signed-off-by: Ammar Faizi --- tools/testing/selftests/nolibc/nolibc-test.c | 45 ++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 8d69f8a0f35a..309cabbddeec 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -442,6 +442,50 @@ int test_getdents64(const char *dir) return ret; } +/* + * Test fork(). + * Make sure the exit code can be read from the parent process. + */ +static int test_fork_and_exit(int expected_code) +{ + int status; + int code; + pid_t ret; + pid_t p; + + p = fork(); + if (p < 0) + return p; + + if (!p) + exit(expected_code); + + do { + ret = waitpid(p, &status, 0); + if (ret < 0) + return ret; + } while (!WIFEXITED(status)); + + code = WEXITSTATUS(status); + if (code != expected_code) { + printf("test_fork_and_exit(): waitpid(): Invalid exit code: %d; expected = %d\n", code, expected_code); + return -1; + } + + return 0; +} + +static int test_fork(void) +{ + int i; + + for (i = 0; i < 255; i++) { + if (test_fork_and_exit(i)) + return -1; + } + return 0; +} + /* Run syscall tests between IDs and . * Return 0 on success, non-zero on failure. */ @@ -494,6 +538,7 @@ int run_syscall(int min, int max) CASE_TEST(dup3_0); tmp = dup3(0, 100, 0); EXPECT_SYSNE(1, tmp, -1); close(tmp); break; CASE_TEST(dup3_m1); tmp = dup3(-1, 100, 0); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break; CASE_TEST(execve_root); EXPECT_SYSER(1, execve("/", (char*[]){ [0] = "/", [1] = NULL }, NULL), -1, EACCES); break; + CASE_TEST(fork); EXPECT_SYSZR(1, test_fork()); break; CASE_TEST(getdents64_root); EXPECT_SYSNE(1, test_getdents64("/"), -1); break; CASE_TEST(getdents64_null); EXPECT_SYSER(1, test_getdents64("/dev/null"), -1, ENOTDIR); break; CASE_TEST(gettimeofday_null); EXPECT_SYSZR(1, gettimeofday(NULL, NULL)); break; -- Ammar Faizi