-
Notifications
You must be signed in to change notification settings - Fork 291
/
proc_utils.go
98 lines (80 loc) · 2.13 KB
/
proc_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// (C) Copyright 2022-2023 Intel Corporation.
//
// SPDX-License-Identifier: BSD-2-Clause-Patent
//
package common
import (
"os"
"path/filepath"
"strconv"
"strings"
"github.com/pkg/errors"
)
func readProcName(procPath string) (string, error) {
data, err := os.ReadFile(procPath)
if err != nil {
return "", errors.Wrapf(err, "failed to read %q", procPath)
}
if len(data) == 0 {
return "", errors.Wrapf(err, "%q was empty", procPath)
}
return filepath.Base(strings.Split(string(data), "\x00")[0]), nil
}
func getProcPids(procDir, searchName string) (pids []int, _ error) {
if searchName == "" {
return nil, nil
}
allProcs, err := filepath.Glob(filepath.Join(procDir, "*", "cmdline"))
if err != nil {
return nil, errors.Wrap(err, "failed to read process list")
}
for _, proc := range allProcs {
procName, err := readProcName(proc)
if err != nil {
continue
}
if procName != searchName {
continue
}
pid, err := strconv.Atoi(filepath.Base(filepath.Dir(proc)))
if err != nil {
continue
}
pids = append(pids, pid)
}
return
}
// GetProcPids returns a list of pids for the given process name.
func GetProcPids(procName string) ([]int, error) {
return getProcPids("/proc", procName)
}
func getProcName(pid int, procDir string) (string, error) {
return readProcName(filepath.Join(procDir, strconv.Itoa(pid), "cmdline"))
}
// GetProcName returns the name of the process with the given pid.
func GetProcName(pid int) (string, error) {
return getProcName(pid, "/proc")
}
func checkDupeProcess(pid int, procDir string) error {
name, err := getProcName(pid, procDir)
if err != nil {
return errors.Wrap(err, "failed to get process name")
}
pids, err := getProcPids(procDir, name)
if err != nil {
return errors.Wrap(err, "failed to read process list")
}
for _, otherPid := range pids {
if otherPid == pid {
continue
}
return errors.Errorf("another %s process is already running (pid: %d)", name, otherPid)
}
return nil
}
// CheckDupeProcess checks to see if another process with the same name as
// ours is running.
func CheckDupeProcess() error {
return checkDupeProcess(os.Getpid(), "/proc")
}