The following works on iOS and uses sysctlwhich applications, such as Activity Monitor Touch, used in the App Store, should be acceptable to Apple. However, what you intend to do with this path as soon as you receive it may not be acceptable to Apple. If you are not going to submit your application to the App Store, this is probably not a problem.
- (NSString *)pathFromProcessID:(NSUInteger)pid {
int mib[3] = {CTL_KERN, KERN_ARGMAX, 0};
size_t argmaxsize = sizeof(size_t);
size_t size;
int ret = sysctl(mib, 2, &size, &argmaxsize, NULL, 0);
if (ret != 0) {
NSLog(@"Error '%s' (%d) getting KERN_ARGMAX", strerror(errno), errno);
return nil;
}
mib[1] = KERN_PROCARGS2;
mib[2] = (int)pid;
char *procargv = malloc(size);
ret = sysctl(mib, 3, procargv, &size, NULL, 0);
if (ret != 0) {
NSLog(@"Error '%s' (%d) for pid %d", strerror(errno), errno, pid);
free(procargv);
return nil;
}
NSString *path = [NSString stringWithCString:(procargv + sizeof(int))
encoding:NSASCIIStringEncoding];
free(procargv);
return(path);
}
source
share