diff options
author | Gleb Smirnoff <glebius@FreeBSD.org> | 2017-05-16 04:49:13 -0700 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2017-05-16 13:49:13 +0200 |
commit | 5b6463868f824e566d98d35ce62a4ccc6e1e3c33 (patch) | |
tree | 291a2b163b459651799a5c989ca240cc99b7cdf8 | |
parent | ec3e2efea85b95c42ab3eb497337e6e597e0f9b6 (diff) | |
download | psutil-5b6463868f824e566d98d35ce62a4ccc6e1e3c33.tar.gz |
Fix socket to PID translation on FreeBSD. (#1070)
This file was derived from FreeBSD usr.bin/sockstat/sockstat.c. The
logic of socket to PID translation was copied incorrectly. The hash,
that sockstat(1) utility has, is completely internal feature, it isn't
part of FreeBSD API, it is just to speed things up. So, to use this
hash one actually needs to create it: declare array of buckets, populate
it with sockets. In the freebsd_socks.c this wasn't done. This fix
doesn't create the hash, instead it removes remnants of hashing that was
there in sockstat.c. It makes code more simple, but of course slower than
original sockstat(1) in case if machine is running zillions of sockets. I
decided to go this way simply because I am low on time to invest into
psutil, and also because better first provide correct and simple
implementation and then improve it, rather than jump for complexity.
-rw-r--r-- | psutil/arch/bsd/freebsd_socks.c | 19 |
1 files changed, 6 insertions, 13 deletions
diff --git a/psutil/arch/bsd/freebsd_socks.c b/psutil/arch/bsd/freebsd_socks.c index 5c4a47df..d60dc898 100644 --- a/psutil/arch/bsd/freebsd_socks.c +++ b/psutil/arch/bsd/freebsd_socks.c @@ -28,7 +28,6 @@ #include "../../_psutil_common.h" #include "../../_psutil_posix.h" -#define HASHSIZE 1009 // a signaler for connections without an actual status static int PSUTIL_CONN_NONE = 128; static struct xfile *psutil_xfiles; @@ -201,14 +200,12 @@ psutil_populate_xfiles() { int -psutil_get_pid_from_sock(int sock_hash) { +psutil_get_pid_from_sock(void *sock) { struct xfile *xf; - int hash, n; + int n; + for (xf = psutil_xfiles, n = 0; n < psutil_nxfiles; ++n, ++xf) { - if (xf->xf_data == NULL) - continue; - hash = (int)((uintptr_t)xf->xf_data % HASHSIZE); - if (sock_hash == hash) + if (xf->xf_data == sock) return xf->xf_pid; } return -1; @@ -230,7 +227,6 @@ int psutil_gather_inet(int proto, PyObject *py_retlist) { const char *varname = NULL; size_t len, bufsize; void *buf; - int hash; int retry; int type; @@ -320,8 +316,7 @@ int psutil_gather_inet(int proto, PyObject *py_retlist) { char lip[200], rip[200]; - hash = (int)((uintptr_t)so->xso_so % HASHSIZE); - pid = psutil_get_pid_from_sock(hash); + pid = psutil_get_pid_from_sock(so->xso_so); if (pid < 0) continue; lport = ntohs(inp->inp_lport); @@ -377,7 +372,6 @@ int psutil_gather_unix(int proto, PyObject *py_retlist) { size_t len; size_t bufsize; void *buf; - int hash; int retry; int pid; struct sockaddr_un *sun; @@ -434,8 +428,7 @@ int psutil_gather_unix(int proto, PyObject *py_retlist) { if (xup->xu_len != sizeof *xup) goto error; - hash = (int)((uintptr_t) xup->xu_socket.xso_so % HASHSIZE); - pid = psutil_get_pid_from_sock(hash); + pid = psutil_get_pid_from_sock(xup->xu_socket.xso_so); if (pid < 0) continue; |