public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Cc: libc-alpha@sourceware.org,
	 Luca Boccassi <luca.boccassi@gmail.com>,
	Philip Withnall <bugzilla@tecnocode.co.uk>
Subject: Re: [PATCH v4 3/3] linux: Add pidfd_getpid
Date: Mon, 22 May 2023 15:00:13 +0200	[thread overview]
Message-ID: <87h6s44j36.fsf@oldenburg3.str.redhat.com> (raw)
In-Reply-To: <20230517173516.1988283-4-adhemerval.zanella@linaro.org> (Adhemerval Zanella's message of "Wed, 17 May 2023 14:35:16 -0300")

* Adhemerval Zanella:

> This interface allows to obtain the associated pid ID from the

“process ID”

> process file descriptor.  It is done by parsing the procps fdinfo
> information.  Its prototype is:
>
>    pid_t pidfd_getpid (int fd)
>
> It returns the associated pid or -1 in case of an error and set the
> errno accordingly.  The possible errno values are the smae from

Typos: “and set[s] [] errno accordingly.”

“are [those] from”

> open, read, and close (used on procps parsing), along with:
>
>    - EINVAL if the FP is negative (similar to fexecve).
>
>    - EBADF if the FD does not have a PID associated of if the fdinfo

“associated o[r] if”

>      fields contains a value larger than pid_t.
>
>    - EREMOTE if the PID is in a separate namespace.
>
>    - ESRCH if the process is already terminated.

> diff --git a/sysdeps/unix/sysv/linux/pidfd_getpid.c b/sysdeps/unix/sysv/linux/pidfd_getpid.c
> new file mode 100644
> index 0000000000..ae5fa100eb
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/pidfd_getpid.c
> @@ -0,0 +1,122 @@

> +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';
> +
> +  struct parse_fdinfo_t fdinfo = { .found = false, .pid = -1 };
> +  if (procutils_read_file (fdinfoname, parse_fdinfo, &fdinfo) == -1)
> +    /* The fdinfo contains an invalid 'Pid:' value.  */
> +    return INLINE_SYSCALL_ERROR_RETURN_VALUE (EBADF);
> +
> +  /* The FD does not have a 'Pid:' entry associated.  */
> +  if (!fdinfo.found)
> +    return INLINE_SYSCALL_ERROR_RETURN_VALUE (EBADF);
> +
> +  /* The pidfd cannot be resolved because it is in a separate pid
> +     namespace.  */
> +  if (fdinfo.pid == 0)
> +    return INLINE_SYSCALL_ERROR_RETURN_VALUE (EREMOTE);
> +
> +  /* A negative value means the process is terminated.  */
> +  if (fdinfo.pid < 0)
> +    return INLINE_SYSCALL_ERROR_RETURN_VALUE (ESRCH);
> +
> +  return fdinfo.pid;

We are making a ot of error codes here.  How likely is it that they
match what a future ioctl will do?

> diff --git a/sysdeps/unix/sysv/linux/tst-pidfd.c b/sysdeps/unix/sysv/linux/tst-pidfd.c
> index 64d8a2ef40..a03de4b290 100644
> --- a/sysdeps/unix/sysv/linux/tst-pidfd.c
> +++ b/sysdeps/unix/sysv/linux/tst-pidfd.c

> +  /* Check if pidfd_getpid returns ESRCH for exited subprocess.  */
> +  {
> +    int pidfd = pidfd_fork (0);
> +    if (pidfd == 0)
> +      exit (EXIT_SUCCESS);

Should be _exit, so that the atexit handlers do not run.

I think the main remaining question is errno alignment with a future
direct kernel interface for this.

Thanks,
Florian


  reply	other threads:[~2023-05-22 13:00 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-17 17:35 [PATCH v4 0/3] Add pidfd_spawn, pidfd_spawnp, pidfd_fork, and pidfd_getpid Adhemerval Zanella
2023-05-17 17:35 ` [PATCH v4 1/3] posix: Add pidfd_spawn and pidfd_spawnp (BZ# 30349) Adhemerval Zanella
2023-05-22 10:41   ` Florian Weimer
2023-05-22 10:46     ` Andreas Schwab
2023-05-22 14:18     ` Adhemerval Zanella Netto
2023-05-17 17:35 ` [PATCH v4 2/3] posix: Add pidfd_fork Adhemerval Zanella
2023-05-22 10:12   ` Florian Weimer
2023-05-22 12:44     ` Christian Brauner
2023-05-22 14:48     ` Adhemerval Zanella Netto
2023-05-17 17:35 ` [PATCH v4 3/3] linux: Add pidfd_getpid Adhemerval Zanella
2023-05-22 13:00   ` Florian Weimer [this message]
2023-05-22 17:13     ` Adhemerval Zanella Netto
2023-05-22 13:53   ` Joe Simmons-Talbott

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=87h6s44j36.fsf@oldenburg3.str.redhat.com \
    --to=fweimer@redhat.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=bugzilla@tecnocode.co.uk \
    --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).