From f43b5ee3c3e9bbdc08abaccd23746cb2d909c83f Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 28 Feb 2021 22:14:35 -0800 Subject: [PATCH] Propagate agent errors to Sparkle --- Autoupdate/AgentConnection.h | 1 + Autoupdate/AgentConnection.m | 7 ++++ Autoupdate/AppInstaller.m | 9 ++++- Autoupdate/SPUMessageTypes.h | 5 +++ .../InstallerProgressAppController.h | 2 +- .../InstallerProgressAppController.m | 39 ++++++++++++------- .../SUInstallerAgentInitiationProtocol.h | 2 + Sparkle/InstallerProgress/main.m | 2 +- 8 files changed, 51 insertions(+), 16 deletions(-) diff --git a/Autoupdate/AgentConnection.h b/Autoupdate/AgentConnection.h index 7ec02717..77bb2eab 100644 --- a/Autoupdate/AgentConnection.h +++ b/Autoupdate/AgentConnection.h @@ -28,6 +28,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, readonly, nullable) id agent; @property (nonatomic, readonly) BOOL connected; +@property (nonatomic, nullable) NSError *invalidationError; @end diff --git a/Autoupdate/AgentConnection.m b/Autoupdate/AgentConnection.m index 012ff019..ad0223e7 100644 --- a/Autoupdate/AgentConnection.m +++ b/Autoupdate/AgentConnection.m @@ -105,4 +105,11 @@ acknowledgement(); } +- (void)connectionWillInvalidateWithError:(NSError *)error +{ + dispatch_async(dispatch_get_main_queue(), ^{ + self.invalidationError = error; + }); +} + @end diff --git a/Autoupdate/AppInstaller.m b/Autoupdate/AppInstaller.m index f2f6ecef..ce9308df 100644 --- a/Autoupdate/AppInstaller.m +++ b/Autoupdate/AppInstaller.m @@ -279,7 +279,14 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; - (void)agentConnectionDidInvalidate { if (!self.finishedValidation || !self.agentInitiatedConnection) { - [self cleanupAndExitWithStatus:EXIT_FAILURE error:[NSError errorWithDomain:SUSparkleErrorDomain code:SPUInstallerError userInfo:@{ NSLocalizedDescriptionKey: @"Error: Agent connection invalidated before installation began" }]]; + NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:@{ NSLocalizedDescriptionKey: @"Error: Agent connection invalidated before installation began" }]; + + NSError *agentError = self.agentConnection.invalidationError; + if (agentError != nil) { + userInfo[NSUnderlyingErrorKey] = agentError; + } + + [self cleanupAndExitWithStatus:EXIT_FAILURE error:[NSError errorWithDomain:SUSparkleErrorDomain code:SPUInstallerError userInfo:userInfo]]; } } diff --git a/Autoupdate/SPUMessageTypes.h b/Autoupdate/SPUMessageTypes.h index 8b69a006..9f6d244c 100644 --- a/Autoupdate/SPUMessageTypes.h +++ b/Autoupdate/SPUMessageTypes.h @@ -38,6 +38,11 @@ typedef NS_ENUM(int32_t, SPUUpdaterMessageType) SPUUpdaterAlivePong = 3 }; +typedef NS_ENUM(int32_t, SPUAgentMessageType) +{ + SPUAgentError = 100 +}; + BOOL SPUInstallerMessageTypeIsLegal(SPUInstallerMessageType oldMessageType, SPUInstallerMessageType newMessageType); NSString *SPUInstallerServiceNameForBundleIdentifier(NSString *bundleIdentifier); diff --git a/Sparkle/InstallerProgress/InstallerProgressAppController.h b/Sparkle/InstallerProgress/InstallerProgressAppController.h index b60cef6d..f7a5099d 100644 --- a/Sparkle/InstallerProgress/InstallerProgressAppController.h +++ b/Sparkle/InstallerProgress/InstallerProgressAppController.h @@ -18,7 +18,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)run; -- (void)cleanupAndExitWithStatus:(int)status __attribute__((noreturn)); +- (void)cleanupAndExitWithStatus:(int)status error:(NSError * _Nullable)error __attribute__((noreturn)); @end diff --git a/Sparkle/InstallerProgress/InstallerProgressAppController.m b/Sparkle/InstallerProgress/InstallerProgressAppController.m index 33a6c4e5..ad3d6f61 100644 --- a/Sparkle/InstallerProgress/InstallerProgressAppController.m +++ b/Sparkle/InstallerProgress/InstallerProgressAppController.m @@ -15,6 +15,7 @@ #import "SUInstallerAgentInitiationProtocol.h" #import "StatusInfo.h" #import "SUHost.h" +#import "SUErrors.h" /*! * Terminate the application after a delay from launching the new update to avoid OS activation issues @@ -61,25 +62,25 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; if (self != nil) { if (arguments.count != 3) { SULog(SULogLevelError, @"Error: Invalid number of arguments supplied: %@", arguments); - [self cleanupAndExitWithStatus:EXIT_FAILURE]; + [self cleanupAndExitWithStatus:EXIT_FAILURE error:nil]; } NSString *hostBundlePath = arguments[1]; if (hostBundlePath.length == 0) { SULog(SULogLevelError, @"Error: Host bundle path length is 0"); - [self cleanupAndExitWithStatus:EXIT_FAILURE]; + [self cleanupAndExitWithStatus:EXIT_FAILURE error:nil]; } _hostBundle = [NSBundle bundleWithPath:hostBundlePath]; if (_hostBundle == nil) { SULog(SULogLevelError, @"Error: Host bundle for target is nil"); - [self cleanupAndExitWithStatus:EXIT_FAILURE]; + [self cleanupAndExitWithStatus:EXIT_FAILURE error:nil]; } NSString *hostBundleIdentifier = _hostBundle.bundleIdentifier; if (hostBundleIdentifier == nil) { SULog(SULogLevelError, @"Error: Host bundle identifier for target is nil"); - [self cleanupAndExitWithStatus:EXIT_FAILURE]; + [self cleanupAndExitWithStatus:EXIT_FAILURE error:nil]; return nil; // just to silence analyzer warnings later about hostBundleIdentifier being nil } @@ -112,11 +113,14 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; InstallerProgressAppController *strongSelf = weakSelf; if (strongSelf != nil) { int exitStatus = (strongSelf.repliedToRegistration ? EXIT_SUCCESS : EXIT_FAILURE); + NSError *registrationError; if (!strongSelf.repliedToRegistration) { - SULog(SULogLevelError, @"Error: Agent Invalidating without having the chance to reply to installer"); + registrationError = [NSError errorWithDomain:SUSparkleErrorDomain code:SPUAgentError userInfo:@{ NSLocalizedDescriptionKey: @"Error: Agent Invalidating without having the chance to reply to installer" }]; + } else { + registrationError = nil; } if (!strongSelf.willTerminate) { - [strongSelf cleanupAndExitWithStatus:exitStatus]; + [strongSelf cleanupAndExitWithStatus:exitStatus error:registrationError]; } } }); @@ -144,7 +148,7 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(CONNECTION_ACKNOWLEDGEMENT_TIMEOUT * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ if (!self.connected) { SULog(SULogLevelError, @"Timeout error: failed to receive acknowledgement from installer"); - [self cleanupAndExitWithStatus:EXIT_FAILURE]; + [self cleanupAndExitWithStatus:EXIT_FAILURE error:nil]; } }); } @@ -154,8 +158,19 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; [self startConnection]; } -- (void)cleanupAndExitWithStatus:(int)status __attribute__((noreturn)) +- (void)cleanupAndExitWithStatus:(int)status error:(NSError * _Nullable)error __attribute__((noreturn)) { + if (error != nil) { + SULog(SULogLevelError, @"Agent failed with error: %@", error); + + NSError *underlyingError = error.userInfo[NSUnderlyingErrorKey]; + if (underlyingError != nil) { + SULog(SULogLevelError, @"Error: %@", underlyingError); + } + + [(id)self.connection.remoteObjectProxy connectionWillInvalidateWithError:error]; + } + [self.statusInfo invalidate]; [self.connection invalidate]; @@ -200,8 +215,7 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; if (applicationBundlePath != nil && !self.willTerminate) { NSBundle *applicationBundle = [NSBundle bundleWithPath:applicationBundlePath]; if (applicationBundle == nil) { - SULog(SULogLevelError, @"Error: Encountered invalid path for waiting termination: %@", applicationBundlePath); - [self cleanupAndExitWithStatus:EXIT_FAILURE]; + [self cleanupAndExitWithStatus:EXIT_FAILURE error:[NSError errorWithDomain:SUSparkleErrorDomain code:SPUAgentError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Error: Encountered invalid path for waiting termination: %@", applicationBundlePath] }]]; } NSArray *runningApplications = [self runningApplicationsWithBundle:applicationBundle]; @@ -261,8 +275,7 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; // We should at least make sure we're opening a bundle however NSBundle *relaunchBundle = [NSBundle bundleWithPath:pathToRelaunch]; if (relaunchBundle == nil) { - SULog(SULogLevelError, @"Error: Encountered invalid path to relaunch: %@", pathToRelaunch); - [self cleanupAndExitWithStatus:EXIT_FAILURE]; + [self cleanupAndExitWithStatus:EXIT_FAILURE error:[NSError errorWithDomain:SUSparkleErrorDomain code:SPUAgentError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Error: Encountered invalid path to relaunch: %@", pathToRelaunch] }]]; } // We only launch applications, but I'm not sure how reliable -launchApplicationAtURL:options:config: is so we're not using it @@ -276,7 +289,7 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; self.willTerminate = YES; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(SUTerminationTimeDelay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - [self cleanupAndExitWithStatus:EXIT_SUCCESS]; + [self cleanupAndExitWithStatus:EXIT_SUCCESS error:nil]; }); } }); diff --git a/Sparkle/InstallerProgress/SUInstallerAgentInitiationProtocol.h b/Sparkle/InstallerProgress/SUInstallerAgentInitiationProtocol.h index 948e6c89..23c0d17e 100644 --- a/Sparkle/InstallerProgress/SUInstallerAgentInitiationProtocol.h +++ b/Sparkle/InstallerProgress/SUInstallerAgentInitiationProtocol.h @@ -12,4 +12,6 @@ - (void)connectionDidInitiateWithReply:(void (^)(void))acknowledgement; +- (void)connectionWillInvalidateWithError:(NSError *)error; + @end diff --git a/Sparkle/InstallerProgress/main.m b/Sparkle/InstallerProgress/main.m index cfc7ffbc..a387986c 100644 --- a/Sparkle/InstallerProgress/main.m +++ b/Sparkle/InstallerProgress/main.m @@ -27,7 +27,7 @@ int main(int __unused argc, const char __unused *argv[]) dispatch_source_t sigtermSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGTERM, 0, dispatch_get_main_queue()); dispatch_source_set_event_handler(sigtermSource, ^{ - [appController cleanupAndExitWithStatus:SIGTERM]; + [appController cleanupAndExitWithStatus:SIGTERM error:nil]; }); dispatch_resume(sigtermSource);