File: close_range.c

package info (click to toggle)
dtrace 2.0.5-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 24,408 kB
  • sloc: ansic: 61,247; sh: 17,997; asm: 1,717; lex: 947; awk: 754; yacc: 695; perl: 37; sed: 17; makefile: 15
file content (82 lines) | stat: -rw-r--r-- 1,767 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * Oracle Linux DTrace; close a range of fds.
 * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
 * Licensed under the Universal Permissive License v 1.0 as shown at
 * http://oss.oracle.com/licenses/upl.
 */

#include <sys/types.h>
#include <sys/resource.h>
#include <sys/syscall.h>
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>

/*
 * This is only used if glibc does not provide an implementation, in which case
 * glibc will deal with a kernel too old to implement close_range; but even if
 * it doesn't, if a sufficiently new kernel is in use, we should close all fds
 * using the syscall.
 */

int
close_range(unsigned int first, unsigned int last, unsigned int flags)
{
	DIR *fds;
	struct dirent *ent;

#ifdef __NR_close_range
	int ret;

	ret = syscall(__NR_close_range, first, last, flags);
	if (ret >= 0)
		return ret;
#endif

	fds = opendir("/proc/self/fd");
	if (fds == NULL) {
		/*
		 * No /proc/self/fd: fall back to just closing blindly.
		 */
		struct rlimit nfiles;

		if (getrlimit(RLIMIT_NOFILE, &nfiles) < 0)
			return -1;		/* errno is set for us.  */

		if (nfiles.rlim_max == 0)
			return 0;

		/*
		 * Use rlim_max rather than rlim_cur because one can
		 * lower rlim_cur after opening more than rlim_cur files,
		 * leaving files numbered higher than the limit open.
		 */
		if (last >= nfiles.rlim_max)
			last = nfiles.rlim_max - 1;

		while (first <= last)
			close(first++);

		return 0;
	}

	while (errno = 0, (ent = readdir(fds)) != NULL) {
		char *err;
		int fd;
		fd = strtol(ent->d_name, &err, 10);

		/*
		 * Don't close garbage, no matter what.
		 */
		if (*err != '\0')
			continue;

		if (fd < first || fd > last)
			continue;

		close(fd);
	}
	closedir(fds);
	return 0;
}