Skip to content

Instantly share code, notes, and snippets.

@su8
Last active August 17, 2018 09:05
Show Gist options
  • Save su8/68f685dab2af39b71fd9c87cbeecf517 to your computer and use it in GitHub Desktop.
Save su8/68f685dab2af39b71fd9c87cbeecf517 to your computer and use it in GitHub Desktop.
#include <fcntl.h>
#include <kvm.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
int main(int argc,char** args) {
kvm_t* kd;
// Get a handle to libkvm interface
kd = kvm_open(NULL, "/dev/null", NULL, O_RDONLY, "error: ");
struct kinfo_proc *kp, *kp2;
int p_count;
char pathname[256];
size_t length;
length = sizeof(pathname);
kp = kvm_getprocs(kd, KERN_PROC_PROC, 0, &p_count);
for (int x = 1; x <= p_count; x++) {
int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, kp[x].ki_pid };
sysctl(mib, 4, pathname, &length, NULL, 0);
printf("%d %s\n", kp[x].ki_pid, pathname);
pathname[0]='\0';
}
kvm_close(kd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment