oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1//go:build !darwin
2
3package fprobe
4
5import (
6 "os"
7 "strconv"
8 "syscall"
9)
10
11// ProcArgs is, off a Mac, pid's arguments and environment from /proc (the
12// same content KERN_PROCARGS2 has on a Mac).
13func ProcArgs(pid int) ([]byte, error) {
14 dir := "/proc/" + strconv.Itoa(pid)
15 args, err := os.ReadFile(dir + "/cmdline")
16 if err != nil {
17 return nil, err
18 }
19 env, err := os.ReadFile(dir + "/environ")
20 if err != nil {
21 return nil, err
22 }
23 return append(args, env...), nil
24}
25
26// ownPIDs is, off a Mac, every process of this user from /proc.
27func ownPIDs() ([]int, error) {
28 entries, err := os.ReadDir("/proc")
29 if err != nil {
30 return nil, err
31 }
32 var out []int
33 for _, e := range entries {
34 pid, err := strconv.Atoi(e.Name())
35 if err != nil {
36 continue
37 }
38 if info, err := os.Stat("/proc/" + e.Name()); err == nil {
39 if st, ok := info.Sys().(*syscall.Stat_t); ok && int(st.Uid) == os.Getuid() {
40 out = append(out, pid)
41 }
42 }
43 }
44 return out, nil
45}
46