-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -328,4 +328,4 @@ I: 659 | |
|
||
N: wiggin15 | ||
W: https://github.com/wiggin15 | ||
I: 607, 610 | ||
I: 517, 607, 610 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to |
||
|
||
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"); | ||
|
@@ -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)) | ||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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; | ||
|
||
|
@@ -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); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)