diff options
Diffstat (limited to 'sd_login/systemd_process.go')
-rw-r--r-- | sd_login/systemd_process.go | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/sd_login/systemd_process.go b/sd_login/systemd_process.go new file mode 100644 index 0000000..672eb2e --- /dev/null +++ b/sd_login/systemd_process.go @@ -0,0 +1,171 @@ +// ./systemd_process.go.gen +// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT + +package sd_login + +import ( + "net" + "os" + "strconv" + + "golang.org/x/sys/unix" +) + +// A ProcessID represents a process. +type ProcessID int + +func (pid ProcessID) isValid() bool { + return pid >= 1 +} + +func GetPeer(conn *net.UnixConn) (ProcessID, error) { + ucred, err := getpeercred(conn) + if err != nil { + return -1, err + } + return ProcessID(ucred.Pid), nil +} + +func (pid ProcessID) getSysCgroupLeaf() _CgroupLeaf { + cgroup, err := pid.getCgroup() + if err != nil { + return "" + } + cgTree, err := cgGetRootPath() + if err != nil { + return "" + } + return cgTree.Parse(cgroup) +} + +func (pid ProcessID) GetSession() (v SessionName, err error) { + if !pid.isValid() { + err = unix.EINVAL + return + } + + vStr, ok := trimPrefixSuffix(pid.getSysCgroupLeaf().GetUnit(), "session-", ".scope") + _v := SessionName(vStr) + + if !ok || _v.isValid() { + v = _v + } else { + err = unix.ENXIO + } + return +} + +func (pid ProcessID) GetOwner() (v UserID, err error) { + if !pid.isValid() { + err = unix.EINVAL + return + } + + uidStr, ok := trimPrefixSuffix(pid.getSysCgroupLeaf().GetSlice(), "user-", ".slice") + if !ok { + err = unix.ENXIO + return + } + uidInt, err := strconv.Atoi(uidStr) + if err != nil { + err = unix.ENXIO + return + } + _v := UserID(uidInt) + + if _v.isValid() { + v = _v + } else { + err = unix.ENXIO + } + return +} + +func (pid ProcessID) GetMachine() (v MachineName, err error) { + if !pid.isValid() { + err = unix.EINVAL + return + } + + unit := pid.getSysCgroupLeaf().GetUnit() + if unit == "" { + err = unix.ENXIO + return + } + machine, err := os.Readlink("/run/systemd/machines/unit:" + unit) + if err != nil { + return + } + _v := MachineName(machine) + + if _v.isValid() { + v = _v + } else { + err = unix.ENXIO + } + return +} + +func (pid ProcessID) GetSlice() (v string, err error) { + if !pid.isValid() { + err = unix.EINVAL + return + } + + v = pid.getSysCgroupLeaf().GetSlice() + if v == "" { + err = unix.ENXIO + } + return +} + +func (pid ProcessID) GetUnit() (v string, err error) { + if !pid.isValid() { + err = unix.EINVAL + return + } + + v = pid.getSysCgroupLeaf().GetUnit() + if v == "" { + err = unix.ENXIO + } + return +} + +func (pid ProcessID) GetUserSlice() (v string, err error) { + if !pid.isValid() { + err = unix.EINVAL + return + } + + sysLeaf := pid.getSysCgroupLeaf() + if !valid_user_tree(sysLeaf.GetUnit()) { + err = unix.ENXIO + } + userLeaf := sysLeaf.GetRest() + v = userLeaf.GetSlice() + + if v == "" { + err = unix.ENXIO + } + return +} + +func (pid ProcessID) GetUserUnit() (v string, err error) { + if !pid.isValid() { + err = unix.EINVAL + return + } + + sysLeaf := pid.getSysCgroupLeaf() + if !valid_user_tree(sysLeaf.GetUnit()) { + err = unix.ENXIO + } + userLeaf := sysLeaf.GetRest() + v = userLeaf.GetUnit() + + if v == "" { + err = unix.ENXIO + } + return +} |