Douglas William Thrift 14 years ago
commit
9eeb26adfd
4 changed files with 44 additions and 0 deletions
  1. 2 0
      .gitattributes
  2. 1 0
      .gitignore
  3. 6 0
      GNUmakefile
  4. 35 0
      pstree.cpp

+ 2 - 0
.gitattributes

@@ -0,0 +1,2 @@
+*.cpp	ident
+# vim: shiftwidth=8 tabstop=8

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+pstree

+ 6 - 0
GNUmakefile

@@ -0,0 +1,6 @@
+LDFLAGS := -lkvm
+
+all: pstree
+
+clean:
+	rm -f pstree

+ 35 - 0
pstree.cpp

@@ -0,0 +1,35 @@
+// DT PS Tree
+//
+// Douglas Thrift
+//
+// $Id$
+
+#include <err.h>
+#include <fcntl.h>
+#include <kvm.h>
+#include <limits.h>
+#include <paths.h>
+#include <stdio.h>
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#include <sys/user.h>
+
+int main(int argc, char *argv[])
+{
+	char error[_POSIX2_LINE_MAX];
+	kvm_t *kd = kvm_openfiles(NULL, _PATH_DEVNULL, NULL, O_RDONLY, error);
+
+	if (!kd)
+		errx(1, "%s", error);
+
+	int count;
+	struct kinfo_proc *procs = kvm_getprocs(kd, KERN_PROC_PROC, 0, &count);
+
+	if (!procs)
+		errx(1, "%s", kvm_geterr(kd));
+
+	for (struct kinfo_proc *proc = procs, *end = procs + count; proc != end; ++proc)
+		printf("%s %i %i\n", proc->ki_comm, proc->ki_pid, proc->ki_ppid);
+
+	return 0;
+}