Skip to content

Commit 990f781

Browse files
authored
Move global state in FreeBSD net_connections implementation into function (#2588)
Signed-off-by: Lysandros Nikolaou <[email protected]>
1 parent 6a99365 commit 990f781

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

psutil/arch/freebsd/sys_socks.c

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,35 @@
2727
#include "../../_psutil_posix.h"
2828

2929

30-
static struct xfile *psutil_xfiles;
31-
static int psutil_nxfiles;
32-
33-
3430
static int
35-
psutil_populate_xfiles(void) {
36-
size_t len;
31+
psutil_populate_xfiles(struct xfile **psutil_xfiles, int *psutil_nxfiles) {
32+
struct xfile *new_psutil_xfiles;
33+
size_t len = sizeof(struct xfile);
3734

38-
if ((psutil_xfiles = malloc(len = sizeof *psutil_xfiles)) == NULL) {
39-
PyErr_NoMemory();
40-
return 0;
41-
}
42-
while (sysctlbyname("kern.file", psutil_xfiles, &len, 0, 0) == -1) {
35+
while (sysctlbyname("kern.file", *psutil_xfiles, &len, 0, 0) == -1) {
4336
if (errno != ENOMEM) {
4437
PyErr_SetFromErrno(0);
4538
return 0;
4639
}
4740
len *= 2;
48-
if ((psutil_xfiles = realloc(psutil_xfiles, len)) == NULL) {
41+
new_psutil_xfiles = realloc(*psutil_xfiles, len);
42+
if (new_psutil_xfiles == NULL) {
4943
PyErr_NoMemory();
5044
return 0;
5145
}
46+
*psutil_xfiles = new_psutil_xfiles;
5247
}
53-
if (len > 0 && psutil_xfiles->xf_size != sizeof *psutil_xfiles) {
48+
if (len > 0 && (*psutil_xfiles)->xf_size != sizeof(struct xfile)) {
5449
PyErr_Format(PyExc_RuntimeError, "struct xfile size mismatch");
5550
return 0;
5651
}
57-
psutil_nxfiles = len / sizeof *psutil_xfiles;
52+
*psutil_nxfiles = len / sizeof(struct xfile);
5853
return 1;
5954
}
6055

6156

6257
static struct xfile *
63-
psutil_get_file_from_sock(kvaddr_t sock) {
58+
psutil_get_file_from_sock(kvaddr_t sock, struct xfile *psutil_xfiles, int psutil_nxfiles) {
6459
struct xfile *xf;
6560
int n;
6661

@@ -76,7 +71,8 @@ psutil_get_file_from_sock(kvaddr_t sock) {
7671
// https://github.com/freebsd/freebsd/blob/master/usr.bin/sockstat/sockstat.c
7772
static int
7873
psutil_gather_inet(
79-
int proto, int include_v4, int include_v6, PyObject *py_retlist)
74+
int proto, int include_v4, int include_v6, PyObject *py_retlist,
75+
struct xfile *psutil_xfiles, int psutil_nxfiles)
8076
{
8177
struct xinpgen *xig, *exig;
8278
struct xinpcb *xip;
@@ -186,7 +182,7 @@ psutil_gather_inet(
186182

187183
char lip[200], rip[200];
188184

189-
xf = psutil_get_file_from_sock(so->xso_so);
185+
xf = psutil_get_file_from_sock(so->xso_so, psutil_xfiles, psutil_nxfiles);
190186
if (xf == NULL)
191187
continue;
192188
lport = ntohs(inp->inp_lport);
@@ -243,7 +239,8 @@ psutil_gather_inet(
243239

244240

245241
static int
246-
psutil_gather_unix(int proto, PyObject *py_retlist) {
242+
psutil_gather_unix(int proto, PyObject *py_retlist,
243+
struct xfile *psutil_xfiles, int psutil_nxfiles) {
247244
struct xunpgen *xug, *exug;
248245
struct xunpcb *xup;
249246
const char *varname = NULL;
@@ -308,7 +305,7 @@ psutil_gather_unix(int proto, PyObject *py_retlist) {
308305
if (xup->xu_len != sizeof *xup)
309306
goto error;
310307

311-
xf = psutil_get_file_from_sock(xup->xu_socket.xso_so);
308+
xf = psutil_get_file_from_sock(xup->xu_socket.xso_so, psutil_xfiles, psutil_nxfiles);
312309
if (xf == NULL)
313310
continue;
314311

@@ -363,7 +360,8 @@ psutil_int_in_seq(int value, PyObject *py_seq) {
363360

364361
PyObject*
365362
psutil_net_connections(PyObject* self, PyObject* args) {
366-
int include_v4, include_v6, include_unix, include_tcp, include_udp;
363+
int include_v4, include_v6, include_unix, include_tcp, include_udp, psutil_nxfiles;
364+
struct xfile *psutil_xfiles;
367365
PyObject *py_af_filter = NULL;
368366
PyObject *py_type_filter = NULL;
369367
PyObject *py_retlist = PyList_New(0);
@@ -389,38 +387,47 @@ psutil_net_connections(PyObject* self, PyObject* args) {
389387
if ((include_udp = psutil_int_in_seq(SOCK_DGRAM, py_type_filter)) == -1)
390388
goto error;
391389

392-
if (psutil_populate_xfiles() != 1)
390+
psutil_xfiles = malloc(sizeof(struct xfile));
391+
if (psutil_xfiles == NULL) {
392+
PyErr_NoMemory();
393393
goto error;
394+
}
395+
396+
if (psutil_populate_xfiles(&psutil_xfiles, &psutil_nxfiles) != 1)
397+
goto error_free_psutil_xfiles;
394398

395399
// TCP
396400
if (include_tcp == 1) {
397401
if (psutil_gather_inet(
398-
IPPROTO_TCP, include_v4, include_v6, py_retlist) == 0)
402+
IPPROTO_TCP, include_v4, include_v6, py_retlist,
403+
psutil_xfiles, psutil_nxfiles) == 0)
399404
{
400-
goto error;
405+
goto error_free_psutil_xfiles;
401406
}
402407
}
403408
// UDP
404409
if (include_udp == 1) {
405410
if (psutil_gather_inet(
406-
IPPROTO_UDP, include_v4, include_v6, py_retlist) == 0)
411+
IPPROTO_UDP, include_v4, include_v6, py_retlist,
412+
psutil_xfiles, psutil_nxfiles) == 0)
407413
{
408-
goto error;
414+
goto error_free_psutil_xfiles;
409415
}
410416
}
411417
// UNIX
412418
if (include_unix == 1) {
413-
if (psutil_gather_unix(SOCK_STREAM, py_retlist) == 0)
414-
goto error;
415-
if (psutil_gather_unix(SOCK_DGRAM, py_retlist) == 0)
416-
goto error;
419+
if (psutil_gather_unix(SOCK_STREAM, py_retlist, psutil_xfiles, psutil_nxfiles) == 0)
420+
goto error_free_psutil_xfiles;
421+
if (psutil_gather_unix(SOCK_DGRAM, py_retlist, psutil_xfiles, psutil_nxfiles) == 0)
422+
goto error_free_psutil_xfiles;
417423
}
418424

419425
free(psutil_xfiles);
420426
return py_retlist;
421427

428+
error_free_psutil_xfiles:
429+
free(psutil_xfiles);
422430
error:
423431
Py_DECREF(py_retlist);
424-
free(psutil_xfiles);
425432
return NULL;
426433
}

0 commit comments

Comments
 (0)