Skip to content

Commit

Permalink
samples: make pidfd-metadata fail gracefully on older kernels
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
ldv-alt authored and brauner committed Jun 24, 2019
1 parent 9014143 commit bee19cd
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions samples/pidfd/pidfd-metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,19 @@ 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;
ssize_t bytes;

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);
Expand Down

0 comments on commit bee19cd

Please sign in to comment.