Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp10109967imu; Sun, 30 Dec 2018 13:03:53 -0800 (PST) X-Google-Smtp-Source: ALg8bN7dMgp1DhMhjBOHVUk2sU8FcuN7a+RvKhLkAy9sm9i3MYwKDORJ5GfTxVRNCubX3k7BDQ8e X-Received: by 2002:a63:3602:: with SMTP id d2mr5452530pga.404.1546203833294; Sun, 30 Dec 2018 13:03:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546203833; cv=none; d=google.com; s=arc-20160816; b=nxyNvHyqjvRzXNhXYbVgLjdG4eYt3+fvgkJ1JCCftJTrd7hXM+ENhQs9x8CFaKHMR4 930BvDu/PtY5M9CqGRoNvVfNXV9V4MZPrSwCD4dQXVQP60Qw0hGsSBLRbast5mIee5/Z DygGQCiTm/b4XHqsdPUsv2h0vw/k9fv+YusFiXP/IjJOYejaR51P/FworUu4jfnGOHXH VTKnN1ne5TNgeipV5IMCamJNwazD4LRoAOXxziEjLSrCfoDOn+Zp4myP29aQHvYySx6X VvfJXC++AJvDq2ALxNIxbEJ9d2pkYhtRdiC17cC1Ufi8PgxnQZ1yssBy87nifMivTfkF 9G+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=XtTsDoWNQkUV/tl71wjeyVHpbihxdhvO9iAPnhbhu3A=; b=W2MVk3qFbyrg4ynysczqerKEsBJQhamjsLTY9duzXU4O1R37lZvCeD1OShZlkzz0Dw 2GbWtgE5gqAyuR2/mkC19QDtA50bCm1ALZrHkV5wbM9gBdYBU1STFzqQUwR65+/RJun0 +Q1xPtQ1duqbxX8o89EpWlpxjLQ7lw8eG3doQqXdz5xFk6JExUFIMu/sb6mGJPZQasUg /tXRi2DSA8zH6qzCkfaAS5nFocsIJMS9XuOsiB+MO71rdcueWi0Ffp7+61kehqYa36Kq 7/6Xg1cLpiZL3GN7JrpmSe0r6NFv4hUwPQ82/HALoSKJDmmGXIiNbzsV1HQhBjouQq6s viyw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id cc16si33632488plb.377.2018.12.30.13.03.37; Sun, 30 Dec 2018 13:03:53 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726798AbeL3VCt (ORCPT + 99 others); Sun, 30 Dec 2018 16:02:49 -0500 Received: from mail.hallyn.com ([178.63.66.53]:42588 "EHLO mail.hallyn.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726007AbeL3VCt (ORCPT ); Sun, 30 Dec 2018 16:02:49 -0500 Received: by mail.hallyn.com (Postfix, from userid 1001) id 0F9B5D55; Sun, 30 Dec 2018 15:02:45 -0600 (CST) Date: Sun, 30 Dec 2018 15:02:45 -0600 From: "Serge E. Hallyn" To: Christian Brauner Cc: 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, 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 Subject: Re: [PATCH v6 2/2] selftests: add tests for pidfd_send_signal() Message-ID: <20181230210245.GA30252@mail.hallyn.com> References: <20181229222756.30027-1-christian@brauner.io> <20181229222756.30027-2-christian@brauner.io> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20181229222756.30027-2-christian@brauner.io> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Dec 29, 2018 at 11:27:56PM +0100, Christian Brauner wrote: > 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 Acked-by: Serge Hallyn Not saying you need to do this, but it would be neat if you could test sending to a pid which has been recycled :) > 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 > --- > /* 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(); > +} > -- > 2.19.1