forked from SelfControlApp/selfcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckupMain.m
66 lines (51 loc) · 2.24 KB
/
CheckupMain.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
/*
* CheckupMain.c
* SelfControl
*
* Created by Charlie Stigler on 7/13/10.
* Copyright 2010 Harvard-Westlake Student. All rights reserved.
*
*/
#include "CheckupMain.h"
int main(int argc, char* argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
if(geteuid()) {
NSLog(@"ERROR: SUID bit not set on scheckup.");
printStatus(-201);
exit(EX_NOPERM);
}
NSDictionary* curDictionary = [NSDictionary dictionaryWithContentsOfFile: SelfControlLockFilePath];
NSDate* blockStartedDate = [curDictionary objectForKey: @"BlockStartedDate"];
NSTimeInterval blockDuration = [[curDictionary objectForKey: @"BlockDuration"] intValue];
if(blockStartedDate == nil || [[NSDate distantFuture] isEqualToDate: blockStartedDate] || blockDuration < 1) {
// The lock file seems to be broken. Try defaults.
NSLog(@"WARNING: Lock file unreadable or invalid");
[NSUserDefaults resetStandardUserDefaults];
seteuid(getuid());
defaults = [NSUserDefaults standardUserDefaults];
[defaults addSuiteNamed:@"org.eyebeam.SelfControl"];
blockStartedDate = [defaults objectForKey: @"BlockStartedDate"];
blockDuration = [[defaults objectForKey: @"BlockDuration"] intValue];
[defaults synchronize];
[NSUserDefaults resetStandardUserDefaults];
seteuid(0);
if(blockStartedDate == nil || [[NSDate distantFuture] isEqualToDate: blockStartedDate] || blockDuration < 1) {
// Defaults is broken too! Let's get out of here!
NSLog(@"WARNING: Checkup ran but no block found. Attempting to remove block.");
// get rid of this block
removeBlock(getuid());
printStatus(-215);
exit(EX_SOFTWARE);
}
}
// convert to seconds
blockDuration *= 60;
NSTimeInterval timeSinceStarted = [[NSDate date] timeIntervalSinceDate: blockStartedDate];
if( blockStartedDate == nil || blockDuration < 1 || [[NSDate distantFuture] isEqualToDate: blockStartedDate] || timeSinceStarted >= blockDuration) {
NSLog(@"INFO: Checkup helper ran, block expired, removing block.");
removeBlock(getuid());
}
[pool drain];
NSLog(@"INFO: scheckup run, but block should still be ongoing.");
exit(EXIT_SUCCESS);
}