2018-02-02 07:14:27 +08:00
|
|
|
package procstat
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2021-09-29 05:16:32 +08:00
|
|
|
"os"
|
2020-06-09 04:25:59 +08:00
|
|
|
"regexp"
|
2018-02-02 07:14:27 +08:00
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2024-12-18 01:10:18 +08:00
|
|
|
gopsprocess "github.com/shirou/gopsutil/v4/process"
|
2018-02-02 07:14:27 +08:00
|
|
|
)
|
|
|
|
|
|
2022-09-09 02:49:36 +08:00
|
|
|
// NativeFinder uses gopsutil to find processes
|
2023-11-28 04:50:30 +08:00
|
|
|
type NativeFinder struct{}
|
2018-02-02 07:14:27 +08:00
|
|
|
|
2022-09-09 02:49:36 +08:00
|
|
|
// Uid will return all pids for the given user
|
2024-12-18 01:10:18 +08:00
|
|
|
func (pg *NativeFinder) uid(user string) ([]pid, error) {
|
|
|
|
|
var dst []pid
|
|
|
|
|
procs, err := gopsprocess.Processes()
|
2018-02-02 07:14:27 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return dst, err
|
|
|
|
|
}
|
|
|
|
|
for _, p := range procs {
|
|
|
|
|
username, err := p.Username()
|
|
|
|
|
if err != nil {
|
2024-09-19 17:03:28 +08:00
|
|
|
// skip, this can be caused by the pid no longer exists, or you don't have permissions to access it
|
2018-02-02 07:14:27 +08:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if username == user {
|
2024-12-18 01:10:18 +08:00
|
|
|
dst = append(dst, pid(p.Pid))
|
2018-02-02 07:14:27 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return dst, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-09 02:49:36 +08:00
|
|
|
// PidFile returns the pid from the pid file given.
|
2024-12-18 01:10:18 +08:00
|
|
|
func (pg *NativeFinder) pidFile(path string) ([]pid, error) {
|
|
|
|
|
var pids []pid
|
2021-09-29 05:16:32 +08:00
|
|
|
pidString, err := os.ReadFile(path)
|
2018-02-02 07:14:27 +08:00
|
|
|
if err != nil {
|
2023-11-01 06:03:36 +08:00
|
|
|
return pids, fmt.Errorf("failed to read pidfile %q: %w", path, err)
|
2018-02-02 07:14:27 +08:00
|
|
|
}
|
2024-12-18 01:10:18 +08:00
|
|
|
processID, err := strconv.ParseInt(strings.TrimSpace(string(pidString)), 10, 32)
|
2018-02-02 07:14:27 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return pids, err
|
|
|
|
|
}
|
2024-12-18 01:10:18 +08:00
|
|
|
pids = append(pids, pid(processID))
|
2018-02-02 07:14:27 +08:00
|
|
|
return pids, nil
|
|
|
|
|
}
|
2020-06-09 04:25:59 +08:00
|
|
|
|
2022-09-09 02:49:36 +08:00
|
|
|
// FullPattern matches on the command line when the process was executed
|
2024-12-18 01:10:18 +08:00
|
|
|
func (pg *NativeFinder) fullPattern(pattern string) ([]pid, error) {
|
|
|
|
|
var pids []pid
|
2020-06-09 04:25:59 +08:00
|
|
|
regxPattern, err := regexp.Compile(pattern)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return pids, err
|
|
|
|
|
}
|
2024-12-18 01:10:18 +08:00
|
|
|
procs, err := pg.fastProcessList()
|
2020-06-09 04:25:59 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return pids, err
|
|
|
|
|
}
|
|
|
|
|
for _, p := range procs {
|
|
|
|
|
cmd, err := p.Cmdline()
|
|
|
|
|
if err != nil {
|
2024-09-19 17:03:28 +08:00
|
|
|
// skip, this can be caused by the pid no longer exists, or you don't have permissions to access it
|
2020-06-09 04:25:59 +08:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if regxPattern.MatchString(cmd) {
|
2024-12-18 01:10:18 +08:00
|
|
|
pids = append(pids, pid(p.Pid))
|
2020-06-09 04:25:59 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return pids, err
|
|
|
|
|
}
|
2020-06-17 01:46:32 +08:00
|
|
|
|
2023-11-28 04:50:30 +08:00
|
|
|
// Children matches children pids on the command line when the process was executed
|
2024-12-18 01:10:18 +08:00
|
|
|
func (pg *NativeFinder) children(processID pid) ([]pid, error) {
|
2023-11-28 04:50:30 +08:00
|
|
|
// Get all running processes
|
2024-12-18 01:10:18 +08:00
|
|
|
p, err := gopsprocess.NewProcess(int32(processID))
|
2023-11-14 07:11:31 +08:00
|
|
|
if err != nil {
|
2024-12-18 01:10:18 +08:00
|
|
|
return nil, fmt.Errorf("getting process %d failed: %w", processID, err)
|
2023-11-14 07:11:31 +08:00
|
|
|
}
|
|
|
|
|
|
2023-11-28 04:50:30 +08:00
|
|
|
// Get all children of the current process
|
|
|
|
|
children, err := p.Children()
|
2023-11-14 07:11:31 +08:00
|
|
|
if err != nil {
|
2023-11-28 04:50:30 +08:00
|
|
|
return nil, fmt.Errorf("unable to get children of process %d: %w", p.Pid, err)
|
2023-11-14 07:11:31 +08:00
|
|
|
}
|
2024-12-18 01:10:18 +08:00
|
|
|
pids := make([]pid, 0, len(children))
|
2023-11-28 04:50:30 +08:00
|
|
|
for _, child := range children {
|
2024-12-18 01:10:18 +08:00
|
|
|
pids = append(pids, pid(child.Pid))
|
2023-11-14 07:11:31 +08:00
|
|
|
}
|
2023-11-28 04:50:30 +08:00
|
|
|
|
2023-11-14 07:11:31 +08:00
|
|
|
return pids, err
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-18 01:10:18 +08:00
|
|
|
func (pg *NativeFinder) fastProcessList() ([]*gopsprocess.Process, error) {
|
|
|
|
|
pids, err := gopsprocess.Pids()
|
2020-06-17 01:46:32 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-18 01:10:18 +08:00
|
|
|
result := make([]*gopsprocess.Process, 0, len(pids))
|
2022-12-12 22:05:33 +08:00
|
|
|
for _, pid := range pids {
|
2024-12-18 01:10:18 +08:00
|
|
|
result = append(result, &gopsprocess.Process{Pid: pid})
|
2020-06-17 01:46:32 +08:00
|
|
|
}
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
2023-11-28 04:50:30 +08:00
|
|
|
|
|
|
|
|
// Pattern matches on the process name
|
2024-12-18 01:10:18 +08:00
|
|
|
func (pg *NativeFinder) pattern(pattern string) ([]pid, error) {
|
|
|
|
|
var pids []pid
|
2023-11-28 04:50:30 +08:00
|
|
|
regxPattern, err := regexp.Compile(pattern)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return pids, err
|
|
|
|
|
}
|
2024-12-18 01:10:18 +08:00
|
|
|
procs, err := pg.fastProcessList()
|
2023-11-28 04:50:30 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return pids, err
|
|
|
|
|
}
|
|
|
|
|
for _, p := range procs {
|
|
|
|
|
name, err := processName(p)
|
|
|
|
|
if err != nil {
|
2024-09-19 17:03:28 +08:00
|
|
|
// skip, this can be caused by the pid no longer exists, or you don't have permissions to access it
|
2023-11-28 04:50:30 +08:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if regxPattern.MatchString(name) {
|
2024-12-18 01:10:18 +08:00
|
|
|
pids = append(pids, pid(p.Pid))
|
2023-11-28 04:50:30 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return pids, err
|
|
|
|
|
}
|