This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; |