public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Luca Boccassi <luca.boccassi@gmail.com>
To: adhemerval.zanella@linaro.org
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH 4/4] linux: Add pidfd_getpid
Date: Wed, 19 Apr 2023 11:29:30 +0100	[thread overview]
Message-ID: <6283d77d0625d9644216c21d4adc197a5e48881f.camel@gmail.com> (raw)
In-Reply-To: <20230418213505.3834934-5-adhemerval.zanella@linaro.org>

[-- Attachment #1: Type: text/plain, Size: 1942 bytes --]

> +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.

> +   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"?

> +  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.

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

-- 
Kind regards,
Luca Boccassi

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2023-04-19 10:29 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 [this message]
2023-04-19 13:56     ` Adhemerval Zanella Netto
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=6283d77d0625d9644216c21d4adc197a5e48881f.camel@gmail.com \
    --to=luca.boccassi@gmail.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=libc-alpha@sourceware.org \
    /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).