github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/plugin/builtin/shell/subtreee_unix.go (about)

     1  // +build linux solaris
     2  
     3  package shell
     4  
     5  import (
     6  	"io"
     7  	"os"
     8  	"strconv"
     9  )
    10  
    11  // listProc() returns a list of active pids on the system, by listing the contents of /proc
    12  // and looking for entries that appear to be valid pids. Only usable on systems with a /proc
    13  // filesystem (Solaris and UNIX/Linux)
    14  func listProc() ([]int, error) {
    15  	d, err := os.Open("/proc")
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  	defer d.Close()
    20  
    21  	results := make([]int, 0, 50)
    22  	for {
    23  		fis, err := d.Readdir(10)
    24  		if err == io.EOF {
    25  			break
    26  		}
    27  		if err != nil {
    28  			return nil, err
    29  		}
    30  
    31  		for _, fi := range fis {
    32  			// Pid must be a directory with a numeric name
    33  			if !fi.IsDir() {
    34  				continue
    35  			}
    36  
    37  			// Using Atoi here will also filter out . and ..
    38  			pid, err := strconv.Atoi(fi.Name())
    39  			if err != nil {
    40  				continue
    41  			}
    42  			results = append(results, pid)
    43  		}
    44  	}
    45  	return results, nil
    46  }