Skip to content

Commit

Permalink
fix #2138 (critical): re-define ethtool_cmd_speed
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Sep 19, 2022
1 parent a3eca35 commit b88f4d8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ XXXX-XX-XX

- 2135_, [macOS]: `Process.environ()`_ may contain garbage data. Fix
out-of-bounds read around ``sysctl_procargs``. (patch by Bernhard Urban-Forster)
- 2138_, [Linux], **[critical]**: can't compile psutil on Android due to
undefined ``ethtool_cmd_speed`` symbol.
- 2142_, [POSIX]: `net_if_stats()`_ 's ``flags`` on Python 2 returned unicode
instead of str. (patch by Matthieu Darbois)

Expand Down
8 changes: 7 additions & 1 deletion psutil/_psutil_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ ioprio_set(int which, int who, int ioprio) {
return syscall(__NR_ioprio_set, which, who, ioprio);
}

// defined in linux/ethtool.h but not always available (e.g. Android)
static inline uint32_t
psutil_ethtool_cmd_speed(const struct ethtool_cmd *ecmd) {
return (ecmd->speed_hi << 16) | ecmd->speed;
}

#define IOPRIO_CLASS_SHIFT 13
#define IOPRIO_PRIO_MASK ((1UL << IOPRIO_CLASS_SHIFT) - 1)

Expand Down Expand Up @@ -441,7 +447,7 @@ psutil_net_if_duplex_speed(PyObject* self, PyObject* args) {
duplex = ethcmd.duplex;
// speed is returned from ethtool as a __u32 ranging from 0 to INT_MAX
// or SPEED_UNKNOWN (-1)
uint_speed = ethtool_cmd_speed(&ethcmd);
uint_speed = psutil_ethtool_cmd_speed(&ethcmd);
if (uint_speed == (__u32)SPEED_UNKNOWN || uint_speed > INT_MAX) {
speed = 0;
}
Expand Down

0 comments on commit b88f4d8

Please sign in to comment.