source: trunk/Sparkle/relaunch.m @ 1046

Revision 1015, 1.4 KB checked in by speck, 10 months ago (diff)

Lots of changes for xcode 4.1 (added delegate declarations, fixed deprecated function calls, upgraded SDK and platforms).

Line 
1
2#import <AppKit/AppKit.h>
3
4#include <unistd.h>
5
6@interface TerminationListener : NSObject
7{
8    const char *executablePath;
9    pid_t parentProcessId;
10}
11
12- (void) relaunch;
13
14@end
15
16@implementation TerminationListener
17
18- (id) initWithExecutablePath:(const char *)execPath parentProcessId:(pid_t)ppid
19{
20    self = [super init];
21    if (self != nil)
22    {
23        executablePath = execPath;
24        parentProcessId = ppid;
25        if (getppid() == 1) // ppid is launchd (1) => parent terminated already
26            [self relaunch];
27        [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(watchdog:) userInfo:nil repeats:YES];
28    }
29    return self;
30}
31
32- (void)watchdog:(NSTimer *)timer
33{
34    ProcessSerialNumber psn;
35    if (GetProcessForPID(parentProcessId, &psn) == procNotFound)
36        [self relaunch];
37}
38
39- (void) relaunch
40{
41    [[NSWorkspace sharedWorkspace] openFile:[[NSFileManager defaultManager] stringWithFileSystemRepresentation:executablePath length:strlen(executablePath)]];
42    [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"relaunch"] error:nil];
43    exit(0);
44}
45
46@end
47
48int main (int argc, const char * argv[])
49{
50    if (argc != 3) return EXIT_FAILURE;
51   
52    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
53   
54    [NSApplication sharedApplication];
55    [[[TerminationListener alloc] initWithExecutablePath:argv[1] parentProcessId:atoi(argv[2])] autorelease];
56    [[NSApplication sharedApplication] run];
57   
58    [pool drain];
59   
60    return EXIT_SUCCESS;
61}
Note: See TracBrowser for help on using the repository browser.