Browse Source

samples: make pidfd-metadata fail gracefully on older kernels

Initialize pidfd to an invalid descriptor, to fail gracefully on
those kernels that do not implement CLONE_PIDFD and leave pidfd
unchanged.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
Signed-off-by: Christian Brauner <christian@brauner.io>
master
Dmitry V. Levin 2 years ago
committed by Christian Brauner
parent
commit
bee19cd8f2
No known key found for this signature in database GPG Key ID: 8EB056D53EECB12D
1 changed files with 6 additions and 2 deletions
  1. +6
    -2
      samples/pidfd/pidfd-metadata.c

+ 6
- 2
samples/pidfd/pidfd-metadata.c View File

@ -83,7 +83,7 @@ static int pidfd_metadata_fd(pid_t pid, int pidfd)
int main(int argc, char *argv[])
{
int pidfd = 0, ret = EXIT_FAILURE;
int pidfd = -1, ret = EXIT_FAILURE;
char buf[4096] = { 0 };
pid_t pid;
int procfd, statusfd;
@ -91,7 +91,11 @@ int main(int argc, char *argv[])
pid = pidfd_clone(CLONE_PIDFD, &pidfd);
if (pid < 0)
exit(ret);
err(ret, "CLONE_PIDFD");
if (pidfd == -1) {
warnx("CLONE_PIDFD is not supported by the kernel");
goto out;
}
procfd = pidfd_metadata_fd(pid, pidfd);
close(pidfd);


Loading…
Cancel
Save