Skip to content

Instantly share code, notes, and snippets.

@s4y
Created August 26, 2011 17:11
Show Gist options
  • Save s4y/1173880 to your computer and use it in GitHub Desktop.
Save s4y/1173880 to your computer and use it in GitHub Desktop.
For Mac OS X: Get a list of running processes, and tell if a particular process is running, by name

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.

#import <Foundation/Foundation.h>
NSArray* allProcesses();
BOOL processIsRunning(NSString* executableName, NSArray* processes);
#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;
}
@Lessica
Copy link

Lessica commented May 27, 2024

ATTENTION PLEASE: USE WITH CAUTION!

Line 33:

size_t size = maxArgumentSize;
char* buffer = (char *)malloc(length);  // <-- `length` is allocated
if (sysctl((int[]){ CTL_KERN, KERN_PROCARGS2, pid }, 3, buffer, &size, NULL, 0) == 0) {  // <-- while the maximum length is `size`

This will fail if proc argument is larger than the size of kinfo_proc array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment