forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fds_linux.go
38 lines (34 loc) · 1.04 KB
/
fds_linux.go
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
package system
import (
"io/ioutil"
"strconv"
"syscall"
)
// Works similarly to OpenBSD's "closefrom(2)":
// The closefrom() call deletes all descriptors numbered fd and higher from
// the per-process file descriptor table. It is effectively the same as
// calling close(2) on each descriptor.
// http://www.openbsd.org/cgi-bin/man.cgi?query=closefrom&sektion=2
//
// See also http://stackoverflow.com/a/918469/433558
func CloseFdsFrom(minFd int) error {
fdList, err := ioutil.ReadDir("/proc/self/fd")
if err != nil {
return err
}
for _, fi := range fdList {
fd, err := strconv.Atoi(fi.Name())
if err != nil {
// ignore non-numeric file names
continue
}
if fd < minFd {
// ignore descriptors lower than our specified minimum
continue
}
// intentionally ignore errors from syscall.Close
syscall.Close(fd)
// the cases where this might fail are basically file descriptors that have already been closed (including and especially the one that was created when ioutil.ReadDir did the "opendir" syscall)
}
return nil
}