Skip to content

Commit

Permalink
sensor: Expose max id of sensor device through hw.sensors.dev_idmax
Browse files Browse the repository at this point in the history
There could be so many sensor devices that MAXSENSORDEVICES is far
from enough, e.g. coretemp.
  • Loading branch information
Sepherosa Ziehau committed Mar 31, 2015
1 parent 27305ea commit 2dc01a0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
18 changes: 17 additions & 1 deletion sys/kern/kern_sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@

#include <sys/mplock2.h>

static TAILQ_HEAD(, ksensordev) sensordev_list =
static int sensordev_idmax;
static TAILQ_HEAD(sensordev_list, ksensordev) sensordev_list =
TAILQ_HEAD_INITIALIZER(sensordev_list);

static struct ksensordev *sensordev_get(int);
Expand Down Expand Up @@ -94,6 +95,9 @@ sensordev_install(struct ksensordev *sensdev)
TAILQ_INSERT_AFTER(&sensordev_list, after, sensdev, list);
}

/* Save max sensor device id */
sensordev_idmax = TAILQ_LAST(&sensordev_list, sensordev_list)->num + 1;

/* Install sysctl node for this sensor device */
sensordev_sysctl_install(sensdev);

Expand Down Expand Up @@ -145,10 +149,19 @@ sensor_attach(struct ksensordev *sensdev, struct ksensor *sens)
void
sensordev_deinstall(struct ksensordev *sensdev)
{
struct ksensordev *last;

SYSCTL_XLOCK();

TAILQ_REMOVE(&sensordev_list, sensdev, list);

/* Adjust max sensor device id */
last = TAILQ_LAST(&sensordev_list, sensordev_list);
if (last != NULL)
sensordev_idmax = last->num + 1;
else
sensordev_idmax = 0;

/*
* Deinstall sensor device's sysctl node; this also
* removes all attached sensors' sysctl nodes.
Expand Down Expand Up @@ -322,6 +335,9 @@ SYSCTL_NODE(_hw, OID_AUTO, sensors, CTLFLAG_RD, NULL,
SYSCTL_NODE(_hw, HW_SENSORS, _sensors, CTLFLAG_RD, sysctl_sensors_handler,
"Hardware Sensors XP MIB interface");

SYSCTL_INT(_hw_sensors, OID_AUTO, dev_idmax, CTLFLAG_RD,
&sensordev_idmax, 0, "Max sensor device id");

static void
sensordev_sysctl_install(struct ksensordev *sensdev)
{
Expand Down
13 changes: 10 additions & 3 deletions usr.bin/systat/sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,15 @@ void
fetchsensors(void)
{
enum sensor_type type;
size_t slen, sdlen;
int mib[5], dev, numt;
size_t slen, sdlen, idmax_len;
int mib[5], dev, numt, idmax;
int maxsensordevices;

maxsensordevices = MAXSENSORDEVICES;
idmax_len = sizeof(idmax);
if (sysctlbyname("hw.sensors.dev_idmax", &idmax, &idmax_len,
NULL, 0) == 0)
maxsensordevices = idmax;

mib[0] = CTL_HW;
mib[1] = HW_SENSORS;
Expand All @@ -81,7 +88,7 @@ fetchsensors(void)
wmove(wnd, row, 0);
wclrtobot(wnd);

for (dev = 0; dev < MAXSENSORDEVICES; dev++) {
for (dev = 0; dev < maxsensordevices; dev++) {
mib[2] = dev;
if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
if (errno != ENOENT)
Expand Down

0 comments on commit 2dc01a0

Please sign in to comment.