Skip to content

lib: handle sysfs extraction failures by enabling failbit exception#183

Open
sidtronics wants to merge 1 commit intofenrus75:masterfrom
sidtronics:master
Open

lib: handle sysfs extraction failures by enabling failbit exception#183
sidtronics wants to merge 1 commit intofenrus75:masterfrom
sidtronics:master

Conversation

@sidtronics
Copy link

ensure read failure is detected if file is empty or contains invalid data. Closes #179.

Copy link

@paulmenzel paulmenzel left a comment

Choose a reason for hiding this comment

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

Awesome, that you came up with a fix. Maybe elaborate in the commit message and give your battery reporting issue as an example? Also give the old and new power usage value?

On some systems, reading a sysfs attribute may fail due to underlying
kernel or firmware (BIOS/ACPI) issues. In my case, accessing the
"current_now" attribute while on battery power results in:

$ cat /sys/class/power_supply/BAT0/current_now
cat: /sys/class/power_supply/BAT0/current_now: No such device

This commit simplifies the error handling in read_sysfs() function by
enabling failbit exception, which also covers the edge case mentioned
above.

Closes fenrus75#179.

Signed-off-by: Siddhesh Dharme <siddheshdharme18@gmail.com>
@sidtronics sidtronics changed the title lib: handle stream extraction failure with fail() check lib: handle sysfs extraction failures by enabling failbit exception Jul 7, 2025
@sidtronics
Copy link
Author

Awesome, that you came up with a fix. Maybe elaborate in the commit message and give your battery reporting issue as an example? Also give the old and new power usage value?

Thanks! I’ve updated the commit message. Here are before and after screenshots:

Before:
powertop-before

After:
powertop-after

The discharge rate is now omitted because read_sysfs() fails cleanly and returns 0 when the sysfs read is invalid, instead of parsing garbage values. Since 0 < 0.0001, it's filtered out in the UI:

if (pw > 0.0001) {
char buf[32];
wprintw(win, _("The battery reports a discharge rate of %sW\n"),
fmt_prefix(pw, buf));
wprintw(win, _("The energy consumed was %sJ\n"),
fmt_prefix(joules, buf));
need_linebreak = 1;
}

I think that's preferable to showing incorrect values.

@paulmenzel
Copy link

Awesome, that you came up with a fix. Maybe elaborate in the commit message and give your battery reporting issue as an example? Also give the old and new power usage value?

Thanks! I’ve updated the commit message. Here are before and after screenshots:

[…]

Awesome. Thank you for the great commit message, and your thorough answer. Much appreciated!

Copy link

@dyfrgi dyfrgi left a comment

Choose a reason for hiding this comment

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

Would it make sense to enable exceptions for the other two failure modes, eof and bad? eof would occur if the file exists but is empty, and bad if there's something like a failure in allocation or other exception caught by the istream operations.

Otherwise, LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unusually high battery discharge rate reported by powertop

3 participants