Skip to content

Instantly share code, notes, and snippets.

View nguyen-phillip's full-sized avatar

Phillip Nguyen nguyen-phillip

  • Google
  • NYC
View GitHub Profile
@nguyen-phillip
nguyen-phillip / proc.py
Created November 15, 2019 01:33
Using libproc from Python with ctypes
import ctypes
libproc = ctypes.cdll.LoadLibrary('/usr/lib/libproc.dylib')
# int proc_listallpids(void * buffer, int buffersize)
libproc.proc_listallpids.res_type = ctypes.c_int
libproc.proc_listallpids.arg_types = [ctypes.c_void_p, ctypes.c_int]
# int proc_pidpath(int pid, void * buffer, uint32_t buffersize)
libproc.proc_pidpath.res_type = ctypes.c_int
@nguyen-phillip
nguyen-phillip / proc.c
Last active November 30, 2023 13:01
Using libproc.h
#include <stdio.h>
#include <stdlib.h>
#include <libproc.h>
// Uses proc_pidinfo from libproc.h to find the parent of given pid.
// Call this repeatedly until ppid(pid) == pid to get ancestors.
int ppid(pid_t pid) {
struct proc_bsdinfo info;
proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &info, sizeof(info));
return info.pbi_ppid;