From 8a240d9ec45f986a3f8e7ff9e017e90c1aa28b1f Mon Sep 17 00:00:00 2001 From: "Pranay Kr. Srivastava" Date: Mon, 27 Apr 2026 12:16:25 +0530 Subject: [PATCH] FLEET-364: Add ListInhibitors API Add ListInhibitors API. Signed-off-by: Pranay Kr. Srivastava --- login1/dbus.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/login1/dbus.go b/login1/dbus.go index 1a0fd2d5..68cef3df 100644 --- a/login1/dbus.go +++ b/login1/dbus.go @@ -112,6 +112,15 @@ type User struct { Path dbus.ObjectPath } +type Inhibitor struct { + What string + Who string + Why string + Mode string + Pid int64 + Uid int64 +} + func (s Session) toInterface() []interface{} { return []interface{}{s.ID, s.UID, s.User, s.Seat, s.Path} } @@ -166,6 +175,39 @@ func userFromInterfaces(user []interface{}) (*User, error) { return &ret, nil } +func inhibitorFromInterfaces(inhibitor []interface{}) (*Inhibitor, error) { + if len(inhibitor) < 6 { + return nil, fmt.Errorf("invalid number of inhibitor fields: %d", len(inhibitor)) + } + values := []string{} + fieldToString := func(index int) error { + if val, ok := inhibitor[index].(string); !ok { + return fmt.Errorf("failed to typecast inhibitor field %d to string", index) + } else { + values[index] = val + } + return nil + } + for i := 0; i < 4; i += 1 { + if err := fieldToString(i); err != nil { + return nil, err + } + } + pid, ok := inhibitor[4].(int64) + if !ok { + return nil, fmt.Errorf("failed to typecast inhibitor field 5 to int64") + } + uid, ok := inhibitor[5].(int64) + if !ok { + return nil, fmt.Errorf("failed to typecast inhibitor field 6 to int64") + } + + return &Inhibitor{What: values[0], Who: values[1], + Why: values[2], Mode: values[3], + Pid: pid, Uid: uid, + }, nil +} + // GetActiveSession may be used to get the session object path for the current active session func (c *Conn) GetActiveSession() (dbus.ObjectPath, error) { var seat0Path dbus.ObjectPath @@ -365,6 +407,24 @@ func (c *Conn) ScheduleShutdown(typ ScheduleShutdownType, when time.Time) error return call.Err } +// ListInhibitors lists the current active inhibitors. +func (c *Conn) ListInhibitors() ([]Inhibitor, error) { + out := [][]interface{}{} + + if err := c.object.Call(dbusManagerInterface+".ListInhibitors", 0).Store(&out); err != nil { + return nil, err + } + result := []Inhibitor{} + for _, v := range out { + if inhibitor, err := inhibitorFromInterfaces(v); err != nil { + return nil, err + } else { + result = append(result, *inhibitor) + } + } + return result, nil +} + func (c *Conn) SetWallMessage(message string, enable bool) error { call := c.object.Call(dbusManagerInterface+".SetWallMessage", 0, message, enable) return call.Err