From patchwork Tue Mar 23 13:44:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 42754 X-Patchwork-Delegate: siddhesh@gotplt.org Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C19003850427; Tue, 23 Mar 2021 13:45:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C19003850427 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1616507119; bh=yZU9oglykHjh7jxvxSJZSuA2xa3swvB0/Srtfl0eC50=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=YReODI2oV6AnNu3tZ0st6L7jGd2ApwYr7BHNByLNoeF4MpfT30rTs6y5G1V7M3rhm uPnITsrlHKcmw9aT/TsafbgJoxtFUaPT737OEI2RgXWhIyMIR5H1Pd44A8OplREHra poymD4Q3HI7qywObWFqoB025xuMSUrlJOxM7xYnU= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x82a.google.com (mail-qt1-x82a.google.com [IPv6:2607:f8b0:4864:20::82a]) by sourceware.org (Postfix) with ESMTPS id 250D1385041D for ; Tue, 23 Mar 2021 13:45:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 250D1385041D Received: by mail-qt1-x82a.google.com with SMTP id c6so14910302qtc.1 for ; Tue, 23 Mar 2021 06:45:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yZU9oglykHjh7jxvxSJZSuA2xa3swvB0/Srtfl0eC50=; b=AmLmHzOSiI6b8p/CRYk1E0Lx5JSmNc2SU3rwi+1Bw3ZCJPnA4x2cB9X4hbXYGbhewX Pt0/ESg4DbpEzNAFQrcanL22zOEgCsXJbdLAPJD8EqzePBPlWC2pvPUUqyRt0mqBe8Wg WOFxJNkY+o9C71FJ33UrvKmxuTKQFS/A45sVLGKqlJD1BduSHzYWnz/NGJ5QuOE0xQDQ FP8iDzE6NrP/b6jQEO9sm8mekO3eW2B0bk2Nf5dABrB/tlqLlBgrvrmUlzjZURi9LyM7 XU79LHh/b534EBTarhNTlY265XYAQJEqVqNLmnryOkSvup4hnzqKuUzaT2dPmaIJrJ2/ 8SNw== X-Gm-Message-State: AOAM530vCs179xTt7Yx9PjLgBh3le066AZt9UAf1i3khueLolBQjJ4KQ RKUeB6q1AHcMOcLCvMUbhUu8xDFeyV0oHw== X-Google-Smtp-Source: ABdhPJzA76WSAsH9f+Sy1NY3sGssyVEgK4oPQ7dqKSCKOefrGMwKzFGtXOLYLI/HIOtXY/EJgfQ/Kg== X-Received: by 2002:ac8:5a42:: with SMTP id o2mr4444070qta.191.1616507115110; Tue, 23 Mar 2021 06:45:15 -0700 (PDT) Received: from localhost.localdomain ([177.194.41.149]) by smtp.googlemail.com with ESMTPSA id z24sm12783384qkz.65.2021.03.23.06.45.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Mar 2021 06:45:14 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 11/15] posix: Add wait3 tests Date: Tue, 23 Mar 2021 10:44:54 -0300 Message-Id: <20210323134458.2563022-12-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210323134458.2563022-1-adhemerval.zanella@linaro.org> References: <20210323134458.2563022-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" The tst-wait4 is moved to common file and used for wait3 tests. Checked on x86_64-linux-gnu and i686-linux-gnu. Reviewed-by: Siddhesh Poyarekar --- posix/Makefile | 2 +- posix/tst-wait-skeleton.c | 209 ++++++++++++++++++++++++++++++++++++++ posix/tst-wait3.c | 22 ++++ posix/tst-wait4.c | 193 +---------------------------------- 4 files changed, 235 insertions(+), 191 deletions(-) create mode 100644 posix/tst-wait-skeleton.c create mode 100644 posix/tst-wait3.c diff --git a/posix/Makefile b/posix/Makefile index be0c72f0bb..64494557f1 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -84,7 +84,7 @@ tests := test-errno tstgetopt testfnm runtests runptests \ tst-nice tst-nanosleep tst-regex2 \ transbug tst-rxspencer tst-pcre tst-boost \ bug-ga1 tst-vfork1 tst-vfork2 tst-vfork3 \ - tst-waitid tst-wait4 \ + tst-waitid tst-wait4 tst-wait3 \ tst-getaddrinfo2 bug-glob2 bug-glob3 tst-sysconf \ tst-execvp1 tst-execvp2 tst-execlp1 tst-execlp2 \ tst-execv1 tst-execv2 tst-execl1 tst-execl2 \ diff --git a/posix/tst-wait-skeleton.c b/posix/tst-wait-skeleton.c new file mode 100644 index 0000000000..57022cb61c --- /dev/null +++ b/posix/tst-wait-skeleton.c @@ -0,0 +1,209 @@ +/* Test framework for wait3 and wait4. + Copyright (C) 2020-2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +static void +test_child (void) +{ + /* First thing, we stop ourselves. */ + raise (SIGSTOP); + + /* Hey, we got continued! */ + while (1) + pause (); +} + +#ifndef WEXITED +# define WEXITED 0 +# define WCONTINUED 0 +# define WSTOPPED WUNTRACED +#endif + +/* Set with only SIGCHLD on do_test_waitid. */ +static sigset_t chldset; + +#ifdef SA_SIGINFO +static void +sigchld (int signo, siginfo_t *info, void *ctx) +{ +} +#endif + +static void +check_sigchld (int code, int status, pid_t pid) +{ +#ifdef SA_SIGINFO + siginfo_t siginfo; + TEST_COMPARE (sigwaitinfo (&chldset, &siginfo), SIGCHLD); + + TEST_COMPARE (siginfo.si_signo, SIGCHLD); + TEST_COMPARE (siginfo.si_code, code); + TEST_COMPARE (siginfo.si_status, status); + TEST_COMPARE (siginfo.si_pid, pid); +#endif +} + +static int +do_test_wait (pid_t pid) +{ + /* Adding process_state_tracing_stop ('t') allows the test to work under + trace programs such as ptrace. */ + enum support_process_state stop_state = support_process_state_stopped + | support_process_state_tracing_stop; + + support_process_state_wait (pid, stop_state); + + check_sigchld (CLD_STOPPED, SIGSTOP, pid); + + pid_t ret; + int wstatus; + struct rusage rusage; + + ret = WAIT_CALL (pid, &wstatus, WUNTRACED|WCONTINUED|WNOHANG, NULL); + if (ret == -1 && errno == ENOTSUP) + FAIL_RET ("waitid WNOHANG on stopped: %m"); + TEST_COMPARE (ret, pid); + TEST_VERIFY (WIFSTOPPED (wstatus)); + + /* Issue again but with struct rusage input. */ + ret = WAIT_CALL (pid, &wstatus, WUNTRACED|WCONTINUED|WNOHANG, &rusage); + /* With WNOHANG and WUNTRACED, if the children has not changes its state + since previous call the expected result it 0. */ + TEST_COMPARE (ret, 0); + + /* Some sanity tests to check if 'wtatus' and 'rusage' possible + input values. */ + ret = WAIT_CALL (pid, NULL, WUNTRACED|WCONTINUED|WNOHANG, &rusage); + TEST_COMPARE (ret, 0); + ret = WAIT_CALL (pid, NULL, WUNTRACED|WCONTINUED|WNOHANG, NULL); + TEST_COMPARE (ret, 0); + + if (kill (pid, SIGCONT) != 0) + FAIL_RET ("kill (%d, SIGCONT): %m\n", pid); + + /* Wait for the child to have continued. */ + support_process_state_wait (pid, support_process_state_sleeping); + +#if WCONTINUED != 0 + check_sigchld (CLD_CONTINUED, SIGCONT, pid); + + ret = WAIT_CALL (pid, &wstatus, WCONTINUED|WNOHANG, NULL); + TEST_COMPARE (ret, pid); + TEST_VERIFY (WIFCONTINUED (wstatus)); + + /* Issue again but with struct rusage input. */ + ret = WAIT_CALL (pid, &wstatus, WUNTRACED|WCONTINUED|WNOHANG, &rusage); + /* With WNOHANG and WUNTRACED, if the children has not changes its state + since previous call the expected result it 0. */ + TEST_COMPARE (ret, 0); + + /* Now stop him again and test waitpid with WCONTINUED. */ + if (kill (pid, SIGSTOP) != 0) + FAIL_RET ("kill (%d, SIGSTOP): %m\n", pid); + + /* Wait the child stop. The waitid call below will block until it has + stopped, but if we are real quick and enter the waitid system call + before the SIGCHLD has been generated, then it will be discarded and + never delivered. */ + support_process_state_wait (pid, stop_state); + + ret = WAIT_CALL (pid, &wstatus, WUNTRACED|WNOHANG, &rusage); + TEST_COMPARE (ret, pid); + + check_sigchld (CLD_STOPPED, SIGSTOP, pid); + + if (kill (pid, SIGCONT) != 0) + FAIL_RET ("kill (%d, SIGCONT): %m\n", pid); + + /* Wait for the child to have continued. */ + support_process_state_wait (pid, support_process_state_sleeping); + + check_sigchld (CLD_CONTINUED, SIGCONT, pid); + + ret = WAIT_CALL (pid, &wstatus, WCONTINUED|WNOHANG, NULL); + TEST_COMPARE (ret, pid); + TEST_VERIFY (WIFCONTINUED (wstatus)); +#endif + + /* Die, child, die! */ + if (kill (pid, SIGKILL) != 0) + FAIL_RET ("kill (%d, SIGKILL): %m\n", pid); + + support_process_state_wait (pid, support_process_state_zombie); + + ret = WAIT_CALL (pid, &wstatus, 0, &rusage); + TEST_COMPARE (ret, pid); + TEST_VERIFY (WIFSIGNALED (wstatus)); + TEST_VERIFY (WTERMSIG (wstatus) == SIGKILL); + + check_sigchld (CLD_KILLED, SIGKILL, pid); + + return 0; +} + +static int +do_test (void) +{ +#ifdef SA_SIGINFO + { + struct sigaction sa; + sa.sa_flags = SA_SIGINFO | SA_RESTART; + sa.sa_sigaction = sigchld; + sigemptyset (&sa.sa_mask); + xsigaction (SIGCHLD, &sa, NULL); + } +#endif + + sigemptyset (&chldset); + sigaddset (&chldset, SIGCHLD); + + /* The SIGCHLD shall has blocked at the time of the call to sigwait; + otherwise, the behavior is undefined. */ + sigprocmask (SIG_BLOCK, &chldset, NULL); + + pid_t pid = xfork (); + if (pid == 0) + { + test_child (); + _exit (127); + } + + do_test_wait (pid); + + xsignal (SIGCHLD, SIG_IGN); + kill (pid, SIGKILL); /* Make sure it's dead if we bailed early. */ + + return 0; +} + +#include diff --git a/posix/tst-wait3.c b/posix/tst-wait3.c new file mode 100644 index 0000000000..27411c04e4 --- /dev/null +++ b/posix/tst-wait3.c @@ -0,0 +1,22 @@ +/* Tests for wait3. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#define WAIT_CALL(__pid, __wstatus, __options, __rusage) \ + wait3 (__wstatus, __options, __rusage) + +#include "tst-wait-skeleton.c" diff --git a/posix/tst-wait4.c b/posix/tst-wait4.c index e4ce79d2cd..b7532d3891 100644 --- a/posix/tst-wait4.c +++ b/posix/tst-wait4.c @@ -16,194 +16,7 @@ License along with the GNU C Library; if not, see . */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#define WAIT_CALL(__pid, __wstatus, __options, __rusage) \ + wait4 (__pid, __wstatus, __options, __rusage) -#include -#include -#include -#include - -static void -test_child (void) -{ - /* First thing, we stop ourselves. */ - raise (SIGSTOP); - - /* Hey, we got continued! */ - while (1) - pause (); -} - -#ifndef WEXITED -# define WEXITED 0 -# define WCONTINUED 0 -# define WSTOPPED WUNTRACED -#endif - -/* Set with only SIGCHLD on do_test_waitid. */ -static sigset_t chldset; - -#ifdef SA_SIGINFO -static void -sigchld (int signo, siginfo_t *info, void *ctx) -{ -} -#endif - -static void -check_sigchld (int code, int status, pid_t pid) -{ -#ifdef SA_SIGINFO - siginfo_t siginfo; - TEST_COMPARE (sigwaitinfo (&chldset, &siginfo), SIGCHLD); - - TEST_COMPARE (siginfo.si_signo, SIGCHLD); - TEST_COMPARE (siginfo.si_code, code); - TEST_COMPARE (siginfo.si_status, status); - TEST_COMPARE (siginfo.si_pid, pid); -#endif -} - -static int -do_test_wait4 (pid_t pid) -{ - /* Adding process_state_tracing_stop ('t') allows the test to work under - trace programs such as ptrace. */ - enum support_process_state stop_state = support_process_state_stopped - | support_process_state_tracing_stop; - - support_process_state_wait (pid, stop_state); - - check_sigchld (CLD_STOPPED, SIGSTOP, pid); - - pid_t ret; - int wstatus; - struct rusage rusage; - - ret = wait4 (pid, &wstatus, WUNTRACED|WCONTINUED|WNOHANG, NULL); - if (ret == -1 && errno == ENOTSUP) - FAIL_RET ("waitid WNOHANG on stopped: %m"); - TEST_COMPARE (ret, pid); - TEST_VERIFY (WIFSTOPPED (wstatus)); - - /* Issue again but with struct rusage input. */ - ret = wait4 (pid, &wstatus, WUNTRACED|WCONTINUED|WNOHANG, &rusage); - /* With WNOHANG and WUNTRACED, if the children has not changes its state - since previous call the expected result it 0. */ - TEST_COMPARE (ret, 0); - - /* Some sanity tests to check if 'wtatus' and 'rusage' possible - input values. */ - ret = wait4 (pid, NULL, WUNTRACED|WCONTINUED|WNOHANG, &rusage); - TEST_COMPARE (ret, 0); - ret = wait4 (pid, NULL, WUNTRACED|WCONTINUED|WNOHANG, NULL); - TEST_COMPARE (ret, 0); - - if (kill (pid, SIGCONT) != 0) - FAIL_RET ("kill (%d, SIGCONT): %m\n", pid); - - /* Wait for the child to have continued. */ - support_process_state_wait (pid, support_process_state_sleeping); - -#if WCONTINUED != 0 - check_sigchld (CLD_CONTINUED, SIGCONT, pid); - - ret = wait4 (pid, &wstatus, WCONTINUED|WNOHANG, NULL); - TEST_COMPARE (ret, pid); - TEST_VERIFY (WIFCONTINUED (wstatus)); - - /* Issue again but with struct rusage input. */ - ret = wait4 (pid, &wstatus, WUNTRACED|WCONTINUED|WNOHANG, &rusage); - /* With WNOHANG and WUNTRACED, if the children has not changes its state - since previous call the expected result it 0. */ - TEST_COMPARE (ret, 0); - - /* Now stop him again and test waitpid with WCONTINUED. */ - if (kill (pid, SIGSTOP) != 0) - FAIL_RET ("kill (%d, SIGSTOP): %m\n", pid); - - /* Wait the child stop. The waitid call below will block until it has - stopped, but if we are real quick and enter the waitid system call - before the SIGCHLD has been generated, then it will be discarded and - never delivered. */ - support_process_state_wait (pid, stop_state); - - ret = wait4 (pid, &wstatus, WUNTRACED|WNOHANG, &rusage); - TEST_COMPARE (ret, pid); - - check_sigchld (CLD_STOPPED, SIGSTOP, pid); - - if (kill (pid, SIGCONT) != 0) - FAIL_RET ("kill (%d, SIGCONT): %m\n", pid); - - /* Wait for the child to have continued. */ - support_process_state_wait (pid, support_process_state_sleeping); - - check_sigchld (CLD_CONTINUED, SIGCONT, pid); - - ret = wait4 (pid, &wstatus, WCONTINUED|WNOHANG, NULL); - TEST_COMPARE (ret, pid); - TEST_VERIFY (WIFCONTINUED (wstatus)); -#endif - - /* Die, child, die! */ - if (kill (pid, SIGKILL) != 0) - FAIL_RET ("kill (%d, SIGKILL): %m\n", pid); - - support_process_state_wait (pid, support_process_state_zombie); - - ret = wait4 (pid, &wstatus, 0, &rusage); - TEST_COMPARE (ret, pid); - TEST_VERIFY (WIFSIGNALED (wstatus)); - TEST_VERIFY (WTERMSIG (wstatus) == SIGKILL); - - check_sigchld (CLD_KILLED, SIGKILL, pid); - - return 0; -} - -static int -do_test (void) -{ -#ifdef SA_SIGINFO - { - struct sigaction sa; - sa.sa_flags = SA_SIGINFO | SA_RESTART; - sa.sa_sigaction = sigchld; - sigemptyset (&sa.sa_mask); - xsigaction (SIGCHLD, &sa, NULL); - } -#endif - - sigemptyset (&chldset); - sigaddset (&chldset, SIGCHLD); - - /* The SIGCHLD shall has blocked at the time of the call to sigwait; - otherwise, the behavior is undefined. */ - sigprocmask (SIG_BLOCK, &chldset, NULL); - - pid_t pid = xfork (); - if (pid == 0) - { - test_child (); - _exit (127); - } - - do_test_wait4 (pid); - - xsignal (SIGCHLD, SIG_IGN); - kill (pid, SIGKILL); /* Make sure it's dead if we bailed early. */ - - return 0; -} - -#include +#include "tst-wait-skeleton.c"