master
go 74 lines 1.71 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 //go:build linux
4
5 package logind
6
7 import (
8 "context"
9 "time"
10
11 "github.com/coreos/go-systemd/v22/login1"
12 "github.com/godbus/dbus/v5"
13 )
14
15 type logindConnection interface {
16 Close()
17
18 ListSessions() ([]login1.Session, error)
19 GetSessionProperties(dbus.ObjectPath) (map[string]dbus.Variant, error)
20
21 ListUsers() ([]login1.User, error)
22 GetUserProperty(dbus.ObjectPath, string) (*dbus.Variant, error)
23 }
24
25 func newLogindConnection(timeout time.Duration) (logindConnection, error) {
26 conn, err := login1.New()
27 if err != nil {
28 return nil, err
29 }
30 return &logindDBusConnection{
31 conn: conn,
32 timeout: timeout,
33 }, nil
34 }
35
36 type logindDBusConnection struct {
37 conn *login1.Conn
38 timeout time.Duration
39 }
40
41 func (c *logindDBusConnection) Close() {
42 if c.conn != nil {
43 c.conn.Close()
44 c.conn = nil
45 }
46 }
47
48 func (c *logindDBusConnection) ListSessions() ([]login1.Session, error) {
49 ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
50 defer cancel()
51
52 return c.conn.ListSessionsContext(ctx)
53 }
54
55 func (c *logindDBusConnection) ListUsers() ([]login1.User, error) {
56 ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
57 defer cancel()
58
59 return c.conn.ListUsersContext(ctx)
60 }
61
62 func (c *logindDBusConnection) GetSessionProperties(path dbus.ObjectPath) (map[string]dbus.Variant, error) {
63 ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
64 defer cancel()
65
66 return c.conn.GetSessionPropertiesContext(ctx, path)
67 }
68
69 func (c *logindDBusConnection) GetUserProperty(path dbus.ObjectPath, property string) (*dbus.Variant, error) {
70 ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
71 defer cancel()
72
73 return c.conn.GetUserPropertyContext(ctx, path, property)
74 }