diff options
author | Gleb Smirnoff <glebius@FreeBSD.org> | 2017-05-17 19:17:40 -0700 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2017-05-18 04:17:40 +0200 |
commit | 5250b63787dec4a4cefbc1c93d84b58ac874405a (patch) | |
tree | 583374f21bf08966566c30b756e79c223a942914 | |
parent | 61b6ed0830d1f3045913bab67f2e6364de65b7b3 (diff) | |
download | psutil-5250b63787dec4a4cefbc1c93d84b58ac874405a.tar.gz |
Fixes to net_connections() on FreeBSD. (#1079)
* File descriptor 0 is a valid value, for example for a daemon.
* On FreeBSD fill in file descriptor values.
This not only removes ugly workaround from test_connections.py, but also
fixes several failures in this test. Without file descriptor value, two
local sockets connected to each other, would be equal objects. Since
in the _psbsd.py:net_connections(), the returned value is a Python set,
it will exclude any duplicates, resulting in shrinked list of sockets.
-rw-r--r-- | psutil/arch/freebsd/sys_socks.c | 30 | ||||
-rw-r--r-- | psutil/tests/__init__.py | 2 | ||||
-rwxr-xr-x | psutil/tests/test_connections.py | 4 |
3 files changed, 17 insertions, 19 deletions
diff --git a/psutil/arch/freebsd/sys_socks.c b/psutil/arch/freebsd/sys_socks.c index 3387838e..e0e2046b 100644 --- a/psutil/arch/freebsd/sys_socks.c +++ b/psutil/arch/freebsd/sys_socks.c @@ -57,16 +57,16 @@ psutil_populate_xfiles() { } -int -psutil_get_pid_from_sock(void *sock) { +struct xfile * +psutil_get_file_from_sock(void *sock) { struct xfile *xf; int n; for (xf = psutil_xfiles, n = 0; n < psutil_nxfiles; ++n, ++xf) { if (xf->xf_data == sock) - return xf->xf_pid; + return xf; } - return -1; + return NULL; } @@ -129,7 +129,8 @@ int psutil_gather_inet(int proto, PyObject *py_retlist) { } while (xig->xig_gen != exig->xig_gen && retry--); for (;;) { - int lport, rport, pid, status, family; + struct xfile *xf; + int lport, rport, status, family; xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len); if (xig >= exig) @@ -174,8 +175,8 @@ int psutil_gather_inet(int proto, PyObject *py_retlist) { char lip[200], rip[200]; - pid = psutil_get_pid_from_sock(so->xso_so); - if (pid < 0) + xf = psutil_get_file_from_sock(so->xso_so); + if (xf == NULL) continue; lport = ntohs(inp->inp_lport); rport = ntohs(inp->inp_fport); @@ -203,13 +204,13 @@ int psutil_gather_inet(int proto, PyObject *py_retlist) { goto error; py_tuple = Py_BuildValue( "(iiiNNii)", - -1, // fd + xf->xf_fd, // fd family, // family type, // type py_laddr, // laddr py_raddr, // raddr status, // status - pid); // pid + xf->xf_pid); // pid if (!py_tuple) goto error; if (PyList_Append(py_retlist, py_tuple)) @@ -238,7 +239,6 @@ int psutil_gather_unix(int proto, PyObject *py_retlist) { size_t bufsize; void *buf; int retry; - int pid; struct sockaddr_un *sun; char path[PATH_MAX]; @@ -286,6 +286,8 @@ int psutil_gather_unix(int proto, PyObject *py_retlist) { } while (xug->xug_gen != exug->xug_gen && retry--); for (;;) { + struct xfile *xf; + xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len); if (xug >= exug) break; @@ -293,8 +295,8 @@ int psutil_gather_unix(int proto, PyObject *py_retlist) { if (xup->xu_len != sizeof *xup) goto error; - pid = psutil_get_pid_from_sock(xup->xu_socket.xso_so); - if (pid < 0) + xf = psutil_get_file_from_sock(xup->xu_socket.xso_so); + if (xf == NULL) continue; sun = (struct sockaddr_un *)&xup->xu_addr; @@ -306,13 +308,13 @@ int psutil_gather_unix(int proto, PyObject *py_retlist) { goto error; py_tuple = Py_BuildValue("(iiiOsii)", - -1, // fd + xf->xf_fd, // fd AF_UNIX, // family proto, // type py_lpath, // lpath "", // rath PSUTIL_CONN_NONE, // status - pid); // pid + xf->xf_pid); // pid if (!py_tuple) goto error; if (PyList_Append(py_retlist, py_tuple)) diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index f14b1c98..927e8cf6 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -930,7 +930,7 @@ def check_connection_ntuple(conn): # check fd if has_fd: - assert conn.fd > 0, conn + assert conn.fd >= 0, conn if hasattr(socket, 'fromfd') and not WINDOWS: try: dupsock = socket.fromfd(conn.fd, conn.family, conn.type) diff --git a/psutil/tests/test_connections.py b/psutil/tests/test_connections.py index c4d896ee..27822a80 100755 --- a/psutil/tests/test_connections.py +++ b/psutil/tests/test_connections.py @@ -132,10 +132,6 @@ class Base(object): raise # Filter for this proc PID and exlucde PIDs from the tuple. sys_cons = [c[:-1] for c in sys_cons if c.pid == pid] - if FREEBSD: - # On FreeBSD all fds are set to -1 so exclude them - # from comparison. - proc_cons = [pconn(*[-1] + list(x[1:])) for x in proc_cons] sys_cons.sort() proc_cons.sort() self.assertEqual(proc_cons, sys_cons) |