| 1 |
#import <Cocoa/Cocoa.h> |
|---|
| 2 |
#import <IOKit/IOMessage.h> |
|---|
| 3 |
#import <IOKit/pwr_mgt/IOPMLib.h> |
|---|
| 4 |
#import <Security/Security.h> |
|---|
| 5 |
|
|---|
| 6 |
#import "Libs/SSHAgent.h" |
|---|
| 7 |
#import "TunnelController.h" |
|---|
| 8 |
|
|---|
| 9 |
io_connect_t root_port; |
|---|
| 10 |
|
|---|
| 11 |
int sleep_timestamp; |
|---|
| 12 |
|
|---|
| 13 |
void powerchange_callback(void *x, io_service_t y, natural_t type, void *argument) |
|---|
| 14 |
{ |
|---|
| 15 |
switch(type) |
|---|
| 16 |
{ |
|---|
| 17 |
case kIOMessageSystemWillSleep: |
|---|
| 18 |
sleep_timestamp = time(nil); |
|---|
| 19 |
IOAllowPowerChange(root_port,(long)argument); |
|---|
| 20 |
break; |
|---|
| 21 |
case kIOMessageCanSystemSleep: |
|---|
| 22 |
sleep_timestamp = time(nil); |
|---|
| 23 |
IOAllowPowerChange(root_port,(long)argument); |
|---|
| 24 |
break; |
|---|
| 25 |
case kIOMessageSystemHasPoweredOn: |
|---|
| 26 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"SKWake" object:nil]; |
|---|
| 27 |
break; |
|---|
| 28 |
} |
|---|
| 29 |
} |
|---|
| 30 |
|
|---|
| 31 |
OSStatus keychain_locked(SecKeychainEvent keychainEvent, SecKeychainCallbackInfo *info, void *context) |
|---|
| 32 |
{ |
|---|
| 33 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"AppleKeychainLocked" object:nil]; |
|---|
| 34 |
return 0; |
|---|
| 35 |
} |
|---|
| 36 |
|
|---|
| 37 |
OSStatus keychain_unlocked(SecKeychainEvent keychainEvent, SecKeychainCallbackInfo *info, void *context) |
|---|
| 38 |
{ |
|---|
| 39 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"AppleKeychainUnlocked" object:nil]; |
|---|
| 40 |
return 0; |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
void sighandler(int num) |
|---|
| 44 |
{ |
|---|
| 45 |
SSHAgent *agent = [SSHAgent currentAgent]; |
|---|
| 46 |
[agent stop]; |
|---|
| 47 |
[[TunnelController sharedController] closeAllTunnels]; |
|---|
| 48 |
exit(0); |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
int main(int argc, const char *argv[]) |
|---|
| 52 |
{ |
|---|
| 53 |
IONotificationPortRef notify; |
|---|
| 54 |
io_object_t theIterator; |
|---|
| 55 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|---|
| 56 |
|
|---|
| 57 |
if((root_port = IORegisterForSystemPower(0, ¬ify, powerchange_callback, &theIterator)) == nil) |
|---|
| 58 |
{ |
|---|
| 59 |
[NSException raise: NSInternalInconsistencyException |
|---|
| 60 |
format: @"Failed to register process for System Power Notifications"]; |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notify), kCFRunLoopDefaultMode); |
|---|
| 64 |
|
|---|
| 65 |
signal(SIGTERM, sighandler); |
|---|
| 66 |
|
|---|
| 67 |
SecKeychainAddCallback(&keychain_locked, kSecLockEventMask, nil); |
|---|
| 68 |
SecKeychainAddCallback(&keychain_unlocked, kSecUnlockEventMask, nil); |
|---|
| 69 |
|
|---|
| 70 |
[pool release]; |
|---|
| 71 |
|
|---|
| 72 |
return NSApplicationMain(argc, argv); |
|---|
| 73 |
} |
|---|