diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2016-10-25 00:10:29 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2016-10-25 00:10:29 +0200 |
commit | 3a9cfd3931f9d358d3aa414d3f1bcda198435b53 (patch) | |
tree | 23e153dc3628e5f1f2f5ff0ea9044009206863c5 /psutil/_psutil_posix.c | |
parent | a0a06a0d5ea2770dc3f221b6d3cd55b43ef6505c (diff) | |
download | psutil-3a9cfd3931f9d358d3aa414d3f1bcda198435b53.tar.gz |
linux: separate IFFLAGS function
Diffstat (limited to 'psutil/_psutil_posix.c')
-rw-r--r-- | psutil/_psutil_posix.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/psutil/_psutil_posix.c b/psutil/_psutil_posix.c index 54506322..9087443f 100644 --- a/psutil/_psutil_posix.c +++ b/psutil/_psutil_posix.c @@ -267,6 +267,7 @@ psutil_net_if_mtu(PyObject *self, PyObject *args) { ret = ioctl(sock, SIOCGIFMTU, &ifr); if (ret == -1) goto error; + close(sock); mtu = ifr.ifr_mtu; return Py_BuildValue("i", mtu); @@ -280,6 +281,44 @@ error: /* + * Inspect NIC flags, returns a bool indicating whether the NIC is + * running. References: + * http://www.i-scream.org/libstatgrab/ + */ +static PyObject * +psutil_net_if_flags(PyObject *self, PyObject *args) { + char *nic_name; + int sock = 0; + int ret; + 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, SIOCGIFFLAGS, &ifr); + if (ret == -1) + goto error; + + close(sock); + if ((ifr.ifr_flags & IFF_UP) != 0) + return Py_BuildValue("O", Py_True); + else + return Py_BuildValue("O", Py_False); + +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. */ @@ -506,6 +545,8 @@ PsutilMethods[] = { "Retrieve NICs information"}, {"net_if_mtu", psutil_net_if_mtu, METH_VARARGS, "Retrieve NIC MTU"}, + {"net_if_flags", psutil_net_if_flags, METH_VARARGS, + "Retrieve NIC flags"}, #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__) {"net_if_stats", psutil_net_if_stats, METH_VARARGS, "Return NIC stats."}, |