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
Subject: Re: [PATCH 4/4] linux: Add pidfd_getpid
Date: Wed, 19 Apr 2023 10:56:18 -0300	[thread overview]
Message-ID: <8e868c6e-e59c-0290-0250-165330ea116f@linaro.org> (raw)
In-Reply-To: <6283d77d0625d9644216c21d4adc197a5e48881f.camel@gmail.com>



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 CLOSURE should return false if the read should continue, or false
>> +   if the function should stop.
> 
> Did you mean "true if the read should continue"?

Indeed.

> 
>> +  TEST_COMPARE (pidfd_getpid (INT_MAX), -1);
>> +  {
>> +    pid_t querypid = pidfd_getpid (pidfd);
>> +    TEST_COMPARE (querypid, pid);
>> +  }
> 
> I think it would be a good idea to add a negative test, eg for a
> process that exited or so.

Alright, I will add a more comprehensible test.

> 
> (I am not subscribed to the list, so please CC directly if needed)
> 

  reply	other threads:[~2023-04-19 13:56 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 [this message]
2023-04-19 14:01       ` Luca Boccassi
2023-04-19 14:11         ` Adhemerval Zanella Netto
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=8e868c6e-e59c-0290-0250-165330ea116f@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --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).