From 8db00b1d7db570db3b0b2ba2dd7593244efb7841 Mon Sep 17 00:00:00 2001 From: Ricardo Branco Date: Sun, 7 Jan 2024 16:57:26 +0100 Subject: [PATCH] Add C implementation for FreeBSD --- restartable.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 restartable.c diff --git a/restartable.c b/restartable.c new file mode 100644 index 0000000..d85bfff --- /dev/null +++ b/restartable.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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); +}