From patchwork Sat Dec 29 22:27:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 10745031 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D6F3B13BF for ; Sat, 29 Dec 2018 22:29:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C64D8209CC for ; Sat, 29 Dec 2018 22:29:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B786626242; Sat, 29 Dec 2018 22:29:06 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 516BC21F61 for ; Sat, 29 Dec 2018 22:29:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727517AbeL2W25 (ORCPT ); Sat, 29 Dec 2018 17:28:57 -0500 Received: from mail-ed1-f67.google.com ([209.85.208.67]:34085 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727485AbeL2W25 (ORCPT ); Sat, 29 Dec 2018 17:28:57 -0500 Received: by mail-ed1-f67.google.com with SMTP id b3so20212589ede.1 for ; Sat, 29 Dec 2018 14:28:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brauner.io; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ed35wjoDSEptb5WxV+67NIlIHh2DDBucYK0RteGlU4g=; b=I1P4s4IZ9hlZV8M8DqmirGflrzSAdHimEvmIosQ9FVKyveJBbIwQFFJkQOW+ii9god RVc5vS7e8mUuDTZ79zBP5eKHHXc4PxKVU8x2tdM7YEKm5SFw13dZYLgmcBxCVLCh9KeP AsAQTQLU/LgGB+Vt/QdwBfxqPUAa/j0b1g8l8/IBQk8KUH+iiPIibpjfp/ENtc1QalUi JBiHsYGaytnOKnQzmmvmGf72fgwHQr6Ps6Uf5mZlPJ6mMalFHqD9fT05CK02DppQTO8x lLh/M6QcnrWlhZ3guzFiWsovSyBcn/dhNO1Y711uQtvDIa/3hyqbpBzwM5M0rQlNpG18 u6/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Ed35wjoDSEptb5WxV+67NIlIHh2DDBucYK0RteGlU4g=; b=rB6f0yJAO97T96BMMxgXc+L7Mmn5eqwG17sJ9QNwwGM2pVmD9k45Bukww4bD3jF2c7 4MZXDIwul33dvtNs0282afswpR9bzjccC/o6y3BYrA0nquCKmeJvolO+MQssL2ncPqiZ 3ykuIQ9YHCiseTmTKk8iw6aYmAloRuDzU3eqa+Q0PceGDgMtUU/NOGVIBjWp9Q5klf1z hMa5GO0+NBz7NRbacvJnFNHmpFBLfdSWUug0d9lB2C2awQpBDSW92hlYA3JdTVdNYr43 5waKLXpltutehaxA9bvJ/Cm6xmfgmpkYov93yq/lGTHEs+RIfjGRn4zmzmVeZ8Zs2SqA doiQ== X-Gm-Message-State: AA+aEWbJH7gfbPM6fey71+O8PERNcD/ro+YUZ8S0VDzmjAr5dRFWjtNd FWjchhm422P+z9tslBHupJ5amw== X-Google-Smtp-Source: AFSGD/UpF+HRfQocb4zYXFJ8TvC6qW2dXybnP8Nqik6mMpixSCsQEdMmGRx6ssorJSbrm97EcmYdmg== X-Received: by 2002:a50:9106:: with SMTP id e6mr25773315eda.148.1546122534893; Sat, 29 Dec 2018 14:28:54 -0800 (PST) Received: from localhost.localdomain ([2a02:8109:b6c0:d6c:c95d:2ebe:c81e:96a0]) by smtp.gmail.com with ESMTPSA id s5-v6sm8212729eji.25.2018.12.29.14.28.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 29 Dec 2018 14:28:54 -0800 (PST) From: Christian Brauner To: linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, luto@kernel.org, arnd@arndb.de, serge@hallyn.com, keescook@chromium.org, akpm@linux-foundation.org Cc: jannh@google.com, oleg@redhat.com, cyphar@cyphar.com, viro@zeniv.linux.org.uk, linux-fsdevel@vger.kernel.org, dancol@google.com, timmurray@google.com, fweimer@redhat.com, tglx@linutronix.de, x86@kernel.org, ebiederm@xmission.com, Christian Brauner Subject: [PATCH v6 2/2] selftests: add tests for pidfd_send_signal() Date: Sat, 29 Dec 2018 23:27:56 +0100 Message-Id: <20181229222756.30027-2-christian@brauner.io> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181229222756.30027-1-christian@brauner.io> References: <20181229222756.30027-1-christian@brauner.io> MIME-Version: 1.0 X-Patchwork-Bot: notify Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP As suggested by Andrew Morton in [1] add selftests for the new sys_pidfd_send_signal() syscall. This tests whether we can send a signal to an existing process and whether sending a signal to a process that has already exited fails with ESRCH. [1]: https://lore.kernel.org/lkml/20181228152012.dbf0508c2508138efc5f2bbe@linux-foundation.org/ Cc: Arnd Bergmann Cc: "Eric W. Biederman" Cc: Kees Cook Cc: Serge Hallyn Cc: Jann Horn Cc: Andy Lutomirsky Cc: Andrew Morton Cc: Oleg Nesterov Cc: Aleksa Sarai Cc: Al Viro Cc: Florian Weimer Signed-off-by: Christian Brauner Acked-by: Serge Hallyn --- /* Changelog */ v6: - patch introduced v5..v0: - patch not present --- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/pidfd/Makefile | 6 + tools/testing/selftests/pidfd/pidfd_test.c | 130 +++++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 tools/testing/selftests/pidfd/Makefile create mode 100644 tools/testing/selftests/pidfd/pidfd_test.c diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index 24b9934fb269..63b0d8a0ebf7 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -27,6 +27,7 @@ TARGETS += net TARGETS += netfilter TARGETS += networking/timestamping TARGETS += nsfs +TARGETS += pidfd TARGETS += powerpc TARGETS += proc TARGETS += pstore diff --git a/tools/testing/selftests/pidfd/Makefile b/tools/testing/selftests/pidfd/Makefile new file mode 100644 index 000000000000..deaf8073bc06 --- /dev/null +++ b/tools/testing/selftests/pidfd/Makefile @@ -0,0 +1,6 @@ +CFLAGS += -g -I../../../../usr/include/ + +TEST_GEN_PROGS := pidfd_test + +include ../lib.mk + diff --git a/tools/testing/selftests/pidfd/pidfd_test.c b/tools/testing/selftests/pidfd/pidfd_test.c new file mode 100644 index 000000000000..edcd59979b10 --- /dev/null +++ b/tools/testing/selftests/pidfd/pidfd_test.c @@ -0,0 +1,130 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +static inline int sys_pidfd_send_signal(int pidfd, int sig, siginfo_t *info, + unsigned int flags) +{ + return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags); +} + +static int signal_received; + +static void do_exit_success(int sig) +{ + signal_received = 1; +} + +/* + * Straightforward test to see whether pidfd_send_signal() works is to send + * a signal to ourselves. + */ +static int test_pidfd_send_signal_simple_success(void) +{ + int pidfd, ret; + const char *test_name = "pidfd_send_signal send SIGUSR1"; + + pidfd = open("/proc/self", O_DIRECTORY | O_CLOEXEC); + if (pidfd < 0) + ksft_exit_fail_msg( + "%s test: Failed to open process file descriptor\n", + test_name); + + signal(SIGUSR1, do_exit_success); + + ret = sys_pidfd_send_signal(pidfd, SIGUSR1, NULL, 0); + close(pidfd); + if (ret < 0) + ksft_exit_fail_msg("%s test: Failed to send signal\n", + test_name); + + if (signal_received != 1) + ksft_exit_fail_msg("%s test: Failed to receive signal\n", + test_name); + + signal_received = 0; + ksft_test_result_pass("%s test: Sent signal\n", test_name); + return 0; +} + +static void wait_for_pid(pid_t pid) +{ + int status, ret; + +again: + ret = waitpid(pid, &status, 0); + if (ret == -1) { + if (errno == EINTR) + goto again; + + return; + } + + if (ret != pid) + goto again; +} + +static int test_pidfd_send_signal_exited_fail(void) +{ + int pidfd, ret, saved_errno; + char buf[256]; + pid_t pid; + const char *test_name = "pidfd_send_signal signal exited process"; + + pid = fork(); + if (pid < 0) + ksft_exit_fail_msg("%s test: Failed to create new process\n", + test_name); + + if (pid == 0) + _exit(EXIT_SUCCESS); + + snprintf(buf, sizeof(buf), "/proc/%d", pid); + + pidfd = open(buf, O_DIRECTORY | O_CLOEXEC); + + wait_for_pid(pid); + + if (pidfd < 0) + ksft_exit_fail_msg( + "%s test: Failed to open process file descriptor\n", + test_name); + + ret = sys_pidfd_send_signal(pidfd, 0, NULL, 0); + saved_errno = errno; + close(pidfd); + if (ret == 0) + ksft_exit_fail_msg( + "%s test: Managed to send signal to process even though it should have failed\n", + test_name); + + if (saved_errno != ESRCH) + ksft_exit_fail_msg( + "%s test: Expected to receive ESRCH as errno value but received %d instead\n", + test_name, saved_errno); + + ksft_test_result_pass("%s test: Failed to send signal as expected\n", + test_name); + return 0; +} + +int main(int argc, char **argv) +{ + ksft_print_header(); + + test_pidfd_send_signal_simple_success(); + test_pidfd_send_signal_exited_fail(); + + return ksft_exit_pass(); +}