Skip to content

Commit d53122b

Browse files
committed
Added methods to get active session and to retrieve user and display from a specific session
1 parent 2d78030 commit d53122b

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

login1/dbus.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
)
2525

2626
const (
27+
dbusDest = "org.freedesktop.login1"
2728
dbusInterface = "org.freedesktop.login1.Manager"
2829
dbusPath = "/org/freedesktop/login1"
2930
)
@@ -155,6 +156,72 @@ func userFromInterfaces(user []interface{}) (*User, error) {
155156
return &ret, nil
156157
}
157158

159+
//GetActiveSession may be used to get the session object path for the current active session
160+
func (c *Conn) GetActiveSession() (dbus.ObjectPath, error) {
161+
var activeSessionPath dbus.ObjectPath
162+
var seat0Path dbus.ObjectPath
163+
err := c.object.Call(dbusInterface+".GetSeat", 0, "seat0").Store(&seat0Path)
164+
165+
if err != nil {
166+
return activeSessionPath, err
167+
}
168+
169+
seat0Obj := c.conn.Object(dbusDest, seat0Path)
170+
activeSession, err := seat0Obj.GetProperty(dbusDest + ".Seat.ActiveSession")
171+
if err != nil {
172+
return activeSessionPath, err
173+
}
174+
175+
activeSessionPath = activeSession.Value().([]interface{})[1].(dbus.ObjectPath)
176+
return activeSessionPath, nil
177+
}
178+
179+
//GetSessionUser may be used to get the user of specific session
180+
func (c *Conn) GetSessionUser(sessionPath dbus.ObjectPath) (*User, error) {
181+
var user User
182+
activeSessionObj := c.conn.Object(dbusDest, sessionPath)
183+
sessionUserName, err := activeSessionObj.GetProperty(dbusDest + ".Session.Name")
184+
if err != nil {
185+
return &user, err
186+
}
187+
188+
sessionUser, err := activeSessionObj.GetProperty(dbusDest + ".Session.User")
189+
if err != nil {
190+
return &user, err
191+
}
192+
dbusUser, ok := sessionUser.Value().([]interface{})
193+
if !ok {
194+
return &user, fmt.Errorf("failed to typecast dbus session user")
195+
}
196+
197+
if len(dbusUser) < 2 {
198+
return nil, fmt.Errorf("invalid number of user fields: %d", len(dbusUser))
199+
}
200+
uid, ok := dbusUser[0].(uint32)
201+
if !ok {
202+
return nil, fmt.Errorf("failed to typecast user field 0 to uint32")
203+
}
204+
path, ok := dbusUser[1].(dbus.ObjectPath)
205+
if !ok {
206+
return nil, fmt.Errorf("failed to typecast user field 1 to ObjectPath")
207+
}
208+
209+
user = User{UID: uid, Name: sessionUserName.String(), Path: path}
210+
211+
return &user, err
212+
}
213+
214+
//GetSessionDisplay may be used to get the display for specific session
215+
func (c *Conn) GetSessionDisplay(sessionPath dbus.ObjectPath) (string, error) {
216+
sessionObj := c.conn.Object(dbusDest, sessionPath)
217+
display, err := sessionObj.GetProperty(dbusDest + ".Session.Display")
218+
if err != nil {
219+
return "", err
220+
}
221+
222+
return display.String(), err
223+
}
224+
158225
// GetSession may be used to get the session object path for the session with the specified ID.
159226
func (c *Conn) GetSession(id string) (dbus.ObjectPath, error) {
160227
var out interface{}

0 commit comments

Comments
 (0)