~andymatuschak/sparkle/main

16 by andym
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
1
#import <AppKit/AppKit.h>
2
3
#include <unistd.h>
159.1.1 by Stuart Morgan
Fixes several warnings
4
5
@interface TerminationListener : NSObject
16 by andym
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
6
{
7
	const char *executablePath;
8
	pid_t parentProcessId;
9
}
10
11
- (void) relaunch;
12
13
@end
14
15
@implementation TerminationListener
16
17
- (id) initWithExecutablePath:(const char *)execPath parentProcessId:(pid_t)ppid
18
{
19
	self = [super init];
20
	if (self != nil)
169 by Andy Matuschak
Fixes 240385
21
	{
22
		executablePath = execPath;
16 by andym
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
23
		parentProcessId = ppid;
24
		if (getppid() == 1) // ppid is launchd (1) => parent terminated already
169 by Andy Matuschak
Fixes 240385
25
			[self relaunch];
26
		[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(watchdog:) userInfo:nil repeats:YES];
27
	}
16 by andym
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
28
	return self;
29
}
30
31
- (void)watchdog:(NSTimer *)timer
169 by Andy Matuschak
Fixes 240385
32
{
16 by andym
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
33
	ProcessSerialNumber psn;
169 by Andy Matuschak
Fixes 240385
34
	if (GetProcessForPID(parentProcessId, &psn) == procNotFound)
35
		[self relaunch];
16 by andym
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
36
}
37
38
- (void) relaunch
39
{
40
	[[NSWorkspace sharedWorkspace] openFile:[[NSFileManager defaultManager] stringWithFileSystemRepresentation:executablePath length:strlen(executablePath)]];
181 by Andy Matuschak
Replaced UTF8String with fileSystemRepresentation and [NSString initWithUTF8String] with [NSFileManager stringWithFileSystemRepresentation:length:] as appropriate.
41
	[[NSFileManager defaultManager] removeFileAtPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"relaunch"] handler:nil];
127.1.6 by David Smith
At Andy's suggestion, don't bother with overly paranoid fallback behavior. If we can't copy to NSTemporaryDirectory(), something is very very wrong and we should bail. This greatly simplifies some things and lets me clean up the copy of relaunch once it's done. :)
42
	exit(0);
107 by andym
Fixed an issue that would prevent people running FileVault from using Sparkle due to the disk image checking.
43
}
16 by andym
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
44
45
@end
46
47
int main (int argc, const char * argv[])
48
{
49
	if (argc != 3) return EXIT_FAILURE;
50
	
51
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
52
	
53
	[NSApplication sharedApplication];
24 by catlan
add [NSApplication sharedApplication] to be sure relaucher stay alive and check if pid exists
54
	[[[TerminationListener alloc] initWithExecutablePath:argv[1] parentProcessId:atoi(argv[2])] autorelease];
16 by andym
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
55
	[[NSApplication sharedApplication] run];
28 by andym
Fixed the relaunch tool to have Leoaprd compatibility (thanks, Cedric Luthi)
56
	
16 by andym
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
57
	[pool drain];
185 by Andy Matuschak
Fixes 239512
58
	
16 by andym
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
59
	return EXIT_SUCCESS;
60
}
62 by andym
Fixed warnings for missing newlines at the end of the file.
61