Skip to content

Commit 8c57a15

Browse files
committed
fix(daemon): block conflicting systemd gateway owners
1 parent ec46d30 commit 8c57a15

6 files changed

Lines changed: 860 additions & 40 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Docs: https://docs.openclaw.ai
5555

5656
### Fixes
5757

58+
- **Linux gateway service ownership:** refuse user-scope systemd publication and activation when the same gateway unit name is already owned or cannot be verified in the system scope, including `--force`, with actionable recovery guidance instead of creating restart-looping dual managers. Fixes #116129.
5859
- **Control UI update reconciliation:** preserve an unresolved managed-update request across disconnects, accept the replacement Gateway version when it proves success, and otherwise show explicit recovery guidance instead of trusting an unrelated cached update result or failing silently. Fixes #116075. Thanks @shakkernerd.
5960
- **Control UI model readiness:** put AI setup first when no model is selectable, distinguish signed-in credentials from ready providers, and route accounts with no exposed models directly to provider recovery instead of leading with disabled default controls.
6061
- **Control UI Talk session isolation:** stop active realtime Talk media and retire its callbacks before chat session changes, Gateway disconnects, or pane disposal so previous-session audio, transcript, camera, and status updates cannot leak into the next view. Thanks @shakkernerd.

‎src/cli/daemon-cli/install.integration.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,24 @@ describe("runDaemonInstall integration", () => {
140140
expect(runtimeLogs.join("\n")).toContain("Refusing to install or rewrite the gateway service");
141141
});
142142

