Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion apps/server/src/cloud/bootService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1492,15 +1492,24 @@ process.disconnect();

it.effect("stops a Windows launcher by ending its task, and restarts one left disabled", () =>
Effect.gen(function* () {
const { service, commands, control, makeService } = yield* makeHarness("win32");
const { service, fs, commands, control, makeService } = yield* makeHarness("win32");
const plan = yield* service.install();
const launcher = new NodeSqlite.DatabaseSync(serviceLauncherLockPath(plan.baseDir));
launcher.exec("BEGIN EXCLUSIVE");
control.launcher = launcher;
const headlessTask = (yield* fs.readFileString(plan.unitPath)).replace(
"<LogonType>InteractiveToken</LogonType>",
"<LogonType>S4U</LogonType>",
);
yield* fs.writeFileString(plan.unitPath, headlessTask);
control.task = { xml: headlessTask, enabled: true };
expect((yield* service.status).current).toBe(true);

// Task Scheduler runs its own copy of the task, so a deferred restart registers the new one.
const newer = yield* makeService(undefined, "1.2.4");
yield* newer.install({ start: false });
expect(yield* fs.readFileString(plan.unitPath)).toContain("<LogonType>S4U</LogonType>");
expect(control.task?.xml).toContain("<LogonType>S4U</LogonType>");
const newerRuntime = pinnedRuntimePaths(yield* Path.Path, plan.baseDir, "1.2.4", "win32");
expect(BootService.bootServiceProgramOf(control.task?.xml ?? "")).toBe(
newerRuntime.entryPath,
Expand Down
23 changes: 16 additions & 7 deletions apps/server/src/cloud/bootService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,12 @@ export function renderBootServicePlist(
*/
export function renderBootServiceTask(
plan: BootServicePlan,
options: { readonly userId: string; readonly homeDir: string; readonly conhostPath: string },
options: {
readonly userId: string;
readonly homeDir: string;
readonly conhostPath: string;
readonly logonType?: "InteractiveToken" | "S4U";
},
): string {
const launcherArguments = [
...plan.program,
Expand Down Expand Up @@ -246,7 +251,7 @@ export function renderBootServiceTask(
` <Principals>`,
` <Principal id="Author">`,
` <UserId>${escapeXmlText(options.userId)}</UserId>`,
` <LogonType>InteractiveToken</LogonType>`,
` <LogonType>${options.logonType ?? "InteractiveToken"}</LogonType>`,
` <RunLevel>LeastPrivilege</RunLevel>`,
` </Principal>`,
` </Principals>`,
Expand Down Expand Up @@ -286,7 +291,7 @@ const STOP_STEP_TIMEOUT = Duration.seconds(120);
export interface BootServiceManager {
readonly kind: "systemd" | "launchd" | "schtasks";
readonly unitPath: string;
readonly render: (plan: BootServicePlan) => string;
readonly render: (plan: BootServicePlan, existingUnit?: string) => string;
readonly stop: ReadonlyArray<BootServiceStep>;
readonly activate: ReadonlyArray<BootServiceStep>;
readonly restart: ReadonlyArray<BootServiceStep>;
Expand Down Expand Up @@ -493,11 +498,14 @@ function schtasksManager(input: {
return {
kind: "schtasks",
unitPath,
render: (plan) =>
render: (plan, existingUnit) =>
renderBootServiceTask(plan, {
userId: input.session.userId,
homeDir: input.homeDir,
conhostPath: input.path.join(input.session.systemRoot, "System32", "conhost.exe"),
logonType: existingUnit?.includes("<LogonType>S4U</LogonType>")
? "S4U"
: "InteractiveToken",
}),
stop,
// Registration also enables the task.
Expand Down Expand Up @@ -1378,7 +1386,8 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
.pipe(Effect.mapError((cause) => new BootServiceInstallError({ cause })));
// Files can match the new version even when activation fails; keep that install retryable.
yield* fs.writeFileString(restartPendingPath, `${input.cliVersion}\n`, { mode: 0o600 });
const plan = planForUnit(currentlyInstalled ? yield* fs.readFileString(unitPath) : "");
const previousUnit = currentlyInstalled ? yield* fs.readFileString(unitPath) : "";
const plan = planForUnit(previousUnit);
yield* writeDurably(
statePath,
// @effect-diagnostics-next-line globalJsonInEffect:off - fixed launcher-owned document.
Expand All @@ -1397,7 +1406,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
return yield* new BootServiceUpdatePendingError();
}
}
const unit = manager.render(plan);
const unit = manager.render(plan, previousUnit);
yield* writeDurably(unitPath, unit);
if (!start && manager.kind === "schtasks") yield* refreshWindowsTask(unit);
return plan;
Expand Down Expand Up @@ -1578,7 +1587,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
current:
problems.length === 0 &&
!(manager.kind === "systemd" && systemdUnsafeValue(planForUnit(unit))) &&
normalizeUnit(unit) === normalizeUnit(manager.render(planForUnit(unit))) &&
normalizeUnit(unit) === normalizeUnit(manager.render(planForUnit(unit), unit)) &&
runtimeEntryExists &&
Option.isSome(runtimeSentinel) &&
runtimeSentinel.value.trim() === input.cliVersion &&
Expand Down
Loading