Skip to content

Commit dba2d33

Browse files
committed
Consolidate init-system detection into shared inits package
1 parent 6b0571b commit dba2d33

3 files changed

Lines changed: 46 additions & 35 deletions

File tree

cmd/cloudflared/inits/inits.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Package inits provides detection of the init system managing the host
2+
// (systemd, OpenRC, or SysV). It is shared by the service installer and the
3+
// auto-updater so that init-system detection lives in a single place.
4+
//
5+
// The functions are safe to call on any GOOS; on non-Linux platforms they
6+
// report that no Linux init system is in use.
7+
package inits
8+
9+
import (
10+
"os"
11+
"runtime"
12+
)
13+
14+
// IsSystemd reports whether the host is managed by systemd.
15+
func IsSystemd() bool {
16+
_, err := os.Stat("/run/systemd/system")
17+
return err == nil
18+
}
19+
20+
// IsOpenRC reports whether the host is managed by OpenRC.
21+
func IsOpenRC() bool {
22+
for _, path := range []string{"/sbin/openrc-run", "/usr/sbin/openrc-run", "/usr/bin/openrc-run"} {
23+
if _, err := os.Stat(path); err == nil {
24+
return true
25+
}
26+
}
27+
return false
28+
}
29+
30+
// IsSysV reports whether the host relies on a SysV-style init system, i.e. a
31+
// Linux host that is managed by neither systemd nor OpenRC. systemd and OpenRC
32+
// keep the service alive themselves, so only SysV needs the process to restart
33+
// itself after an auto-update.
34+
func IsSysV() bool {
35+
if runtime.GOOS != "linux" {
36+
return false
37+
}
38+
return !IsSystemd() && !IsOpenRC()
39+
}

cmd/cloudflared/linux_service.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"github.com/urfave/cli/v2"
1313

1414
"github.com/cloudflare/cloudflared/cmd/cloudflared/cliutil"
15+
"github.com/cloudflare/cloudflared/cmd/cloudflared/inits"
1516
"github.com/cloudflare/cloudflared/cmd/cloudflared/tunnel"
16-
"github.com/cloudflare/cloudflared/cmd/cloudflared/updater"
1717
"github.com/cloudflare/cloudflared/config"
1818
"github.com/cloudflare/cloudflared/logger"
1919
)
@@ -235,11 +235,6 @@ var noUpdateServiceFlag = &cli.BoolFlag{
235235
Value: false,
236236
}
237237

238-
func isSystemd() bool {
239-
_, err := os.Stat("/run/systemd/system")
240-
return err == nil
241-
}
242-
243238
func installLinuxService(c *cli.Context) error {
244239
log := logger.CreateLoggerFromContext(c, logger.EnableTerminalLog)
245240

@@ -269,10 +264,10 @@ func installLinuxService(c *cli.Context) error {
269264
templateArgs.ExtraArgs = extraArgs
270265

271266
switch {
272-
case isSystemd():
267+
case inits.IsSystemd():
273268
log.Info().Msgf("Using Systemd")
274269
err = installSystemd(&templateArgs, autoUpdate, log)
275-
case updater.IsOpenRC():
270+
case inits.IsOpenRC():
276271
log.Info().Msgf("Using OpenRC")
277272
err = installOpenRC(&templateArgs, autoUpdate)
278273
default:
@@ -417,10 +412,10 @@ func uninstallLinuxService(c *cli.Context) error {
417412

418413
var err error
419414
switch {
420-
case isSystemd():
415+
case inits.IsSystemd():
421416
log.Info().Msg("Using Systemd")
422417
err = uninstallSystemd(log)
423-
case updater.IsOpenRC():
418+
case inits.IsOpenRC():
424419
log.Info().Msg("Using OpenRC")
425420
err = uninstallOpenRC(log)
426421
default:

cmd/cloudflared/updater/update.go

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
"github.com/cloudflare/cloudflared/cmd/cloudflared/cliutil"
1818
cfdflags "github.com/cloudflare/cloudflared/cmd/cloudflared/flags"
19+
"github.com/cloudflare/cloudflared/cmd/cloudflared/inits"
1920
"github.com/cloudflare/cloudflared/config"
2021
"github.com/cloudflare/cloudflared/logger"
2122
)
@@ -248,7 +249,7 @@ func (a *AutoUpdater) Run(ctx context.Context) error {
248249
updateOutcome := loggedUpdate(a.log, updateOptions{updateDisabled: !a.configurable.enabled})
249250
if updateOutcome.Updated {
250251
buildInfo.CloudflaredVersion = updateOutcome.Version
251-
if IsSysV() {
252+
if inits.IsSysV() {
252253
// SysV doesn't have a mechanism to keep service alive, we have to restart the process
253254
a.log.Info().Msg("Restarting service managed by SysV...")
254255
pid, err := a.listeners.StartProcess()
@@ -300,27 +301,3 @@ func wasInstalledFromPackageManager() bool {
300301
func isRunningFromTerminal() bool {
301302
return term.IsTerminal(int(os.Stdout.Fd()))
302303
}
303-
304-
func IsSysV() bool {
305-
if runtime.GOOS != "linux" {
306-
return false
307-
}
308-
309-
// systemd and OpenRC keep the service alive, so let them restart it.
310-
if _, err := os.Stat("/run/systemd/system"); err == nil {
311-
return false
312-
}
313-
if IsOpenRC() {
314-
return false
315-
}
316-
return true
317-
}
318-
319-
func IsOpenRC() bool {
320-
for _, path := range []string{"/sbin/openrc-run", "/usr/sbin/openrc-run", "/usr/bin/openrc-run"} {
321-
if _, err := os.Stat(path); err == nil {
322-
return true
323-
}
324-
}
325-
return false
326-
}

0 commit comments

Comments
 (0)