Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp1405345ybi; Wed, 19 Jun 2019 20:13:28 -0700 (PDT) X-Google-Smtp-Source: APXvYqzaSsfqDzj4Z6ffZrFy3RnwALfCgGgwtYUt3Mf/z91cWWChk37+ZQqct3/BJ3dpE2BFSv6X X-Received: by 2002:a65:63d2:: with SMTP id n18mr10583071pgv.278.1561000408202; Wed, 19 Jun 2019 20:13:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561000408; cv=none; d=google.com; s=arc-20160816; b=HvHr6sqO7wRZ0mfhyTdHi4Pp5Qj7+/nWCbl053/aRK3hF7DpfYlNARShz5ZlALwlFe S+On1Fk510TwDHTKJeIwR4AzkCsEAMkgL7i4UOmAo1Uj9oTyUABKh9DmKaU8LU0UVX5Q 7/TchRU+YYOVzKb3t2lGjm2qxQYPMgAjA7bAC7jiCpBYUST35VbsYtX5jHzjUX2dq1+l l7GMre7P9nS016oyoFGUDVK79KSS75wo7eUlnhXX6s6e3WJIJj+qC+IIabfljWTBqbUt lW71pSwePH13ieOmxC5hZsvq5EsrsKg7VxtDJxdCbHKTNU4P9RcOmdXC0VgvLSTT3k4R uJkA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition:mime-version :message-id:subject:cc:to:from:date; bh=YVxBbCpTUbaUrf5ixU3l36jLikbnPPUowsmKKAWtJJQ=; b=h5TQUuwwv/PUrvKVlnK2+nuK7YSD0xnvb4g9YKMKom9dpm5qq6zAJUgZBgGnXLQ1Z+ tATij/X3qlTqRelKZfJxettfGH3FZnDElIvzu5vubPKlc3hfLG31C3jmJA/+zqssKOs5 5KC/vJSbg0x1zvgdaiDY85WMI9QoX9q5gHRDSo5/WNIvQr3/b0ke2xg5rHTI13hMjth/ v+qoFpLnBqCQowUtES1BC70jRAAqxSzDfrvA67Yyes1L3EpNS3GB3ITZxu2QfOUjuMc1 Y92GNBMDOw7PmE7KuBohJHi1IQMmsbeX6Js3vBrxlfXjwG/7k2TptxV5NdJnHwqVp8VV rDtw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z14si4642359pgc.581.2019.06.19.20.13.11; Wed, 19 Jun 2019 20:13:28 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726679AbfFTDLq (ORCPT + 99 others); Wed, 19 Jun 2019 23:11:46 -0400 Received: from vmicros1.altlinux.org ([194.107.17.57]:35600 "EHLO vmicros1.altlinux.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726370AbfFTDLq (ORCPT ); Wed, 19 Jun 2019 23:11:46 -0400 Received: from mua.local.altlinux.org (mua.local.altlinux.org [192.168.1.14]) by vmicros1.altlinux.org (Postfix) with ESMTP id A345F72CC64; Thu, 20 Jun 2019 06:11:44 +0300 (MSK) Received: by mua.local.altlinux.org (Postfix, from userid 508) id 895497CCE2E; Thu, 20 Jun 2019 06:11:44 +0300 (MSK) Date: Thu, 20 Jun 2019 06:11:44 +0300 From: "Dmitry V. Levin" To: Christian Brauner Cc: linux-kernel@vger.kernel.org Subject: [PATCH] samples: make pidfd-metadata fail gracefully on older kernels Message-ID: <20190620031144.GA32182@altlinux.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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