|
7 | 7 | #import "LCKXPCServer.h" |
8 | 8 | #import "LCTaskForPIDTweak.h" |
9 | 9 |
|
10 | | -@implementation LCKXPCService |
11 | | -- (instancetype)init { |
| 10 | +@implementation LCKProcessInterface |
| 11 | +- (instancetype)initWithConnection:(NSXPCConnection *)connection { |
12 | 12 | self = [super init]; |
13 | | - self.listener = [NSXPCListener anonymousListener]; |
14 | | - self.listener.delegate = self; |
15 | | - [self.listener resume]; |
16 | | - |
17 | | - self.processList = [NSMutableDictionary dictionary]; |
| 13 | + self.connection = connection; |
18 | 14 | return self; |
19 | 15 | } |
| 16 | +- (void)checkinWithInfo:(NSDictionary *)info { |
| 17 | + if(!self.info) self.info = info; |
| 18 | +} |
| 19 | +- (void)testShowAlert:(NSString *)message { |
| 20 | + LCShowAlert([NSString stringWithFormat:@"LCTaskForPIDTweak: %@", message]); |
| 21 | +} |
| 22 | +- (void)allRunningProcessesWithReply:(void (^)(NSArray<NSNumber *> *))reply { |
| 23 | + reply(LCKXPCService.sharedInstanceIfExists.processList.allKeys); |
| 24 | +} |
20 | 25 |
|
21 | | -// Protocol |
22 | 26 | - (void)proc_pidpath:(pid_t)pid reply:(void (^)(NSString *))reply { |
23 | | - reply(self.processList[@(pid)][@"ProgramArguments"][0]); |
| 27 | + reply(LCKXPCService.sharedInstanceIfExists.processList[@(pid)].info[@"ProgramArguments"][0]); |
| 28 | +} |
| 29 | +@end |
| 30 | + |
| 31 | +@implementation LCKXPCService |
| 32 | +static LCKXPCService *sharedInstance; |
| 33 | ++ (instancetype)sharedInstanceIfExists { |
| 34 | + return sharedInstance; |
24 | 35 | } |
25 | 36 |
|
26 | | -- (void)pid:(pid_t)pid checkinWithInfo:(NSDictionary *)info { |
27 | | - // only allow writing to uninitialized entry |
28 | | - if(self.processList[@(pid)] != NSNull.null) { |
29 | | - //LCShowAlert([NSString stringWithFormat:@"LCKXPCService Violation: guest tried to overwrite exitsing process info"]); |
30 | | - return; |
31 | | - } |
| 37 | +- (instancetype)init { |
| 38 | + self = sharedInstance = [super init]; |
| 39 | + self.listener = [NSXPCListener anonymousListener]; |
| 40 | + self.listener.delegate = self; |
| 41 | + [self.listener resume]; |
32 | 42 |
|
33 | | - self.processList[@(pid)] = info; |
34 | | -} |
35 | | -- (void)testShowAlert:(NSString *)message { |
36 | | - LCShowAlert([NSString stringWithFormat:@"LCTaskForPIDTweak: %@", message]); |
| 43 | + self.processList = [NSMutableDictionary dictionary]; |
| 44 | + return self; |
37 | 45 | } |
38 | 46 |
|
39 | 47 | // NSXPCListenerDelegate |
40 | 48 | - (BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection { |
41 | | - // set process entry to uninitialized |
42 | | - self.processList[@(newConnection.processIdentifier)] = NSNull.null; |
43 | | - |
| 49 | + LCKProcessInterface *obj = [[LCKProcessInterface alloc] initWithConnection:newConnection]; |
| 50 | + self.processList[@(newConnection.processIdentifier)] = obj; |
44 | 51 | newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(LCKXPCServiceProtocol)]; |
45 | | - newConnection.exportedObject = self; |
| 52 | + newConnection.exportedObject = obj; |
46 | 53 | [newConnection resume]; |
47 | 54 | return YES; |
48 | 55 | } |
|
0 commit comments