Skip to content

Commit

Permalink
Print full argv for FreeBSD C version
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Jan 7, 2024
1 parent a059f46 commit a16f841
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion unix/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CFLAGS = -Wall -Wextra

restartable: restartable.c
$(CC) $(CFLAGS) -o $@ $< -lutil
$(CC) $(CFLAGS) -o $@ $< -lutil -lkvm
51 changes: 45 additions & 6 deletions unix/restartable.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,37 @@

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <err.h>
#include <sys/types.h>
#include <sys/user.h>
#include <libutil.h>

void
print_proc(const struct kinfo_proc *kp) {
#include <fcntl.h>
#include <kvm.h>
#include <limits.h> /* _POSIX2_LINE_MAX */
#include <paths.h> /* _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;

Expand All @@ -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);
}

0 comments on commit a16f841

Please sign in to comment.