Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #517 (net_io_counters on Solaris 10) #680

Merged
merged 4 commits into from
Sep 6, 2015
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,4 @@ I: 659

N: wiggin15
W: https://github.com/wiggin15
I: 607, 610
I: 517, 607, 610
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Bug tracker at https://github.com/giampaolo/psutil/issues

- #677: [Linux] can't install psutil due to bug in setup.py.
- #610: [SunOS] fix build and tests on Solaris 10
- #517: [SunOS] fix net_io_counters on Solaris 10
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please be more specific (describe what issue is being addressed)



3.2.0 - 2015-09-02
Expand Down
80 changes: 49 additions & 31 deletions psutil/_psutil_sunos.c
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,9 @@ psutil_net_io_counters(PyObject *self, PyObject *args) {
kstat_ctl_t *kc = NULL;
kstat_t *ksp;
kstat_named_t *rbytes, *wbytes, *rpkts, *wpkts, *ierrs, *oerrs;
int ret;
int sock = -1;
struct lifreq ifr;

PyObject *py_retdict = PyDict_New();
PyObject *py_ifc_info = NULL;
Expand All @@ -685,25 +688,30 @@ psutil_net_io_counters(PyObject *self, PyObject *args) {
if (kc == NULL)
goto error;

sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
goto error;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to PyErr_SetFromErrno(PyExc_OSError) before goto error;.


ksp = kc->kc_chain;
while (ksp != NULL) {
if (ksp->ks_type != KSTAT_TYPE_NAMED)
goto next;
if (strcmp(ksp->ks_class, "net") != 0)
goto next;
/*
// XXX "lo" (localhost) interface makes kstat_data_lookup() fail
// (maybe because "ifconfig -a" says it's a virtual interface?).
if ((strcmp(ksp->ks_module, "link") != 0) &&
(strcmp(ksp->ks_module, "lo") != 0)) {
goto skip;
*/
if ((strcmp(ksp->ks_module, "link") != 0))
// skip 'lo' (localhost) because it doesn't have the statistics we need
// and it makes kstat_data_lookup() fail
if (strcmp(ksp->ks_module, "lo") == 0)
goto next;

// check if this is a network interface by sending a ioctl
strncpy(ifr.lifr_name, ksp->ks_name, sizeof(ifr.lifr_name));
ret = ioctl(sock, SIOCGLIFFLAGS, &ifr);
if (ret == -1)
goto next;

if (kstat_read(kc, ksp, NULL) == -1) {
errno = 0;
continue;
goto next;
}

rbytes = (kstat_named_t *)kstat_data_lookup(ksp, "rbytes");
Expand All @@ -720,26 +728,32 @@ psutil_net_io_counters(PyObject *self, PyObject *args) {
goto error;
}

#if defined(_INT64_TYPE)
py_ifc_info = Py_BuildValue("(KKKKkkii)",
wbytes->value.ui64,
rbytes->value.ui64,
wpkts->value.ui64,
rpkts->value.ui64,
ierrs->value.ui32,
oerrs->value.ui32,
#else
py_ifc_info = Py_BuildValue("(kkkkkkii)",
wbytes->value.ui32,
rbytes->value.ui32,
wpkts->value.ui32,
rpkts->value.ui32,
ierrs->value.ui32,
oerrs->value.ui32,
#endif
0, // dropin not supported
0 // dropout not supported
);
if (rbytes->data_type == KSTAT_DATA_UINT64)
{
py_ifc_info = Py_BuildValue("(KKKKIIii)",
wbytes->value.ui64,
rbytes->value.ui64,
wpkts->value.ui64,
rpkts->value.ui64,
ierrs->value.ui32,
oerrs->value.ui32,
0, // dropin not supported
0 // dropout not supported
);
}
else
{
py_ifc_info = Py_BuildValue("(IIIIIIii)",
wbytes->value.ui32,
rbytes->value.ui32,
wpkts->value.ui32,
rpkts->value.ui32,
ierrs->value.ui32,
oerrs->value.ui32,
0, // dropin not supported
0 // dropout not supported
);
}
if (!py_ifc_info)
goto error;
if (PyDict_SetItemString(py_retdict, ksp->ks_name, py_ifc_info))
Expand All @@ -752,13 +766,17 @@ psutil_net_io_counters(PyObject *self, PyObject *args) {
}

kstat_close(kc);
close(sock);
return py_retdict;

error:
Py_XDECREF(py_ifc_info);
Py_DECREF(py_retdict);
if (kc != NULL)
kstat_close(kc);
if (sock != -1) {
close(sock);
}
return NULL;
}

Expand Down Expand Up @@ -1138,7 +1156,7 @@ psutil_net_if_stats(PyObject* self, PyObject* args) {
kstat_t *ksp;
kstat_named_t *knp;
int ret;
int sock = 0;
int sock = -1;
int duplex;
int speed;

Expand Down Expand Up @@ -1226,7 +1244,7 @@ psutil_net_if_stats(PyObject* self, PyObject* args) {
Py_XDECREF(py_is_up);
Py_XDECREF(py_ifc_info);
Py_DECREF(py_retdict);
if (sock != 0)
if (sock != -1)
close(sock);
if (kc != NULL)
kstat_close(kc);
Expand Down