in google_guest_agent/ps/ps_linux.go [47:116]
func (p LinuxClient) Find(exeMatch string) ([]Process, error) {
var result []Process
procExpression, err := regexp.Compile("^[0-9]*$")
if err != nil {
return nil, fmt.Errorf("failed to compile process dir expression: %+v", err)
}
exeExpression, err := regexp.Compile(exeMatch)
if err != nil {
return nil, fmt.Errorf("failed to compile process exec matching expression: %+v", err)
}
files, err := os.ReadDir(linuxProcDir)
if err != nil {
return nil, fmt.Errorf("failed to read linux proc dir: %+v", err)
}
for _, file := range files {
if !file.IsDir() {
continue
}
if !procExpression.MatchString(file.Name()) {
continue
}
processRootDir := path.Join(linuxProcDir, file.Name())
exeLinkPath := path.Join(processRootDir, "exe")
exePath, err := os.Readlink(exeLinkPath)
if err != nil {
continue
}
if !exeExpression.MatchString(exePath) {
continue
}
cmdlinePath := path.Join(processRootDir, "cmdline")
dat, err := os.ReadFile(cmdlinePath)
if err != nil {
return nil, fmt.Errorf("error reading cmdline file: %v", err)
}
var commandLine []string
var token []byte
for _, curr := range dat {
if curr == 0 {
commandLine = append(commandLine, string(token))
token = nil
} else {
token = append(token, curr)
}
}
pid, err := strconv.Atoi(file.Name())
if err != nil {
return nil, fmt.Errorf("error parsing PID: %v", err)
}
result = append(result, Process{
Pid: pid,
Exe: exePath,
CommandLine: commandLine,
})
}
return result, nil
}