-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
97 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
var ErrNoProcessFound = errors.New("no process found") | ||
|
||
// findProcessWithPrefix finds a process with the given prefix in its command line | ||
func findProcessWithPrefix(prefix string) (int, error) { | ||
d, err := os.Open("/proc") | ||
if err != nil { | ||
return 0, err | ||
} | ||
defer d.Close() | ||
|
||
for { | ||
names, err := d.Readdirnames(10) | ||
if err == io.EOF { | ||
break | ||
} | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
for _, name := range names { | ||
// We only care if the name starts with a numeric | ||
if name[0] < '0' || name[0] > '9' { | ||
continue | ||
} | ||
|
||
// From this point forward, any errors we just ignore, because | ||
// it might simply be that the process doesn't exist anymore. | ||
pid, err := strconv.ParseInt(name, 10, 0) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
cmdline, err := readCmdline(int(pid)) | ||
if err != nil { | ||
continue | ||
} | ||
if strings.HasPrefix(cmdline, prefix) { | ||
return int(pid), nil | ||
} | ||
} | ||
} | ||
return 0, ErrNoProcessFound | ||
} | ||
|
||
// readCmdline reads the command line of a process | ||
func readCmdline(pid int) (string, error) { | ||
cmdlinePath := fmt.Sprintf("/proc/%d/cmdline", pid) | ||
dataBytes, err := os.ReadFile(cmdlinePath) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(dataBytes), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters