1
0
mirror of https://github.com/opencontainers/runc.git synced 2025-07-29 06:41:12 +03:00

Windows: Factor out CloseExecFrom

Signed-off-by: John Howard <jhoward@microsoft.com>
This commit is contained in:
John Howard
2015-06-26 20:13:17 -07:00
parent b6c196fd18
commit 9f80f3f181
2 changed files with 33 additions and 26 deletions

View File

@ -0,0 +1,33 @@
// +build !windows
package utils
import (
"io/ioutil"
"strconv"
"syscall"
)
func CloseExecFrom(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.CloseOnExec
syscall.CloseOnExec(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
}