summaryrefslogtreecommitdiffhomepage
path: root/clientupdate/systemd_linux.go
blob: 810f7dd552e7860eb4d158196f27d2a4279cc4a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause

package clientupdate

import (
	"context"
	"errors"
	"fmt"

	"github.com/coreos/go-systemd/v22/dbus"
)

func restartSystemdUnit(ctx context.Context) error {
	c, err := dbus.NewWithContext(ctx)
	if err != nil {
		// Likely not a systemd-managed distro.
		return errors.ErrUnsupported
	}
	defer c.Close()
	if err := c.ReloadContext(ctx); err != nil {
		return fmt.Errorf("failed to reload tailsacled.service: %w", err)
	}
	ch := make(chan string, 1)
	if _, err := c.RestartUnitContext(ctx, "tailscaled.service", "replace", ch); err != nil {
		return fmt.Errorf("failed to restart tailsacled.service: %w", err)
	}
	select {
	case res := <-ch:
		if res != "done" {
			return fmt.Errorf("systemd service restart failed with result %q", res)
		}
	case <-ctx.Done():
		return ctx.Err()
	}
	return nil
}