diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2016-10-24 22:47:30 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2016-10-24 22:47:30 +0200 |
commit | a0a06a0d5ea2770dc3f221b6d3cd55b43ef6505c (patch) | |
tree | 75e1b72d30b9df4a159f905154c4ed77b6f94c5b | |
parent | 0f2b34c151b496bfa8de9e1614043689661d5d90 (diff) | |
download | psutil-a0a06a0d5ea2770dc3f221b6d3cd55b43ef6505c.tar.gz |
share C function to retrieve MTU across all UNIXes
-rw-r--r-- | psutil/_psbsd.py | 3 | ||||
-rw-r--r-- | psutil/_pslinux.py | 3 | ||||
-rw-r--r-- | psutil/_psosx.py | 3 | ||||
-rw-r--r-- | psutil/_psutil_linux.c | 11 | ||||
-rw-r--r-- | psutil/_psutil_posix.c | 46 |
5 files changed, 46 insertions, 20 deletions
diff --git a/psutil/_psbsd.py b/psutil/_psbsd.py index 8474881b..46698a22 100644 --- a/psutil/_psbsd.py +++ b/psutil/_psbsd.py @@ -313,7 +313,8 @@ def net_if_stats(): names = net_io_counters().keys() ret = {} for name in names: - isup, duplex, speed, mtu = cext_posix.net_if_stats(name) + mtu = cext_posix.net_if_mtu(name) + isup, duplex, speed = cext_posix.net_if_stats(name) if hasattr(_common, 'NicDuplex'): duplex = _common.NicDuplex(duplex) ret[name] = _common.snicstats(isup, duplex, speed, mtu) diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 5d0f2787..0dfabacf 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -893,7 +893,8 @@ def net_if_stats(): names = net_io_counters().keys() ret = {} for name in names: - isup, duplex, speed, mtu = cext.net_if_stats(name) + mtu = cext_posix.net_if_mtu(name) + isup, duplex, speed = cext.net_if_stats(name) duplex = duplex_map[duplex] ret[name] = _common.snicstats(isup, duplex, speed, mtu) return ret diff --git a/psutil/_psosx.py b/psutil/_psosx.py index 9e312400..74d6cc3d 100644 --- a/psutil/_psosx.py +++ b/psutil/_psosx.py @@ -194,7 +194,8 @@ def net_if_stats(): names = net_io_counters().keys() ret = {} for name in names: - isup, duplex, speed, mtu = cext_posix.net_if_stats(name) + mtu = cext_posix.net_if_mtu(name) + isup, duplex, speed, = cext_posix.net_if_stats(name) if hasattr(_common, 'NicDuplex'): duplex = _common.NicDuplex(duplex) ret[name] = _common.snicstats(isup, duplex, speed, mtu) diff --git a/psutil/_psutil_linux.c b/psutil/_psutil_linux.c index c9be53d4..7d828faa 100644 --- a/psutil/_psutil_linux.c +++ b/psutil/_psutil_linux.c @@ -486,7 +486,6 @@ psutil_net_if_stats(PyObject* self, PyObject* args) { int ret; int duplex; int speed; - int mtu; struct ifreq ifr; struct ethtool_cmd ethcmd; PyObject *py_is_up = NULL; @@ -510,12 +509,6 @@ psutil_net_if_stats(PyObject* self, PyObject* args) { py_is_up = Py_False; Py_INCREF(py_is_up); - // MTU - ret = ioctl(sock, SIOCGIFMTU, &ifr); - if (ret == -1) - goto error; - mtu = ifr.ifr_mtu; - // duplex and speed memset(ðcmd, 0, sizeof ethcmd); ethcmd.cmd = ETHTOOL_GSET; @@ -540,7 +533,7 @@ psutil_net_if_stats(PyObject* self, PyObject* args) { } } - py_retlist = Py_BuildValue("[Oiii]", py_is_up, duplex, speed, mtu); + py_retlist = Py_BuildValue("[Oii]", py_is_up, duplex, speed); if (!py_retlist) goto error; close(sock); @@ -583,7 +576,7 @@ PsutilMethods[] = { {"users", psutil_users, METH_VARARGS, "Return currently connected users as a list of tuples"}, {"net_if_stats", psutil_net_if_stats, METH_VARARGS, - "Return NIC stats (isup, duplex, speed, mtu)"}, + "Return NIC stats (isup, duplex, speed)"}, // --- linux specific diff --git a/psutil/_psutil_posix.c b/psutil/_psutil_posix.c index 8098fbb9..54506322 100644 --- a/psutil/_psutil_posix.c +++ b/psutil/_psutil_posix.c @@ -245,6 +245,41 @@ error: /* + * Return NIC MTU. References: + * http://www.i-scream.org/libstatgrab/ + */ +static PyObject * +psutil_net_if_mtu(PyObject *self, PyObject *args) { + char *nic_name; + int sock = 0; + int ret; + int mtu; + struct ifreq ifr; + + if (! PyArg_ParseTuple(args, "s", &nic_name)) + return NULL; + + sock = socket(AF_INET, SOCK_DGRAM, 0); + if (sock == -1) + goto error; + + strncpy(ifr.ifr_name, nic_name, sizeof(ifr.ifr_name)); + ret = ioctl(sock, SIOCGIFMTU, &ifr); + if (ret == -1) + goto error; + mtu = ifr.ifr_mtu; + + return Py_BuildValue("i", mtu); + +error: + if (sock != 0) + close(sock); + PyErr_SetFromErrno(PyExc_OSError); + return NULL; +} + + +/* * net_if_stats() implementation. This is here because it is common * to both OSX and FreeBSD and I didn't know where else to put it. */ @@ -402,7 +437,6 @@ psutil_net_if_stats(PyObject *self, PyObject *args) { int ret; int duplex; int speed; - int mtu; struct ifreq ifr; struct ifmediareq ifmed; @@ -426,12 +460,6 @@ psutil_net_if_stats(PyObject *self, PyObject *args) { py_is_up = Py_False; Py_INCREF(py_is_up); - // MTU - ret = ioctl(sock, SIOCGIFMTU, &ifr); - if (ret == -1) - goto error; - mtu = ifr.ifr_mtu; - // speed / duplex memset(&ifmed, 0, sizeof(struct ifmediareq)); strlcpy(ifmed.ifm_name, nic_name, sizeof(ifmed.ifm_name)); @@ -453,7 +481,7 @@ psutil_net_if_stats(PyObject *self, PyObject *args) { close(sock); Py_DECREF(py_is_up); - return Py_BuildValue("[Oiii]", py_is_up, duplex, speed, mtu); + return Py_BuildValue("[Oii]", py_is_up, duplex, speed); error: Py_XDECREF(py_is_up); @@ -476,6 +504,8 @@ PsutilMethods[] = { "Set process priority"}, {"net_if_addrs", psutil_net_if_addrs, METH_VARARGS, "Retrieve NICs information"}, + {"net_if_mtu", psutil_net_if_mtu, METH_VARARGS, + "Retrieve NIC MTU"}, #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__) {"net_if_stats", psutil_net_if_stats, METH_VARARGS, "Return NIC stats."}, |