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=-2.3 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=no 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 442DFECE58C for ; Fri, 11 Oct 2019 17:08:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 242A32084C for ; Fri, 11 Oct 2019 17:08:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727149AbfJKRIx (ORCPT ); Fri, 11 Oct 2019 13:08:53 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:48859 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728086AbfJKRIx (ORCPT ); Fri, 11 Oct 2019 13:08:53 -0400 Received: from [213.220.153.21] (helo=wittgenstein) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1iIyPK-0004El-S2; Fri, 11 Oct 2019 17:08:50 +0000 Date: Fri, 11 Oct 2019 19:08:50 +0200 From: Christian Brauner To: Jann Horn Cc: Christian Kellner , kernel list , Linux API , Christian Kellner , Shuah Khan , Andrew Morton , "Peter Zijlstra (Intel)" , Ingo Molnar , Michal Hocko , Thomas Gleixner , Elena Reshetova , Roman Gushchin , Andrea Arcangeli , "Joel Fernandes (Google)" , Al Viro , "Dmitry V. Levin" , "open list:KERNEL SELFTEST FRAMEWORK" Subject: Re: [PATCH v2 2/2] pidfd: add tests for NSpid info in fdinfo Message-ID: <20191011170848.nbfxzzl5qckkshkr@wittgenstein> References: <20191008133641.23019-1-ckellner@redhat.com> <20191009160532.20674-1-ckellner@redhat.com> <20191009160532.20674-2-ckellner@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20180716 Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org On Fri, Oct 11, 2019 at 05:09:29PM +0200, Jann Horn wrote: > On Wed, Oct 9, 2019 at 6:10 PM Christian Kellner wrote: > > Add tests that check that if pid namespaces are configured the fdinfo > > file of a pidfd contains an NSpid: entry containing the process id > > in the current and additionally all nested namespaces. > [...] > > +static int compare_fdinfo_nspid(int pidfd, char *expect, size_t len) > > +{ > > + char path[512]; > > + FILE *f; > > + size_t n = 0; > > + ssize_t k; > > + char *line = NULL; > > + int r = -1; > > + > > + snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", pidfd); > > (Maybe at some point the selftests code should add some more concise > alternative to snprintf() calls on separate lines. A macro or > something like that so that you can write stuff like `f = > fopen(tprintf("/proc/self/fdinfo/%d", pidfd), "re")`.) > > > + f = fopen(path, "re"); > > + if (!f) > > + return -1; > > + > > + while ((k = getline(&line, &n, f)) != -1) { > > + if (strncmp(line, "NSpid:", 6)) > > + continue; > > + > > + line[k - 1] = '\0'; > > + ksft_print_msg("Child: fdinfo NSpid line: '%s'.\n", line); > > + r = strncmp(line + 6, expect, len); > > Wouldn't it be better to get rid of the nullbyte assignment and change > the strncmp() into a strcmp() here... > > [...] > > + /* The child will have pid 1 in the new pid namespace, > > + * so the line must be 'NSPid:\t\t1' > > + */ > > + n = snprintf(expect, sizeof(expect), "\t%d\t%d", pid, 1); > > ... and add a "\n" to the format string? It's shorter and doesn't > silently ignore it if the line doesn't end at that point. Also, what Christian just told me and what I wanted to suggest is that we add tests for sending around pidfds and reading fdinfo too.