[Toybox] [PATCH] acpi: change when to recurse

Isaac Dunham ibid.ag at gmail.com
Sat Oct 4 13:52:16 PDT 2014


acpi: never follow symlinks, always recurse in dirs.

/sys/class contains several directories, each containing symlinks to
devices of a given type.
The symlinks are where we want to end recursion.
This makes for a simpler, cleaner, and more correct check, avoids needless
recursion, and shrinks the text segment by 25 bytes (at least here).

--
I noticed that this approach was possible while poking around thinking
about a possible Busybox port, plotting to add -c and -t, and experimenting
with using nftw.

acpi -c seems to be fairly simple:
  recurse through /sys/class/thermal,
  read type, max_state, and cur_state,
  if max_state and cur_state were unreadable,
    printf("Cooling %d: %s no state information available\n", ctr++, type)
  else printf("Cooling %d: %s %d of %d\n",ctr++,type,cur_state, max_state)

It would be rather difficult to do this in the same callback as -ab.

Copying acpi -t is simple if you omit the state:
  recurse through /sys/class (there are temperature sensors in batteries...)
  read temp
  if ((temp > 1000) && (temp/100*100 == temp))
    temp = temp/100; 
  printf("Thermal %d: %.1f degrees C\n", ctr++, ((float)temp/10))

But finding state is the tricky part.
My guess (based on the files acpi looks at) is that it's something like
compare to trip_point_*_{type,temp}, then if it's less than the
lowest trip point, state = "ok"
(otherwise, I would *guess* it's state=trip_point_%d_type).
The resulting output seems to be
"Thermal %d: %s, %.1f degrees C\n", ctr++, state, ((float)temp/10)

Of course, the documentation says nothing about the output, and 
I haven't seen any state other than "ok"...
I'm inclined to just ignore state.

The above method does ignore quite a few sensors;
there's no obvious way to get HDD temps besides these commands:
 smartctl -A -d ata /dev/sda |grep Temperature
# or (Hitachi-only, but no spin-up)
 hdparm -H /dev/sda

Then also Thinkpads have multiple sensors, which are apparently named 
(in sysfs) temp*_input
The "official" acpi misses both the Thinkpad sensors and HDD sensors.

Thanks,
Isaac Dunham

-------------- next part --------------
diff --git a/toys/other/acpi.c b/toys/other/acpi.c
index 1b31f31..ae6cd89 100644
--- a/toys/other/acpi.c
+++ b/toys/other/acpi.c
@@ -46,8 +46,8 @@ int acpi_callback(struct dirtree *tree)
 
   if (tree->name[0]=='.') return 0;
 
-  if (strlen(dirtree_path(tree, NULL)) < 26)
-    return DIRTREE_RECURSE | DIRTREE_SYMFOLLOW;
+  if (S_IFDIR&tree->st.st_mode)
+    return DIRTREE_RECURSE;
 
   if (0 <= (dfd = open(dirtree_path(tree, NULL), O_RDONLY))) {
     if ((fd = openat(dfd, "type", O_RDONLY)) < 0) goto done;


More information about the Toybox mailing list