github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/runc/libcontainer/utils/utils_unix.go (about)

     1  // +build !windows
     2  
     3  package utils
     4  
     5  import (
     6  	"io/ioutil"
     7  	"strconv"
     8  	"syscall"
     9  )
    10  
    11  func CloseExecFrom(minFd int) error {
    12  	fdList, err := ioutil.ReadDir("/proc/self/fd")
    13  	if err != nil {
    14  		return err
    15  	}
    16  	for _, fi := range fdList {
    17  		fd, err := strconv.Atoi(fi.Name())
    18  		if err != nil {
    19  			// ignore non-numeric file names
    20  			continue
    21  		}
    22  
    23  		if fd < minFd {
    24  			// ignore descriptors lower than our specified minimum
    25  			continue
    26  		}
    27  
    28  		// intentionally ignore errors from syscall.CloseOnExec
    29  		syscall.CloseOnExec(fd)
    30  		// 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)
    31  	}
    32  	return nil
    33  }