func()

in internal/ps/ps_linux.go [77:123]


func (p linuxClient) FindRegex(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: %w", err)
	}

	exeExpression, err := regexp.Compile(exeMatch)
	if err != nil {
		return nil, fmt.Errorf("failed to compile process exec matching expression: %w", err)
	}

	files, err := os.ReadDir(p.procDir)
	if err != nil {
		return nil, fmt.Errorf("failed to read linux proc dir: %w", err)
	}

	for _, file := range files {
		if !file.IsDir() {
			continue
		}

		if !procExpression.MatchString(file.Name()) {
			continue
		}

		// Ignore the error due to the `procExpression` regex, which ensures that
		// the file name is a valid PID, and therefore we should not expect any
		// errors.
		pid, _ := strconv.Atoi(file.Name())

		process, err := p.readPidDetails(pid)
		if err != nil {
			galog.Debugf("Failed to read process(%d), while finding processes matching regex %q: [%v], skipping...", pid, exeMatch, err)
			continue
		}

		if !exeExpression.MatchString(process.Exe) {
			continue
		}

		result = append(result, process)
	}

	return result, nil
}