linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] samples: make pidfd-metadata fail gracefully on older kernels
@ 2019-06-20  3:11 Dmitry V. Levin
  2019-06-20 10:31 ` Christian Brauner
  0 siblings, 1 reply; 15+ messages in thread
From: Dmitry V. Levin @ 2019-06-20  3:11 UTC (permalink / raw)
  To: Christian Brauner; +Cc: linux-kernel

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>
---
 samples/pidfd/pidfd-metadata.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/samples/pidfd/pidfd-metadata.c b/samples/pidfd/pidfd-metadata.c
index 14b454448429..ff109fdac3a5 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 < 0) {
+		warnx("CLONE_PIDFD is not supported by the kernel");
+		goto out;
+	}
 
 	procfd = pidfd_metadata_fd(pid, pidfd);
 	close(pidfd);
-- 
ldv

^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2019-06-24 13:49 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-20  3:11 [PATCH] samples: make pidfd-metadata fail gracefully on older kernels Dmitry V. Levin
2019-06-20 10:31 ` Christian Brauner
2019-06-20 11:00   ` Dmitry V. Levin
2019-06-20 11:10     ` Christian Brauner
2019-06-21 17:06       ` Dmitry V. Levin
2019-06-21 22:13         ` Christian Brauner
2019-06-23 11:27           ` [PATCH 1/2] CLONE_PIDFD: do not use the value pointed by parent_tidptr Dmitry V. Levin
2019-06-23 11:28             ` [PATCH 2/2] samples: make pidfd-metadata fail gracefully on older kernels Dmitry V. Levin
2019-06-24  9:50               ` Christian Brauner
2019-06-24  9:49             ` [PATCH 1/2] CLONE_PIDFD: do not use the value pointed by parent_tidptr Christian Brauner
2019-06-24 11:59               ` Christian Brauner
2019-06-24 13:45                 ` Dmitry V. Levin
2019-06-24 13:49                   ` Christian Brauner
2019-06-23 11:32           ` [PATCH] samples: make pidfd-metadata fail gracefully on older kernels Dmitry V. Levin
2019-06-24  9:52             ` Christian Brauner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).