Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp5937175ybp; Tue, 15 Oct 2019 07:16:19 -0700 (PDT) X-Google-Smtp-Source: APXvYqx6svesI+Bb8onBhtzHfKDA9avzva7WpdVeyBaseKRS9i96TuLm9CG0kWHLPF0UzY63V6dO X-Received: by 2002:a17:906:7f03:: with SMTP id d3mr35569709ejr.209.1571148978908; Tue, 15 Oct 2019 07:16:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571148978; cv=none; d=google.com; s=arc-20160816; b=GG2OzUEaRK+w+XuTnCDQw3WMuk299E4i1JMMT6RjRmYLGYiPdOdjseHsyAwXwU0pBw Bs8cCwDGG1BCMzHWLr5uPWLfg/UveCPhI3MALivh3Ceh5OacnWWtUxk9YE5A48+DHWbG 1CIbXH503FoUab+k28HdVqsVZZsPdsR3zUF2CnqWFHYrj2hK/BXPCTrfw4uKgoE6WhcH 2PY0e0DgAoO6o/j9IOICmYaqCkLNA83AOfYhz5ThNc7fLyBI2bbEv9i2ZnKnUxnjlv1E mS2Wnq3t4kmtaWauu612bFXZwQYgXrgH8dN/wbMCzfC7GgO2aCewjfjhz73P3g4alN33 HTdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=k3LXdhm0GAJJkgnosSzGQ2lVjTPutEP0RD3iKiQNyO0=; b=oP1BUK/d6fpq/7gNIQW3YfPAv+i3K1hrwv76zA/RvFkL6iifBSNi1+Gu5KAcNBcA5t t8MeaY2sWC6FOiY/RrwEk1wFQSw/PjEtkyBWsmx1bugWMtKsp4JtVSwVZD121dhjl9e2 tyiTDY55953s2TUMfyO1Akix01gltZOVSKXFwzPcVIBex2FYa7aApvuiRhe5oAZ41f30 09unpRvi2t6wjWiazLgA9/rflerCks8N9YURs37ceZVgJQ81Ezat5kfN1Y0scxjbmRWC czp1O5cyTu1zrgI33kwDxMibqwLA7T72YT9TylkZ4nx8Cytam9yvw1FJW0biobH5EHBN dlrw== 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 e22si13084369ejr.19.2019.10.15.07.15.54; Tue, 15 Oct 2019 07:16:18 -0700 (PDT) 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 S1732602AbfJOONy (ORCPT + 99 others); Tue, 15 Oct 2019 10:13:54 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:39661 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732546AbfJOONl (ORCPT ); Tue, 15 Oct 2019 10:13:41 -0400 Received: from [213.220.153.21] (helo=localhost.localdomain) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1iKNZz-0004YA-8W; Tue, 15 Oct 2019 14:13:39 +0000 From: Christian Brauner To: linux-kernel@vger.kernel.org Cc: Shuah Khan , Andrew Morton , "Peter Zijlstra (Intel)" , Ingo Molnar , Michal Hocko , Thomas Gleixner , Elena Reshetova , Christian Kellner , Roman Gushchin , Andrea Arcangeli , Al Viro , Aleksa Sarai , "Dmitry V. Levin" , linux-kselftest@vger.kernel.org, Christian Brauner Subject: [PATCH 2/2] test: verify fdinfo for pidfd of reaped process Date: Tue, 15 Oct 2019 16:13:32 +0200 Message-Id: <20191015141332.4055-2-christian.brauner@ubuntu.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191015141332.4055-1-christian.brauner@ubuntu.com> References: <20191015141332.4055-1-christian.brauner@ubuntu.com> 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 Test that the fdinfo field of a pidfd referring to a dead process correctly shows Pid: -1 and NSpid: -1. Cc: Christian Kellner Signed-off-by: Christian Brauner --- .../selftests/pidfd/pidfd_fdinfo_test.c | 59 ++++++++++++++----- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/tools/testing/selftests/pidfd/pidfd_fdinfo_test.c b/tools/testing/selftests/pidfd/pidfd_fdinfo_test.c index 3721be994abd..22558524f71c 100644 --- a/tools/testing/selftests/pidfd/pidfd_fdinfo_test.c +++ b/tools/testing/selftests/pidfd/pidfd_fdinfo_test.c @@ -113,11 +113,15 @@ static struct child clone_newns(int (*fn)(void *), void *args, return ret; } +static inline void child_close(struct child *child) +{ + close(child->fd); +} + static inline int child_join(struct child *child, struct error *err) { int r; - (void)close(child->fd); r = wait_for_pid(child->pid); if (r < 0) error_set(err, PIDFD_ERROR, "waitpid failed (ret %d, errno %d)", @@ -128,6 +132,12 @@ static inline int child_join(struct child *child, struct error *err) return r; } +static inline int child_join_close(struct child *child, struct error *err) +{ + child_close(child); + return child_join(child, err); +} + static inline void trim_newline(char *str) { char *pos = strrchr(str, '\n'); @@ -136,8 +146,8 @@ static inline void trim_newline(char *str) *pos = '\0'; } -static int verify_fdinfo_nspid(int pidfd, struct error *err, - const char *expect, ...) +static int verify_fdinfo(int pidfd, struct error *err, const char *prefix, + size_t prefix_len, const char *expect, ...) { char buffer[512] = {0, }; char path[512] = {0, }; @@ -160,17 +170,20 @@ static int verify_fdinfo_nspid(int pidfd, struct error *err, pidfd); while (getline(&line, &n, f) != -1) { - if (strncmp(line, "NSpid:", 6)) + char *val; + + if (strncmp(line, prefix, prefix_len)) continue; found = 1; - r = strcmp(line + 6, buffer); + val = line + prefix_len; + r = strcmp(val, buffer); if (r != 0) { trim_newline(line); trim_newline(buffer); - error_set(err, PIDFD_FAIL, "NSpid: '%s' != '%s'", - line + 6, buffer); + error_set(err, PIDFD_FAIL, "%s '%s' != '%s'", + prefix, val, buffer); } break; } @@ -179,8 +192,8 @@ static int verify_fdinfo_nspid(int pidfd, struct error *err, fclose(f); if (found == 0) - return error_set(err, PIDFD_FAIL, "NSpid not found for fd %d", - pidfd); + return error_set(err, PIDFD_FAIL, "%s not found for fd %d", + prefix, pidfd); return PIDFD_PASS; } @@ -213,7 +226,7 @@ static int child_fdinfo_nspid_test(void *args) } pidfd = *(int *)args; - r = verify_fdinfo_nspid(pidfd, &err, "\t0\n"); + r = verify_fdinfo(pidfd, &err, "NSpid:", 6, "\t0\n"); if (r != PIDFD_PASS) ksft_print_msg("NSpid fdinfo check failed: %s\n", err.msg); @@ -242,24 +255,42 @@ static void test_pidfd_fdinfo_nspid(void) /* The children will have pid 1 in the new pid namespace, * so the line must be 'NSPid:\t\t1'. */ - verify_fdinfo_nspid(a.fd, &err, "\t%d\t%d\n", a.pid, 1); - verify_fdinfo_nspid(b.fd, &err, "\t%d\t%d\n", b.pid, 1); + verify_fdinfo(a.fd, &err, "NSpid:", 6, "\t%d\t%d\n", a.pid, 1); + verify_fdinfo(b.fd, &err, "NSpid:", 6, "\t%d\t%d\n", b.pid, 1); /* wait for the process, check the exit status and set * 'err' accordingly, if it is not already set. */ + child_join_close(&a, &err); + child_join_close(&b, &err); + + error_report(&err, test_name); +} + +static void test_pidfd_dead_fdinfo(void) +{ + struct child a; + struct error err = {0, }; + const char *test_name = "pidfd check fdinfo for dead process"; + + /* Create a new child in a new pid and mount namespace */ + a = clone_newns(child_fdinfo_nspid_test, NULL, &err); + error_check(&err, test_name); child_join(&a, &err); - child_join(&b, &err); + verify_fdinfo(a.fd, &err, "Pid:", 4, "\t-1\n"); + verify_fdinfo(a.fd, &err, "NSpid:", 6, "\t-1\n"); + child_close(&a); error_report(&err, test_name); } int main(int argc, char **argv) { ksft_print_header(); - ksft_set_plan(1); + ksft_set_plan(2); test_pidfd_fdinfo_nspid(); + test_pidfd_dead_fdinfo(); return ksft_exit_pass(); } -- 2.23.0