I am trying to get ports used by processes. It can be done via lsof on macOS, i am trying to do it via libproc.
#include <iostream>
#include <libproc.h>
int main(int argc, const char * argv[]) {
pid_t pids[3072];
int count = proc_listpids(PROC_ALL_PIDS, 0, pids, sizeof(pids));
for (int i = 0; i < count; i++) {
char buffer[1024];
for (int j = 1; j < 50000; j++) { //port range
int ret = proc_pidfileportinfo(pids[i], j, PROC_PIDFILEPORTVNODEPATHINFO, buffer, sizeof(buffer));
if(ret != 0) {
printf("proc_pidfileportinfo returned %d bytes of data\n", ret);
printf("%s\n", name);
}
}
}
return 0;
}
proc_pidfileportinfo
function is not working for any port, i tried iterating till 50K. What i am doing wrong with proc_pidfileportinfo?
how to properly use proc_pidfileportinfo?
I work on something similar - socskstat, a tool well-known in FreeBSD word. You can try and say if it does the thing: https://github.com/mezantrop/sockstat