From patchwork Sun Jan 8 13:10:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 13092492 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A66F1C53210 for ; Sun, 8 Jan 2023 13:10:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234446AbjAHNKz (ORCPT ); Sun, 8 Jan 2023 08:10:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233824AbjAHNKx (ORCPT ); Sun, 8 Jan 2023 08:10:53 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ABBE4BCA; Sun, 8 Jan 2023 05:10:49 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.183.184]) by gnuweeb.org (Postfix) with ESMTPSA id 36D9A7E620; Sun, 8 Jan 2023 13:10:45 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1673183449; bh=XNGtWJcW+7/4kkcyC8FDH4yTsKXskMlGZGvdbnPIy3k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=q15BaMA4non2mn6QpbDSAFjOXrdXs1bABzYf6yU0hEIqGK6n2PzMFaZvNEB0HKe/m zAFg/wEJKb/UHyEK/rBL9ftTdkiU0f80ZV4y8yNNtjgylUNtcIgs7Td5pFOybVCKEv DuoifHiHZjdqxpdv+CbgWhi0nX567gSRvezyibOPR79QgnRTn8ymQ0v1n+Ni6o3Srk BGfZf3tFYd4iaWxysJYPwthhtwwgaWwwH9b4GF58OPFa131oIUDLH/abhpJOgeGWVL NsYnw5VlENeepO+xoZY1nSelDTq1lon6E44jCGk26IxCa0siE0T//Lh6V8J7a2CdeB kGjn1BBOGlOhQ== From: Ammar Faizi To: Willy Tarreau Cc: Ammar Faizi , Shuah Khan , "Paul E. McKenney" , Gilang Fachrezy , Alviro Iskandar Setiawan , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [PATCH v2 1/4] nolibc/sys: Implement `sigaction(2)` function Date: Sun, 8 Jan 2023 20:10:35 +0700 Message-Id: <20230108131038.841204-2-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230108131038.841204-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> <20221222043452.GB29086@1wt.eu> <20221222134615.3535422-1-ammar.faizi@intel.com> <20221227062640.GA5337@1wt.eu> <00eee75f-59fa-83b2-c7e1-f0da347b2dde@gnuweeb.org> <20221227184902.GA6287@1wt.eu> <23e84c59-4f2c-01b4-5b8a-80af39a1d761@gnuweeb.org> <20221228133513.GA7457@1wt.eu> <39d68044-2641-75da-929a-f5e852f0a3d0@gnuweeb.org> <20230103035427.GA4474@1wt.eu> <086ff43d-2647-0459-d993-6fc90d7ae779@gnuweeb.org> <20230108131038.841204-1-ammar.faizi@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Ammar Faizi This commit adds the initial implementation of nolibc `sigaction()` function. Currently, this implementation is only available on the x86-64 arch. `sigaction()` needs an architecture-dependent "signal trampoline" function that invokes __rt_sigreturn syscall to resume the process after a signal gets handled. The "signal trampoline" function is called `__restore_rt` in this implementation. The naming `__restore_rt` is important for GDB. It also has to be given a special optimization attribute "omit-frame-pointer" to prevent the compiler from creating a stack frame that makes the `%rsp` value no longer points to the `struct rt_sigframe` that the kernel constructed. Link: https://lore.kernel.org/lkml/20221228133513.GA7457@1wt.eu Suggested-by: Willy Tarreau Signed-off-by: Ammar Faizi --- Side note: This has been tested on x86-64 arch and `__restore_rt` generates the correct code. The `__restore_rt` codegen correctness on other architectures need to be evaluated as well. If it can't generate the correct code, it has to be written in inline Assembly. tools/include/nolibc/sys.h | 72 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index acf7cf438010..7d594155e77f 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -1047,6 +1047,78 @@ pid_t setsid(void) return ret; } +typedef void (*sighandler_t)(int sig); + +/* + * int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact); + */ + +static __attribute__((unused)) +int sys_sigaction(int signum, const struct sigaction *act, + struct sigaction *oldact) +{ + return my_syscall4(__NR_rt_sigaction, signum, act, oldact, + sizeof(sigset_t)); +} + +__attribute__((weak,unused,noreturn,optimize("omit-frame-pointer"),section(".text.__restore_rt"))) +void __restore_rt(void) +{ + my_syscall0(__NR_rt_sigreturn); + __builtin_unreachable(); +} + +static __attribute__((unused)) +int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) +{ + struct sigaction act2 = *act; + int ret; + + /* + * On Linux x86-64, libc's sigaction() always sets the + * @act->sa_restorer when the caller passes a NULL. + * + * @act->sa_restorer is an arch-specific function used + * as a "signal trampoline". + * + * @act->sa_handler is a signal handler provided by the + * user. + * + * When the handled signal is caught, the %rip jumps to + * @act->sa_handler with user stack already set by the + * kernel as below: + * + * |--------------------| + * %rsp -> | act->sa_restorer | (return address) + * |--------------------| + * | struct rt_sigframe | (process context info) + * | | + * | | + * .................... + * + * Once this signal handler executes the "ret" instruction, + * the %rip jumps to @act->sa_restorer. The sa_restorer + * function has to invoke the __rt_sigreturn syscall with + * %rsp pointing to the `struct rt_sigframe` that the kernel + * constructed previously to resume the process. + * + * `struct rt_sigframe` contains the registers' value before + * the signal is caught. + * + */ + if (!act2.sa_restorer) { + act2.sa_flags |= SA_RESTORER; + act2.sa_restorer = __restore_rt; + } + + ret = sys_sigaction(signum, &act2, oldact); + if (ret < 0) { + SET_ERRNO(-ret); + ret = -1; + } + return ret; +} + /* * int stat(const char *path, struct stat *buf); From patchwork Sun Jan 8 13:10:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 13092494 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0869FC53210 for ; Sun, 8 Jan 2023 13:11:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232624AbjAHNLM (ORCPT ); Sun, 8 Jan 2023 08:11:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234729AbjAHNLE (ORCPT ); Sun, 8 Jan 2023 08:11:04 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9AB04E0FA; Sun, 8 Jan 2023 05:10:52 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.183.184]) by gnuweeb.org (Postfix) with ESMTPSA id 91A277E6A7; Sun, 8 Jan 2023 13:10:49 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1673183452; bh=qZ0V/QjSoT8k1kLlfLdweTxUATQfYPsfP6+ph+Sfr8I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jvHZRqWP20s3qYnaEqTsf3KXTUJWYP9isc4qvSoApF72/xdhiZLcx//qd0ut3kZC2 c5q3NU1LDKLjQeEk5/TwwBemkvFy4jw+YLrYdTuspu8J9G6GGO+QzC8kIVgxTAz3xp I0QswhSr+Pm7XGEus9QV8UGxKJDRGJPVVVwlGVmEAKdYm10TZA6ZW/CuH3HzsTe5jo 57dcATbgWDNb5mviBEzXouO7ufpTbh7geUe8oeUIZG6504BKiuTKGP4oq+T1HO+H1v jA8dx+u12JGqdJeSgMx4TBrlWUVp18fOnRPYWkXhsz+aLRzojk5Quv+ad3cTiPzR+F 0YVBpibZpY3aA== From: Ammar Faizi To: Willy Tarreau Cc: Ammar Faizi , Shuah Khan , "Paul E. McKenney" , Gilang Fachrezy , Alviro Iskandar Setiawan , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [PATCH v2 2/4] nolibc/sys: Implement `signal(2)` function Date: Sun, 8 Jan 2023 20:10:36 +0700 Message-Id: <20230108131038.841204-3-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230108131038.841204-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> <20221222043452.GB29086@1wt.eu> <20221222134615.3535422-1-ammar.faizi@intel.com> <20221227062640.GA5337@1wt.eu> <00eee75f-59fa-83b2-c7e1-f0da347b2dde@gnuweeb.org> <20221227184902.GA6287@1wt.eu> <23e84c59-4f2c-01b4-5b8a-80af39a1d761@gnuweeb.org> <20221228133513.GA7457@1wt.eu> <39d68044-2641-75da-929a-f5e852f0a3d0@gnuweeb.org> <20230103035427.GA4474@1wt.eu> <086ff43d-2647-0459-d993-6fc90d7ae779@gnuweeb.org> <20230108131038.841204-1-ammar.faizi@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Ammar Faizi signal() function is the simpler version of sigaction(). Unlike sigaction(), which fully controls the `struct sigaction`, the caller only cares about the sa_handler when calling the signal() function. signal() internally calls sigaction(). Signed-off-by: Ammar Faizi --- tools/include/nolibc/sys.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 7d594155e77f..54e51f154b1f 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -1119,6 +1119,31 @@ int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) return ret; } +/* + * sighandler_t signal(int signum, sighandler_t handler); + */ + +static __attribute__((unused)) +sighandler_t signal(int signum, sighandler_t handler) +{ + const struct sigaction act = { + .sa_handler = handler, + .sa_flags = SA_RESTORER, + .sa_restorer = __restore_rt + }; + sighandler_t old_sah; + struct sigaction old; + int ret; + + ret = sys_sigaction(signum, &act, &old); + if (ret < 0) { + SET_ERRNO(-ret); + old_sah = SIG_ERR; + } else { + old_sah = old.sa_handler; + } + return old_sah; +} /* * int stat(const char *path, struct stat *buf); From patchwork Sun Jan 8 13:10:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 13092493 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B92CBC54EBC for ; Sun, 8 Jan 2023 13:11:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235364AbjAHNLL (ORCPT ); Sun, 8 Jan 2023 08:11:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234817AbjAHNLE (ORCPT ); Sun, 8 Jan 2023 08:11:04 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42DB4F038; Sun, 8 Jan 2023 05:10:56 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.183.184]) by gnuweeb.org (Postfix) with ESMTPSA id EC04B7E608; Sun, 8 Jan 2023 13:10:52 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1673183455; bh=FDH1SiFwyTAYw73kTXZ3LWXEORDAq51IpBI/AfSBiCw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UHCB4Wa03U16A5et4qHAdqJlgi4ONmQjmrLdiT8BZTB5JQllKYVsN+eHqzlwLs1FQ j90XgXpG1Y1tnigshdVDUStKwdMSe/gg45D/RS69c8b5sLjqEz5DuuF8c5Syd+1fwI PAGMf9X+KSQmAcrX2mDcSSTk5epyFly4HuT76WbzPJIB81IXv9agIUJuZ+PwI3op73 20I/e6Uq60Lfu1/Hlvh4/Oret0lRF/c7BGN4vb58+qRN+Sys4gsOVkbQbp+cjr5jTY 5W3pqypaRVgzTQMbZPkQ/Ggq67YYlRXhRbCUwZhrnNMm6XxNbPdZZ+6xKhb7ieU/ax NL2T+VEk4a+uw== From: Ammar Faizi To: Willy Tarreau Cc: Ammar Faizi , Shuah Khan , "Paul E. McKenney" , Gilang Fachrezy , Alviro Iskandar Setiawan , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [PATCH v2 3/4] selftests/nolibc: Add `fork(2)` selftest Date: Sun, 8 Jan 2023 20:10:37 +0700 Message-Id: <20230108131038.841204-4-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230108131038.841204-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> <20221222043452.GB29086@1wt.eu> <20221222134615.3535422-1-ammar.faizi@intel.com> <20221227062640.GA5337@1wt.eu> <00eee75f-59fa-83b2-c7e1-f0da347b2dde@gnuweeb.org> <20221227184902.GA6287@1wt.eu> <23e84c59-4f2c-01b4-5b8a-80af39a1d761@gnuweeb.org> <20221228133513.GA7457@1wt.eu> <39d68044-2641-75da-929a-f5e852f0a3d0@gnuweeb.org> <20230103035427.GA4474@1wt.eu> <086ff43d-2647-0459-d993-6fc90d7ae779@gnuweeb.org> <20230108131038.841204-1-ammar.faizi@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org 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 3a78399f4624..cb6ec9f71aae 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -471,6 +471,50 @@ static int test_getpagesize(void) return !c; } +/* + * 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. */ @@ -523,6 +567,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; From patchwork Sun Jan 8 13:10:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 13092495 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DCC0DC53210 for ; Sun, 8 Jan 2023 13:11:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235082AbjAHNL0 (ORCPT ); Sun, 8 Jan 2023 08:11:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235087AbjAHNLI (ORCPT ); Sun, 8 Jan 2023 08:11:08 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB2C7F5BA; Sun, 8 Jan 2023 05:10:59 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.183.184]) by gnuweeb.org (Postfix) with ESMTPSA id 51D877E6B3; Sun, 8 Jan 2023 13:10:56 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1673183459; bh=HR/stZAKrmeLGzDqyWnPp7zQ1g2rp8o4A70mrDeKDAw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qh7iQnaZ6dK/A66O/KPZUCDFE3aI1s74lIbvLwrNV8CX/kUjKf+/aa330KJJ+Bs0S B8IcBKLhSogg8yz5aDjbIB6KyGxwep+pZY/T0K8cAdxZliSc9kAOmrWESTgSPzMpZ/ TLMLXVe9xuj1/VXQKAmefegnws1GJaYulLt3sKbIyqCc1v1QrWiksZpmfz8tmCKHvD QWJViHt+DlKLXHufYR40irB3IZRa8n2SMxzydTHJLeSREl7oUwEFW3z64PqTogNMri Etxr/amZKm7nUYI6GonL52Wx0CFkYG1e9tzTP/2F5Qlpd4Bmnr9szvLde7S/Rq8xIw ZZLCWzQjMgBrw== From: Ammar Faizi To: Willy Tarreau Cc: Ammar Faizi , Shuah Khan , "Paul E. McKenney" , Gilang Fachrezy , Alviro Iskandar Setiawan , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [PATCH v2 4/4] selftests/nolibc: Add `sigaction(2)` selftest Date: Sun, 8 Jan 2023 20:10:38 +0700 Message-Id: <20230108131038.841204-5-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230108131038.841204-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> <20221222043452.GB29086@1wt.eu> <20221222134615.3535422-1-ammar.faizi@intel.com> <20221227062640.GA5337@1wt.eu> <00eee75f-59fa-83b2-c7e1-f0da347b2dde@gnuweeb.org> <20221227184902.GA6287@1wt.eu> <23e84c59-4f2c-01b4-5b8a-80af39a1d761@gnuweeb.org> <20221228133513.GA7457@1wt.eu> <39d68044-2641-75da-929a-f5e852f0a3d0@gnuweeb.org> <20230103035427.GA4474@1wt.eu> <086ff43d-2647-0459-d993-6fc90d7ae779@gnuweeb.org> <20230108131038.841204-1-ammar.faizi@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Ammar Faizi Test the sigaction() function implementation. Test steps: - Set a signal handler. - Then send a signal to itself using the kill() syscall. - The program has to survive and store the caught signal number in a volatile global variable. - Validate the volatile global variable value. - Restore the original signal handler. Signed-off-by: Ammar Faizi --- tools/testing/selftests/nolibc/nolibc-test.c | 127 +++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index cb6ec9f71aae..946ed0132f93 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -515,6 +515,131 @@ static int test_fork(void) return 0; } +static volatile int g_test_sig; + +static void test_signal_handler(int sig) +{ + g_test_sig = sig; +} + +static int test_sigaction_sig(int sig) +{ + const struct sigaction new = { + .sa_handler = test_signal_handler + }; + struct sigaction old; + int ret; + + /* + * Set the signal handler. + */ + ret = sigaction(sig, &new, &old); + if (ret) { + printf("test_sigaction_sig(%d): Failed to set a signal handler\n", sig); + return ret; + } + + /* + * Test the signal handler. + */ + g_test_sig = 0; + kill(getpid(), sig); + + /* + * test_signal_handler() must set @g_test_sig to @sig. + */ + if (g_test_sig != sig) { + printf("test_sigaction_sig(%d): Invalid g_test_sig value (%d != %d)\n", sig, g_test_sig, sig); + return -1; + } + + /* + * Restore the original signal handler. + */ + ret = sigaction(sig, &old, NULL); + if (ret) { + printf("test_sigaction_sig(%d): Failed to restore the signal handler\n", sig); + return ret; + } + + return 0; +} + +static int test_signal_sig(int sig) +{ + sighandler_t old; + + /* + * Set the signal handler. + */ + old = signal(sig, test_signal_handler); + if (old == SIG_ERR) { + printf("test_signal_sig(%d): Failed to set a signal handler\n", sig); + return -1; + } + + /* + * Test the signal handler. + */ + g_test_sig = 0; + kill(getpid(), sig); + + /* + * test_signal_handler() must set @g_test_sig to @sig. + */ + if (g_test_sig != sig) { + printf("test_signal_sig(%d): Invalid g_test_sig value (%d != %d)\n", sig, g_test_sig, sig); + return -1; + } + + /* + * Restore the original signal handler. + */ + old = signal(sig, old); + if (old == SIG_ERR) { + printf("test_signal_sig(%d): Failed to restore the signal handler\n", sig); + return -1; + } + + return 0; +} + +static const int g_sig_to_test[] = { + SIGINT, + SIGHUP, + SIGTERM, + SIGQUIT, + SIGSEGV +}; + +static int test_sigaction(void) +{ + size_t i; + int ret; + + for (i = 0; i < (sizeof(g_sig_to_test) / sizeof(g_sig_to_test[0])); i++) { + ret = test_sigaction_sig(g_sig_to_test[i]); + if (ret) + return ret; + } + + return 0; +} + +static int test_signal(void) +{ + size_t i; + int ret; + + for (i = 0; i < (sizeof(g_sig_to_test) / sizeof(g_sig_to_test[0])); i++) { + ret = test_signal_sig(g_sig_to_test[i]); + if (ret) + return ret; + } + + return 0; +} + /* Run syscall tests between IDs and . * Return 0 on success, non-zero on failure. */ @@ -596,6 +721,8 @@ int run_syscall(int min, int max) CASE_TEST(select_null); EXPECT_SYSZR(1, ({ struct timeval tv = { 0 }; select(0, NULL, NULL, NULL, &tv); })); break; CASE_TEST(select_stdout); EXPECT_SYSNE(1, ({ fd_set fds; FD_ZERO(&fds); FD_SET(1, &fds); select(2, NULL, &fds, NULL, NULL); }), -1); break; CASE_TEST(select_fault); EXPECT_SYSER(1, select(1, (void *)1, NULL, NULL, 0), -1, EFAULT); break; + CASE_TEST(sigaction); EXPECT_SYSZR(1, test_sigaction()); break; + CASE_TEST(signal); EXPECT_SYSZR(1, test_signal()); break; CASE_TEST(stat_blah); EXPECT_SYSER(1, stat("/proc/self/blah", &stat_buf), -1, ENOENT); break; CASE_TEST(stat_fault); EXPECT_SYSER(1, stat(NULL, &stat_buf), -1, EFAULT); break; CASE_TEST(symlink_root); EXPECT_SYSER(1, symlink("/", "/"), -1, EEXIST); break;