Skip to content

Commit

Permalink
Fix socket to PID translation on FreeBSD. (#1070)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
glebius authored and giampaolo committed May 16, 2017
1 parent ec3e2ef commit 5b64638
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions psutil/arch/bsd/freebsd_socks.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 5b64638

Please sign in to comment.