Skip to content

Commit

Permalink
Add non-dbus alternative to detect system shutdown
Browse files Browse the repository at this point in the history
Signed-off-by: keliramu <ramunas.keliuotis@nordsec.com>
  • Loading branch information
keliramu committed May 24, 2024
1 parent 2cea95a commit 8ab8cfd
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cmd/daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ func main() {
go rpc.StartJobs()
go meshService.StartJobs()
rpc.StartKillSwitch()
go rpc.RunSystemShutdownMonitor()
go rpc.StartSystemShutdownMonitor()

if cfg.AutoConnect {
go rpc.StartAutoConnect(network.ExponentialBackoff)
Expand Down
18 changes: 12 additions & 6 deletions daemon/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,21 @@ func (r *RPC) StopKillSwitch() error {
return fmt.Errorf("loading daemon config: %w", err)
}

// do not unset killswitch rules if system is in shutdown or reboot
if cfg.KillSwitch && !r.systemShutdown.Load() {
if err := r.netw.UnsetKillSwitch(); err != nil {
return fmt.Errorf("unsetting killswitch: %w", err)
if cfg.KillSwitch {
// do not unset killswitch rules if system is in shutdown or reboot
shutdownIsActive := (r.dbusAvailable.Load() && r.systemShutdown.Load()) ||
(!r.dbusAvailable.Load() && internal.IsSystemShutdown())
if !shutdownIsActive {
if err := r.netw.UnsetKillSwitch(); err != nil {
return fmt.Errorf("unsetting killswitch: %w", err)
}
}
}
return nil
}

// RunSystemShutdownMonitor to be run on separate goroutine
func (r *RPC) RunSystemShutdownMonitor() {
// StartSystemShutdownMonitor to be run on separate goroutine
func (r *RPC) StartSystemShutdownMonitor() {
// get connection to system dbus
conn, err := dbus.SystemBus()
if err != nil {
Expand All @@ -101,6 +105,8 @@ func (r *RPC) RunSystemShutdownMonitor() {
}
defer conn.Close()

r.dbusAvailable.Store(true)

// register dbus signal monitor
err = conn.AddMatchSignal(
dbus.WithMatchInterface("org.freedesktop.systemd1.Manager"),
Expand Down
1 change: 1 addition & 0 deletions daemon/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type RPC struct {
analytics events.Analytics
norduser service.NorduserService
meshRegistry mesh.Registry
dbusAvailable atomic.Bool
systemShutdown atomic.Bool
pb.UnimplementedDaemonServer
}
Expand Down
9 changes: 0 additions & 9 deletions internal/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,15 +442,6 @@ func CliDimensions() ([]string, error) {
return strings.Split(strings.Trim(string(out), "\n"), " "), nil
}

// IsServiceActive check if given service is active
func IsServiceActive(service string) bool {
out, err := exec.Command(SystemctlExec, "is-active", service).Output()
if err != nil {
return false
}
return "active" == strings.Trim(strings.Trim(string(out), "\n"), " ")
}

// MachineID return unique machine identification id
func MachineID() uuid.UUID {
// systemd machine id
Expand Down
20 changes: 20 additions & 0 deletions internal/systemd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package internal

import (
"os/exec"
"strings"
)

// IsServiceActive check if given service is active
func IsServiceActive(service string) bool {
out, err := exec.Command(SystemctlExec, "is-active", service).Output()
if err != nil {
return false
}
return "active" == strings.Trim(strings.Trim(string(out), "\n"), " ")
}

// IsSystemShutdown detect if system is being shutdown
func IsSystemShutdown() bool {
return FileExists("/run/nologin") || FileExists("/var/run/nologin")
}

0 comments on commit 8ab8cfd

Please sign in to comment.