2022-06-09 08:35:02 -04:00
|
|
|
package zenutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2022-06-13 18:46:03 -04:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2022-06-09 08:35:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ParseWindowId is internal.
|
|
|
|
func ParseWindowId(id string) any {
|
|
|
|
if pid, err := strconv.ParseUint(id, 0, 64); err == nil {
|
|
|
|
return int(pid)
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetParentWindowId is internal.
|
|
|
|
func GetParentWindowId() int {
|
|
|
|
pid := os.Getppid()
|
|
|
|
for {
|
2022-06-13 18:46:03 -04:00
|
|
|
kinfo, err := unix.SysctlKinfoProc("kern.proc.pid", pid)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
ppid := kinfo.Eproc.Ppid
|
2022-06-09 08:35:02 -04:00
|
|
|
switch ppid {
|
|
|
|
case 0:
|
|
|
|
return 0
|
|
|
|
case 1:
|
|
|
|
return pid
|
|
|
|
default:
|
2022-06-13 18:46:03 -04:00
|
|
|
pid = int(ppid)
|
2022-06-09 08:35:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|