HorlogeSkynet / archey4

:computer: Maintained fork of the original Archey (Linux) system tool
https://git.io/archey4
GNU General Public License v3.0
291 stars 37 forks source link

[BUGFIX] Fix crash if no read permissions for /proc/uptime, and use fallbacks instead #66

Closed ingrinder closed 4 years ago

ingrinder commented 4 years ago

On some systems without read permissions on /proc/uptime (e.g. Android), or lacking /proc/uptime (BSD) Archey will fail with a traceback. This PR fixes this bug and adds additional fallback detections to the Uptime entry to support these systems.

Description

Reworked the uptime entry somewhat to now try a few methods of detecting uptime, hopefully preventing complete failure with a traceback. The uptime is now found as follows:

Added test cases for this new behaviour, as well as getting uptime output from various systems and making sure these are all handled correctly.

How has this been tested ?

Using the test cases with various different systems' behaviour, and on my local machine as usual 😃

Types of changes :

Checklist :

(I removed the unneeded checklist items so our PR progress bar is somewhat meaningful 😃)

ingrinder commented 4 years ago

Sorry, I didn't really think then -- I only force pushed a rebase to master, feel free to force push over it if you're working on anything on this branch (I've gotten too used to working on branches on my own 😕)

HorlogeSkynet commented 4 years ago

Hey Michael, no problem I've not started to work on this one last WE. I'm now reviewing this, and I'll try to DRY it a bit.

First question though, is uptime --pretty not widely available ? Just in case, it would allow us to drastically simplify its parsing :ok_hand: Bye :wave:

ingrinder commented 4 years ago

Unfortunately no 😦 - both busybox and the BSDs accept no arguments to uptime, and throw errors if any are specified (not to mention the --arg format is invalid BSD syntax).

It may be possible to use the output of sysctl on the BSDs (free/open/macOS all seem to have the kern.boottime value) however this wouldn't help in the case of e.g. Android which is Linux-based but simply has no user read access from /proc - so I'm not sure if it's worth trying to parse sysctl if we're going to need to specify how to parse the uptime command anyway...?

Sidenote: I've noticed we also traceback in the Model entry, CPU entry and RAM entry on the BSDs due to accessing /proc files which don't exist. Perhaps we should start another branch with these fixes to add official BSD support - the distro package already gives sensible info on it and I believe these tracebacks are the only showstoppers (plus some ASCII art 😄).

HorlogeSkynet commented 4 years ago

Thanks for the precision ! I'll try to propose you my changes very soon 🙂

EDIT : Yes you're right, no need to play with sysctl if we end up parsing uptime anyhow for some cases 👌

ingrinder commented 4 years ago

I've made some changes to the tests so they're a lot less hotch-potch, with purposefully picked values on edge-cases etc. I've also added variations for the current time, and users/loadaverage sections taking us to many thousands of strings tested on the regex... Maybe it's extreme but it picked up on a problem when I added seconds support in the last commit 😃. Let me know if this is better!