Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(endpoint): quote paths in generated systemd ExecStart lines

shukieshahf4badd9a3bec
diff --git a/cli/beacon/internal/endpoint/service/service_test.go b/cli/beacon/internal/endpoint/service/service_test.go
index de028942..ca0de76e 100644
--- a/cli/beacon/internal/endpoint/service/service_test.go
+++ b/cli/beacon/internal/endpoint/service/service_test.go
@@ -387,7 +387,8 @@ func TestSystemdLabelAndUnitPath(t *testing.T) {
func TestSystemdUnitPreservesLaunchdSemantics(t *testing.T) {
content := unitFile("/opt/beacon/bin/beacon-otelcol", "/etc/beacon/endpoint/otelcol.yaml", false)
for _, want := range []string{
- "ExecStart=/opt/beacon/bin/beacon-otelcol --config /etc/beacon/endpoint/otelcol.yaml",
+ // Quoted, so systemd takes each path as one argument -- see systemdArg.
+ `ExecStart="/opt/beacon/bin/beacon-otelcol" --config "/etc/beacon/endpoint/otelcol.yaml"`,
"Restart=always", // KeepAlive
"WantedBy=multi-user.target", // RunAtLoad
"StandardOutput=journal", // replaces /tmp/<label>.out
@@ -636,3 +637,53 @@ func TestEnableLingerNeverReportsAnEmptyDetailWhenItApplies(t *testing.T) {
t.Error("without systemd as PID 1, EnableLinger should still say why it did nothing")
}
}
+
+// systemd splits ExecStart on whitespace, so a path containing a space silently becomes two
+// arguments and the unit fails with "No such file or directory". That is not an exotic input: a
+// user-mode install lives under the user's home, and --collector takes an arbitrary path.
+func TestSystemdUnitQuotesExecStartArguments(t *testing.T) {
+ program := "/home/first last/.beacon/endpoint/beacon-otelcol"
+ config := "/home/first last/.beacon/endpoint/otelcol.yaml"
+ unit := unitFile(program, config, true)
+
+ var execStart string
+ for _, line := range strings.Split(unit, "\n") {
+ if strings.HasPrefix(line, "ExecStart=") {
+ execStart = line
+ }
+ }
+ if execStart == "" {
+ t.Fatal("unit has no ExecStart line")
+ }
+ // Each path must arrive as one quoted argument, not as bare text systemd would split.
+ for _, want := range []string{`"` + program + `"`, `"` + config + `"`} {
+ if !strings.Contains(execStart, want) {
+ t.Errorf("ExecStart does not carry %s as a single quoted argument:\n%s", want, execStart)
+ }
+ }
+}
+
+// A `%` in a path is a unit specifier prefix anywhere on the line, quoted or not, so it has to be
+// doubled or systemd expands it into something else entirely.
+func TestSystemdArgEscapesSpecifiersAndQuotes(t *testing.T) {
+ cases := map[string]string{
+ "/opt/beacon/bin/beacon": `"/opt/beacon/bin/beacon"`,
+ "/tmp/100% sure/beacon": `"/tmp/100%% sure/beacon"`,
+ `/tmp/quote"here/beacon`: `"/tmp/quote\"here/beacon"`,
+ `/tmp/back\slash/beacon`: `"/tmp/back\\slash/beacon"`,
+ }
+ for in, want := range cases {
+ if got := systemdArg(in); got != want {
+ t.Errorf("systemdArg(%q) = %q, want %q", in, got, want)
+ }
+ }
+}
+
+// The updater unit is written by a separate function and had the same defect.
+func TestUpdaterUnitQuotesExecStart(t *testing.T) {
+ program := "/home/first last/bin/beacon"
+ unit := updaterServiceUnit(program)
+ if !strings.Contains(unit, `ExecStart="`+program+`" endpoint update --scheduled`) {
+ t.Errorf("updater ExecStart does not quote the program path:\n%s", unit)
+ }
+}
diff --git a/cli/beacon/internal/endpoint/service/supervised.go b/cli/beacon/internal/endpoint/service/supervised.go
index 253eb049..b668f742 100644
--- a/cli/beacon/internal/endpoint/service/supervised.go
+++ b/cli/beacon/internal/endpoint/service/supervised.go
@@ -128,7 +128,13 @@ func (b supervisedBackend) load(userMode bool) error {
if f, ferr := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644); ferr == nil {
cmd.Stdout = f
cmd.Stderr = f
- defer f.Close()
+ // Discarding this Close error is correct rather than sloppy, which is worth saying because
+ // a write handle closed without checking normally is a data-loss bug. cmd.Start duplicates
+ // the descriptor into the child, so this handle is the parent's copy and the parent never
+ // writes a byte through it -- there is nothing buffered for a close to fail to flush. The
+ // child holds the descriptor that matters and keeps it for its lifetime. Closing here is
+ // what stops the parent leaking a descriptor.
+ defer func() { _ = f.Close() }()
}
if err := cmd.Start(); err != nil {
return fmt.Errorf("start supervised collector: %w", err)
diff --git a/cli/beacon/internal/endpoint/service/systemd.go b/cli/beacon/internal/endpoint/service/systemd.go
index c8fc3dc9..fdec3535 100644
--- a/cli/beacon/internal/endpoint/service/systemd.go
+++ b/cli/beacon/internal/endpoint/service/systemd.go
@@ -236,6 +236,23 @@ func LingerEnabled(username string) bool {
return strings.EqualFold(strings.TrimSpace(out), "yes")
}
+// systemdArg renders one value as a single ExecStart argument.
+//
+// systemd splits ExecStart on whitespace, so an unquoted path containing a space becomes two
+// arguments and the unit fails with a confusing "No such file or directory". That is not exotic:
+// a user-mode install lives under the user's home directory, and `--collector` takes an arbitrary
+// path from the caller.
+//
+// Double quotes are systemd's own quoting, inside which backslash escapes apply -- so both the
+// quote and the backslash have to be escaped. `%` is separate: it introduces a unit specifier
+// anywhere on the line, quoted or not, and is escaped by doubling.
+func systemdArg(value string) string {
+ value = strings.ReplaceAll(value, `\`, `\\`)
+ value = strings.ReplaceAll(value, `"`, `\"`)
+ value = strings.ReplaceAll(value, "%", "%%")
+ return `"` + value + `"`
+}
+
// unitFile renders the collector unit.
func unitFile(program, configPath string, userMode bool) string {
var b strings.Builder
@@ -247,7 +264,7 @@ func unitFile(program, configPath string, userMode bool) string {
b.WriteString("After=network.target\n")
b.WriteString("\n[Service]\n")
b.WriteString("Type=simple\n")
- fmt.Fprintf(&b, "ExecStart=%s --config %s\n", program, configPath)
+ fmt.Fprintf(&b, "ExecStart=%s --config %s\n", systemdArg(program), systemdArg(configPath))
// KeepAlive equivalent.
b.WriteString("Restart=always\n")
b.WriteString("RestartSec=5\n")
diff --git a/cli/beacon/internal/endpoint/service/updater.go b/cli/beacon/internal/endpoint/service/updater.go
index e2fc9074..b37f333f 100644
--- a/cli/beacon/internal/endpoint/service/updater.go
+++ b/cli/beacon/internal/endpoint/service/updater.go
@@ -248,7 +248,7 @@ ExecStart=%s endpoint update --scheduled
StandardOutput=journal
StandardError=journal
User=root
-`, program)
+`, systemdArg(program))
}
// updaterTimerUnit schedules the job at the same business-hour intervals the launchd plist