| 1 |
/* |
| 2 |
* Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. |
| 3 |
* Use of this source code is governed by a BSD-style license that can be |
| 4 |
* found in the LICENSE file. |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <Python.h> |
| 8 |
#include <linux/sockios.h> |
| 9 |
#include <sys/socket.h> |
| 10 |
#include <sys/ioctl.h> |
| 11 |
#include <linux/if.h> |
| 12 |
#include <linux/version.h> |
| 13 |
#include <unistd.h> |
| 14 |
|
| 15 |
#include "../../arch/all/init.h" |
| 16 |
|
| 17 |
// see: https://github.com/giampaolo/psutil/issues/659 |
| 18 |
#ifdef PSUTIL_ETHTOOL_MISSING_TYPES |
| 19 |
#include <linux/types.h> |
| 20 |
typedef __u64 u64; |
| 21 |
typedef __u32 u32; |
| 22 |
typedef __u16 u16; |
| 23 |
typedef __u8 u8; |
| 24 |
#endif |
| 25 |
|
| 26 |
// Avoid redefinition of struct sysinfo with musl libc. |
| 27 |
#define _LINUX_SYSINFO_H |
| 28 |
#include <linux/ethtool.h> |
| 29 |
|
| 30 |
|
| 31 |
// * defined in linux/ethtool.h but not always available (e.g. Android) |
| 32 |
// * #ifdef check needed for old kernels, see: |
| 33 |
// https://github.com/giampaolo/psutil/issues/2164 |
| 34 |
static inline uint32_t |
| 35 |
psutil_ethtool_cmd_speed(const struct ethtool_cmd *ecmd) { |
| 36 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27) |
| 37 |
return ecmd->speed; |
| 38 |
#else |
| 39 |
return (ecmd->speed_hi << 16) | ecmd->speed; |
| 40 |
#endif |
| 41 |
} |
| 42 |
|
| 43 |
// May happen on old RedHat versions, see: |
| 44 |
// https://github.com/giampaolo/psutil/issues/607 |
| 45 |
#ifndef DUPLEX_UNKNOWN |
| 46 |
#define DUPLEX_UNKNOWN 0xff |
| 47 |
#endif |
| 48 |
// https://github.com/giampaolo/psutil/pull/2156 |
| 49 |
#ifndef SPEED_UNKNOWN |
| 50 |
#define SPEED_UNKNOWN -1 |
| 51 |
#endif |
| 52 |
|
| 53 |
|
| 54 |
// References: |
| 55 |
// * https://github.com/dpaleino/wicd/blob/master/wicd/backends/be-ioctl.py |
| 56 |
// * http://www.i-scream.org/libstatgrab/ |
| 57 |
PyObject* |
| 58 |
psutil_net_if_duplex_speed(PyObject* self, PyObject* args) { |
| 59 |
char *nic_name; |
| 60 |
int sock = 0; |
| 61 |
int ret; |
| 62 |
int duplex; |
| 63 |
__u32 uint_speed; |
| 64 |
int speed; |
| 65 |
struct ifreq ifr; |
| 66 |
struct ethtool_cmd ethcmd; |
| 67 |
PyObject *py_retlist = NULL; |
| 68 |
|
| 69 |
if (! PyArg_ParseTuple(args, "s", &nic_name)) |
| 70 |
return NULL; |
| 71 |
|
| 72 |
sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 73 |
if (sock == -1) |
| 74 |
return psutil_PyErr_SetFromOSErrnoWithSyscall("socket()"); |
| 75 |
PSUTIL_STRNCPY(ifr.ifr_name, nic_name, sizeof(ifr.ifr_name)); |
| 76 |
|
| 77 |
// duplex and speed |
| 78 |
memset(ðcmd, 0, sizeof ethcmd); |
| 79 |
ethcmd.cmd = ETHTOOL_GSET; |
| 80 |
ifr.ifr_data = (void *)ðcmd; |
| 81 |
ret = ioctl(sock, SIOCETHTOOL, &ifr); |
| 82 |
|
| 83 |
if (ret != -1) { |
| 84 |
duplex = ethcmd.duplex; |
| 85 |
// speed is returned from ethtool as a __u32 ranging from 0 to INT_MAX |
| 86 |
// or SPEED_UNKNOWN (-1) |
| 87 |
uint_speed = psutil_ethtool_cmd_speed(ðcmd); |
| 88 |
if (uint_speed == (__u32)SPEED_UNKNOWN || uint_speed > INT_MAX) { |
| 89 |
speed = 0; |
| 90 |
} |
| 91 |
else { |
| 92 |
speed = (int)uint_speed; |
| 93 |
} |
| 94 |
} |
| 95 |
else { |
| 96 |
if ((errno == EOPNOTSUPP) || (errno == EINVAL)) { |
| 97 |
// EOPNOTSUPP may occur in case of wi-fi cards. |
| 98 |
// For EINVAL see: |
| 99 |
// https://github.com/giampaolo/psutil/issues/797 |
| 100 |
// #issuecomment-202999532 |
| 101 |
duplex = DUPLEX_UNKNOWN; |
| 102 |
speed = 0; |
| 103 |
} |
| 104 |
else { |
| 105 |
psutil_PyErr_SetFromOSErrnoWithSyscall("ioctl(SIOCETHTOOL)"); |
| 106 |
goto error; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
py_retlist = Py_BuildValue("[ii]", duplex, speed); |
| 111 |
if (!py_retlist) |
| 112 |
goto error; |
| 113 |
close(sock); |
| 114 |
return py_retlist; |
| 115 |
|
| 116 |
error: |
| 117 |
if (sock != -1) |
| 118 |
close(sock); |
| 119 |
return NULL; |
| 120 |
} |