
ADDED: * ispriv, which returns some information useful for determining if running with extra permissions, in sudo, etc.
69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
//go:build unix
|
|
|
|
package ispriv
|
|
|
|
import (
|
|
`os`
|
|
|
|
`github.com/shirou/gopsutil/v4/process`
|
|
)
|
|
|
|
/*
|
|
GetProcIDs returns a ProcIDs from a given PID. An error will be raised if the process ID doesn't exist.
|
|
A negative value indicates "self" (see also GetProcIDsSelf).
|
|
|
|
Note that if you are not EUID == 0 (root) or you/the sudo target user does not own the process,
|
|
the returning ProcIDs is HIGHLY LIKELY to be very inaccurate.
|
|
*/
|
|
func GetProcIDs(pid int32) (p *ProcIDs, err error) {
|
|
|
|
var proc ProcIDs
|
|
var ids []uint32
|
|
|
|
if pid < 0 {
|
|
pid = int32(os.Getpid())
|
|
}
|
|
|
|
if proc.proc, err = process.NewProcess(pid); err != nil {
|
|
return
|
|
}
|
|
if ids, err = proc.proc.Gids(); err != nil {
|
|
return
|
|
}
|
|
p.gids = &IdInfo{
|
|
real: uint(ids[0]),
|
|
effective: uint(ids[1]),
|
|
savedSet: uint(ids[2]),
|
|
filesystem: nil,
|
|
}
|
|
if len(ids) == 4 {
|
|
p.gids.filesystem = new(uint)
|
|
*p.gids.filesystem = uint(ids[3])
|
|
}
|
|
if ids, err = proc.proc.Uids(); err != nil {
|
|
return
|
|
}
|
|
p.uids = &IdInfo{
|
|
real: uint(ids[0]),
|
|
effective: uint(ids[1]),
|
|
savedSet: uint(ids[2]),
|
|
filesystem: nil,
|
|
}
|
|
if len(ids) == 4 {
|
|
p.uids.filesystem = new(uint)
|
|
*p.uids.filesystem = uint(ids[3])
|
|
}
|
|
|
|
p = &proc
|
|
|
|
return
|
|
}
|
|
|
|
// GetProcIDsSelf returns a ProcIDs from the current process' PID.
|
|
func GetProcIDsSelf() (p *ProcIDs, err error) {
|
|
|
|
p, err = GetProcIDs(int32(os.Getpid()))
|
|
|
|
return
|
|
}
|