Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Dmitry V. Levin and committed by
Christian Brauner
bee19cd8 9014143b

+6 -2
+6 -2
samples/pidfd/pidfd-metadata.c
··· 83 83 84 84 int main(int argc, char *argv[]) 85 85 { 86 - int pidfd = 0, ret = EXIT_FAILURE; 86 + int pidfd = -1, ret = EXIT_FAILURE; 87 87 char buf[4096] = { 0 }; 88 88 pid_t pid; 89 89 int procfd, statusfd; ··· 91 91 92 92 pid = pidfd_clone(CLONE_PIDFD, &pidfd); 93 93 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 + } 95 99 96 100 procfd = pidfd_metadata_fd(pid, pidfd); 97 101 close(pidfd);