diff options
Diffstat (limited to 'src/go/plugin/go.d/modules/logind/connection.go')
-rw-r--r-- | src/go/plugin/go.d/modules/logind/connection.go | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/go/plugin/go.d/modules/logind/connection.go b/src/go/plugin/go.d/modules/logind/connection.go new file mode 100644 index 000000000..b97387acf --- /dev/null +++ b/src/go/plugin/go.d/modules/logind/connection.go @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +//go:build linux +// +build linux + +package logind + +import ( + "context" + "time" + + "github.com/coreos/go-systemd/v22/login1" + "github.com/godbus/dbus/v5" +) + +type logindConnection interface { + Close() + + ListSessions() ([]login1.Session, error) + GetSessionProperties(dbus.ObjectPath) (map[string]dbus.Variant, error) + + ListUsers() ([]login1.User, error) + GetUserProperty(dbus.ObjectPath, string) (*dbus.Variant, error) +} + +func newLogindConnection(timeout time.Duration) (logindConnection, error) { + conn, err := login1.New() + if err != nil { + return nil, err + } + return &logindDBusConnection{ + conn: conn, + timeout: timeout, + }, nil +} + +type logindDBusConnection struct { + conn *login1.Conn + timeout time.Duration +} + +func (c *logindDBusConnection) Close() { + if c.conn != nil { + c.conn.Close() + c.conn = nil + } +} + +func (c *logindDBusConnection) ListSessions() ([]login1.Session, error) { + ctx, cancel := context.WithTimeout(context.Background(), c.timeout) + defer cancel() + + return c.conn.ListSessionsContext(ctx) +} + +func (c *logindDBusConnection) ListUsers() ([]login1.User, error) { + ctx, cancel := context.WithTimeout(context.Background(), c.timeout) + defer cancel() + + return c.conn.ListUsersContext(ctx) +} + +func (c *logindDBusConnection) GetSessionProperties(path dbus.ObjectPath) (map[string]dbus.Variant, error) { + ctx, cancel := context.WithTimeout(context.Background(), c.timeout) + defer cancel() + + return c.conn.GetSessionPropertiesContext(ctx, path) +} + +func (c *logindDBusConnection) GetUserProperty(path dbus.ObjectPath, property string) (*dbus.Variant, error) { + ctx, cancel := context.WithTimeout(context.Background(), c.timeout) + defer cancel() + + return c.conn.GetUserPropertyContext(ctx, path, property) +} |