Skip to contentSkip to conversation

e2fsck: check new sysfs interface to determine if we are on battery

PATCH In review Theodore Ts'o 2 participants 3 messages Feb 21, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 e2fsck: check new sysfs interface to determine if we are on battery

...since the old way is deprecated.

Addresses-SourceForge-Bug: #3439277

---
 e2fsck/unix.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
e2fsck/unix.c +8 -0
index c38b67a..6f97b0f 100644
@@ -252,6 +252,14 @@ static int is_on_batt(void)
 	unsigned int	acflag;
 	struct dirent*	de;
 
+	f = fopen("/sys/class/power_supply/AC/online", "r");
+	if (f) {
+		if (fscanf(f, "%d\n", &acflag) == 1) {
+			fclose(f);
+			return (!acflag);
+		}
+		fclose(f);
+	}
 	f = fopen("/proc/apm", "r");
 	if (f) {
 		if (fscanf(f, "%s %s %s %x", tmp, tmp, tmp, &acflag) != 4)
Signature · 1 line
1.7.9.107.g97f9a
#1 lore raw
Quoted 3 lines · Theodore Ts'o

...since the old way is deprecated.

Addresses-SourceForge-Bug: #3439277

Red Hat bug #644629 too

But https://bugzilla.redhat.com/show_bug.cgi?id=644629#c5
made it sound harder than this, so I was ... keeping that bug on the back burner ...

Quoted 5 lines

You need to enumerate through all power supply devices with the "type" = "Mains" attribute, and check if any of them have the "online" attribute as "1". If so you have AC power. If don't find any power supply of "type" = "Mains" then you are on AC power too. Only if you have one or more power supplies of "type" = "Mains" and all of them have "online" = "0" then you are on battery.

If your patch fixes the general case, I say go for it! :)

-Eric

Quoted 25 lines
---
 e2fsck/unix.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
e2fsck/unix.c +8 -0
index c38b67a..6f97b0f 100644
@@ -252,6 +252,14 @@ static int is_on_batt(void)
 	unsigned int	acflag;
 	struct dirent*	de;
 
+	f = fopen("/sys/class/power_supply/AC/online", "r");
+	if (f) {
+		if (fscanf(f, "%d\n", &acflag) == 1) {
+			fclose(f);
+			return (!acflag);
+		}
+		fclose(f);
+	}
 	f = fopen("/proc/apm", "r");
 	if (f) {
 		if (fscanf(f, "%s %s %s %x", tmp, tmp, tmp, &acflag) != 4)
author
#2 lore raw
Quoted 7 lines · Eric Sandeen
Quoted 7 lines

You need to enumerate through all power supply devices with the "type" = "Mains" attribute, and check if any of them have the "online" attribute as "1". If so you have AC power. If don't find any power supply of "type" = "Mains" then you are on AC power too. Only if you have one or more power supplies of "type" = "Mains" and all of them have "online" = "0" then you are on battery.

*Ugh*. Why is it that the new interfaces are always more complicated. :-(

Quoted 1 line

If your patch fixes the general case, I say go for it! :)

All's the world a Thinkpad, right? :-)

- Ted