-
Notifications
You must be signed in to change notification settings - Fork 7
/
relaunch.m
71 lines (56 loc) · 1.64 KB
/
relaunch.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#import <Cocoa/Cocoa.h>
#import <unistd.h>
@interface TerminationListener : NSObject
{
@private
const char *executablePath;
pid_t parentProcessId;
}
- (void)relaunch __dead2;
@end
@implementation TerminationListener
- (void)watchdog:(NSTimer *)timer
{
ProcessSerialNumber psn;
if (GetProcessForPID(parentProcessId, &psn) == procNotFound)
[self relaunch];
}
- (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) relaunch
{
[[NSWorkspace sharedWorkspace] openFile:[[NSFileManager defaultManager] stringWithFileSystemRepresentation:executablePath length:strlen(executablePath)]];
NSString* path = NSTemporaryDirectory();
if (path)
{
path = [path stringByAppendingPathComponent:@"relaunch"];
#if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4
[[NSFileManager defaultManager] removeFileAtPath:path handler:nil];
#else
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
#endif
}
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 drain];
return EXIT_SUCCESS;
}