summaryrefslogtreecommitdiff
path: root/proc.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-02-15 22:52:11 -0600
committerJeff Carr <[email protected]>2024-02-15 22:52:11 -0600
commitc9df5a7aceaecd7d2f022a6ebccc7b8a9909059b (patch)
tree8fd54055f0f44744dc53f346e42ecd46d7b45a14 /proc.go
parent98730aed8ae78d41ee45f923b5110e1912f0a2fb (diff)
start deprecating and modernizing this codev0.20.8
Diffstat (limited to 'proc.go')
-rw-r--r--proc.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/proc.go b/proc.go
new file mode 100644
index 0000000..d78fbcf
--- /dev/null
+++ b/proc.go
@@ -0,0 +1,57 @@
+package shell
+
+import (
+ "fmt"
+ "io/ioutil"
+ "strconv"
+ "strings"
+)
+
+// get your parent PID
+func GetPPID(pid int) (int, error) {
+ data, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/stat", pid))
+ if err != nil {
+ return 0, err
+ }
+
+ parts := strings.Fields(string(data))
+ if len(parts) < 4 {
+ return 0, fmt.Errorf("unexpected format of /proc/%d/stat", pid)
+ }
+
+ ppid, err := strconv.Atoi(parts[3])
+ if err != nil {
+ return 0, err
+ }
+
+ return ppid, nil
+}
+
+// get comm from proc
+func GetComm(pid int) (string, error) {
+ data, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/comm", pid))
+ if err != nil {
+ return "", err
+ }
+
+ return strings.TrimSpace(string(data)), nil
+}
+
+/*
+func main() {
+ pid := os.Getpid()
+ ppid, err := getPPID(pid)
+ if err != nil {
+ fmt.Println("Error getting PPID:", err)
+ return
+ }
+
+ comm, err := getComm(ppid)
+ if err != nil {
+ fmt.Println("Error getting comm:", err)
+ return
+ }
+
+ fmt.Println(comm)
+}
+*/