// ./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 }