diff options
-rw-r--r-- | psutil/arch/bsd/netbsd_socks.c | 133 |
1 files changed, 75 insertions, 58 deletions
diff --git a/psutil/arch/bsd/netbsd_socks.c b/psutil/arch/bsd/netbsd_socks.c index bd260ad3..8f713055 100644 --- a/psutil/arch/bsd/netbsd_socks.c +++ b/psutil/arch/bsd/netbsd_socks.c @@ -66,7 +66,8 @@ static void psutil_kiflist_clear(void); static void psutil_kpcblist_init(void); static void psutil_kpcblist_clear(void); static int psutil_get_files(void); -static int psutil_get_sockets(const char *name); +static int psutil_get_sockets(const char *name, struct kinfo_pcb *pcb, + struct kpcb *kpcb); static int psutil_get_info(int aff); @@ -116,6 +117,8 @@ psutil_get_files(void) { int mib[6]; char *buf; off_t offset; + struct kinfo_file *ki = NULL; + struct kif *kif = NULL; mib[0] = CTL_KERN; mib[1] = KERN_FILE2; @@ -144,10 +147,10 @@ psutil_get_files(void) { } len /= sizeof(struct kinfo_file); - struct kinfo_file *ki = (struct kinfo_file *)(buf + offset); + ki = (struct kinfo_file *)(buf + offset); for (j = 0; j < len; j++) { - struct kif *kif = malloc(sizeof(struct kif)); + kif = malloc(sizeof(struct kif)); kif->kif = &ki[j]; SLIST_INSERT_HEAD(&kihead, kif, kifs); } @@ -166,10 +169,11 @@ psutil_get_files(void) { // Get open sockets. static int -psutil_get_sockets(const char *name) { +psutil_get_sockets(const char *name, + struct kinfo_pcb *pcb, + struct kpcb *kpcb) { size_t namelen; int mib[8]; - struct kinfo_pcb *pcb; size_t len; size_t j; @@ -204,7 +208,7 @@ psutil_get_sockets(const char *name) { struct kinfo_pcb *kp = (struct kinfo_pcb *)pcb; for (j = 0; j < len; j++) { - struct kpcb *kpcb = malloc(sizeof(struct kpcb)); + kpcb = malloc(sizeof(struct kpcb)); kpcb->kpcb = &kp[j]; SLIST_INSERT_HEAD(&kpcbhead, kpcb, kpcbs); } @@ -226,84 +230,96 @@ psutil_get_sockets(const char *name) { // Collect open file and connections. static int psutil_get_info(int aff) { + struct kinfo_pcb *pcb = NULL; + struct kpcb *kpcb = NULL; + switch (aff) { case INET: - if (psutil_get_sockets("net.inet.tcp.pcblist") != 0) - return -1; - if (psutil_get_sockets("net.inet.udp.pcblist") != 0) - return -1; - if (psutil_get_sockets("net.inet6.tcp6.pcblist") != 0) - return -1; - if (psutil_get_sockets("net.inet6.udp6.pcblist") != 0) - return -1; + if (psutil_get_sockets("net.inet.tcp.pcblist", pcb, kpcb) != 0) + goto error; + if (psutil_get_sockets("net.inet.udp.pcblist", pcb, kpcb) != 0) + goto error; + if (psutil_get_sockets("net.inet6.tcp6.pcblist", pcb, kpcb) != 0) + goto error; + if (psutil_get_sockets("net.inet6.udp6.pcblist", pcb, kpcb) != 0) + goto error; break; case INET4: - if (psutil_get_sockets("net.inet.tcp.pcblist") != 0) - return -1; - if (psutil_get_sockets("net.inet.udp.pcblist") != 0) - return -1; + if (psutil_get_sockets("net.inet.tcp.pcblist", pcb, kpcb) != 0) + goto error; + if (psutil_get_sockets("net.inet.udp.pcblist", pcb, kpcb) != 0) + goto error; break; case INET6: - if (psutil_get_sockets("net.inet6.tcp6.pcblist") != 0) - return -1; - if (psutil_get_sockets("net.inet6.udp6.pcblist") != 0) - return -1; + if (psutil_get_sockets("net.inet6.tcp6.pcblist", pcb, kpcb) != 0) + goto error; + if (psutil_get_sockets("net.inet6.udp6.pcblist", pcb, kpcb) != 0) + goto error; break; case TCP: - if (psutil_get_sockets("net.inet.tcp.pcblist") != 0) - return -1; - if (psutil_get_sockets("net.inet6.tcp6.pcblist") != 0) - return -1; + if (psutil_get_sockets("net.inet.tcp.pcblist", pcb, kpcb) != 0) + goto error; + if (psutil_get_sockets("net.inet6.tcp6.pcblist", pcb, kpcb) != 0) + goto error; break; case TCP4: - if (psutil_get_sockets("net.inet.tcp.pcblist") != 0) - return -1; + if (psutil_get_sockets("net.inet.tcp.pcblist", pcb, kpcb) != 0) + goto error; break; case TCP6: - if (psutil_get_sockets("net.inet6.tcp6.pcblist") != 0) - return -1; + if (psutil_get_sockets("net.inet6.tcp6.pcblist", pcb, kpcb) != 0) + goto error; break; case UDP: - if (psutil_get_sockets("net.inet.udp.pcblist") != 0) - return -1; - if (psutil_get_sockets("net.inet6.udp6.pcblist") != 0) - return -1; + if (psutil_get_sockets("net.inet.udp.pcblist", pcb, kpcb) != 0) + goto error; + if (psutil_get_sockets("net.inet6.udp6.pcblist", pcb, kpcb) != 0) + goto error; break; case UDP4: - if (psutil_get_sockets("net.inet.udp.pcblist") != 0) - return -1; + if (psutil_get_sockets("net.inet.udp.pcblist", pcb, kpcb) != 0) + goto error; break; case UDP6: - if (psutil_get_sockets("net.inet6.udp6.pcblist") != 0) - return -1; + if (psutil_get_sockets("net.inet6.udp6.pcblist", pcb, kpcb) != 0) + goto error; break; case UNIX: - if (psutil_get_sockets("net.local.stream.pcblist") != 0) - return -1; - if (psutil_get_sockets("net.local.seqpacket.pcblist") != 0) - return -1; - if (psutil_get_sockets("net.local.dgram.pcblist") != 0) - return -1; + if (psutil_get_sockets("net.local.stream.pcblist", pcb, kpcb) != 0) + goto error; + if (psutil_get_sockets("net.local.seqpacket.pcblist", pcb, kpcb) != 0) + goto error; + if (psutil_get_sockets("net.local.dgram.pcblist", pcb, kpcb) != 0) + goto error; break; case ALL: - if (psutil_get_sockets("net.inet.tcp.pcblist") != 0) - return -1; - if (psutil_get_sockets("net.inet.udp.pcblist") != 0) - return -1; - if (psutil_get_sockets("net.inet6.tcp6.pcblist") != 0) - return -1; - if (psutil_get_sockets("net.inet6.udp6.pcblist") != 0) - return -1; - if (psutil_get_sockets("net.local.stream.pcblist") != 0) - return -1; - if (psutil_get_sockets("net.local.seqpacket.pcblist") != 0) - return -1; - if (psutil_get_sockets("net.local.dgram.pcblist") != 0) - return -1; + if (psutil_get_sockets("net.inet.tcp.pcblist", pcb, kpcb) != 0) + goto error; + if (psutil_get_sockets("net.inet.udp.pcblist", pcb, kpcb) != 0) + goto error; + if (psutil_get_sockets("net.inet6.tcp6.pcblist", pcb, kpcb) != 0) + goto error; + if (psutil_get_sockets("net.inet6.udp6.pcblist", pcb, kpcb) != 0) + goto error; + if (psutil_get_sockets("net.local.stream.pcblist", pcb, kpcb) != 0) + goto error; + if (psutil_get_sockets("net.local.seqpacket.pcblist", pcb, kpcb) != 0) + goto error; + if (psutil_get_sockets("net.local.dgram.pcblist", pcb, kpcb) != 0) + goto error; break; } + free(pcb); + free(kpcb); return 0; + +error: + if (pcb != NULL) + free(pcb); + if (kpcb != NULL) + free(kpcb); + return -1; } @@ -333,6 +349,7 @@ psutil_net_connections(PyObject *self, PyObject *args) { psutil_kpcblist_init(); if (psutil_get_files() != 0) goto error; + if (psutil_get_info(ALL) != 0) goto error; |