@@ -19,11 +19,13 @@ import (
1919 "fmt"
2020 "os"
2121 "strconv"
22+ "strings"
2223
2324 "github.com/godbus/dbus/v5"
2425)
2526
2627const (
28+ dbusDest = "org.freedesktop.login1"
2729 dbusInterface = "org.freedesktop.login1.Manager"
2830 dbusPath = "/org/freedesktop/login1"
2931)
@@ -155,6 +157,82 @@ func userFromInterfaces(user []interface{}) (*User, error) {
155157 return & ret , nil
156158}
157159
160+ // GetActiveSession may be used to get the session object path for the current active session
161+ func (c * Conn ) GetActiveSession () (dbus.ObjectPath , error ) {
162+ var seat0Path dbus.ObjectPath
163+ if err := c .object .Call (dbusInterface + ".GetSeat" , 0 , "seat0" ).Store (& seat0Path ); err != nil {
164+ return "" , err
165+ }
166+
167+ seat0Obj := c .conn .Object (dbusDest , seat0Path )
168+ activeSession , err := seat0Obj .GetProperty (dbusDest + ".Seat.ActiveSession" )
169+ if err != nil {
170+ return "" , err
171+ }
172+ activeSessionMap , ok := activeSession .Value ().([]interface {})
173+ if ! ok || len (activeSessionMap ) < 2 {
174+ return "" , fmt .Errorf ("failed to typecast active session map" )
175+ }
176+
177+ activeSessionPath , ok := activeSessionMap [1 ].(dbus.ObjectPath )
178+ if ! ok {
179+ return "" , fmt .Errorf ("failed to typecast dbus active session Path" )
180+ }
181+ return activeSessionPath , nil
182+ }
183+
184+ // GetSessionUser may be used to get the user of specific session
185+ func (c * Conn ) GetSessionUser (sessionPath dbus.ObjectPath ) (* User , error ) {
186+ if len (sessionPath ) == 0 {
187+ return nil , fmt .Errorf ("empty sessionPath" )
188+ }
189+
190+ activeSessionObj := c .conn .Object (dbusDest , sessionPath )
191+ sessionUserName , err := activeSessionObj .GetProperty (dbusDest + ".Session.Name" )
192+ if err != nil {
193+ return nil , err
194+ }
195+
196+ sessionUser , err := activeSessionObj .GetProperty (dbusDest + ".Session.User" )
197+ if err != nil {
198+ return nil , err
199+ }
200+ dbusUser , ok := sessionUser .Value ().([]interface {})
201+ if ! ok {
202+ return nil , fmt .Errorf ("failed to typecast dbus session user" )
203+ }
204+
205+ if len (dbusUser ) < 2 {
206+ return nil , fmt .Errorf ("invalid number of user fields: %d" , len (dbusUser ))
207+ }
208+ uid , ok := dbusUser [0 ].(uint32 )
209+ if ! ok {
210+ return nil , fmt .Errorf ("failed to typecast user field 0 to uint32" )
211+ }
212+ path , ok := dbusUser [1 ].(dbus.ObjectPath )
213+ if ! ok {
214+ return nil , fmt .Errorf ("failed to typecast user field 1 to ObjectPath" )
215+ }
216+
217+ user := User {UID : uid , Name : strings .Trim (sessionUserName .String (), "\" " ), Path : path }
218+
219+ return & user , nil
220+ }
221+
222+ // GetSessionDisplay may be used to get the display for specific session
223+ func (c * Conn ) GetSessionDisplay (sessionPath dbus.ObjectPath ) (string , error ) {
224+ if len (sessionPath ) == 0 {
225+ return "" , fmt .Errorf ("empty sessionPath" )
226+ }
227+ sessionObj := c .conn .Object (dbusDest , sessionPath )
228+ display , err := sessionObj .GetProperty (dbusDest + ".Session.Display" )
229+ if err != nil {
230+ return "" , err
231+ }
232+
233+ return strings .Trim (display .String (), "\" " ), nil
234+ }
235+
158236// GetSession may be used to get the session object path for the session with the specified ID.
159237func (c * Conn ) GetSession (id string ) (dbus.ObjectPath , error ) {
160238 var out interface {}
0 commit comments