fix: enable systemd lingering for gateway
This commit is contained in:
44
src/daemon/systemd.test.ts
Normal file
44
src/daemon/systemd.test.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { readSystemdUserLingerStatus } from "./systemd.js";
|
||||
import { runExec } from "../process/exec.js";
|
||||
|
||||
vi.mock("../process/exec.js", () => ({
|
||||
runExec: vi.fn(),
|
||||
runCommandWithTimeout: vi.fn(),
|
||||
}));
|
||||
|
||||
const runExecMock = vi.mocked(runExec);
|
||||
|
||||
describe("readSystemdUserLingerStatus", () => {
|
||||
beforeEach(() => {
|
||||
runExecMock.mockReset();
|
||||
});
|
||||
|
||||
it("returns yes when loginctl reports Linger=yes", async () => {
|
||||
runExecMock.mockResolvedValue({
|
||||
stdout: "Linger=yes\n",
|
||||
stderr: "",
|
||||
});
|
||||
const result = await readSystemdUserLingerStatus({ USER: "tobi" });
|
||||
expect(result).toEqual({ user: "tobi", linger: "yes" });
|
||||
});
|
||||
|
||||
it("returns no when loginctl reports Linger=no", async () => {
|
||||
runExecMock.mockResolvedValue({
|
||||
stdout: "Linger=no\n",
|
||||
stderr: "",
|
||||
});
|
||||
const result = await readSystemdUserLingerStatus({ USER: "tobi" });
|
||||
expect(result).toEqual({ user: "tobi", linger: "no" });
|
||||
});
|
||||
|
||||
it("returns null when Linger is missing", async () => {
|
||||
runExecMock.mockResolvedValue({
|
||||
stdout: "UID=1000\n",
|
||||
stderr: "",
|
||||
});
|
||||
const result = await readSystemdUserLingerStatus({ USER: "tobi" });
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
@@ -7,6 +8,7 @@ import {
|
||||
GATEWAY_SYSTEMD_SERVICE_NAME,
|
||||
LEGACY_GATEWAY_SYSTEMD_SERVICE_NAMES,
|
||||
} from "./constants.js";
|
||||
import { runCommandWithTimeout, runExec } from "../process/exec.js";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
@@ -30,6 +32,83 @@ function resolveSystemdUnitPath(
|
||||
return resolveSystemdUnitPathForName(env, GATEWAY_SYSTEMD_SERVICE_NAME);
|
||||
}
|
||||
|
||||
function resolveLoginctlUser(
|
||||
env: Record<string, string | undefined>,
|
||||
): string | null {
|
||||
const fromEnv = env.USER?.trim() || env.LOGNAME?.trim();
|
||||
if (fromEnv) return fromEnv;
|
||||
try {
|
||||
return os.userInfo().username;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export type SystemdUserLingerStatus = {
|
||||
user: string;
|
||||
linger: "yes" | "no";
|
||||
};
|
||||
|
||||
export async function readSystemdUserLingerStatus(
|
||||
env: Record<string, string | undefined>,
|
||||
): Promise<SystemdUserLingerStatus | null> {
|
||||
const user = resolveLoginctlUser(env);
|
||||
if (!user) return null;
|
||||
try {
|
||||
const { stdout } = await runExec(
|
||||
"loginctl",
|
||||
["show-user", user, "-p", "Linger"],
|
||||
{ timeoutMs: 5_000 },
|
||||
);
|
||||
const line = stdout
|
||||
.split("\n")
|
||||
.map((entry) => entry.trim())
|
||||
.find((entry) => entry.startsWith("Linger="));
|
||||
const value = line?.split("=")[1]?.trim().toLowerCase();
|
||||
if (value === "yes" || value === "no") {
|
||||
return { user, linger: value };
|
||||
}
|
||||
} catch {
|
||||
// ignore; loginctl may be unavailable
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function enableSystemdUserLinger(params: {
|
||||
env: Record<string, string | undefined>;
|
||||
user?: string;
|
||||
sudoMode?: "prompt" | "non-interactive";
|
||||
}): Promise<{ ok: boolean; stdout: string; stderr: string; code: number }> {
|
||||
const user = params.user ?? resolveLoginctlUser(params.env);
|
||||
if (!user) {
|
||||
return { ok: false, stdout: "", stderr: "Missing user", code: 1 };
|
||||
}
|
||||
const needsSudo =
|
||||
typeof process.getuid === "function" ? process.getuid() !== 0 : true;
|
||||
const sudoArgs =
|
||||
needsSudo && params.sudoMode !== undefined
|
||||
? ["sudo", ...(params.sudoMode === "non-interactive" ? ["-n"] : [])]
|
||||
: [];
|
||||
const argv = [
|
||||
...sudoArgs,
|
||||
"loginctl",
|
||||
"enable-linger",
|
||||
user,
|
||||
];
|
||||
try {
|
||||
const result = await runCommandWithTimeout(argv, { timeoutMs: 30_000 });
|
||||
return {
|
||||
ok: result.code === 0,
|
||||
stdout: result.stdout,
|
||||
stderr: result.stderr,
|
||||
code: result.code ?? 1,
|
||||
};
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
return { ok: false, stdout: "", stderr: message, code: 1 };
|
||||
}
|
||||
}
|
||||
|
||||
function systemdEscapeArg(value: string): string {
|
||||
if (!/[\s"\\]/.test(value)) return value;
|
||||
return `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
||||
|
||||
Reference in New Issue
Block a user