forked from SelfControlApp/selfcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckupMain.m
60 lines (46 loc) · 1.83 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
/*
* 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[]) {
@autoreleasepool {
if(geteuid()) {
NSLog(@"ERROR: SUID bit not set on scheckup.");
printStatus(-201);
exit(EX_NOPERM);
}
registerDefaults(getuid());
NSDictionary* curDictionary = [NSDictionary dictionaryWithContentsOfFile: SelfControlLockFilePath];
NSDate* blockStartedDate = curDictionary[@"BlockStartedDate"];
NSTimeInterval blockDuration = [curDictionary[@"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");
NSDictionary* defaults = getDefaultsDict(getuid());
blockStartedDate = defaults[@"BlockStartedDate"];
blockDuration = [defaults[@"BlockDuration"] intValue];
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());
exit(EXIT_SUCCESS);
}
}
// 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());
exit(EXIT_SUCCESS);
}
}
NSLog(@"INFO: scheckup run, but block should still be ongoing.");
exit(EXIT_SUCCESS);
}