-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclose_range.c
50 lines (39 loc) · 913 Bytes
/
close_range.c
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
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
int close_range(int fd, int max_fd) {
#ifdef __NR_close_range
int err = syscall(__NR_close_range, fd, max_fd, 0);
if (err >= 0)
return err;
#endif
for (int i = fd; i < max_fd; i++)
close(i);
return 0;
}
static void parse_arg(char* str, int* start, int* end) {
char* endptr = NULL;
*start = strtol(str, &endptr, 10);
*end = *start;
if (*endptr == 0)
return;
if (*endptr != ':' || !endptr[1])
errx(1, "invalid argument \"%s\"", str);
*end = strtol(endptr + 1, &endptr, 10);
}
int main(int argc, char** argv) {
for (int i = 1; i < argc; i++) {
if (!strcmp("--", argv[i])) {
if (execvp(argv[i + 1], argv + i + 1) < 0)
err(1, "execvp");
}
int start, end;
parse_arg(argv[i], &start, &end);
if (start == end)
close(start);
else
close_range(start, end);
}
errx(1, "No command given");
}