public
Created

For Mac OS X: Get a list of running processes, and tell if a particular process is running, by name

  • Download Gist
LICENSE.md
Markdown

Copyright (c) 2010 DeepTech, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

processes.h
Objective-C
1 2 3 4
#import <Foundation/Foundation.h>
 
NSArray* allProcesses();
BOOL processIsRunning(NSString* executableName, NSArray* processes);
processes.m
Objective-C
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
#include "processes.h"
#import <sys/sysctl.h>
 
NSArray* allProcesses(){
static int maxArgumentSize = 0;
if (maxArgumentSize == 0) {
size_t size = sizeof(maxArgumentSize);
if (sysctl((int[]){ CTL_KERN, KERN_ARGMAX }, 2, &maxArgumentSize, &size, NULL, 0) == -1) {
perror("sysctl argument size");
maxArgumentSize = 4096; // Default
}
}
NSMutableArray *processes = [NSMutableArray array];
int mib[3] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL};
struct kinfo_proc *info;
size_t length;
int count;
if (sysctl(mib, 3, NULL, &length, NULL, 0) < 0)
return nil;
if (!(info = malloc(length)))
return nil;
if (sysctl(mib, 3, info, &length, NULL, 0) < 0) {
free(info);
return nil;
}
count = length / sizeof(struct kinfo_proc);
for (int i = 0; i < count; i++) {
pid_t pid = info[i].kp_proc.p_pid;
if (pid == 0) {
continue;
}
size_t size = maxArgumentSize;
char* buffer = (char *)malloc(length);
if (sysctl((int[]){ CTL_KERN, KERN_PROCARGS2, pid }, 3, buffer, &size, NULL, 0) == 0) {
NSString* executable = [NSString stringWithCString:(buffer+sizeof(int)) encoding:NSUTF8StringEncoding];
[processes addObject:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:pid], @"pid",
executable, @"executable",
nil]];
}
free(buffer);
}
free(info);
return processes;
}
 
BOOL processIsRunning(NSString* executableName, NSArray* processes){
if (!processes) {
processes = allProcesses();
}
BOOL searchIsPath = [executableName isAbsolutePath];
NSEnumerator* processEnumerator = [processes objectEnumerator];
NSDictionary* process;
while ((process = (NSDictionary*)[processEnumerator nextObject])) {
NSString* executable = [process objectForKey:@"executable"];
if ([(searchIsPath ? executable : [executable lastPathComponent]) isEqual:executableName]) {
return YES;
}
}
return NO;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.