-
Notifications
You must be signed in to change notification settings - Fork 7
/
FindProcess.m
113 lines (90 loc) · 3.02 KB
/
FindProcess.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#import "FindProcess.h"
#import <objc/runtime.h>
#include <stdio.h>
#include <stdint.h>
#include <mach/mach.h>
#include <dlfcn.h>
#include <pthread.h>
#include <unistd.h>
#import <Foundation/Foundation.h>
#include <mach-o/dyld.h>
#import <objc/runtime.h>
#include <sys/cdefs.h>
#include <sys/types.h>
#include <sys/param.h>
#include <mach/boolean.h>
#include <dispatch/dispatch.h>
#include <stdlib.h>
#include <spawn.h>
#include <assert.h>
#import <Foundation/Foundation.h>
#import <Security/Security.h>
#define DLog(format, ...) CFShow((__bridge CFStringRef)[NSString stringWithFormat:format, ## __VA_ARGS__]);
extern char*** _NSGetEnviron(void);
extern int proc_listallpids(void*, int);
extern int proc_pidpath(int, void*, uint32_t);
static int process_buffer_size = 4096;
@implementation FindProcess
+ (int)pidFromItemDescription:(NSString *)desc {
NSScanner *theScanner = nil;
NSString *text = nil;
//NSString *myString = @"<OS_xpc_connection: <connection: 0x133dc3790> { name = com.apple.sharingd.peer.0x133dc3790, listener = false, pid = 3731, euid = 501, egid = 501, asid = 0 }>";
theScanner = [NSScanner scannerWithString:desc];
while ([theScanner isAtEnd] == NO) {
// find start of tag
[theScanner scanUpToString:@"pid =" intoString:NULL] ;
// find end of tag
[theScanner scanUpToString:@"," intoString:&text] ;
}
NSString *clean = [text stringByReplacingOccurrencesOfString:@"pid = " withString:@""];
NSLog(@"clean: %@", clean);
return [clean intValue];
}
+ (boolean_t) process:(pid_t)ppid matches:(const char* )name {
//pid_t *pid_buffer;
char path_buffer[MAXPATHLEN];
boolean_t res = FALSE;
int ret = proc_pidpath(ppid, (void*)path_buffer, sizeof(path_buffer));
if(ret < 0) {
printf("(%s:%d) proc_pidinfo() call failed.\n", __FILE__, __LINE__);
}
if(strstr(path_buffer, name)) {
res = TRUE;
}
return res;
}
//plucked and modified from AppSyncUnified
+ (pid_t) find_process:(const char*) name {
pid_t *pid_buffer;
char path_buffer[MAXPATHLEN];
int count, i, ret;
boolean_t res = FALSE;
pid_t ppid_ret = 0;
pid_buffer = (pid_t*)calloc(1, process_buffer_size);
assert(pid_buffer != NULL);
count = proc_listallpids(pid_buffer, process_buffer_size);
if(count) {
for(i = 0; i < count; i++) {
pid_t ppid = pid_buffer[i];
ret = proc_pidpath(ppid, (void*)path_buffer, sizeof(path_buffer));
if(ret < 0) {
printf("(%s:%d) proc_pidinfo() call failed.\n", __FILE__, __LINE__);
continue;
}
if (strncmp(path_buffer, name, strlen(path_buffer)) == 0){
res = TRUE;
ppid_ret = ppid;
break;
}
/*
if(strstr(path_buffer, name)) {
res = TRUE;
ppid_ret = ppid;
break;
}*/
}
}
free(pid_buffer);
return ppid_ret;
}
@end