143+
it.each([
144+
{ force: undefined, label: "normal install" },
145+
{ force: true, label: "forced reinstall" },
146+
])("does not bypass system ownership during $label", async ({ force }) => {
147+
serviceMock.install.mockRejectedValueOnce(
148+
new Error(
149+
"System systemd unit openclaw-gateway.service already owns this gateway unit name. --force does not override system ownership.",
150+
),
151+
);
152+
153+
await expect(runDaemonInstall({ json: true, force })).rejects.toThrow("__exit__:1");
154+
155+
expect(serviceMock.install).toHaveBeenCalledTimes(1);
156+
const joined = runtimeLogs.join("\n");
157+
expect(joined).toContain("System systemd unit openclaw-gateway.service");
158+
expect(joined).toContain("--force does not override system ownership");
159+
});
160+
143161
it("auto-mints token when no source exists without embedding it into service env", async () => {
144162
await fs.writeFile(
145163
configPath,

‎src/daemon/systemd-system.test.ts‎

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
// System systemd ownership tests cover loaded, installed, and unverifiable states.
2+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
3+
4+
const state = vi.hoisted(() => ({
5+
systemctl: { stdout: "not-found\n", stderr: "", code: 0 },
6+
managerUnitPath: {
7+
stdout:
8+
"/etc/systemd/system /run/systemd/system /usr/local/lib/systemd/system /usr/lib/systemd/system\n",
9+
stderr: "",
10+
code: 0,
11+
},
12+
paths: new Set<string>(),
13+
pathErrors: new Map<string, string>(),
14+
}));
15+
16+
function fsError(code: string, target: string): NodeJS.ErrnoException {
17+
return Object.assign(new Error(`${code}: ${target}`), { code });
18+
}
19+
20+
vi.mock("node:fs/promises", () => {
21+
const mocked = {
22+
lstat: vi.fn(async (target: string) => {
23+
const code = state.pathErrors.get(target);
24+
if (code) {
25+
throw fsError(code, target);
26+
}
27+
if (!state.paths.has(target)) {
28+
throw fsError("ENOENT", target);
29+
}
30+
return {};
31+
}),
32+
};
33+
return { ...mocked, default: mocked };
34+
});
35+
36+
const execFileUtf8 = vi.hoisted(() =>
37+
vi.fn(async (_command: string, args: string[]) =>
38+
args.includes("--property=UnitPath") ? state.managerUnitPath : state.systemctl,
39+
),
40+
);
41+
vi.mock("./exec-file.js", () => ({ execFileUtf8 }));
42+
43+
import { assertNoSystemSystemdOwnership } from "./systemd-system.js";
44+
45+
describe("system systemd ownership", () => {
46+
const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform");
47+
48+
beforeEach(() => {
49+
vi.clearAllMocks();
50+
state.systemctl = { stdout: "not-found\n", stderr: "", code: 0 };
51+
state.managerUnitPath = {
52+
stdout:
53+
"/etc/systemd/system /run/systemd/system /usr/local/lib/systemd/system /usr/lib/systemd/system\n",
54+
stderr: "",
55+
code: 0,
56+
};
57+
state.paths.clear();
58+
state.pathErrors.clear();
59+
execFileUtf8.mockReset();
60+
execFileUtf8.mockImplementation(async (_command: string, args: string[]) =>
61+
args.includes("--property=UnitPath") ? state.managerUnitPath : state.systemctl,
62+
);
63+
if (originalPlatformDescriptor) {
64+
Object.defineProperty(process, "platform", {
65+
...originalPlatformDescriptor,
66+
value: "linux",
67+
});
68+
}
69+
});
70+
71+
afterEach(() => {
72+
if (originalPlatformDescriptor) {
73+
Object.defineProperty(process, "platform", originalPlatformDescriptor);
74+
}
75+
});
76+
77+
it("reports a unit loaded by the system manager", async () => {
78+
state.systemctl = { stdout: "loaded\n", stderr: "", code: 0 };
79+
80+
await expect(assertNoSystemSystemdOwnership("openclaw-gateway.service")).rejects.toMatchObject({
81+
ownership: { status: "loaded", unitName: "openclaw-gateway.service" },
82+
});
83+
});
84+
85+
it.each([
86+
"/etc/systemd/system/openclaw-gateway.service",
87+
"/run/systemd/system/openclaw-gateway.service",
88+
"/usr/local/lib/systemd/system/openclaw-gateway.service",
89+
])("detects a custom same-name system unit at %s", async (unitPath) => {
90+
state.paths.add(unitPath);
91+
92+
await expect(assertNoSystemSystemdOwnership("openclaw-gateway.service")).rejects.toMatchObject({
93+
ownership: {
94+
status: "installed",
95+
unitName: "openclaw-gateway.service",
96+
unitPath,
97+
},
98+
});
99+
});
100+
101+
it("ignores differently named profile and custom units", async () => {
102+
state.paths.add("/etc/systemd/system/openclaw-gateway-rescue.service");
103+
state.paths.add("/etc/systemd/system/vendor-openclaw.service");
104+
105+
await expect(
106+
assertNoSystemSystemdOwnership("openclaw-gateway-primary.service"),
107+
).resolves.toBeUndefined();
108+
expect(execFileUtf8).toHaveBeenCalledTimes(3);
109+
});
110+
111+
it("fails closed when the system manager cannot be queried", async () => {
112+
state.systemctl = {
113+
stdout: "",
114+
stderr: "Failed to connect to bus: Permission denied",
115+
code: 1,
116+
};
117+
118+
await expect(assertNoSystemSystemdOwnership("openclaw-gateway.service")).rejects.toMatchObject({
119+
ownership: {
120+
status: "unverifiable",
121+
unitName: "openclaw-gateway.service",
122+
operation: "systemctl",
123+
detail: "Failed to connect to bus: Permission denied",
124+
},
125+
});
126+
});
127+
128+
it("does not mistake a missing system bus for a missing unit", async () => {
129+
state.systemctl = {
130+
stdout: "",
131+
stderr: "Failed to connect to bus: No such file or directory",
132+
code: 1,
133+
};
134+
135+
await expect(assertNoSystemSystemdOwnership("openclaw-gateway.service")).rejects.toMatchObject({
136+
ownership: {
137+
status: "unverifiable",
138+
operation: "systemctl",
139+
detail: "Failed to connect to bus: No such file or directory",
140+
},
141+
});
142+
});
143+
144+
it("fails closed when an exact system path cannot be inspected", async () => {
145+
const unitPath = "/etc/systemd/system/openclaw-gateway.service";
146+
state.pathErrors.set(unitPath, "EACCES");
147+
148+
await expect(assertNoSystemSystemdOwnership("openclaw-gateway.service")).rejects.toMatchObject({
149+
ownership: {
150+
status: "unverifiable",
151+
unitName: "openclaw-gateway.service",
152+
operation: "filesystem",
153+
detail: `${unitPath}: EACCES: ${unitPath}`,
154+
},
155+
});
156+
});
157+
158+
it("fails closed when manager unit load paths cannot be enumerated", async () => {
159+
state.managerUnitPath = {
160+
stdout: "",
161+
stderr: "manager UnitPath unavailable",
162+
code: 1,
163+
};
164+
165+
await expect(assertNoSystemSystemdOwnership("openclaw-gateway.service")).rejects.toMatchObject({
166+
ownership: {
167+
status: "unverifiable",
168+
operation: "systemctl",
169+
detail: "manager UnitPath unavailable",
170+
},
171+
});
172+
});
173+
174+
it("rechecks the system manager after a negative filesystem snapshot", async () => {
175+
let systemctlCalls = 0;
176+
execFileUtf8.mockImplementation(async (_command: string, args: string[]) => {
177+
if (args.includes("--property=UnitPath")) {
178+
return state.managerUnitPath;
179+
}
180+
systemctlCalls += 1;
181+
return systemctlCalls === 1
182+
? { stdout: "not-found\n", stderr: "", code: 0 }
183+
: { stdout: "loaded\n", stderr: "", code: 0 };
184+
});
185+
186+
await expect(assertNoSystemSystemdOwnership("openclaw-gateway.service")).rejects.toMatchObject({
187+
ownership: { status: "loaded", unitName: "openclaw-gateway.service" },
188+
});
189+
});
190+
191+
it.each([
192+
{ uid: 1000, prefix: "sudo " },
193+
{ uid: 0, prefix: "" },
194+
])("renders actionable ownership recovery for uid $uid", async ({ uid, prefix }) => {
195+
const existingGeteuid = Object.getOwnPropertyDescriptor(process, "geteuid");
196+
Object.defineProperty(process, "geteuid", {
197+
configurable: true,
198+
value: () => uid,
199+
});
200+
state.paths.add("/etc/systemd/system/openclaw-gateway.service");
201+
202+
try {
203+
const error = await assertNoSystemSystemdOwnership("openclaw-gateway.service").catch(
204+
(caught: unknown) => caught,
205+
);
206+
207+
expect(error).toMatchObject({
208+
name: "SystemSystemdOwnershipError",
209+
code: "SYSTEM_SYSTEMD_OWNERSHIP",
210+
ownership: { status: "installed" },
211+
});
212+
expect(String(error)).toContain("--force does not override system ownership");
213+
expect(String(error)).toContain(`${prefix}systemctl disable --now openclaw-gateway.service`);
214+
expect(String(error)).toContain(`${prefix}rm /etc/systemd/system/openclaw-gateway.service`);
215+
} finally {
216+
if (existingGeteuid) {
217+
Object.defineProperty(process, "geteuid", existingGeteuid);
218+
} else {
219+
Reflect.deleteProperty(process, "geteuid");
220+
}
221+
}
222+
});
223+
224+
it("does not recommend deleting package- or generator-owned units", async () => {
225+
state.paths.add("/usr/lib/systemd/system/openclaw-gateway.service");
226+
227+
const error = await assertNoSystemSystemdOwnership("openclaw-gateway.service").catch(
228+
(caught: unknown) => caught,
229+
);
230+
231+
expect(String(error)).toContain("uninstall or reconfigure the package, generator");
232+
expect(String(error)).not.toContain("rm /usr/lib/systemd/system/openclaw-gateway.service");
233+
});
234+
});

0 commit comments

Comments
 (0)