aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/samples/pidfd
diff options
context:
space:
mode:
authorDmitry V. Levin <ldv@altlinux.org>2019-06-23 14:28:00 +0300
committerChristian Brauner <christian@brauner.io>2019-06-24 15:55:50 +0200
commitbee19cd8f241ab3cd1bf79e03884e5371f9ef514 (patch)
tree49430f9d17dcbc8c1d47b1cd5b15426a5ec6ba0d /samples/pidfd
parentfork: don't check parent_tidptr with CLONE_PIDFD (diff)
downloadwireguard-linux-bee19cd8f241ab3cd1bf79e03884e5371f9ef514.tar.xz
wireguard-linux-bee19cd8f241ab3cd1bf79e03884e5371f9ef514.zip
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>
Diffstat (limited to 'samples/pidfd')
-rw-r--r--samples/pidfd/pidfd-metadata.c8
1 files changed, 6 insertions, 2 deletions
diff --git a/samples/pidfd/pidfd-metadata.c b/samples/pidfd/pidfd-metadata.c
index 14b454448429..c459155daf9a 100644
--- a/samples/pidfd/pidfd-metadata.c
+++ b/samples/pidfd/pidfd-metadata.c
@@ -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);