Skip to content

Commit

Permalink
Add C implementation for FreeBSD
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Jan 7, 2024
1 parent 9a50e3c commit 8db00b1
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions restartable.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <err.h>
#include <fcntl.h>
#include <kvm.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include <sys/user.h>
#include <libutil.h>

void
print_proc(const struct kinfo_proc *kp) {
struct kinfo_vmentry *vmmap;
int i, count;

vmmap = kinfo_getvmmap(kp->ki_pid, &count);
if (vmmap == NULL && errno != EPERM)
err(1, "kinfo_getvmmap(): %d", kp->ki_pid);

for (i = 0; i < count; i++)
if (vmmap[i].kve_type == KVME_TYPE_VNODE && vmmap[i].kve_protection & VM_PROT_EXECUTE && vmmap[i].kve_path[0] == '\0')
printf("%d\t%d\t%d\t%s\t%s\n", kp->ki_pid, kp->ki_ppid, kp->ki_ruid, kp->ki_login, kp->ki_comm);

free(vmmap);
}

int
print_all(void) {
struct kinfo_proc *procs;
int count;

procs = kinfo_getallproc(&count);
if (procs == NULL)
err(1, "kinfo_getallproc()");

for (int i = 0; i < count; i++)
print_proc(&procs[i]);

free(procs);
return 0;
}

int
main(void) {
printf("PID\tPPID\tUID\tUser\tCommand\n");
exit(print_all() != 0);
}

0 comments on commit 8db00b1

Please sign in to comment.