From b2eab97d7327b618e700d50e089d9d6181bf5747 Mon Sep 17 00:00:00 2001 From: andym Date: Thu, 25 Oct 2007 14:27:55 +0000 Subject: [PATCH] Made the relaunching process much snazzier, courtesy of Cedric Luthi. Now we use a separate program to kill the host app and to watch for when it is actually closed to launch its successor. Please let me know if you have any problems with this.K --- SUUpdater.m | 22 +++-- Sparkle.xcodeproj/project.pbxproj | 135 ++++++++++++++++++++++++++++++ relaunch.m | 63 ++++++++++++++ 3 files changed, 208 insertions(+), 12 deletions(-) create mode 100644 relaunch.m diff --git a/SUUpdater.m b/SUUpdater.m index c7db7e47..e57b7fae 100644 --- a/SUUpdater.m +++ b/SUUpdater.m @@ -578,18 +578,16 @@ [[NSNotificationCenter defaultCenter] postNotificationName:SUUpdaterWillRestartNotification object:self]; - // Thanks to Allan Odgaard for this restart code, which is much more clever than mine was. - setenv("LAUNCH_PATH", [currentAppPath UTF8String], 1); - setenv("TEMP_FOLDER", [[downloadPath stringByDeletingLastPathComponent] UTF8String], 1); // delete the temp stuff after it's all over - system("/bin/bash -c '{ for (( i = 0; i < 3000 && $(echo $(/bin/ps -xp $PPID|/usr/bin/wc -l))-1; i++ )); do\n" - " /bin/sleep .2;\n" - " done\n" - " if [[ $(/bin/ps -xp $PPID|/usr/bin/wc -l) -ne 2 ]]; then\n" - " /usr/bin/open \"${LAUNCH_PATH}\"\n" - " fi\n" - " rm -rf \"${TEMP_FOLDER}\"\n" - "} &>/dev/null &'"); - [NSApp terminate:self]; + NSString *relaunchPath = [[[[NSBundle bundleForClass:[self class]] executablePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"relaunch"]; + if (!relaunchPath) // slight hack to resolve issues with running with in configurations + { + NSString *frameworkPath = [[[NSBundle mainBundle] sharedFrameworksPath] stringByAppendingPathComponent:@"Sparkle.framework"]; + NSBundle *framework = [NSBundle bundleWithPath:frameworkPath]; + relaunchPath = [[[framework executablePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"relaunch"]; + } + + [NSTask launchedTaskWithLaunchPath:relaunchPath arguments:[NSArray arrayWithObjects:currentAppPath, [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]], nil]]; + [NSApp terminate:self]; } - (IBAction)cancelDownload:sender diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 1a359091..7a65c0ce 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -23,6 +23,8 @@ 61299A8D09CA790200B7442F /* SUUnarchiver.h in Headers */ = {isa = PBXBuildFile; fileRef = 61299A8B09CA790200B7442F /* SUUnarchiver.h */; settings = {ATTRIBUTES = (Public, ); }; }; 61299A8E09CA790200B7442F /* SUUnarchiver.m in Sources */ = {isa = PBXBuildFile; fileRef = 61299A8C09CA790200B7442F /* SUUnarchiver.m */; }; 61299B3609CB04E000B7442F /* Sparkle.h in Headers */ = {isa = PBXBuildFile; fileRef = 61299B3509CB04E000B7442F /* Sparkle.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 6132424C0CD06DE900106AA4 /* relaunch.m in Sources */ = {isa = PBXBuildFile; fileRef = 613242130CD06CEF00106AA4 /* relaunch.m */; }; + 613242610CD06E2200106AA4 /* relaunch in CopyFiles */ = {isa = PBXBuildFile; fileRef = 613242400CD06DB000106AA4 /* relaunch */; }; 61407C390A4099050009F71F /* Sparkle.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 8DC2EF5B0486A6940098B216 /* Sparkle.framework */; }; 6196CFF909C72148000DC222 /* SUStatusController.h in Headers */ = {isa = PBXBuildFile; fileRef = 6196CFE309C71ADE000DC222 /* SUStatusController.h */; settings = {ATTRIBUTES = (Public, ); }; }; 6196CFFA09C72149000DC222 /* SUStatusController.m in Sources */ = {isa = PBXBuildFile; fileRef = 6196CFE409C71ADE000DC222 /* SUStatusController.m */; }; @@ -57,6 +59,13 @@ /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ + 6132425C0CD06E0500106AA4 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 0867D690FE84028FC02AAC07 /* Project object */; + proxyType = 1; + remoteGlobalIDString = 6132423F0CD06DB000106AA4 /* Relaunch Tool */; + remoteInfo = "Relaunch Tool"; + }; 61B5F91B09C4CF7200B25A18 /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; containerPortal = 0867D690FE84028FC02AAC07 /* Project object */; @@ -77,6 +86,16 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + DA142A520CC79E5A00CE54F3 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = ""; + dstSubfolderSpec = 6; + files = ( + 613242610CD06E2200106AA4 /* relaunch in CopyFiles */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ @@ -99,6 +118,8 @@ 61299A8B09CA790200B7442F /* SUUnarchiver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUUnarchiver.h; sourceTree = ""; }; 61299A8C09CA790200B7442F /* SUUnarchiver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUUnarchiver.m; sourceTree = ""; }; 61299B3509CB04E000B7442F /* Sparkle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Sparkle.h; sourceTree = ""; }; + 613242130CD06CEF00106AA4 /* relaunch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = relaunch.m; sourceTree = ""; }; + 613242400CD06DB000106AA4 /* relaunch */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = relaunch; sourceTree = BUILT_PRODUCTS_DIR; }; 6196CFE309C71ADE000DC222 /* SUStatusController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUStatusController.h; sourceTree = ""; }; 6196CFE409C71ADE000DC222 /* SUStatusController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUStatusController.m; sourceTree = ""; }; 61AAE8230A321A7F00D8810D /* en */ = {isa = PBXFileReference; fileEncoding = 10; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Sparkle.strings; sourceTree = ""; }; @@ -207,9 +228,17 @@ 8DC2EF5A0486A6940098B216 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist; path = Info.plist; sourceTree = ""; }; 8DC2EF5B0486A6940098B216 /* Sparkle.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Sparkle.framework; sourceTree = BUILT_PRODUCTS_DIR; }; D2F7E79907B2D74100F64583 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = /System/Library/Frameworks/CoreData.framework; sourceTree = ""; }; + DA142A740CC79FEA00CE54F3 /* relaunch */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; path = relaunch; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ + 6132423E0CD06DB000106AA4 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 61B5F90009C4CEE200B25A18 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -237,6 +266,7 @@ children = ( 8DC2EF5B0486A6940098B216 /* Sparkle.framework */, 61B5F90209C4CEE200B25A18 /* Sparkle Test App.app */, + 613242400CD06DB000106AA4 /* relaunch */, ); name = Products; sourceTree = ""; @@ -268,6 +298,7 @@ children = ( 8DC2EF5A0486A6940098B216 /* Info.plist */, 610D5A740A1670A4004AAD9C /* SUStatus.nib */, + DA142A740CC79FEA00CE54F3 /* relaunch */, ); name = Resources; sourceTree = ""; @@ -285,6 +316,7 @@ 61299A8C09CA790200B7442F /* SUUnarchiver.m */, 345AF9E20A5D707200D7DA6F /* SUStatusChecker.h */, 345AF9E30A5D707200D7DA6F /* SUStatusChecker.m */, + 613242130CD06CEF00106AA4 /* relaunch.m */, ); name = "Sparkle Classes"; sourceTree = ""; @@ -418,6 +450,22 @@ /* End PBXHeadersBuildPhase section */ /* Begin PBXNativeTarget section */ + 6132423F0CD06DB000106AA4 /* Relaunch Tool */ = { + isa = PBXNativeTarget; + buildConfigurationList = 613242500CD06DEE00106AA4 /* Build configuration list for PBXNativeTarget "Relaunch Tool" */; + buildPhases = ( + 6132423D0CD06DB000106AA4 /* Sources */, + 6132423E0CD06DB000106AA4 /* Frameworks */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = "Relaunch Tool"; + productName = "Relaunch Tool"; + productReference = 613242400CD06DB000106AA4 /* relaunch */; + productType = "com.apple.product-type.tool"; + }; 61B5F90109C4CEE200B25A18 /* Sparkle Test App */ = { isa = PBXNativeTarget; buildConfigurationList = 61B5F90509C4CEE300B25A18 /* Build configuration list for PBXNativeTarget "Sparkle Test App" */; @@ -445,10 +493,13 @@ 8DC2EF520486A6940098B216 /* Resources */, 8DC2EF540486A6940098B216 /* Sources */, 8DC2EF560486A6940098B216 /* Frameworks */, + DA142A520CC79E5A00CE54F3 /* CopyFiles */, + DA142B130CC7A66B00CE54F3 /* ShellScript */, ); buildRules = ( ); dependencies = ( + 6132425D0CD06E0500106AA4 /* PBXTargetDependency */, ); name = Sparkle; productInstallPath = "$(HOME)/Library/Frameworks"; @@ -504,6 +555,7 @@ targets = ( 8DC2EF4F0486A6940098B216 /* Sparkle */, 61B5F90109C4CEE200B25A18 /* Sparkle Test App */, + 6132423F0CD06DB000106AA4 /* Relaunch Tool */, ); }; /* End PBXProject section */ @@ -532,7 +584,31 @@ }; /* End PBXResourcesBuildPhase section */ +/* Begin PBXShellScriptBuildPhase section */ + DA142B130CC7A66B00CE54F3 /* ShellScript */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputPaths = ( + ); + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "ln -sf \"`basename \\\"${VERSIONS_FOLDER_PATH}\\\"`/${CURRENT_VERSION}/relaunch\" \"`dirname \\\"${BUILT_PRODUCTS_DIR}/${VERSIONS_FOLDER_PATH}\\\"`\""; + }; +/* End PBXShellScriptBuildPhase section */ + /* Begin PBXSourcesBuildPhase section */ + 6132423D0CD06DB000106AA4 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 6132424C0CD06DE900106AA4 /* relaunch.m in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 61B5F8FF09C4CEE200B25A18 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -567,6 +643,11 @@ /* End PBXSourcesBuildPhase section */ /* Begin PBXTargetDependency section */ + 6132425D0CD06E0500106AA4 /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = 6132423F0CD06DB000106AA4 /* Relaunch Tool */; + targetProxy = 6132425C0CD06E0500106AA4 /* PBXContainerItemProxy */; + }; 61B5F91C09C4CF7200B25A18 /* PBXTargetDependency */ = { isa = PBXTargetDependency; target = 8DC2EF4F0486A6940098B216 /* Sparkle */; @@ -796,6 +877,51 @@ }; name = Release; }; + 613242420CD06DB000106AA4 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + COPY_PHASE_STRIP = NO; + GCC_DYNAMIC_NO_PIC = NO; + GCC_ENABLE_FIX_AND_CONTINUE = YES; + GCC_MODEL_TUNING = G5; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PRECOMPILE_PREFIX_HEADER = YES; + GCC_PREFIX_HEADER = "$(SYSTEM_LIBRARY_DIR)/Frameworks/AppKit.framework/Headers/AppKit.h"; + INSTALL_PATH = /usr/local/bin; + OTHER_LDFLAGS = ( + "-framework", + Foundation, + "-framework", + AppKit, + ); + PREBINDING = NO; + PRODUCT_NAME = relaunch; + ZERO_LINK = YES; + }; + name = Debug; + }; + 613242430CD06DB000106AA4 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + COPY_PHASE_STRIP = YES; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + GCC_ENABLE_FIX_AND_CONTINUE = NO; + GCC_MODEL_TUNING = G5; + GCC_PRECOMPILE_PREFIX_HEADER = YES; + GCC_PREFIX_HEADER = "$(SYSTEM_LIBRARY_DIR)/Frameworks/AppKit.framework/Headers/AppKit.h"; + INSTALL_PATH = /usr/local/bin; + OTHER_LDFLAGS = ( + "-framework", + Foundation, + "-framework", + AppKit, + ); + PREBINDING = NO; + PRODUCT_NAME = relaunch; + ZERO_LINK = NO; + }; + name = Release; + }; 61B5F90609C4CEE300B25A18 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -868,6 +994,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 613242500CD06DEE00106AA4 /* Build configuration list for PBXNativeTarget "Relaunch Tool" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 613242420CD06DB000106AA4 /* Debug */, + 613242430CD06DB000106AA4 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 61B5F90509C4CEE300B25A18 /* Build configuration list for PBXNativeTarget "Sparkle Test App" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/relaunch.m b/relaunch.m new file mode 100644 index 00000000..ebdae109 --- /dev/null +++ b/relaunch.m @@ -0,0 +1,63 @@ +// gcc -Wall -arch i386 -arch ppc -Os -s -framework AppKit -o relaunch relaunch.m + +#import +#import + +@interface TerminationListener : NSObject +{ + const char *executablePath; + pid_t parentProcessId; +} + +- (void) relaunch; + +@end + +@implementation TerminationListener + +- (id) initWithExecutablePath:(const char *)execPath parentProcessId:(pid_t)ppid +{ + self = [super init]; + if (self != nil) { + executablePath = execPath; + parentProcessId = ppid; + + [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationDidTerminate:) name:NSWorkspaceDidTerminateApplicationNotification object:nil]; + if (getppid() == 1) { + // ppid is launchd (1) => parent terminated already + [self relaunch]; + } + } + return self; +} + +- (void) applicationDidTerminate:(NSNotification *)notification +{ + if (parentProcessId == [[[notification userInfo] valueForKey:@"NSApplicationProcessIdentifier"] intValue]) { + // parent just terminated + [self relaunch]; + } +} + +- (void) relaunch +{ + [[NSWorkspace sharedWorkspace] launchApplication:[NSString stringWithUTF8String:executablePath]]; + [NSApp terminate:self]; +} + +@end + +int main (int argc, const char * argv[]) +{ + if (argc != 3) return EXIT_FAILURE; + + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + [[[TerminationListener alloc] initWithExecutablePath:argv[1] parentProcessId:atoi(argv[2])] autorelease]; + [NSApp run]; + + // This will not be executed because -[NSApp terminate:] is called + [pool release]; + + return EXIT_SUCCESS; +} \ No newline at end of file