Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp4974241pxu; Tue, 13 Oct 2020 11:27:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz8dmcJNIQJJ+Af2w21eU9fbb5jNa0P5h6mNK9ux81chBXYhXqSCuE/nxW0ST+H101sSbaW X-Received: by 2002:a17:906:9389:: with SMTP id l9mr1103293ejx.462.1602613642224; Tue, 13 Oct 2020 11:27:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602613642; cv=none; d=google.com; s=arc-20160816; b=QmUWES2EKIhk8qWIb/KNFJVaasmv2bI5yQF5tJlu2EWqBFZ5v0IHTBHhCdWJBLjEPB bP4Js1PRZNqn8Lci6rV+sMPQGi6e/HhGi9b8QlT0i8pN+H+XKMn8HqFHW6fApLSrm40Z Mptc8cpnBajRZbgPuZ1NjvyEdVssC/cfhvQdKrFXljuX95AImMd6hiJGylLRkxDGKQJW 6G+bTxLElPh5DxFOOMgTsai2pgf7okZEk8uLCFm5dEKJwRfdFWFlk+LVOrUWZYM2Wgo4 7uOtMRidsuJ1rhROPXfcV2zHJqXclmvgnixqWTy/xrhhnef+Q5Y0bfhZV/NKy2BxRWc7 dE/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=s2c0z0nnK5G/Cjc6T69QlHdgqzKaRCEYH2YcGXaZfvs=; b=xECJyGyJSFnfLZJDhM1q5Jfq/Y2GDg7+FmCEx2Hdic+sOtqZ+f0xW0rK9bXp6jDaEx /afeJQJjdtr5meIf0coL1XS2xyLKlEKkptQu6lVly+uXsWb4Fb5boDwJmElOX5dBGeLP f06TyjRRgc3oLztrOOsWCBXE4k5xq5YppkNejevG9n9x/9+ed8fATU89iMtpxyIPD6+g V3BXkgiiW6JF9o1JdDX2/7nW++pbME9duGk3VO3xmvCJlvwJ9QD456fJ6JLe1kSh/9Av zzON4yxF6W7FBq82QTajBft0z0rLXU2SAxQe7P4y2rivtsPx3Kh+VeCxD/ra37S2IpfV LiJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=P4FirxbJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id mb2si378861ejb.352.2020.10.13.11.26.59; Tue, 13 Oct 2020 11:27:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=P4FirxbJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388327AbgJMOGe (ORCPT + 99 others); Tue, 13 Oct 2020 10:06:34 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:49912 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388242AbgJMOG2 (ORCPT ); Tue, 13 Oct 2020 10:06:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1602597987; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=s2c0z0nnK5G/Cjc6T69QlHdgqzKaRCEYH2YcGXaZfvs=; b=P4FirxbJIc9tN/z5XTum55FtDyvH/H098hpw1W3TAhbZ4IhJJWnqLpqaFiv+Ts8Ecv5NI2 jPX4F0VzHPYqjs9dWGLgY2/E/BMtHGW+1WlvihbhpmK94k/9MzgOpzbMeTPIr/mjO1h3IB uOQsomr4oEOWrCZzvIpiWQWGo3a7tYM= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-358-BqIk-o_9Ps67-_bglI0HLw-1; Tue, 13 Oct 2020 10:06:25 -0400 X-MC-Unique: BqIk-o_9Ps67-_bglI0HLw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CC27E1020905; Tue, 13 Oct 2020 14:06:23 +0000 (UTC) Received: from lithium.redhat.com (ovpn-112-43.ams2.redhat.com [10.36.112.43]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6B9655C1C2; Tue, 13 Oct 2020 14:06:22 +0000 (UTC) From: Giuseppe Scrivano To: linux-kernel@vger.kernel.org Cc: viro@zeniv.linux.org.uk, linux-fsdevel@vger.kernel.org, christian.brauner@ubuntu.com, containers@lists.linux-foundation.org Subject: [PATCH 2/2] selftests: add tests for CLOSE_RANGE_CLOEXEC Date: Tue, 13 Oct 2020 16:06:09 +0200 Message-Id: <20201013140609.2269319-3-gscrivan@redhat.com> In-Reply-To: <20201013140609.2269319-1-gscrivan@redhat.com> References: <20201013140609.2269319-1-gscrivan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Signed-off-by: Giuseppe Scrivano --- .../testing/selftests/core/close_range_test.c | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tools/testing/selftests/core/close_range_test.c b/tools/testing/selftests/core/close_range_test.c index c99b98b0d461..b8789262cd7d 100644 --- a/tools/testing/selftests/core/close_range_test.c +++ b/tools/testing/selftests/core/close_range_test.c @@ -23,6 +23,10 @@ #define CLOSE_RANGE_UNSHARE (1U << 1) #endif +#ifndef CLOSE_RANGE_CLOEXEC +#define CLOSE_RANGE_CLOEXEC (1U << 2) +#endif + static inline int sys_close_range(unsigned int fd, unsigned int max_fd, unsigned int flags) { @@ -224,4 +228,44 @@ TEST(close_range_unshare_capped) EXPECT_EQ(0, WEXITSTATUS(status)); } +TEST(close_range_cloexec) +{ + int i, ret; + int open_fds[101]; + + for (i = 0; i < ARRAY_SIZE(open_fds); i++) { + int fd; + + fd = open("/dev/null", O_RDONLY); + ASSERT_GE(fd, 0) { + if (errno == ENOENT) + XFAIL(return, "Skipping test since /dev/null does not exist"); + } + + open_fds[i] = fd; + } + + EXPECT_EQ(-1, sys_close_range(open_fds[0], open_fds[100], -1)) { + if (errno == ENOSYS) + XFAIL(return, "close_range() syscall not supported"); + } + + EXPECT_EQ(0, sys_close_range(open_fds[0], open_fds[50], CLOSE_RANGE_CLOEXEC)); + + for (i = 0; i <= 50; i++) { + int flags = fcntl(open_fds[i], F_GETFD); + + EXPECT_GT(flags, -1); + EXPECT_EQ(flags & FD_CLOEXEC, FD_CLOEXEC); + } + + for (i = 51; i <= 100; i++) { + int flags = fcntl(open_fds[i], F_GETFD); + + EXPECT_GT(flags, -1); + EXPECT_EQ(flags & FD_CLOEXEC, 0); + } +} + + TEST_HARNESS_MAIN -- 2.26.2