mirror of
https://github.com/sparkle-project/Sparkle
synced 2026-11-25 04:47:55 +00:00
The relaunch tool sadly now polls for the host app being closed, rather than registering for notifications. But there was some race condition which had problems with the latter.
60 lines
1.4 KiB
Objective-C
60 lines
1.4 KiB
Objective-C
|
|
#import <AppKit/AppKit.h>
|
|
|
|
@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;
|
|
if (getppid() == 1) // ppid is launchd (1) => parent terminated already
|
|
[self relaunch];
|
|
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(watchdog:) userInfo:nil repeats:YES];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)watchdog:(NSTimer *)timer
|
|
{
|
|
ProcessSerialNumber psn;
|
|
if (GetProcessForPID(parentProcessId, &psn) == procNotFound)
|
|
[self relaunch];
|
|
}
|
|
|
|
- (void) relaunch
|
|
{
|
|
[[NSWorkspace sharedWorkspace] launchApplication:[NSString stringWithUTF8String:executablePath]];
|
|
[[NSFileManager defaultManager] removeFileAtPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"relaunch"] handler:nil];
|
|
exit(0);
|
|
}
|
|
|
|
@end
|
|
|
|
int main (int argc, const char * argv[])
|
|
{
|
|
if (argc != 3) return EXIT_FAILURE;
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
[NSApplication sharedApplication];
|
|
[[[TerminationListener alloc] initWithExecutablePath:argv[1] parentProcessId:atoi(argv[2])] autorelease];
|
|
[[NSApplication sharedApplication] run];
|
|
|
|
[pool release];
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|