Skip to content

Instantly share code, notes, and snippets.

@haxrob
Created June 30, 2024 09:02
Show Gist options
  • Save haxrob/7075b5212fb66bf50461f96b90e87246 to your computer and use it in GitHub Desktop.
Save haxrob/7075b5212fb66bf50461f96b90e87246 to your computer and use it in GitHub Desktop.
### NOTE: partly generated by ChatGTP 4.
# compile with -lkvm
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#include <fcntl.h>
#include <kvm.h>
#include <limits.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main() {
kvm_t *kd;
char errbuf[_POSIX2_LINE_MAX];
struct kinfo_proc *proc;
int count, i;
char **args;
kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, errbuf);
if (kd == NULL) {
fprintf(stderr, "Error opening kvm: %s\n", errbuf);
exit(1);
}
proc = kvm_getprocs(kd, KERN_PROC_PROC, 0, &count);
if (proc == NULL) {
fprintf(stderr, "Error getting process info: %s\n", kvm_geterr(kd));
kvm_close(kd);
exit(1);
}
for (i = 0; i < count; i++) {
printf("%d\t%si\t", proc[i].ki_pid, proc[i].ki_comm);
args = kvm_getargv(kd, &proc[i], 0);
if (args != NULL && args[0] != NULL) {
for (int j = 0; args[j] != NULL; j++) {
printf(" %s", args[j]);
}
}
printf("\n");
}
kvm_close(kd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment