diff --git a/Sparkle/Autoupdate/Autoupdate.m b/Sparkle/Autoupdate/Autoupdate.m index 57fc06a5..a80579e5 100644 --- a/Sparkle/Autoupdate/Autoupdate.m +++ b/Sparkle/Autoupdate/Autoupdate.m @@ -265,15 +265,47 @@ int main(int __unused argc, const char __unused *argv[]) @autoreleasepool { NSArray *args = [[NSProcessInfo processInfo] arguments]; - if (args.count < 5 || args.count > 7) { + if (args.count != 8) { return EXIT_FAILURE; } - NSApplication *application = [NSApplication sharedApplication]; - - BOOL shouldShowUI = (args.count > 6) ? [args[6] boolValue] : YES; - + NSString *hostPath = args[1]; + NSString *relaunchPath = args[2]; NSString *hostBundlePath = args[3]; + NSString *updateDirectoryPath = args[4]; + BOOL shouldRelaunchApp = [args[5] boolValue]; + BOOL shouldShowUI = [args[6] boolValue]; + BOOL shouldRelaunchTool = [args[7] boolValue]; + + if (shouldRelaunchTool) { + NSURL *mainBundleURL = [[NSBundle mainBundle] bundleURL]; + + if (mainBundleURL == nil) { + SULog(@"Error: No bundle path located found for main bundle!"); + return EXIT_FAILURE; + } + + NSMutableArray *launchArguments = [args mutableCopy]; + [launchArguments removeObjectAtIndex:0]; // argv[0] is not important + launchArguments[launchArguments.count - 1] = @"0"; // we don't want to relaunch the tool this time + + // We want to launch our tool through LaunchServices, not through a NSTask instance + // This has a few advantages: one being that we don't inherit the privileges of the parent owner. + // Another is if we try to spawn a task, it may be prematurely terminated if the parent is like a XPC service, + // which is what the shouldRelaunchTool flag exists to prevent. Thus, a caller may specify to relaunch the tool again and + // wait until we exit. When we exit the first time, the caller will be notified, and we can launch a second instance through LS. + // The caller may not have AppKit available which is why it may not launch through LS itself. + NSError *launchError = nil; + NSRunningApplication *newRunningApplication = [[NSWorkspace sharedWorkspace] launchApplicationAtURL:mainBundleURL options:(NSWorkspaceLaunchOptions)(NSWorkspaceLaunchDefault | NSWorkspaceLaunchNewInstance) configuration:@{NSWorkspaceLaunchConfigurationArguments : [launchArguments copy]} error:&launchError]; + + if (newRunningApplication == nil) { + SULog(@"Failed to create second instance of tool with error: %@", launchError); + } + + return EXIT_SUCCESS; + } + + NSApplication *application = [NSApplication sharedApplication]; NSNumber *activeProcessIdentifier = nil; for (NSRunningApplication *runningApplication in [[NSWorkspace sharedWorkspace] runningApplications]) { @@ -283,11 +315,11 @@ int main(int __unused argc, const char __unused *argv[]) } } - AppInstaller *appInstaller = [[AppInstaller alloc] initWithHostPath:args[1] - relaunchPath:args[2] + AppInstaller *appInstaller = [[AppInstaller alloc] initWithHostPath:hostPath + relaunchPath:relaunchPath hostProcessIdentifier:activeProcessIdentifier - updateFolderPath:args[4] - shouldRelaunch:(args.count > 5) ? [args[5] boolValue] : YES + updateFolderPath:updateDirectoryPath + shouldRelaunch:shouldRelaunchApp shouldShowUI:shouldShowUI]; [application setDelegate:appInstaller]; [application run]; diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 455e643e..039b85ca 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -525,8 +525,7 @@ if ([updaterDelegate respondsToSelector:@selector(updaterWillRelaunchApplication:)]) [updaterDelegate updaterWillRelaunchApplication:self.updater]; - NSBundle *relaunchToolBundle = [NSBundle bundleWithPath:self.relaunchPath]; - NSString *relaunchToolPath = [relaunchToolBundle executablePath]; + NSString *relaunchToolPath = [[NSBundle bundleWithPath:self.relaunchPath] executablePath]; if (!relaunchToolPath || ![[NSFileManager defaultManager] fileExistsAtPath:self.relaunchPath]) { // Note that we explicitly use the host app's name here, since updating plugin for Mail relaunches Mail, not just the plugin. [self abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SURelaunchError userInfo:@{ @@ -547,13 +546,36 @@ self.host.bundlePath, self.tempDir, relaunch ? @"1" : @"0", - showUI ? @"1" : @"0"]; + showUI ? @"1" : @"0", + @"1"]; // last one signifies the relaunch tool should exit & reply back to us immediately - // Make sure the launched task finishes. If this is a XPC process, and we don't wait for it to finish, we risk the task terminating prematurely - NSTask *launchedTask = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/open" arguments:[@[@"-n", relaunchToolBundle.bundlePath, @"--args"] arrayByAddingObjectsFromArray:launchArguments]]; - [launchedTask waitUntilExit]; + // Make sure the launched task finishes & replies back. + // If it succeeds, it will have launched a second instance of the tool through LaunchServices + // This is necessary if we are a XPC process, because otherwise we risk exiting prematurely + // Further, we don't launch through LS here because we don't want to reference AppKit here + int terminationStatus = 0; + BOOL taskDidLaunch = NO; + @try { + NSTask *launchedTask = [NSTask launchedTaskWithLaunchPath:relaunchToolPath arguments:launchArguments]; + [launchedTask waitUntilExit]; + taskDidLaunch = YES; + terminationStatus = launchedTask.terminationStatus; + } @catch (NSException *exception) { + SULog(@"Raised exception when launching update tool: %@", exception); + } - [self terminateApp]; + if (!taskDidLaunch || terminationStatus != 0) { + if (taskDidLaunch) { + SULog(@"Update tool failed with exit code: %d", terminationStatus); + } + + [self abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SURelaunchError userInfo:@{ + NSLocalizedDescriptionKey: SULocalizedString(@"An error occurred while launching the updater. Please try again later.", nil), + NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"Couldn't launch relauncher at %@", relaunchToolPath] + }]]; + } else { + [self terminateApp]; + } } // Note: this is overridden by the automatic update driver to not terminate in some cases