public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
To: Luca Boccassi <luca.boccassi@gmail.com>
Cc: libc-alpha@sourceware.org, Lennart Poettering <lennart@poettering.net>
Subject: Re: [PATCH 4/4] linux: Add pidfd_getpid
Date: Wed, 19 Apr 2023 11:11:16 -0300	[thread overview]
Message-ID: <017622ea-f4ae-94de-2abd-7d87531ecf1c@linaro.org> (raw)
In-Reply-To: <CAMw=ZnQ-4x4CqGe9TjGYk2Raht4LKJ60-jS4MHaqgZW8JGRsdQ@mail.gmail.com>



On 19/04/23 11:01, Luca Boccassi wrote:
> On Wed, 19 Apr 2023 at 14:56, Adhemerval Zanella Netto
> <adhemerval.zanella@linaro.org> 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.


  reply	other threads:[~2023-04-19 14:11 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-18 21:35 [PATCH 0/4] Add pidfd_spawn, pidfd_spawnp, pidfd_fork, and pidfd_getpid Adhemerval Zanella
2023-04-18 21:35 ` [PATCH 1/4] posix: Re-flow and sort multiline definitions Adhemerval Zanella
2023-04-18 21:35 ` [PATCH 2/4] posix: Add pidfd_spawn and pidfd_spawnp (BZ# 30349) Adhemerval Zanella
2023-04-18 21:35 ` [PATCH 3/4] posix: Add pidfd_fork Adhemerval Zanella
2023-04-18 21:35 ` [PATCH 4/4] linux: Add pidfd_getpid Adhemerval Zanella
2023-04-19 10:29   ` Luca Boccassi
2023-04-19 13:56     ` Adhemerval Zanella Netto
2023-04-19 14:01       ` Luca Boccassi
2023-04-19 14:11         ` Adhemerval Zanella Netto [this message]
2023-04-19 14:13           ` Luca Boccassi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=017622ea-f4ae-94de-2abd-7d87531ecf1c@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=lennart@poettering.net \
    --cc=libc-alpha@sourceware.org \
    --cc=luca.boccassi@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).