|
1 | 1 | import { probeSpawn, probeSpawnSync } from "./mutation-guard.mjs"; |
2 | | -import { accessSync, constants, realpathSync, statSync } from "node:fs"; |
| 2 | +import { accessSync, constants, readFileSync, realpathSync, statSync } from "node:fs"; |
3 | 3 | import { dirname, isAbsolute } from "node:path"; |
4 | 4 |
|
5 | 5 | /** |
@@ -171,6 +171,76 @@ export function resetSystemdRunProbeForTests(): void { |
171 | 171 | systemdRunProbePending = undefined; |
172 | 172 | } |
173 | 173 |
|
| 174 | +export interface DashboardRestartContext { |
| 175 | + platform?: NodeJS.Platform; |
| 176 | + env?: NodeJS.ProcessEnv; |
| 177 | + /** The resolved scope launcher, or undefined when none is usable. */ |
| 178 | + systemdRun: string | undefined; |
| 179 | + /** The OpenCodex-managed user unit (`TASK` in src/service/state.ts) without `.service`. */ |
| 180 | + unitName: string; |
| 181 | + readCgroup?: () => string; |
| 182 | +} |
| 183 | + |
| 184 | +function cgroupPaths(cgroup: string): string[] { |
| 185 | + const rows = cgroup.split("\n").map(line => line.split(":")).filter(parts => parts.length >= 3); |
| 186 | + // cgroup v2 ("0::") or the v1 systemd hierarchy name the unit; controller lines are noise. |
| 187 | + const unified = rows.filter(parts => parts[1] === "" || parts[1] === "name=systemd"); |
| 188 | + return (unified.length > 0 ? unified : rows).map(parts => parts.slice(2).join(":").trim()).filter(Boolean); |
| 189 | +} |
| 190 | + |
| 191 | +/** The innermost systemd unit named by a cgroup path: its last `.service` or `.scope` component. */ |
| 192 | +function innermostUnit(path: string): { unit: string; index: number; parts: string[] } | null { |
| 193 | + const parts = path.split("/"); |
| 194 | + for (let index = parts.length - 1; index >= 0; index--) { |
| 195 | + const part = parts[index]!; |
| 196 | + if (part.endsWith(".service") || part.endsWith(".scope")) return { unit: part, index, parts }; |
| 197 | + } |
| 198 | + return null; |
| 199 | +} |
| 200 | + |
| 201 | +/** |
| 202 | + * Why a dashboard update must not run against this proxy, or null when it may (#7967). |
| 203 | + * |
| 204 | + * A dashboard update stops the proxy before it replaces the package, with or without a requested |
| 205 | + * restart, and the worker survives that stop only from its own transient scope. Only OpenCodex's |
| 206 | + * own user unit is restarted by the updater. A proxy inside any systemd service -- detected from |
| 207 | + * `INVOCATION_ID` or from a `.service` cgroup, so a unit that unsets the variable is still seen |
| 208 | + * -- is therefore refused unless the scope launcher works and the cgroup places it in |
| 209 | + * `<unitName>.service` under a per-user manager (a delegated child cgroup below it is fine). A |
| 210 | + * custom system unit with `User=` would lose the worker with its cgroup, and the restart would aim |
| 211 | + * at an unrelated, possibly inactive user unit. Outside systemd nothing changes. |
| 212 | + */ |
| 213 | +export function dashboardRestartRefusal(context: DashboardRestartContext): string | null { |
| 214 | + const platform = context.platform ?? process.platform; |
| 215 | + const env = context.env ?? process.env; |
| 216 | + if (platform !== "linux") return null; |
| 217 | + let cgroup: string | null; |
| 218 | + try { cgroup = (context.readCgroup ?? (() => readFileSync("/proc/self/cgroup", "utf8")))(); } |
| 219 | + catch { cgroup = null; } |
| 220 | + const paths = cgroup === null ? [] : cgroupPaths(cgroup); |
| 221 | + // The innermost unit decides: a terminal or app scope below the per-user manager |
| 222 | + // (user@<uid>.service/app.slice/vte-spawn-1.scope) is not a service; a system or user service is. |
| 223 | + const units: ReturnType<typeof innermostUnit>[] = paths.map(innermostUnit); |
| 224 | + const observed = units.filter((found): found is NonNullable<typeof found> => found !== null); |
| 225 | + // The observed cgroup wins over an inherited INVOCATION_ID (a shell moved into its own scope |
| 226 | + // keeps the variable); without an observation the variable alone fails closed. |
| 227 | + const inService = observed.length > 0 |
| 228 | + ? observed.some(found => found.unit.endsWith(".service") && !found.unit.startsWith("user@")) |
| 229 | + : Boolean(env.INVOCATION_ID); |
| 230 | + if (!inService) return null; |
| 231 | + const manual = "Stop the service with its own manager, update the package from a shell (for example `ocx update`), then start the service again."; |
| 232 | + if (!context.systemdRun || !env.INVOCATION_ID) { |
| 233 | + return `Dashboard update is unavailable: this proxy runs inside a systemd service, and the update worker cannot leave the service's cgroup (no usable \`systemd-run --user --scope\`), so stopping the proxy for the update would stop the update too. ${manual}`; |
| 234 | + } |
| 235 | + const ownUnit = `${context.unitName}.service`; |
| 236 | + const managedByOwnUnit = units.some((found): boolean => found !== null && found.unit === ownUnit |
| 237 | + && found.parts.slice(0, found.index).some((part: string) => part.startsWith("user@") && part.endsWith(".service"))); |
| 238 | + if (!managedByOwnUnit) { |
| 239 | + return `Dashboard update is unavailable: this proxy runs under a systemd unit that is not OpenCodex's own \`${context.unitName}.service\` user unit, so the updater can neither keep its worker alive nor restart that unit safely. ${manual}`; |
| 240 | + } |
| 241 | + return null; |
| 242 | +} |
| 243 | + |
174 | 244 | export function guiUpdateWorkerCommand( |
175 | 245 | execPath: string, |
176 | 246 | args: readonly string[], |
|
0 commit comments