Skip to content

Commit bee19cd

Browse files
ldv-altbrauner
authored andcommitted
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>
1 parent 9014143 commit bee19cd

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

‎samples/pidfd/pidfd-metadata.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,19 @@ static int pidfd_metadata_fd(pid_t pid, int pidfd)
8383

8484
int main(int argc, char *argv[])
8585
{
86-
int pidfd = 0, ret = EXIT_FAILURE;
86+
int pidfd = -1, ret = EXIT_FAILURE;
8787
char buf[4096] = { 0 };
8888
pid_t pid;
8989
int procfd, statusfd;
9090
ssize_t bytes;
9191

9292
pid = pidfd_clone(CLONE_PIDFD, &pidfd);
9393
if (pid < 0)
94-
exit(ret);
94+
err(ret, "CLONE_PIDFD");
95+
if (pidfd == -1) {
96+
warnx("CLONE_PIDFD is not supported by the kernel");
97+
goto out;
98+
}
9599

96100
procfd = pidfd_metadata_fd(pid, pidfd);
97101
close(pidfd);

0 commit comments

Comments
 (0)