From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yw1-x1136.google.com (mail-yw1-x1136.google.com [IPv6:2607:f8b0:4864:20::1136]) by sourceware.org (Postfix) with ESMTPS id 6646D3858D37 for ; Wed, 19 Apr 2023 14:14:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6646D3858D37 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-yw1-x1136.google.com with SMTP id 00721157ae682-54fc337a650so763367b3.4 for ; Wed, 19 Apr 2023 07:14:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681913640; x=1684505640; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=cHXVnIrX51iX5W7Zq7NFmFNIOg/Q2sIh58tH8HBJ9DU=; b=szHWRfvp3MSuLEj2OGxuPvRFKwg37RsIMfw7vaogvQcld2XXCwCZqSqCIMtV9jnTbm hLVAsgAuKqlNjYWGA9Hkm92FtvKyfBkadvfWwH1bIlu5alAOKeRdszg9gJ276z0z2+O6 VATuZVy5tmwCt8SArv1g5He6/AugP81QzLrgka9n2ycN+iDTlWuaplHLw+zYWIb6uFIN 1lrzsE2FpacjNIv2bZI4dLkmiFmtWyt7tYEorLPlzHQ2XYn5vntQ7FbGUdzdES8aCvHO UgmkjgMZpLhu3WzcRXu7wz61TyVecvm4/N7N6+23OVfmOCzl9hl43F9sUXs53xIikEKG zdMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681913640; x=1684505640; 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=cHXVnIrX51iX5W7Zq7NFmFNIOg/Q2sIh58tH8HBJ9DU=; b=TViENxc3CyPRuYvb1hzoS7+FelOfCQWzabHttCL0Yjzhf3Jm3+BuE3ON4fU4TMr/m8 RJWHSDF+4QL9r+1HYe79UIlT5+SlDe59n0nBo82ja39SLTUWBMNUlsjWBndQAsqRKCJc bCj3tLdi6W4kg7yJAcer+GDzn0TABrgJMqopTjUj8pR9q4ap2AekIf3BWLJZPTwLC2KY U9REbd83MvF7K60Ho893CK4Yg6infXuswhTOWLj/WTAMBKqurj76Od6MG9fELdcpJUZy 6VBfGlHoybknBKDCBwvQOQ3FRW3c5oATzqt887etifBel7NfF6NImO9Jhh+W9snAxhnW DU+A== X-Gm-Message-State: AAQBX9fVcNLHIt5RasQRuRvIz5jyzaZjuuavoREH4UDUZLAf2zYc41V8 IKfgNXAYvAWRqnqav9L3ErB/9v5Edy2r3Xz4OTQyQ/qjFC0= X-Google-Smtp-Source: AKy350Yc26onjnicQujALAMYIaRoHUcIorsFgjJJ6y0wlckOggWkP/0Gr/H4S/ZGM2cZ1OW9+bXawu3zT8uVPRUlaKQ= X-Received: by 2002:a81:4645:0:b0:54f:b6b3:b5e5 with SMTP id t66-20020a814645000000b0054fb6b3b5e5mr3311078ywa.18.1681913639724; Wed, 19 Apr 2023 07:13:59 -0700 (PDT) MIME-Version: 1.0 References: <6283d77d0625d9644216c21d4adc197a5e48881f.camel@gmail.com> <8e868c6e-e59c-0290-0250-165330ea116f@linaro.org> <017622ea-f4ae-94de-2abd-7d87531ecf1c@linaro.org> In-Reply-To: <017622ea-f4ae-94de-2abd-7d87531ecf1c@linaro.org> From: Luca Boccassi Date: Wed, 19 Apr 2023 15:13:48 +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.0 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 15:11, Adhemerval Zanella Netto wrote: > On 19/04/23 11:01, Luca Boccassi wrote: > > 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. > > I am kind worried where such errors might clash with other open/read/close > used in parsing, but at least it seems ESRCH should not be generated in such > cases. I am still not sure about EREMOTE, maybe we should just map the > pid equal 0 to -1/EREMOTE. Yes, -1/EREMOTE makes sense to me. Kind regards, Luca Boccassi