From 96fb18e0df45a69b91077863675cd2763d3b2b93 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 14 Feb 2016 09:47:16 -0500 Subject: [PATCH] Launch AutoUpdate via NSWorkspace instead of NSTask This will allow the app to run normally without being terminated because a XPC service was terminated --- Sparkle/Autoupdate/Autoupdate.m | 5 ++++- Sparkle/SUBasicUpdateDriver.m | 35 +++++++++++++++++++++++++++------ Sparkle/SUUpdater.m | 11 +++++++---- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/Sparkle/Autoupdate/Autoupdate.m b/Sparkle/Autoupdate/Autoupdate.m index ddc60f6c..f2ce4dc5 100644 --- a/Sparkle/Autoupdate/Autoupdate.m +++ b/Sparkle/Autoupdate/Autoupdate.m @@ -49,7 +49,10 @@ static const NSTimeInterval SUParentQuitCheckInterval = .25; - (void)startListeningWithCompletion:(void (^)(void))completionBlock { - BOOL alreadyTerminated = (getppid() == 1); // ppid is launchd (1) => parent terminated already +#warning Review this code later as well + // Parent process may not be the app we're installing, so don't rely on that + //BOOL alreadyTerminated = (getppid() == 1); // ppid is launchd (1) => parent terminated already + BOOL alreadyTerminated = (kill(self.processIdentifier, 0) != 0); if (alreadyTerminated) [self cleanupWithCompletion:completionBlock]; else diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 4f34c492..46bd7339 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -510,17 +510,40 @@ if ([updaterDelegate respondsToSelector:@selector(pathToRelaunchForUpdater:)]) { pathToRelaunch = [updaterDelegate pathToRelaunchForUpdater:self.updater]; } - [NSTask launchedTaskWithLaunchPath:relaunchToolPath arguments:@[[self.host bundlePath], - pathToRelaunch, - [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]], - self.tempDir, - relaunch ? @"1" : @"0", - showUI ? @"1" : @"0"]]; + +#warning Review this code later.. + pid_t processIdentifier = [[NSProcessInfo processInfo] processIdentifier]; + for (NSRunningApplication *runningApplication in [[NSWorkspace sharedWorkspace] runningApplications]) { + if ([runningApplication.bundleURL.path isEqual:self.host.bundlePath]) { + processIdentifier = runningApplication.processIdentifier; + break; + } + } + + NSArray *launchArguments = @[[self.host bundlePath], + pathToRelaunch, + [NSString stringWithFormat:@"%d", processIdentifier], + self.tempDir, + relaunch ? @"1" : @"0", + showUI ? @"1" : @"0"]; + + // Don't launch the app via a NSTask; it may be terminated prematurely if we are in a XPC service currently + NSError *launchError = nil; + if ([[NSWorkspace sharedWorkspace] launchApplicationAtURL:[NSURL fileURLWithPath:relaunchToolPath] options:NSWorkspaceLaunchDefault configuration:@{NSWorkspaceLaunchConfigurationArguments : launchArguments} error:&launchError] == nil) { + SULog(@"Failed to launch autoupdate app: %@", launchError); + } + [self terminateApp]; } - (void)terminateApp { + for (NSRunningApplication *runningApplication in [[NSWorkspace sharedWorkspace] runningApplications]) { + if ([runningApplication.bundleURL.path isEqual:self.host.bundlePath]) { + [runningApplication terminate]; + } + } + [NSApp terminate:self]; } diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index d3f708a8..4743c5a5 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -114,10 +114,13 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults } -(void)showAlertText:(NSString *)text informativeText:(NSString *)informativeText { - NSAlert *alert = [[NSAlert alloc] init]; - alert.messageText = text; - alert.informativeText = informativeText; - [self.driver showAlert:alert]; +#warning create a delegate for this + + SULog(@"Failed to check requirements.. %@; %@", text, informativeText); +// NSAlert *alert = [[NSAlert alloc] init]; +// alert.messageText = text; +// alert.informativeText = informativeText; +// [self.driver showAlert:alert]; } -(void)checkIfConfiguredProperly {