Repository navigation
fix(server): macOS service keeps its privacy grants across updates #25999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
58de11d
46604ac
f15a4a6
4fabb88
f4af379
7a90b56
4c04c00
d68d95a
94871ad
5a7ddad
83c66af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ import { | |
| pinnedRuntimePaths, | ||
| pinnedRuntimeVersionsDir, | ||
| PinnedRuntimeInstallError, | ||
| type PinnedRuntimePaths, | ||
| } from "./pinnedRuntime.ts"; | ||
| import { | ||
| SERVICE_LAUNCHER_PROTOCOL, | ||
|
|
@@ -210,6 +211,8 @@ export interface BootServiceManager { | |
| readonly handoff?: ReadonlyArray<BootServiceStep>; | ||
| readonly deactivate: ReadonlyArray<BootServiceStep>; | ||
| readonly finalize: ReadonlyArray<BootServiceStep>; | ||
| /** The unit starts a copy of the runtime kept here instead of its versioned directory. */ | ||
| readonly launcherDir?: string; | ||
| } | ||
|
|
||
| function systemdManager(input: { | ||
|
|
@@ -285,6 +288,7 @@ function systemdManager(input: { | |
| function launchdManager(input: { | ||
| readonly path: Path.Path; | ||
| readonly homeDir: string; | ||
| readonly baseDir: string; | ||
| readonly uid: number; | ||
| readonly environmentPath: string; | ||
| }): BootServiceManager { | ||
|
|
@@ -347,12 +351,15 @@ function launchdManager(input: { | |
| }, | ||
| ], | ||
| finalize: [], | ||
| // TCC keys the launch agent's privacy grants to its program path, so it must not change. | ||
| launcherDir: input.path.join(input.baseDir, "runtime", "launcher"), | ||
| }; | ||
| } | ||
|
|
||
| function selectBootServiceManager(input: { | ||
| readonly platform: NodeJS.Platform; | ||
| readonly homeDir: string; | ||
| readonly baseDir: string; | ||
| readonly xdgConfigHome: string; | ||
| readonly systemdUnitDir: string | undefined; | ||
| readonly uid: number | undefined; | ||
|
|
@@ -380,6 +387,7 @@ function selectBootServiceManager(input: { | |
| return launchdManager({ | ||
| path: input.path, | ||
| homeDir: input.homeDir, | ||
| baseDir: input.baseDir, | ||
| uid: input.uid, | ||
| environmentPath: input.environmentPath, | ||
| }); | ||
|
|
@@ -645,6 +653,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { | |
| const detectedManager = selectBootServiceManager({ | ||
| platform, | ||
| homeDir, | ||
| baseDir: input.baseDir, | ||
| xdgConfigHome, | ||
| systemdUnitDir, | ||
| uid, | ||
|
|
@@ -655,6 +664,15 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { | |
| const logPath = path.join(input.logsDir, BOOT_SERVICE_LOG_FILE); | ||
| const statePath = path.join(input.baseDir, "runtime", SERVICE_STATE_FILE); | ||
| const restartPendingPath = path.join(input.baseDir, "runtime", SERVICE_RESTART_PENDING_FILE); | ||
| const launcherDir = detectedManager?.launcherDir; | ||
| const launcherPaths: PinnedRuntimePaths = | ||
| launcherDir === undefined | ||
| ? runtimePaths | ||
| : { | ||
| versionDir: launcherDir, | ||
| entryPath: path.join(launcherDir, path.basename(runtimePaths.entryPath)), | ||
| sentinelPath: path.join(launcherDir, path.basename(runtimePaths.sentinelPath)), | ||
| }; | ||
| const otherBaseDirOf = (unit: string) => { | ||
| const installedBaseDir = bootServiceBaseDirOf(unit); | ||
| return installedBaseDir !== undefined && | ||
|
|
@@ -682,12 +700,41 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { | |
| }), | ||
| ).pipe(Effect.mapError((cause) => new BootServiceInstallError({ cause }))); | ||
| const plan: BootServicePlan = { | ||
| program: [runtimePaths.entryPath, "__service-launcher"], | ||
| program: [launcherPaths.entryPath, "__service-launcher"], | ||
| baseDir: input.baseDir, | ||
| logPath, | ||
| unitPath, | ||
| }; | ||
|
|
||
| // cp -c clones on APFS and copies all bytes where cloning is unsupported; copyFile never clones. | ||
| const copyLauncher = (source: string, destination: string) => | ||
| runner | ||
| .run({ command: "cp", args: ["-c", "-p", source, destination], timeout: Duration.minutes(1) }) | ||
| .pipe( | ||
| Effect.filterOrFail((result) => result.code === 0), | ||
| Effect.catch(() => fs.copyFile(source, destination)), | ||
| ); | ||
| // `__service-launcher` loads only Node built-ins, so the executable alone is the launcher. | ||
| // Each file is renamed over its predecessor: the program path never goes missing, even if the | ||
| // process dies mid-publish, and a running launcher keeps its old inode instead of being written into. | ||
| const publishLauncher = Effect.fn("cloud.boot_service.publish_launcher")(function* () { | ||
| yield* fs.makeDirectory(launcherPaths.versionDir, { recursive: true }); | ||
| const staging = yield* fs.makeTempDirectory({ | ||
| directory: launcherPaths.versionDir, | ||
| prefix: ".staging-", | ||
| }); | ||
| yield* Effect.gen(function* () { | ||
| const entry = path.join(staging, path.basename(launcherPaths.entryPath)); | ||
| const sentinel = path.join(staging, path.basename(launcherPaths.sentinelPath)); | ||
| yield* copyLauncher(runtimePaths.entryPath, entry); | ||
| yield* fs.writeFileString(sentinel, `${input.cliVersion}\n`); | ||
| yield* fs.rename(entry, launcherPaths.entryPath); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High Concurrent installs can leave 🚀 Reply "fix it for me" or copy this AI Prompt for your agent: |
||
| yield* fs.rename(sentinel, launcherPaths.sentinelPath); | ||
| }).pipe( | ||
| Effect.ensuring(fs.remove(staging, { recursive: true, force: true }).pipe(Effect.ignore)), | ||
| ); | ||
| }); | ||
|
|
||
| const requireManager = Effect.suspend(() => | ||
| detectedManager === undefined | ||
| ? new BootServiceUnsupportedError({ platform }) | ||
|
|
@@ -950,6 +997,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { | |
| return yield* new BootServiceUpdatePendingError(); | ||
| } | ||
| } | ||
| if (manager.launcherDir !== undefined) yield* publishLauncher(); | ||
| yield* writeDurably(unitPath, manager.render(plan)); | ||
|
|
||
| if (start) { | ||
|
|
@@ -1055,9 +1103,14 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { | |
| ) | ||
| return false; | ||
| yield* runSteps(manager.deactivate); | ||
| yield* fs | ||
| .remove(unitPath) | ||
| .pipe(Effect.mapError((cause) => new BootServiceInstallError({ cause }))); | ||
| yield* fs.remove(unitPath).pipe( | ||
| Effect.andThen( | ||
| manager.launcherDir === undefined | ||
| ? Effect.void | ||
| : fs.remove(manager.launcherDir, { recursive: true, force: true }), | ||
| ), | ||
| Effect.mapError((cause) => new BootServiceInstallError({ cause })), | ||
| ); | ||
| yield* runSteps(manager.finalize); | ||
| return true; | ||
| }).pipe(Effect.withSpan("cloud.boot_service.uninstall")); | ||
|
|
@@ -1069,10 +1122,20 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { | |
| if (!(yield* fs.exists(unitPath))) { | ||
| return { supported: true, installed: false, current: false, unitPath, logPath }; | ||
| } | ||
| const [unit, runtimeEntryExists, runtimeSentinel, stateText] = yield* Effect.all([ | ||
| const runtimeCurrent = (paths: PinnedRuntimePaths) => | ||
| Effect.all([ | ||
| fs.exists(paths.entryPath), | ||
| fs.readFileString(paths.sentinelPath).pipe(Effect.option), | ||
| ]).pipe( | ||
| Effect.map( | ||
| ([entryExists, sentinel]) => | ||
| entryExists && Option.isSome(sentinel) && sentinel.value.trim() === input.cliVersion, | ||
| ), | ||
| ); | ||
| const [unit, runtimeIsCurrent, launcherIsCurrent, stateText] = yield* Effect.all([ | ||
| fs.readFileString(unitPath), | ||
| fs.exists(runtimePaths.entryPath), | ||
| fs.readFileString(runtimePaths.sentinelPath).pipe(Effect.option), | ||
| runtimeCurrent(runtimePaths), | ||
| runtimeCurrent(launcherPaths), | ||
| fs.readFileString(statePath).pipe(Effect.option), | ||
| ]); | ||
| const state = Option.isSome(stateText) ? parseServiceState(stateText.value) : undefined; | ||
|
|
@@ -1098,9 +1161,8 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { | |
| current: | ||
| problems.length === 0 && | ||
| normalizeUnit(unit) === normalizeUnit(detectedManager.render(plan)) && | ||
| runtimeEntryExists && | ||
| Option.isSome(runtimeSentinel) && | ||
| runtimeSentinel.value.trim() === input.cliVersion && | ||
| runtimeIsCurrent && | ||
| launcherIsCurrent && | ||
| state?.activeVersion === input.cliVersion && | ||
| state?.update?.status !== "pending", | ||
| unitPath, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.