From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 38782C43381 for ; Tue, 26 Mar 2019 15:55:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id DF73A20863 for ; Tue, 26 Mar 2019 15:55:39 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=brauner.io header.i=@brauner.io header.b="cTMTa//P" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732032AbfCZPzi (ORCPT ); Tue, 26 Mar 2019 11:55:38 -0400 Received: from mail-ed1-f65.google.com ([209.85.208.65]:39643 "EHLO mail-ed1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731991AbfCZPzg (ORCPT ); Tue, 26 Mar 2019 11:55:36 -0400 Received: by mail-ed1-f65.google.com with SMTP id p20so10743863eds.6 for ; Tue, 26 Mar 2019 08:55:34 -0700 (PDT) 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=fovWDFpIBIcv9kU9lPWKXGfBIevZ1NKhpTpFZ7NKOLM=; b=cTMTa//P3OeSJbW3N9pNHDv1F1jmSf1tgc7syhLXIJBuXlFr9/Ul8eJL8bVFoQDu7z IAliLBkQJilj5fsFvulFl/yxF8hAkFAEakxnQhxzly8H49TO2dL1Il0ZEH6aXpoGHbFx A2EM+GfchbjZuVXoEq31keA7XpRFkFWQWu0vVWJGyUfE0H/IsCPfyL5v8V3Ox1343/6z wkY0ZWHU0+WTVZT+OMLjftvY9KCeRujlauJdISjrIJy4NK7+lS1jqKkZco9mqZOiLdwK ouTXILKYAtmFQLYppFET+q/Coi2qYU4aUOquXJHd/9ALxwb8dcaVezgPfbJOdB4zS/zO xY2w== 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=fovWDFpIBIcv9kU9lPWKXGfBIevZ1NKhpTpFZ7NKOLM=; b=Do6ERtITXK1sfWKZofeA/tgQTEpCZnZSvQKIOIlhka7OPi/m/1XzJNPGteMDXJB6Ik 47KpewxMnKmVPatjuwmIuU16Yd6COuGgAVGUeOq9jh28IXrz1QsscVJllk+EsKA93xE7 p8m2NmzIQrWSVdCpEx/HbcKy8aK7SGKs+WhJVYJLYnUasAqaVVfjhBmWboaG4IaDqAFL k6tnWFHk24XZHsfHMgBAoEiRkZW2oKaaeoAGoQy02ggc87BChK327hGlbVImJX8Vk7v3 8ZSfIU3k6dmxLQIqS+zNSPtzLaxZoVMayEr9joGBg/kWpC3IXAcI8wCr40VHXLJOt+hY Ep6A== X-Gm-Message-State: APjAAAV4xQQR/fF8/Nk0nTtmiagqal2R1DUGrnrPTLevAs3AsVcOTN78 +YyBOJ+aJsxYSG1sWIJ2AqVojw== X-Google-Smtp-Source: APXvYqwgD2ICEGL1S94S1cvT/MsUGoQ3A3wWTko00oZCeTdNaqBRxISVZZtPcwswq1ZhmfejNM623Q== X-Received: by 2002:a17:906:d69:: with SMTP id s9mr17747299ejh.205.1553615734116; Tue, 26 Mar 2019 08:55:34 -0700 (PDT) Received: from localhost.localdomain (x59cc895e.dyn.telefonica.de. [89.204.137.94]) by smtp.gmail.com with ESMTPSA id k32sm6651057ede.97.2019.03.26.08.55.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 26 Mar 2019 08:55:33 -0700 (PDT) From: Christian Brauner To: jannh@google.com, khlebnikov@yandex-team.ru, luto@kernel.org, dhowells@redhat.com, serge@hallyn.com, ebiederm@xmission.com, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org Cc: arnd@arndb.de, keescook@chromium.org, adobriyan@gmail.com, tglx@linutronix.de, mtk.manpages@gmail.com, bl0pbl33p@gmail.com, ldv@altlinux.org, akpm@linux-foundation.org, oleg@redhat.com, nagarathnam.muthusamy@oracle.com, cyphar@cyphar.com, viro@zeniv.linux.org.uk, joel@joelfernandes.org, dancol@google.com, Christian Brauner Subject: [PATCH v1 4/4] tests: add pidctl() tests Date: Tue, 26 Mar 2019 16:55:13 +0100 Message-Id: <20190326155513.26964-5-christian@brauner.io> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190326155513.26964-1-christian@brauner.io> References: <20190326155513.26964-1-christian@brauner.io> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This adds test cases for all three subcommands and verifies that they succeed and fail as expected. Additionally, the tests verify that pidctl() pidfds are correctly useable with pidfd_send_signal(). Signed-off-by: Christian Brauner Cc: Arnd Bergmann Cc: "Eric W. Biederman" Cc: Kees Cook Cc: Alexey Dobriyan Cc: Thomas Gleixner Cc: Jann Horn Cc: "Michael Kerrisk (man-pages)" Cc: Konstantin Khlebnikov Cc: Jonathan Kowalski Cc: "Dmitry V. Levin" Cc: Andy Lutomirsky Cc: Andrew Morton Cc: Oleg Nesterov Cc: Nagarathnam Muthusamy Cc: Aleksa Sarai Cc: Al Viro --- /* changelog */ v1: - Christian Brauner : - adapt to changing pidfds to CLOEXEC by default --- tools/testing/selftests/pidfd/Makefile | 2 +- tools/testing/selftests/pidfd/pidctl_test.c | 537 ++++++++++++++++++++ 2 files changed, 538 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/pidfd/pidctl_test.c diff --git a/tools/testing/selftests/pidfd/Makefile b/tools/testing/selftests/pidfd/Makefile index deaf8073bc06..29dfa29b3afa 100644 --- a/tools/testing/selftests/pidfd/Makefile +++ b/tools/testing/selftests/pidfd/Makefile @@ -1,6 +1,6 @@ CFLAGS += -g -I../../../../usr/include/ -TEST_GEN_PROGS := pidfd_test +TEST_GEN_PROGS := pidfd_test pidctl_test include ../lib.mk diff --git a/tools/testing/selftests/pidfd/pidctl_test.c b/tools/testing/selftests/pidfd/pidctl_test.c new file mode 100644 index 000000000000..a39d3cd81089 --- /dev/null +++ b/tools/testing/selftests/pidfd/pidctl_test.c @@ -0,0 +1,537 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +static int parent_pidns_fd = -1; +static pid_t parent_pidns_pid = 0; + +static int child_pidns_fd = -1; +static pid_t child_pidns_pid = 0; + +static int cousin_pidns_fd = -1; +static pid_t cousin_pidns_pid = 0; + +static bool pidns_supported = false; + +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 inline int sys_pidctl(unsigned int cmd, pid_t pid, int source, + int target, unsigned int flags) +{ + return syscall(__NR_pidctl, cmd, pid, source, target, flags); +} + +struct cr_clone_arg { + char stack[128] __attribute__((aligned(16))); + char stack_ptr[0]; +}; + +static int child_pidns_creator(void *args) +{ + (void)prctl(PR_SET_PDEATHSIG, SIGKILL); + while (1) + sleep(5); + + exit(0); +} + +static int prepare_pid_namespaces(void) +{ + char path[512]; + struct cr_clone_arg ca; + pid_t pid; + + parent_pidns_fd = open("/proc/self/ns/pid", O_RDONLY | O_CLOEXEC); + if (parent_pidns_fd < 0) { + ksft_print_msg("failed to open current pid namespace"); + return -1; + } + parent_pidns_pid = getpid(); + + pid = clone(child_pidns_creator, ca.stack_ptr, CLONE_NEWPID | SIGCHLD, + NULL); + if (pid < 0) { + ksft_print_msg("failed to clone child-pidns process in new pid namespace"); + return -1; + } + + snprintf(path, sizeof(path), "/proc/%d/ns/pid", pid); + + child_pidns_fd = open(path, O_RDONLY | O_CLOEXEC); + if (child_pidns_fd < 0) { + ksft_print_msg("failed to open pid namespace"); + return -1; + } + child_pidns_pid = pid; + + pid = clone(child_pidns_creator, ca.stack_ptr, CLONE_NEWPID | SIGCHLD, + NULL); + if (pid < 0) { + ksft_print_msg("failed to clone cousin-pidns process in new pid namespace"); + return -1; + } + + snprintf(path, sizeof(path), "/proc/%d/ns/pid", pid); + + cousin_pidns_fd = open(path, O_RDONLY | O_CLOEXEC); + if (cousin_pidns_fd < 0) { + ksft_print_msg("failed to open cousin pid namespace"); + return -1; + } + cousin_pidns_pid = pid; + + return 0; +} + +static int test_pidcmd_query_pid(void) +{ + const char *test_name = "pidctl PIDCMD_QUERY_PID"; + pid_t pid, self; + int parent_pidns_fd2; + + self = getpid(); + + pid = sys_pidctl(PIDCMD_QUERY_PID, self, -1, -1, 1); + if (pid >= 0) { + ksft_print_msg("%s test %d: managed to pass invalid flag\n", + test_name, ksft_test_num()); + return -1; + } + + pid = sys_pidctl(PIDCMD_QUERY_PID, self, -1, -1, 0); + if (!pid || (pid != self)) { + ksft_print_msg("%s test %d: argument pid %d, translated pid %d\n", + test_name, ksft_test_num(), self, pid); + return -1; + } + ksft_inc_pass_cnt(); + + if (!pidns_supported) + goto out; + + parent_pidns_fd2 = open("/proc/self/ns/pid", O_RDONLY | O_CLOEXEC); + if (parent_pidns_fd2 < 0) { + ksft_print_msg("%s test %d: Failed to open current pid namespace\n", + test_name, ksft_test_num()); + return -1; + } + + pid = sys_pidctl(PIDCMD_QUERY_PID, self, parent_pidns_fd, + parent_pidns_fd2, 0); + if (!pid || (pid != self)) { + ksft_print_msg("%s test %d: argument pid %d, translated pid %d\n", + test_name, ksft_test_num(), self, pid); + close(parent_pidns_fd2); + return -1; + } + ksft_inc_pass_cnt(); + + pid = sys_pidctl(PIDCMD_QUERY_PID, self, -1, parent_pidns_fd2, 0); + if (!pid || (pid != self)) { + ksft_print_msg("%s test %d: argument pid %d, translated pid %d\n", + test_name, ksft_test_num(), self, pid); + close(parent_pidns_fd2); + return -1; + } + ksft_inc_pass_cnt(); + + pid = sys_pidctl(PIDCMD_QUERY_PID, self, parent_pidns_fd, -1, 0); + if (!pid || (pid != self)) { + ksft_print_msg("%s test %d: argument pid %d, translated pid %d\n", + test_name, ksft_test_num(), self, pid); + close(parent_pidns_fd2); + return -1; + } + ksft_inc_pass_cnt(); + + close(parent_pidns_fd2); + + pid = sys_pidctl(PIDCMD_QUERY_PID, self, parent_pidns_fd, + child_pidns_fd, 0); + if (pid >= 0 || ((pid < 0) && (errno != ENOENT))) { + ksft_print_msg("%s test %d: argument pid %d, translated pid %d\n", + test_name, ksft_test_num(), self, pid); + return -1; + } + ksft_inc_pass_cnt(); + + pid = sys_pidctl(PIDCMD_QUERY_PID, self, child_pidns_fd, + parent_pidns_fd, 0); + if (pid >= 0 || ((pid < 0) && (errno != ESRCH))) { + ksft_print_msg("%s test %d: argument pid %d, translated pid %d\n", + test_name, ksft_test_num(), self, pid); + return -1; + } + ksft_inc_pass_cnt(); + + pid = sys_pidctl(PIDCMD_QUERY_PID, child_pidns_pid, parent_pidns_fd, + child_pidns_fd, 0); + if (pid != 1) { + ksft_print_msg("%s test %d: argument pid %d, translated pid %d\n", + test_name, ksft_test_num(), child_pidns_pid, pid); + return -1; + } + ksft_inc_pass_cnt(); + + pid = sys_pidctl(PIDCMD_QUERY_PID, 1, child_pidns_fd, parent_pidns_fd, + 0); + if (pid != child_pidns_pid) { + ksft_print_msg("%s test %d: argument pid %d, translated pid %d\n", + test_name, ksft_test_num(), 1, pid); + return -1; + } + ksft_inc_pass_cnt(); + + pid = sys_pidctl(PIDCMD_QUERY_PID, 1, child_pidns_fd, cousin_pidns_fd, 0); + if (pid >= 0 || ((pid < 0) && (errno != ENOENT))) { + ksft_print_msg("%s test %d: argument pid %d, translated pid %d\n", + test_name, ksft_test_num(), 1, pid); + return -1; + } + ksft_inc_pass_cnt(); + + pid = sys_pidctl(PIDCMD_QUERY_PID, cousin_pidns_pid, child_pidns_fd, + cousin_pidns_fd, 0); + if (pid >= 0 || ((pid < 0) && (errno != ESRCH))) { + ksft_print_msg("%s test %d: argument pid %d, translated pid %d\n", + test_name, ksft_test_num(), cousin_pidns_pid, pid); + return -1; + } + ksft_inc_pass_cnt(); + +out: + ksft_test_result_pass("%s test: passed\n", test_name); + return 0; +} + +static int test_pidcmd_query_pidns(void) +{ + const char *test_name = "pidctl PIDCMD_QUERY_PIDNS"; + int parent_pidns_fd2; + int query; + + query = sys_pidctl(PIDCMD_QUERY_PIDNS, 0, -1, -1, 1); + if (query >= 0) { + ksft_print_msg("%s test %d: managed to pass invalid flag\n", + test_name, ksft_test_num()); + return -1; + } + ksft_inc_pass_cnt(); + + query = sys_pidctl(PIDCMD_QUERY_PIDNS, 1234, -1, -1, 0); + if (query >= 0) + ksft_print_msg("%s test %d: managed to pass invalid pid argument\n", + test_name, ksft_test_num()); + ksft_inc_pass_cnt(); + + if (!pidns_supported) + goto out; + + parent_pidns_fd2 = open("/proc/self/ns/pid", O_RDONLY | O_CLOEXEC); + if (parent_pidns_fd2 < 0) { + ksft_print_msg("%s test %d: Failed to open second pid namespace file descriptor\n", + test_name, ksft_test_num()); + return -1; + } + + query = sys_pidctl(PIDCMD_QUERY_PIDNS, 0, parent_pidns_fd, + parent_pidns_fd2, 0); + close(parent_pidns_fd2); + if (query != PIDNS_EQUAL) { + ksft_print_msg("%s test %d: failed to detect that pid namespaces are identical %d\n", + test_name, ksft_test_num(), query); + return -1; + } + ksft_inc_pass_cnt(); + + query = sys_pidctl(PIDCMD_QUERY_PIDNS, 0, parent_pidns_fd, + child_pidns_fd, 0); + if (query != PIDNS_SOURCE_IS_ANCESTOR) { + ksft_print_msg("%s test %d: failed to detect that source pid namespace is ancestor of target pid namespace %d\n", + test_name, ksft_test_num(), query); + return -1; + } + ksft_inc_pass_cnt(); + + query = sys_pidctl(PIDCMD_QUERY_PIDNS, 0, child_pidns_fd, + parent_pidns_fd, 0); + if (query != PIDNS_TARGET_IS_ANCESTOR) { + ksft_print_msg("%s test %d: failed to detect that target pid namespace is ancestor of source pid namespace %d\n", + test_name, ksft_test_num(), query); + return -1; + } + ksft_inc_pass_cnt(); + + query = sys_pidctl(PIDCMD_QUERY_PIDNS, 0, child_pidns_fd, + cousin_pidns_fd, 0); + if (query != PIDNS_UNRELATED) { + ksft_print_msg("%s test %d: failed to detect that pid namespace are not related %d\n", + test_name, ksft_test_num(), query); + return -1; + } + ksft_inc_pass_cnt(); + + query = sys_pidctl(PIDCMD_QUERY_PIDNS, 0, child_pidns_fd, + cousin_pidns_fd, 0); + if (query != PIDNS_UNRELATED) { + ksft_print_msg("%s test %d: failed to detect that pid namespace are not related %d\n", + test_name, ksft_test_num(), query); + return -1; + } + ksft_inc_pass_cnt(); + +out: + ksft_test_result_pass("%s test: passed\n", test_name); + return 0; +} + +static int test_pidcmd_get_pidfd(void) +{ + const char *test_name = "pidctl PIDCMD_GET_PIDFD"; + pid_t self; + int pidfd, parent_pidns_fd2; + + self = getpid(); + + pidfd = sys_pidctl(PIDCMD_GET_PIDFD, self, -1, -1, 0); + if (pidfd < 0) { + ksft_print_msg("%s test %d: failed to pass valid flag\n", + test_name, ksft_test_num()); + return -1; + } + close(pidfd); + ksft_inc_pass_cnt(); + + if (!pidns_supported) + goto out; + + parent_pidns_fd2 = open("/proc/self/ns/pid", O_RDONLY | O_CLOEXEC); + if (parent_pidns_fd2 < 0) { + ksft_print_msg("%s test %d: Failed to open current pid namespace\n", + test_name, ksft_test_num()); + return -1; + } + + pidfd = sys_pidctl(PIDCMD_GET_PIDFD, self, parent_pidns_fd, + parent_pidns_fd2, 0); + if (pidfd < 0) { + ksft_print_msg("%s test %d: failed to retrieve pidfd\n", + test_name, ksft_test_num()); + close(parent_pidns_fd2); + return -1; + } + close(pidfd); + ksft_inc_pass_cnt(); + + pidfd = sys_pidctl(PIDCMD_GET_PIDFD, self, -1, parent_pidns_fd2, 0); + if (pidfd < 0) { + ksft_print_msg("%s test %d: failed to retrieve pidfd\n", + test_name, ksft_test_num()); + close(parent_pidns_fd2); + return -1; + } + close(pidfd); + ksft_inc_pass_cnt(); + + pidfd = sys_pidctl(PIDCMD_GET_PIDFD, self, parent_pidns_fd, -1, 0); + if (pidfd < 0) { + ksft_print_msg("%s test %d: failed to retrieve pidfd\n", + test_name, ksft_test_num()); + close(parent_pidns_fd2); + return -1; + } + close(pidfd); + ksft_inc_pass_cnt(); + + close(parent_pidns_fd2); + + pidfd = sys_pidctl(PIDCMD_GET_PIDFD, self, parent_pidns_fd, + child_pidns_fd, 0); + if (pidfd >= 0 || ((pidfd < 0) && (errno != ENOENT))) { + ksft_print_msg("%s test %d: succeeded to retrieve pidfd but should've failed %s\n", + test_name, ksft_test_num(), strerror(errno)); + return -1; + } + ksft_inc_pass_cnt(); + + pidfd = sys_pidctl(PIDCMD_GET_PIDFD, self, child_pidns_fd, + parent_pidns_fd, 0); + if (pidfd >= 0 || ((pidfd < 0) && (errno != ESRCH))) { + ksft_print_msg("%s test %d: succeeded to retrieve pidfd but should've failed %s\n", + test_name, ksft_test_num(), strerror(errno)); + return -1; + } + ksft_inc_pass_cnt(); + + pidfd = sys_pidctl(PIDCMD_GET_PIDFD, 1, child_pidns_fd, parent_pidns_fd, 0); + if (pidfd < 0) { + ksft_print_msg("%s test %d: failed to retrieve pidfd\n", + test_name, ksft_test_num()); + return -1; + } + close(pidfd); + ksft_inc_pass_cnt(); + + pidfd = sys_pidctl(PIDCMD_GET_PIDFD, 1, child_pidns_fd, cousin_pidns_fd, 0); + if (pidfd >= 0 || ((pidfd < 0) && (errno != ENOENT))) { + ksft_print_msg("%s test %d: succeeded to retrieve pidfd but should've failed %s\n", + test_name, ksft_test_num(), strerror(errno)); + return -1; + } + ksft_inc_pass_cnt(); + + pidfd = sys_pidctl(PIDCMD_GET_PIDFD, cousin_pidns_pid, child_pidns_fd, + cousin_pidns_fd, 0); + if (pidfd >= 0 || ((pidfd < 0) && (errno != ESRCH))) { + ksft_print_msg("%s test %d: succeeded to retrieve pidfd but should've failed %s\n", + test_name, ksft_test_num(), strerror(errno)); + return -1; + } + ksft_inc_pass_cnt(); + +out: + ksft_test_result_pass("%s test: passed\n", test_name); + return 0; +} + +static void test_pidctl_pidfd_send_signal(void) +{ + const char *test_name = "pidctl with pidfd_send_signal"; + int child_pidfd, cousin_pidfd, ret; + + child_pidfd = sys_pidctl(PIDCMD_GET_PIDFD, child_pidns_pid, -1, -1, 0); + if (child_pidfd < 0) + ksft_print_msg("%s test %d: failed to retrieve pidfd\n", + test_name, ksft_test_num()); + ksft_inc_pass_cnt(); + + ret = sys_pidfd_send_signal(child_pidfd, SIGKILL, NULL, 0); + if (ret < 0) { + kill(child_pidns_pid, SIGKILL); + ksft_print_msg("%s test %d: failed to send signal via pidfd\n", + test_name, ksft_test_num()); + } + ksft_inc_pass_cnt(); + + cousin_pidfd = sys_pidctl(PIDCMD_GET_PIDFD, cousin_pidns_pid, -1, -1, 0); + if (cousin_pidfd < 0) + ksft_print_msg("%s test %d: failed to retrieve pidfd\n", + test_name, ksft_test_num()); + ksft_inc_pass_cnt(); + + ret = sys_pidfd_send_signal(cousin_pidfd, SIGKILL, NULL, 0); + if (ret < 0) { + kill(cousin_pidfd, SIGKILL); + ksft_print_msg("%s test %d: failed to send signal via pidfd\n", + test_name, ksft_test_num()); + } + ksft_inc_pass_cnt(); + + ksft_test_result_pass("%s test: passed\n", test_name); +} + +int wait_for_pid(pid_t pid) +{ + int status, ret; + +again: + ret = waitpid(pid, &status, 0); + if (ret == -1) { + if (errno == EINTR) + goto again; + + return -1; + } + + if (ret != pid) + goto again; + + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) + return -1; + + return 0; +} + +int main(int argc, char **argv) +{ + int ret; + pid_t pid; + + pid = fork(); + if (pid < 0) + ksft_exit_fail_msg("Failed to create new process\n"); + + if (pid == 0) { + if (unshare(CLONE_NEWPID) < 0) + exit(EXIT_FAILURE); + + exit(EXIT_SUCCESS); + } + + if (!wait_for_pid(pid)) + pidns_supported = true; + + ksft_print_header(); + + if (pidns_supported) + prepare_pid_namespaces(); + else + ksft_print_msg( + "kernel does not support pid namespaces: skipping pid namespace parts of testsuite"); + + ret = test_pidcmd_query_pid(); + if (ret < 0) { + ksft_print_msg("PIDCMD_QUERY_PID tests failed"); + goto on_error; + } + + ret = test_pidcmd_query_pidns(); + if (ret < 0) { + ksft_print_msg("PIDCMD_QUERY_PIDNS tests failed"); + goto on_error; + } + + ret = test_pidcmd_get_pidfd(); + if (ret < 0) { + ksft_print_msg("PIDCMD_GET_PIDFD tests failed"); + goto on_error; + } + + ret = 0; + +on_error: + if (pidns_supported) + test_pidctl_pidfd_send_signal(); + + if (parent_pidns_fd >= 0) + close(parent_pidns_fd); + + if (child_pidns_fd >= 0) + close(child_pidns_fd); + + if (cousin_pidns_fd >= 0) + close(cousin_pidns_fd); + + return !ret ? ksft_exit_pass() : ksft_exit_fail(); +} -- 2.21.0