From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yb1-xb2d.google.com (mail-yb1-xb2d.google.com [IPv6:2607:f8b0:4864:20::b2d]) by sourceware.org (Postfix) with ESMTPS id 284673858D37 for ; Wed, 19 Apr 2023 14:01:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 284673858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-yb1-xb2d.google.com with SMTP id 3f1490d57ef6-b8ec60decbfso2771442276.1 for ; Wed, 19 Apr 2023 07:01:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681912891; x=1684504891; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=aJM0IxM/Fldqo8DIPgjVuS6Ib2qe8nNOr71z3hPUyfU=; b=Zn/BQYa6ybVeB5gmjCn7R4GDeznUX6d/NgFMFdeRDiHR3hFO9HYaTVm3eTEMKms2OJ f3nsHUi3mTq5LJRR6zG/jV+H1APYv2uz3GE5o5f0vr5KQHL2zFfaWn6drz0BY0H243BG 7JUIZf0bshdSVMsm59+ywb9ZhyswSMCBRj/wRgN22cl1gPGl/qAerAFN393Vao3gfHtS DwujbMi9uf65saNkBj8MEJD/TQShZMiMJrSnom6HZNhkItW+LzIO+5B6ROB3ZQpBfNMN pKJN7JLoySBQulC1elFWY2krZ+svkPXlzUdl9j2c/rYN9rZ5ySxxcbu/EVnhQunaH/60 ZO5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681912891; x=1684504891; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=aJM0IxM/Fldqo8DIPgjVuS6Ib2qe8nNOr71z3hPUyfU=; b=UfeeO40uzsRMkn0dZ7ikL9W00Lwlm1+kWhyugecdaG1RPoQ6OzqY2MvJI1XzS4e5M4 IEgBGrzvg/m0gUFCZKEh6dE1RyNVYRlxC7UomKLXmTrA2kkqcb4FZT7aCOk4c8LskYjs VK5kQZvWvD+dYFqOwxgUtg4bKlAKMNfX1JI8QTa6AyWqA9NdBX9nH1BjO+S4HmasOA0K 4uj5mHsDcY3dMRFUQLBKgNYbKOhgmLp0OqQ+zlZOuJu9c4GSsgh9kWGQg1mmoTW0e5y1 gXBz/+GABw4N3ne/e7UpREdLh+93F/K/0D/S7fLFC77TDMVrLioG+zHhAuF5m3cwjeuf 8WCQ== X-Gm-Message-State: AAQBX9eOXPJRrCGd6imVjLPvXfwBb5I2sK5MblSrJC/XQ74IWU/I4aEr iXq5KEicaCG+i5UiN3ozpemtiPWFV3a65mTqcp84D7La+fQ= X-Google-Smtp-Source: AKy350biFzqFqGYq05qPFgpgVsLFQtakL4agF1QAk5Z6PF6adWvvmLpjIUcIt9++Hjz1TBoj9DBVAnBfJ8i0FzMyCfY= X-Received: by 2002:a05:7500:5e90:b0:fe:c93b:29d1 with SMTP id fk16-20020a0575005e9000b000fec93b29d1mr460322gab.72.1681912891445; Wed, 19 Apr 2023 07:01:31 -0700 (PDT) MIME-Version: 1.0 References: <6283d77d0625d9644216c21d4adc197a5e48881f.camel@gmail.com> <8e868c6e-e59c-0290-0250-165330ea116f@linaro.org> In-Reply-To: <8e868c6e-e59c-0290-0250-165330ea116f@linaro.org> From: Luca Boccassi Date: Wed, 19 Apr 2023 15:01:20 +0100 Message-ID: Subject: Re: [PATCH 4/4] linux: Add pidfd_getpid To: Adhemerval Zanella Netto Cc: libc-alpha@sourceware.org, Lennart Poettering Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Wed, 19 Apr 2023 at 14:56, Adhemerval Zanella Netto wrote: > On 19/04/23 07:29, Luca Boccassi wrote: > >> +parse_fdinfo (const char *l, void *arg) > >> +{ > >> + enum { fieldlen = sizeof ("Pid:") - 1 }; > >> + if (strncmp (l, "Pid:", fieldlen) != 0) > >> + return true; > >> + > >> + l += fieldlen; > >> + > >> + char *endp; > >> + unsigned long int n = strtoul (l, &endp, 10); > >> + if (l == endp || n > INT_MAX) > >> + return true; > >> + > >> + *(pid_t *)arg = n; > >> + return false; > >> +} > >> + > >> +pid_t > >> +pidfd_getpid (int fd) > >> +{ > >> + if (__glibc_unlikely (fd < 0)) > >> + { > >> + __set_errno (EINVAL); > >> + return -1; > >> + } > >> + > >> + char fdinfoname[FDINFO_FILENAME_LEN]; > >> + > >> + char *p = mempcpy (fdinfoname, FDINFO_TO_FILENAME_PREFIX, > >> + strlen (FDINFO_TO_FILENAME_PREFIX)); > >> + *_fitoa_word (fd, p, 10, 0) = '\0'; > >> + > >> + pid_t pid; > >> + if (procutils_read_file (fdinfoname, parse_fdinfo, &pid) == -1) > >> + return -1; > >> + > >> + return pid; > > > > Having implemented this parsing by hand across 3 projects, it is great > > to see a glibc helper coming. > > > > However, please handle the case of Pid being 0 and -1 explicitly, and > > return a recognizable errno. fdinfo listing 0 means the pidfd cannot be > > resolved because it's in a separate pid namespace, so something EREMOTE > > would suffice. -1 means the process exited, so ESRCH seems like the > > right error. The distiction between these cases and other errors is > > important to userspace where we do process tracking, like > > systemd/dbus/polkit. > > Ok, it should a matter to handle the 'Pid:' as signed. However, I am not > sure if it is really worth to set the errno in cases, since the information > will already available from the function return itself. The function returns the pid, so the same information wouldn't be available. Eg. returning -1 because you can't read proc because of selinux permissions is different from returning -1 because the process was reaped and fdinfo contained -1 so it was just passed through. We need to know this distinction in userspace to handle it properly. Setting the errno accordingly seems the best way to do that to me. Kind regards, Luca Boccassi