From 2e5bacfe37aaf7f98e961fecaaae97eed01d0d50 Mon Sep 17 00:00:00 2001 From: yeonsh Date: Fri, 4 Dec 2015 15:14:31 +0900 Subject: [PATCH 1/2] Darwin: Experiment with libproc --- process_darwin.h | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/process_darwin.h b/process_darwin.h index c7742ef..5a4f643 100644 --- a/process_darwin.h +++ b/process_darwin.h @@ -6,6 +6,8 @@ #include #include #include +#include +#include // This is declared in process_darwin.go extern void go_darwin_append_proc(pid_t, pid_t, char *); @@ -17,7 +19,7 @@ extern void go_darwin_append_proc(pid_t, pid_t, char *); // be possible to do this all in Go, I didn't want to go spelunking through // header files to get all the structures properly. It is much easier to just // call it in C and be done with it. -static inline int darwinProcesses() { +static inline int darwinProcesses0() { int err = 0; int i = 0; static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 }; @@ -63,4 +65,26 @@ static inline int darwinProcesses() { return 0; } +static inline int darwinProcesses() { + + int numberOfProcesses = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0); + pid_t pids[1024]; + bzero(pids, 1024); + proc_listpids(PROC_ALL_PIDS, 0, pids, sizeof(pids)); + for (int i=0; i 0) { + printf("path: %s\n", pathBuffer); + go_darwin_append_proc( + pids[i], + 0, + pathBuffer); + } + } + + return 0; +} #endif From 99fd3fcb7c94bcff669b6535f98645f9ba01d09b Mon Sep 17 00:00:00 2001 From: yeonsh Date: Fri, 4 Dec 2015 20:29:33 +0900 Subject: [PATCH 2/2] Fix for Darwin/amd64. Full path name of processes --- process_darwin.h | 88 ++++++++++++++++-------------------------------- 1 file changed, 29 insertions(+), 59 deletions(-) diff --git a/process_darwin.h b/process_darwin.h index 5a4f643..87bd1b0 100644 --- a/process_darwin.h +++ b/process_darwin.h @@ -5,9 +5,10 @@ #include #include -#include -#include +#include +#include #include +#include // This is declared in process_darwin.go extern void go_darwin_append_proc(pid_t, pid_t, char *); @@ -19,72 +20,41 @@ extern void go_darwin_append_proc(pid_t, pid_t, char *); // be possible to do this all in Go, I didn't want to go spelunking through // header files to get all the structures properly. It is much easier to just // call it in C and be done with it. -static inline int darwinProcesses0() { - int err = 0; - int i = 0; - static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 }; - size_t length = 0; - struct kinfo_proc *result = NULL; - size_t resultCount = 0; - // Get the length first - err = sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, - NULL, &length, NULL, 0); - if (err != 0) { - goto ERREXIT; - } +static inline int darwinProcesses() { - // Allocate the appropriate sized buffer to read the process list - result = malloc(length); + uid_t euid = geteuid(); - // Call sysctl again with our buffer to fill it with the process list - err = sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, - result, &length, - NULL, 0); - if (err != 0) { - goto ERREXIT; - } + int pid_buf_size = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0); + int pid_count = pid_buf_size / sizeof(pid_t); - resultCount = length / sizeof(struct kinfo_proc); - for (i = 0; i < resultCount; i++) { - struct kinfo_proc *single = &result[i]; - go_darwin_append_proc( - single->kp_proc.p_pid, - single->kp_eproc.e_ppid, - single->kp_proc.p_comm); - } + pid_t* pids = malloc(pid_buf_size); + bzero(pids, pid_buf_size); -ERREXIT: - if (result != NULL) { - free(result); - } + proc_listpids(PROC_ALL_PIDS, 0, pids, pid_buf_size); + char path_buffer[PROC_PIDPATHINFO_MAXSIZE]; - if (err != 0) { - return errno; - } - return 0; -} + int ppid = 0; -static inline int darwinProcesses() { + for (int i=0; i < pid_count; i++) { + if (pids[i] == 0) break; + + if (euid == 0) { + // You need root permission to get proc_bsdinfo from some processes. + // When you call following function with normal user permission you will + // receive 'operation not permitted' error and it will be terminated. + struct proc_bsdinfo bsdinfo; + proc_pidinfo(pids[i], PROC_PIDTBSDINFO, 0, &bsdinfo, sizeof(struct proc_bsdinfo)); + ppid = bsdinfo.pbi_ppid; + } - int numberOfProcesses = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0); - pid_t pids[1024]; - bzero(pids, 1024); - proc_listpids(PROC_ALL_PIDS, 0, pids, sizeof(pids)); - for (int i=0; i 0) { - printf("path: %s\n", pathBuffer); - go_darwin_append_proc( - pids[i], - 0, - pathBuffer); - } + bzero(path_buffer, PROC_PIDPATHINFO_MAXSIZE); + if (proc_pidpath(pids[i], path_buffer, sizeof(path_buffer)) > 0) { + go_darwin_append_proc(pids[i], ppid, path_buffer); } + } + free(pids); - return 0; + return 0; } #endif