xref: /aosp_15_r20/external/ltp/include/lapi/close_range.h (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /* Copyright (c) 2021 SUSE LLC  */
3*49cdfc7eSAndroid Build Coastguard Worker 
4*49cdfc7eSAndroid Build Coastguard Worker #ifndef LAPI_CLOSE_RANGE__
5*49cdfc7eSAndroid Build Coastguard Worker # define LAPI_CLOSE_RANGE__
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker # include "lapi/syscalls.h"
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker # ifdef HAVE_LINUX_CLOSE_RANGE_H
10*49cdfc7eSAndroid Build Coastguard Worker #  include <linux/close_range.h>
11*49cdfc7eSAndroid Build Coastguard Worker # endif
12*49cdfc7eSAndroid Build Coastguard Worker 
13*49cdfc7eSAndroid Build Coastguard Worker # ifndef CLOSE_RANGE_UNSHARE
14*49cdfc7eSAndroid Build Coastguard Worker #  define CLOSE_RANGE_UNSHARE	(1U << 1)
15*49cdfc7eSAndroid Build Coastguard Worker # endif
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker # ifndef CLOSE_RANGE_CLOEXEC
18*49cdfc7eSAndroid Build Coastguard Worker #  define CLOSE_RANGE_CLOEXEC	(1U << 2)
19*49cdfc7eSAndroid Build Coastguard Worker # endif
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker # ifndef HAVE_CLOSE_RANGE
close_range(unsigned int fd,unsigned int max_fd,unsigned int flags)22*49cdfc7eSAndroid Build Coastguard Worker static inline int close_range(unsigned int fd, unsigned int max_fd,
23*49cdfc7eSAndroid Build Coastguard Worker 			      unsigned int flags)
24*49cdfc7eSAndroid Build Coastguard Worker {
25*49cdfc7eSAndroid Build Coastguard Worker 	return tst_syscall(__NR_close_range, fd, max_fd, flags);
26*49cdfc7eSAndroid Build Coastguard Worker }
27*49cdfc7eSAndroid Build Coastguard Worker # endif
28*49cdfc7eSAndroid Build Coastguard Worker 
close_range_supported_by_kernel(void)29*49cdfc7eSAndroid Build Coastguard Worker static inline void close_range_supported_by_kernel(void)
30*49cdfc7eSAndroid Build Coastguard Worker {
31*49cdfc7eSAndroid Build Coastguard Worker 	long ret;
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	if ((tst_kvercmp(5, 9, 0)) < 0) {
34*49cdfc7eSAndroid Build Coastguard Worker 		/* Check if the syscall is backported on an older kernel */
35*49cdfc7eSAndroid Build Coastguard Worker 		ret = syscall(__NR_close_range, 1, 0, 0);
36*49cdfc7eSAndroid Build Coastguard Worker 		if (ret == -1 && errno == ENOSYS)
37*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TCONF, "Test not supported on kernel version < v5.9");
38*49cdfc7eSAndroid Build Coastguard Worker 	}
39*49cdfc7eSAndroid Build Coastguard Worker }
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker #endif	/* LAPI_CLOSE_RANGE_H__ */
42