diff --git a/unix/Makefile b/unix/Makefile index a787bc7..4187cab 100644 --- a/unix/Makefile +++ b/unix/Makefile @@ -1,4 +1,4 @@ CFLAGS = -Wall -Wextra restartable: restartable.c - $(CC) $(CFLAGS) -o $@ $< -lutil + $(CC) $(CFLAGS) -o $@ $< -lutil -lkvm diff --git a/unix/restartable.c b/unix/restartable.c index 8ca3cd4..2b89f4b 100644 --- a/unix/restartable.c +++ b/unix/restartable.c @@ -28,14 +28,37 @@ #include #include +#include #include #include #include #include #include -void -print_proc(const struct kinfo_proc *kp) { +#include +#include +#include /* _POSIX2_LINE_MAX */ +#include /* _PATH_DEVNULL */ + +static int verbose = 0; + +static void +print_argv(kvm_t *kd, const struct kinfo_proc *kp) { + char **argv = kvm_getargv(kd, kp, 0); + + if (argv == NULL) { + warn("kvm_getargv(): %d: %s", kp->ki_pid, kvm_geterr(kd)); + return; + } + printf("\t"); + do { + printf(" %s", *argv); + } while (*++argv); + printf("\n"); +} + +static void +print_proc(kvm_t *kd, const struct kinfo_proc *kp) { struct kinfo_vmentry *vmmap; int i, count; @@ -44,30 +67,46 @@ print_proc(const struct kinfo_proc *kp) { 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') + 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); + if (verbose) + print_argv(kd, kp); + break; + } free(vmmap); } -int +static int print_all(void) { + char errbuf[_POSIX2_LINE_MAX]; struct kinfo_proc *procs; + kvm_t *kd; int count; + kd = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, errbuf); + if (kd == NULL) + errx(1, "kvm_openfiles(): %s", errbuf); + procs = kinfo_getallproc(&count); if (procs == NULL) err(1, "kinfo_getallproc()"); for (int i = 0; i < count; i++) - print_proc(&procs[i]); + print_proc(kd, &procs[i]); free(procs); + (void)kvm_close(kd); return 0; } int -main(void) { +main(int argc, char *argv[]) { + if (argc > 2) + errx(1, "Usage: %s [-v]\n", argv[0]); + if (argc > 1 && !strcmp(argv[1], "-v")) + verbose = 1; + printf("PID\tPPID\tUID\tUser\tCommand\n"); exit(print_all() != 0); }