Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

login1: add NewWithConnection and RebootWithContext methods #390

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ permissions:

env:
# Minimum supported Go toolchain
ACTION_MINIMUM_TOOLCHAIN: "1.12"
ACTION_MINIMUM_TOOLCHAIN: "1.13"

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# go-systemd

[![godoc](https://img.shields.io/badge/godoc-reference-5272B4)](https://pkg.go.dev/mod/github.com/coreos/go-systemd/v22/?tab=packages)
![minimum golang 1.12](https://img.shields.io/badge/golang-1.12%2B-orange.svg)
![minimum golang 1.13](https://img.shields.io/badge/golang-1.13%2B-orange.svg)


Go bindings to systemd. The project has several packages:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/coreos/go-systemd/v22

go 1.12
go 1.13

require github.com/godbus/dbus/v5 v5.0.4
67 changes: 58 additions & 9 deletions login1/dbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package login1

import (
"context"
"fmt"
"os"
"strconv"
Expand All @@ -32,8 +33,34 @@ const (

// Conn is a connection to systemds dbus endpoint.
type Conn struct {
conn *dbus.Conn
object dbus.BusObject
conn Connection
connManager connectionManager
object Caller
}

// Objector describes functionality required from a given D-Bus connection.
type Connection interface {
Object(string, dbus.ObjectPath) dbus.BusObject
Signal(ch chan<- *dbus.Signal)
// TODO: This should be replaced with AddMatchSignal.
// See https://github.com/coreos/go-systemd/issues/388 for details.
BusObject() dbus.BusObject
}

// ConnectionManager explicitly wraps dependencies on established D-Bus connection.
type connectionManager interface {
Hello() error
Auth(authMethods []dbus.Auth) error
Close() error

Connection
}

// Caller describes required functionality from D-Bus object.
type Caller interface {
// TODO: This method should eventually be removed, as it provides no context support.
Call(method string, flags dbus.Flags, args ...interface{}) *dbus.Call
CallWithContext(ctx context.Context, method string, flags dbus.Flags, args ...interface{}) *dbus.Call
}

// New establishes a connection to the system bus and authenticates.
Expand All @@ -47,20 +74,32 @@ func New() (*Conn, error) {
return c, nil
}

// NewWithConnection creates new login1 client using given D-Bus connection.
func NewWithConnection(connection Connection) (*Conn, error) {
if connection == nil {
return nil, fmt.Errorf("no connection given")
}

return &Conn{
conn: connection,
object: connection.Object(dbusDest, dbusPath),
}, nil
}

// Close closes the dbus connection
func (c *Conn) Close() {
if c == nil {
return
}

if c.conn != nil {
c.conn.Close()
if c.conn != nil && c.connManager != nil {
c.connManager.Close()
}
}

func (c *Conn) initConnection() error {
var err error
c.conn, err = dbus.SystemBusPrivate()
c.connManager, err = dbus.SystemBusPrivate()
if err != nil {
return err
}
Expand All @@ -70,18 +109,19 @@ func (c *Conn) initConnection() error {
// libc)
methods := []dbus.Auth{dbus.AuthExternal(strconv.Itoa(os.Getuid()))}

err = c.conn.Auth(methods)
err = c.connManager.Auth(methods)
if err != nil {
c.conn.Close()
c.connManager.Close()
return err
}

err = c.conn.Hello()
err = c.connManager.Hello()
if err != nil {
c.conn.Close()
c.connManager.Close()
return err
}

c.conn = c.connManager
c.object = c.conn.Object("org.freedesktop.login1", dbus.ObjectPath(dbusPath))

return nil
Expand Down Expand Up @@ -309,6 +349,15 @@ func (c *Conn) Reboot(askForAuth bool) {
c.object.Call(dbusInterface+".Reboot", 0, askForAuth)
}

// Reboot asks logind for a reboot using given context, optionally asking for auth.
func (c *Conn) RebootWithContext(ctx context.Context, askForAuth bool) error {
if call := c.object.CallWithContext(ctx, dbusInterface+".Reboot", 0, askForAuth); call.Err != nil {
return fmt.Errorf("calling reboot: %w", call.Err)
}

return nil
}

// Inhibit takes inhibition lock in logind.
func (c *Conn) Inhibit(what, who, why, mode string) (*os.File, error) {
var fd dbus.UnixFD
Expand Down
Loading