@@ -26,9 +26,11 @@ import (
26
26
)
27
27
28
28
const (
29
- dbusDest = "org.freedesktop.login1"
30
- dbusInterface = "org.freedesktop.login1.Manager"
31
- dbusPath = "/org/freedesktop/login1"
29
+ dbusDest = "org.freedesktop.login1"
30
+ dbusManagerInterface = "org.freedesktop.login1.Manager"
31
+ dbusSessionInterface = "org.freedesktop.login1.Session"
32
+ dbusUserInterface = "org.freedesktop.login1.User"
33
+ dbusPath = "/org/freedesktop/login1"
32
34
)
33
35
34
36
// Conn is a connection to systemds dbus endpoint.
@@ -166,7 +168,7 @@ func userFromInterfaces(user []interface{}) (*User, error) {
166
168
// GetActiveSession may be used to get the session object path for the current active session
167
169
func (c * Conn ) GetActiveSession () (dbus.ObjectPath , error ) {
168
170
var seat0Path dbus.ObjectPath
169
- if err := c .object .Call (dbusInterface + ".GetSeat" , 0 , "seat0" ).Store (& seat0Path ); err != nil {
171
+ if err := c .object .Call (dbusManagerInterface + ".GetSeat" , 0 , "seat0" ).Store (& seat0Path ); err != nil {
170
172
return "" , err
171
173
}
172
174
@@ -242,7 +244,7 @@ func (c *Conn) GetSessionDisplay(sessionPath dbus.ObjectPath) (string, error) {
242
244
// GetSession may be used to get the session object path for the session with the specified ID.
243
245
func (c * Conn ) GetSession (id string ) (dbus.ObjectPath , error ) {
244
246
var out interface {}
245
- if err := c .object .Call (dbusInterface + ".GetSession" , 0 , id ).Store (& out ); err != nil {
247
+ if err := c .object .Call (dbusManagerInterface + ".GetSession" , 0 , id ).Store (& out ); err != nil {
246
248
return "" , err
247
249
}
248
250
@@ -262,7 +264,7 @@ func (c *Conn) ListSessions() ([]Session, error) {
262
264
// ListSessionsContext returns an array with all current sessions.
263
265
func (c * Conn ) ListSessionsContext (ctx context.Context ) ([]Session , error ) {
264
266
out := [][]interface {}{}
265
- if err := c .object .CallWithContext (ctx , dbusInterface + ".ListSessions" , 0 ).Store (& out ); err != nil {
267
+ if err := c .object .CallWithContext (ctx , dbusManagerInterface + ".ListSessions" , 0 ).Store (& out ); err != nil {
266
268
return nil , err
267
269
}
268
270
@@ -285,7 +287,7 @@ func (c *Conn) ListUsers() ([]User, error) {
285
287
// ListUsersContext returns an array with all currently logged-in users.
286
288
func (c * Conn ) ListUsersContext (ctx context.Context ) ([]User , error ) {
287
289
out := [][]interface {}{}
288
- if err := c .object .CallWithContext (ctx , dbusInterface + ".ListUsers" , 0 ).Store (& out ); err != nil {
290
+ if err := c .object .CallWithContext (ctx , dbusManagerInterface + ".ListUsers" , 0 ).Store (& out ); err != nil {
289
291
return nil , err
290
292
}
291
293
@@ -300,36 +302,56 @@ func (c *Conn) ListUsersContext(ctx context.Context) ([]User, error) {
300
302
return ret , nil
301
303
}
302
304
305
+ // GetSessionPropertiesContext takes a session path and returns all of its dbus object properties.
306
+ func (c * Conn ) GetSessionPropertiesContext (ctx context.Context , sessionPath dbus.ObjectPath ) (map [string ]dbus.Variant , error ) {
307
+ return c .getProperties (ctx , sessionPath , dbusSessionInterface )
308
+ }
309
+
310
+ // GetSessionPropertyContext takes a session path and a property name and returns the property value.
311
+ func (c * Conn ) GetSessionPropertyContext (ctx context.Context , sessionPath dbus.ObjectPath , property string ) (* dbus.Variant , error ) {
312
+ return c .getProperty (ctx , sessionPath , dbusSessionInterface , property )
313
+ }
314
+
315
+ // GetUserPropertiesContext takes a user path and returns all of its dbus object properties.
316
+ func (c * Conn ) GetUserPropertiesContext (ctx context.Context , userPath dbus.ObjectPath ) (map [string ]dbus.Variant , error ) {
317
+ return c .getProperties (ctx , userPath , dbusUserInterface )
318
+ }
319
+
320
+ // GetUserPropertyContext takes a user path and a property name and returns the property value.
321
+ func (c * Conn ) GetUserPropertyContext (ctx context.Context , userPath dbus.ObjectPath , property string ) (* dbus.Variant , error ) {
322
+ return c .getProperty (ctx , userPath , dbusUserInterface , property )
323
+ }
324
+
303
325
// LockSession asks the session with the specified ID to activate the screen lock.
304
326
func (c * Conn ) LockSession (id string ) {
305
- c .object .Call (dbusInterface + ".LockSession" , 0 , id )
327
+ c .object .Call (dbusManagerInterface + ".LockSession" , 0 , id )
306
328
}
307
329
308
330
// LockSessions asks all sessions to activate the screen locks. This may be used to lock any access to the machine in one action.
309
331
func (c * Conn ) LockSessions () {
310
- c .object .Call (dbusInterface + ".LockSessions" , 0 )
332
+ c .object .Call (dbusManagerInterface + ".LockSessions" , 0 )
311
333
}
312
334
313
335
// TerminateSession forcibly terminate one specific session.
314
336
func (c * Conn ) TerminateSession (id string ) {
315
- c .object .Call (dbusInterface + ".TerminateSession" , 0 , id )
337
+ c .object .Call (dbusManagerInterface + ".TerminateSession" , 0 , id )
316
338
}
317
339
318
340
// TerminateUser forcibly terminates all processes of a user.
319
341
func (c * Conn ) TerminateUser (uid uint32 ) {
320
- c .object .Call (dbusInterface + ".TerminateUser" , 0 , uid )
342
+ c .object .Call (dbusManagerInterface + ".TerminateUser" , 0 , uid )
321
343
}
322
344
323
345
// Reboot asks logind for a reboot optionally asking for auth.
324
346
func (c * Conn ) Reboot (askForAuth bool ) {
325
- c .object .Call (dbusInterface + ".Reboot" , 0 , askForAuth )
347
+ c .object .Call (dbusManagerInterface + ".Reboot" , 0 , askForAuth )
326
348
}
327
349
328
350
// Inhibit takes inhibition lock in logind.
329
351
func (c * Conn ) Inhibit (what , who , why , mode string ) (* os.File , error ) {
330
352
var fd dbus.UnixFD
331
353
332
- err := c .object .Call (dbusInterface + ".Inhibit" , 0 , what , who , why , mode ).Store (& fd )
354
+ err := c .object .Call (dbusManagerInterface + ".Inhibit" , 0 , what , who , why , mode ).Store (& fd )
333
355
if err != nil {
334
356
return nil , err
335
357
}
@@ -350,5 +372,37 @@ func (c *Conn) Subscribe(members ...string) chan *dbus.Signal {
350
372
351
373
// PowerOff asks logind for a power off optionally asking for auth.
352
374
func (c * Conn ) PowerOff (askForAuth bool ) {
353
- c .object .Call (dbusInterface + ".PowerOff" , 0 , askForAuth )
375
+ c .object .Call (dbusManagerInterface + ".PowerOff" , 0 , askForAuth )
376
+ }
377
+
378
+ func (c * Conn ) getProperties (ctx context.Context , path dbus.ObjectPath , dbusInterface string ) (map [string ]dbus.Variant , error ) {
379
+ if ! path .IsValid () {
380
+ return nil , fmt .Errorf ("invalid object path (%s)" , path )
381
+ }
382
+
383
+ obj := c .conn .Object (dbusDest , path )
384
+
385
+ var props map [string ]dbus.Variant
386
+ err := obj .CallWithContext (ctx , "org.freedesktop.DBus.Properties.GetAll" , 0 , dbusInterface ).Store (& props )
387
+ if err != nil {
388
+ return nil , err
389
+ }
390
+
391
+ return props , nil
392
+ }
393
+
394
+ func (c * Conn ) getProperty (ctx context.Context , path dbus.ObjectPath , dbusInterface , property string ) (* dbus.Variant , error ) {
395
+ if ! path .IsValid () {
396
+ return nil , fmt .Errorf ("invalid object path (%s)" , path )
397
+ }
398
+
399
+ obj := c .conn .Object (dbusDest , path )
400
+
401
+ var prop dbus.Variant
402
+ err := obj .CallWithContext (ctx , "org.freedesktop.DBus.Properties.Get" , 0 , dbusInterface , property ).Store (& prop )
403
+ if err != nil {
404
+ return nil , err
405
+ }
406
+
407
+ return & prop , nil
354
408
}
0 commit comments