From 107f4bcf025e36d687bce63ea93e2e801687b5d7 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Sat, 5 Apr 2025 00:08:23 -0400 Subject: [PATCH] Add MacOS support --- daemon/2server-pidToPath-Darwin.go | 18 ++++++++++++++++++ daemon/2server-pidToPath-Readlink.go | 14 ++++++++++++++ daemon/2server.go | 7 ------- 3 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 daemon/2server-pidToPath-Darwin.go create mode 100644 daemon/2server-pidToPath-Readlink.go diff --git a/daemon/2server-pidToPath-Darwin.go b/daemon/2server-pidToPath-Darwin.go new file mode 100644 index 0000000..b52e9e1 --- /dev/null +++ b/daemon/2server-pidToPath-Darwin.go @@ -0,0 +1,18 @@ +//go:build darwin + +package daemon + +import ( + "os/exec" + "strconv" + "strings" +) + +// pidToPath returns the path of the executable that has the given PID. +func pidToPath(pid int) string { + pidString := strconv.Itoa(pid) + cmd := exec.Command("lsof", "-a", "-dtxt", "-p"+pidString) + output, _ := cmd.Output() + line := strings.Split(string(output), "\n")[1] + return line[strings.Index(line, "/"):] +} diff --git a/daemon/2server-pidToPath-Readlink.go b/daemon/2server-pidToPath-Readlink.go new file mode 100644 index 0000000..e759073 --- /dev/null +++ b/daemon/2server-pidToPath-Readlink.go @@ -0,0 +1,14 @@ +//go:build linux || freebsd + +package daemon + +import ( + "fmt" + "os" +) + +// pidToPath returns the path of the executable that has the given PID. +func pidToPath(pid int) string { + path, _ := os.Readlink(fmt.Sprintf("/proc/%d/"+pidPathFile, pid)) + return path +} diff --git a/daemon/2server.go b/daemon/2server.go index 042e1e4..efc3935 100644 --- a/daemon/2server.go +++ b/daemon/2server.go @@ -4,7 +4,6 @@ import ( "bytes" "crypto/sha256" "errors" - "fmt" "io" "log" "net" @@ -97,12 +96,6 @@ func handleConn(conn net.Conn) { } } -// pidToPath returns the path of the executable that has the given PID. -func pidToPath(pid int) string { - path, _ := os.Readlink(fmt.Sprintf("/proc/%d/"+pidPathFile, pid)) - return path -} - // getFileHash returns the SHA256 hash of the file at the given path. func getFileHash(path string) []byte { file, _ := os.Open(path)