public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: Adhemerval Zanella via Libc-alpha <libc-alpha@sourceware.org>
Cc: Adhemerval Zanella <adhemerval.zanella@linaro.org>,
	 Luca Boccassi <luca.boccassi@gmail.com>
Subject: Re: [PATCH v2 3/3] linux: Add pidfd_getpid
Date: Tue, 02 May 2023 18:07:35 +0200	[thread overview]
Message-ID: <87r0rysomw.fsf@oldenburg.str.redhat.com> (raw)
In-Reply-To: <20230420142037.4063169-4-adhemerval.zanella@linaro.org>

* Adhemerval Zanella via Libc-alpha:

> This interface allows to obtain the associated pid ID from the
> 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
> 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
>      fields contains a value larger than pid_t.
>
>    - EREMOTE if the PID is in a separate namespace.
>
>    - ESRCH if the process is already terminated.

Could you add a manual entry for this?

The documentation for EREMOTE (also in the comment in the code) should
probably say that this is returned if there is no PID to denote the
process in the current namespace.  I assume that the PID is available
from an outer namespace (even across PID namespace boundaries), but not
necessarily in the other direction.

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

> +static bool
> +parse_fdinfo (const char *l, void *arg)
> +{
> +  enum { fieldlen = sizeof ("Pid:") - 1 };
> +  if (strncmp (l, "Pid:", fieldlen) != 0)
> +    return true;
> +
> +  l += fieldlen;
> +
> +  char *endp;
> +  long int n = strtol (l, &endp, 10);
> +  if (l == endp || n > INT_MAX)
> +    return true;

Should this use strtoul?  Otherwise I'm not sure the overflow check will
work on 32-bit.

Rest of the implementation looks okay, but the kernel should really
provide an ioctl or similar for this.

> diff --git a/sysdeps/unix/sysv/linux/procutils.c b/sysdeps/unix/sysv/linux/procutils.c
> new file mode 100644
> index 0000000000..4909afeae1
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/procutils.c
> @@ -0,0 +1,99 @@
> +/* Utilities functions to read/parse Linux procfs and sysfs.

> +int
> +procutils_read_file (const char *filename, procutils_closure_t closure,
> +		     void *arg)
> +{
> +  enum { buffer_size = 1024 };
> +  char buffer[buffer_size];
> +  char *buffer_end = buffer + buffer_size;
> +  char *cp = buffer_end;
> +  char *re = buffer_end;
> +
> +  int fd = __open64_nocancel (filename, O_RDONLY | O_CLOEXEC);
> +  if (fd == -1)
> +    return -1;
> +
> +  char *l;
> +  while ((l = next_line (fd, buffer, &cp, &re, buffer_end)) != NULL)
> +    if (!closure (l, arg))
> +      break;
> +
> +  __close_nocancel_nostatus (fd);
> +
> +  return 0;
> +}

Incomplete error reporting?  Any read failure doesn't make it to the
caller.

The callback interface will be easier to use if you have it return int
and return the callback return value if it is non-zero, I think.

> diff --git a/sysdeps/unix/sysv/linux/tst-pidfd.c b/sysdeps/unix/sysv/linux/tst-pidfd.c
> index 64d8a2ef40..2a73328ef1 100644
> --- a/sysdeps/unix/sysv/linux/tst-pidfd.c
> +++ b/sysdeps/unix/sysv/linux/tst-pidfd.c
> @@ -18,6 +18,7 @@

It would be nice to have a check for the EREMOTE case.

Thanks,
Florian


  parent reply	other threads:[~2023-05-02 16:07 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-20 14:20 [PATCH v2 0/3] Add pidfd_spawn, pidfd_spawnp, pidfd_fork, and pidfd_getpid Adhemerval Zanella
2023-04-20 14:20 ` [PATCH v2 1/3] posix: Add pidfd_spawn and pidfd_spawnp (BZ# 30349) Adhemerval Zanella
2023-04-20 14:20 ` [PATCH v2 2/3] posix: Add pidfd_fork Adhemerval Zanella
2023-04-20 14:20 ` [PATCH v2 3/3] linux: Add pidfd_getpid Adhemerval Zanella
2023-04-20 14:24   ` Luca Boccassi
2023-05-02 16:07   ` Florian Weimer [this message]
2023-05-02 16:19     ` Luca Boccassi
2023-05-02 17:13       ` Florian Weimer
2023-05-15 17:39     ` Adhemerval Zanella Netto

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=87r0rysomw.fsf@oldenburg.str.redhat.com \
    --to=fweimer@redhat.com \
    --cc=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).