1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
|
/* $OpenBSD: sensorsd.c,v 1.61 2017/03/20 15:31:23 bluhm Exp $ */
/*
* Copyright (c) 2003 Henning Brauer <henning@openbsd.org>
* Copyright (c) 2005 Matthew Gream <matthew.gream@pobox.com>
* Copyright (c) 2006 Constantine A. Murenin <cnst+openbsd@bugmail.mojo.ru>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/queue.h>
#include <sys/time.h>
#include <sys/sensors.h>
#include <err.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include <limits.h>
#define RFBUFSIZ 28 /* buffer size for print_sensor */
#define RFBUFCNT 4 /* ring buffers */
#define CHECK_PERIOD 20 /* check every n seconds */
enum sensorsd_s_status {
SENSORSD_S_UNSPEC, /* status is unspecified */
SENSORSD_S_INVALID, /* status is invalid, per SENSOR_FINVALID */
SENSORSD_S_WITHIN, /* status is within limits */
SENSORSD_S_ABOVE, /* status is above the higher limit */
SENSORSD_S_BELOW /* status is below the lower limit */
};
struct limits_t {
TAILQ_ENTRY(limits_t) entries;
enum sensor_type type; /* sensor type */
int numt; /* sensor number */
int64_t last_val;
int64_t lower; /* lower limit */
int64_t upper; /* upper limit */
char *command; /* failure command */
time_t astatus_changed;
time_t ustatus_changed;
enum sensor_status astatus; /* last automatic status */
enum sensor_status astatus2;
enum sensorsd_s_status ustatus; /* last user-limit status */
enum sensorsd_s_status ustatus2;
int acount; /* stat change counter */
int ucount; /* stat change counter */
u_int8_t flags; /* sensorsd limit flags */
#define SENSORSD_L_USERLIMIT 0x0001 /* user specified limit */
#define SENSORSD_L_ISTATUS 0x0002 /* ignore automatic status */
};
struct sdlim_t {
TAILQ_ENTRY(sdlim_t) entries;
char dxname[16]; /* device unix name */
int dev; /* device number */
int sensor_cnt;
TAILQ_HEAD(, limits_t) limits;
};
void usage(void);
void create(void);
struct sdlim_t *create_sdlim(struct sensordev *);
void destroy_sdlim(struct sdlim_t *);
void check(time_t);
void check_sdlim(struct sdlim_t *, time_t);
void execute(char *);
void report(time_t);
void report_sdlim(struct sdlim_t *, time_t);
static char *print_sensor(enum sensor_type, int64_t);
void parse_config(char *);
void parse_config_sdlim(struct sdlim_t *, char *);
int64_t get_val(char *, int, enum sensor_type);
void reparse_cfg(int);
TAILQ_HEAD(sdlimhead_t, sdlim_t);
struct sdlimhead_t sdlims = TAILQ_HEAD_INITIALIZER(sdlims);
char *configfile;
volatile sig_atomic_t reload = 0;
int debug = 0;
void
usage(void)
{
extern char *__progname;
fprintf(stderr, "usage: %s [-d] [-c check] [-f file]\n",
__progname);
exit(1);
}
int
main(int argc, char *argv[])
{
time_t last_report = 0, this_check;
int ch, check_period = CHECK_PERIOD;
const char *errstr;
if (pledge("stdio rpath proc exec", NULL) == -1)
err(1, "pledge");
while ((ch = getopt(argc, argv, "c:df:")) != -1) {
switch (ch) {
case 'c':
check_period = strtonum(optarg, 1, 600, &errstr);
if (errstr)
errx(1, "check %s", errstr);
break;
case 'd':
debug = 1;
break;
case 'f':
configfile = optarg;
if (access(configfile, R_OK) != 0)
err(1, "access configuration file %s",
configfile);
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc > 0)
usage();
openlog("sensorsd", LOG_PID | LOG_NDELAY, LOG_DAEMON);
create();
if (configfile == NULL)
if (asprintf(&configfile, "/etc/sensorsd.conf") == -1)
err(1, "out of memory");
parse_config(configfile);
if (debug == 0 && daemon(0, 0) == -1)
err(1, "unable to fork");
signal(SIGHUP, reparse_cfg);
signal(SIGCHLD, SIG_IGN);
for (;;) {
if (reload) {
parse_config(configfile);
syslog(LOG_INFO, "configuration reloaded");
reload = 0;
}
this_check = time(NULL);
if (!(last_report < this_check))
this_check = last_report + 1;
check(this_check);
report(last_report);
last_report = this_check;
sleep(check_period);
}
}
void
create(void)
{
struct sensordev sensordev;
struct sdlim_t *sdlim;
size_t sdlen = sizeof(sensordev);
int mib[3], dev, sensor_cnt = 0;
mib[0] = CTL_HW;
mib[1] = HW_SENSORS;
for (dev = 0; ; dev++) {
mib[2] = dev;
if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
if (errno == ENXIO)
continue;
if (errno == ENOENT)
break;
warn("sysctl");
}
sdlim = create_sdlim(&sensordev);
TAILQ_INSERT_TAIL(&sdlims, sdlim, entries);
sensor_cnt += sdlim->sensor_cnt;
}
syslog(LOG_INFO, "startup, system has %d sensors", sensor_cnt);
}
struct sdlim_t *
create_sdlim(struct sensordev *snsrdev)
{
struct sensor sensor;
struct sdlim_t *sdlim;
struct limits_t *limit;
size_t slen = sizeof(sensor);
int mib[5], numt;
enum sensor_type type;
if ((sdlim = calloc(1, sizeof(struct sdlim_t))) == NULL)
err(1, "calloc");
strlcpy(sdlim->dxname, snsrdev->xname, sizeof(sdlim->dxname));
mib[0] = CTL_HW;
mib[1] = HW_SENSORS;
mib[2] = sdlim->dev = snsrdev->num;
TAILQ_INIT(&sdlim->limits);
for (type = 0; type < SENSOR_MAX_TYPES; type++) {
mib[3] = type;
for (numt = 0; numt < snsrdev->maxnumt[type]; numt++) {
mib[4] = numt;
if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
if (errno != ENOENT)
warn("sysctl");
continue;
}
if ((limit = calloc(1, sizeof(struct limits_t))) ==
NULL)
err(1, "calloc");
limit->type = type;
limit->numt = numt;
TAILQ_INSERT_TAIL(&sdlim->limits, limit, entries);
sdlim->sensor_cnt++;
}
}
return (sdlim);
}
void
destroy_sdlim(struct sdlim_t *sdlim)
{
struct limits_t *limit;
while ((limit = TAILQ_FIRST(&sdlim->limits)) != NULL) {
TAILQ_REMOVE(&sdlim->limits, limit, entries);
free(limit->command);
free(limit);
}
free(sdlim);
}
void
check(time_t this_check)
{
struct sensordev sensordev;
struct sdlim_t *sdlim, *next;
int mib[3];
int h, t, i;
size_t sdlen = sizeof(sensordev);
if (TAILQ_EMPTY(&sdlims)) {
h = 0;
t = -1;
} else {
h = TAILQ_FIRST(&sdlims)->dev;
t = TAILQ_LAST(&sdlims, sdlimhead_t)->dev;
}
sdlim = TAILQ_FIRST(&sdlims);
mib[0] = CTL_HW;
mib[1] = HW_SENSORS;
/* look ahead for 4 more sensordevs */
for (i = h; i <= t + 4; i++) {
if (sdlim != NULL && i > sdlim->dev)
sdlim = TAILQ_NEXT(sdlim, entries);
if (sdlim == NULL && i <= t)
syslog(LOG_ALERT, "inconsistent sdlim logic");
mib[2] = i;
if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
if (errno != ENOENT)
warn("sysctl");
if (sdlim != NULL && i == sdlim->dev) {
next = TAILQ_NEXT(sdlim, entries);
TAILQ_REMOVE(&sdlims, sdlim, entries);
syslog(LOG_INFO, "%s has disappeared",
sdlim->dxname);
destroy_sdlim(sdlim);
sdlim = next;
}
continue;
}
if (sdlim != NULL && i == sdlim->dev) {
if (strcmp(sdlim->dxname, sensordev.xname) == 0) {
check_sdlim(sdlim, this_check);
continue;
} else {
next = TAILQ_NEXT(sdlim, entries);
TAILQ_REMOVE(&sdlims, sdlim, entries);
syslog(LOG_INFO, "%s has been replaced",
sdlim->dxname);
destroy_sdlim(sdlim);
sdlim = next;
}
}
next = create_sdlim(&sensordev);
/* inserting next before sdlim */
if (sdlim != NULL)
TAILQ_INSERT_BEFORE(sdlim, next, entries);
else
TAILQ_INSERT_TAIL(&sdlims, next, entries);
syslog(LOG_INFO, "%s has appeared", next->dxname);
sdlim = next;
parse_config_sdlim(sdlim, configfile);
check_sdlim(sdlim, this_check);
}
if (TAILQ_EMPTY(&sdlims))
return;
/* Ensure that our queue is consistent. */
for (sdlim = TAILQ_FIRST(&sdlims);
(next = TAILQ_NEXT(sdlim, entries)) != NULL;
sdlim = next)
if (sdlim->dev > next->dev)
syslog(LOG_ALERT, "inconsistent sdlims queue");
}
void
check_sdlim(struct sdlim_t *sdlim, time_t this_check)
{
struct sensor sensor;
struct limits_t *limit;
size_t len;
int mib[5];
mib[0] = CTL_HW;
mib[1] = HW_SENSORS;
mib[2] = sdlim->dev;
len = sizeof(sensor);
TAILQ_FOREACH(limit, &sdlim->limits, entries) {
if ((limit->flags & SENSORSD_L_ISTATUS) &&
!(limit->flags & SENSORSD_L_USERLIMIT))
continue;
mib[3] = limit->type;
mib[4] = limit->numt;
if (sysctl(mib, 5, &sensor, &len, NULL, 0) == -1)
err(1, "sysctl");
if (!(limit->flags & SENSORSD_L_ISTATUS)) {
enum sensor_status newastatus = sensor.status;
if (limit->astatus != newastatus) {
if (limit->astatus2 != newastatus) {
limit->astatus2 = newastatus;
limit->acount = 0;
} else if (++limit->acount >= 3) {
limit->last_val = sensor.value;
limit->astatus2 =
limit->astatus = newastatus;
limit->astatus_changed = this_check;
}
}
}
if (limit->flags & SENSORSD_L_USERLIMIT) {
enum sensorsd_s_status newustatus;
if (sensor.flags & SENSOR_FINVALID)
newustatus = SENSORSD_S_INVALID;
else if (sensor.value > limit->upper)
newustatus = SENSORSD_S_ABOVE;
else if (sensor.value < limit->lower)
newustatus = SENSORSD_S_BELOW;
else
newustatus = SENSORSD_S_WITHIN;
if (limit->ustatus != newustatus) {
if (limit->ustatus2 != newustatus) {
limit->ustatus2 = newustatus;
limit->ucount = 0;
} else if (++limit->ucount >= 3) {
limit->last_val = sensor.value;
limit->ustatus2 =
limit->ustatus = newustatus;
limit->ustatus_changed = this_check;
}
}
}
}
}
void
execute(char *command)
{
char *argp[] = {"sh", "-c", command, NULL};
switch (fork()) {
case -1:
syslog(LOG_CRIT, "execute: fork() failed");
break;
case 0:
execv("/bin/sh", argp);
_exit(1);
/* NOTREACHED */
default:
break;
}
}
void
report(time_t last_report)
{
struct sdlim_t *sdlim;
TAILQ_FOREACH(sdlim, &sdlims, entries)
report_sdlim(sdlim, last_report);
}
void
report_sdlim(struct sdlim_t *sdlim, time_t last_report)
{
struct limits_t *limit;
TAILQ_FOREACH(limit, &sdlim->limits, entries) {
if ((limit->astatus_changed <= last_report) &&
(limit->ustatus_changed <= last_report))
continue;
if (limit->astatus_changed > last_report) {
const char *as = NULL;
switch (limit->astatus) {
case SENSOR_S_UNSPEC:
as = "";
break;
case SENSOR_S_OK:
as = ", OK";
break;
case SENSOR_S_WARN:
as = ", WARN";
break;
case SENSOR_S_CRIT:
as = ", CRITICAL";
break;
case SENSOR_S_UNKNOWN:
as = ", UNKNOWN";
break;
}
syslog(limit->astatus == SENSOR_S_OK ? LOG_INFO :
LOG_ALERT, "%s.%s%d: %s%s",
sdlim->dxname, sensor_type_s[limit->type],
limit->numt,
print_sensor(limit->type, limit->last_val), as);
}
if (limit->ustatus_changed > last_report) {
char us[BUFSIZ];
switch (limit->ustatus) {
case SENSORSD_S_UNSPEC:
snprintf(us, sizeof(us),
"ustatus uninitialised");
break;
case SENSORSD_S_INVALID:
snprintf(us, sizeof(us), "marked invalid");
break;
case SENSORSD_S_WITHIN:
snprintf(us, sizeof(us),
"within limits: %s",
print_sensor(limit->type, limit->last_val));
break;
case SENSORSD_S_ABOVE:
snprintf(us, sizeof(us),
"exceeds limits: %s is above %s",
print_sensor(limit->type, limit->last_val),
print_sensor(limit->type, limit->upper));
break;
case SENSORSD_S_BELOW:
snprintf(us, sizeof(us),
"exceeds limits: %s is below %s",
print_sensor(limit->type, limit->last_val),
print_sensor(limit->type, limit->lower));
break;
}
syslog(limit->ustatus == SENSORSD_S_WITHIN ? LOG_INFO :
LOG_ALERT, "%s.%s%d: %s",
sdlim->dxname, sensor_type_s[limit->type],
limit->numt, us);
}
if (limit->command) {
int i = 0, n = 0, r;
char *cmd = limit->command;
char buf[BUFSIZ];
int len = sizeof(buf);
buf[0] = '\0';
for (i = n = 0; n < len; ++i) {
if (cmd[i] == '\0') {
buf[n++] = '\0';
break;
}
if (cmd[i] != '%') {
buf[n++] = limit->command[i];
continue;
}
i++;
if (cmd[i] == '\0') {
buf[n++] = '\0';
break;
}
switch (cmd[i]) {
case 'x':
r = snprintf(&buf[n], len - n, "%s",
sdlim->dxname);
break;
case 't':
r = snprintf(&buf[n], len - n, "%s",
sensor_type_s[limit->type]);
break;
case 'n':
r = snprintf(&buf[n], len - n, "%d",
limit->numt);
break;
case 'l':
{
char *s = "";
switch (limit->ustatus) {
case SENSORSD_S_UNSPEC:
s = "uninitialised";
break;
case SENSORSD_S_INVALID:
s = "invalid";
break;
case SENSORSD_S_WITHIN:
s = "within";
break;
case SENSORSD_S_ABOVE:
s = "above";
break;
case SENSORSD_S_BELOW:
s = "below";
break;
}
r = snprintf(&buf[n], len - n, "%s",
s);
break;
}
case 's':
{
char *s;
switch (limit->astatus) {
case SENSOR_S_UNSPEC:
s = "UNSPEC";
break;
case SENSOR_S_OK:
s = "OK";
break;
case SENSOR_S_WARN:
s = "WARNING";
break;
case SENSOR_S_CRIT:
s = "CRITICAL";
break;
default:
s = "UNKNOWN";
}
r = snprintf(&buf[n], len - n, "%s",
s);
break;
}
case '2':
r = snprintf(&buf[n], len - n, "%s",
print_sensor(limit->type,
limit->last_val));
break;
case '3':
r = snprintf(&buf[n], len - n, "%s",
print_sensor(limit->type,
limit->lower));
break;
case '4':
r = snprintf(&buf[n], len - n, "%s",
print_sensor(limit->type,
limit->upper));
break;
default:
r = snprintf(&buf[n], len - n, "%%%c",
cmd[i]);
break;
}
if (r < 0 || (r >= len - n)) {
syslog(LOG_CRIT, "could not parse "
"command");
return;
}
if (r > 0)
n += r;
}
if (buf[0])
execute(buf);
}
}
}
const char *drvstat[] = {
NULL, "empty", "ready", "powerup", "online", "idle", "active",
"rebuild", "powerdown", "fail", "pfail"
};
static char *
print_sensor(enum sensor_type type, int64_t value)
{
static char rfbuf[RFBUFCNT][RFBUFSIZ]; /* ring buffer */
static int idx;
char *fbuf;
fbuf = rfbuf[idx++];
if (idx == RFBUFCNT)
idx = 0;
switch (type) {
case SENSOR_TEMP:
snprintf(fbuf, RFBUFSIZ, "%.2f degC",
(value - 273150000) / 1000000.0);
break;
case SENSOR_FANRPM:
snprintf(fbuf, RFBUFSIZ, "%lld RPM", value);
break;
case SENSOR_VOLTS_DC:
snprintf(fbuf, RFBUFSIZ, "%.2f V DC", value / 1000000.0);
break;
case SENSOR_VOLTS_AC:
snprintf(fbuf, RFBUFSIZ, "%.2f V AC", value / 1000000.0);
break;
case SENSOR_WATTS:
snprintf(fbuf, RFBUFSIZ, "%.2f W", value / 1000000.0);
break;
case SENSOR_AMPS:
snprintf(fbuf, RFBUFSIZ, "%.2f A", value / 1000000.0);
break;
case SENSOR_WATTHOUR:
snprintf(fbuf, RFBUFSIZ, "%.2f Wh", value / 1000000.0);
break;
case SENSOR_AMPHOUR:
snprintf(fbuf, RFBUFSIZ, "%.2f Ah", value / 1000000.0);
break;
case SENSOR_INDICATOR:
snprintf(fbuf, RFBUFSIZ, "%s", value? "On" : "Off");
break;
case SENSOR_INTEGER:
snprintf(fbuf, RFBUFSIZ, "%lld", value);
break;
case SENSOR_PERCENT:
snprintf(fbuf, RFBUFSIZ, "%.2f%%", value / 1000.0);
break;
case SENSOR_LUX:
snprintf(fbuf, RFBUFSIZ, "%.2f lx", value / 1000000.0);
break;
case SENSOR_DRIVE:
if (0 < value && value < sizeof(drvstat)/sizeof(drvstat[0]))
snprintf(fbuf, RFBUFSIZ, "%s", drvstat[value]);
else
snprintf(fbuf, RFBUFSIZ, "%lld ???", value);
break;
case SENSOR_TIMEDELTA:
snprintf(fbuf, RFBUFSIZ, "%.6f secs", value / 1000000000.0);
break;
case SENSOR_HUMIDITY:
snprintf(fbuf, RFBUFSIZ, "%.2f%%", value / 1000.0);
break;
case SENSOR_FREQ:
snprintf(fbuf, RFBUFSIZ, "%.2f Hz", value / 1000000.0);
break;
case SENSOR_ANGLE:
snprintf(fbuf, RFBUFSIZ, "%lld", value);
break;
case SENSOR_DISTANCE:
snprintf(fbuf, RFBUFSIZ, "%.2f mm", value / 1000.0);
break;
case SENSOR_PRESSURE:
snprintf(fbuf, RFBUFSIZ, "%.2f Pa", value / 1000.0);
break;
case SENSOR_ACCEL:
snprintf(fbuf, RFBUFSIZ, "%2.4f m/s^2", value / 1000000.0);
break;
default:
snprintf(fbuf, RFBUFSIZ, "%lld ???", value);
}
return (fbuf);
}
void
parse_config(char *cf)
{
struct sdlim_t *sdlim;
TAILQ_FOREACH(sdlim, &sdlims, entries)
parse_config_sdlim(sdlim, cf);
}
void
parse_config_sdlim(struct sdlim_t *sdlim, char *cf)
{
struct limits_t *p;
char *buf = NULL, *ebuf = NULL;
char node[48];
char *cfa[2];
cfa[0] = cf;
cfa[1] = NULL;
TAILQ_FOREACH(p, &sdlim->limits, entries) {
snprintf(node, sizeof(node), "hw.sensors.%s.%s%d",
sdlim->dxname, sensor_type_s[p->type], p->numt);
p->flags = 0;
if (cgetent(&buf, cfa, node) != 0)
if (cgetent(&buf, cfa, sensor_type_s[p->type]) != 0)
continue;
if (cgetcap(buf, "istatus", ':'))
p->flags |= SENSORSD_L_ISTATUS;
if (cgetstr(buf, "low", &ebuf) < 0)
ebuf = NULL;
p->lower = get_val(ebuf, 0, p->type);
if (cgetstr(buf, "high", &ebuf) < 0)
ebuf = NULL;
p->upper = get_val(ebuf, 1, p->type);
if (cgetstr(buf, "command", &ebuf) < 0)
ebuf = NULL;
if (ebuf != NULL) {
p->command = ebuf;
ebuf = NULL;
}
free(buf);
buf = NULL;
if (p->lower != LLONG_MIN || p->upper != LLONG_MAX)
p->flags |= SENSORSD_L_USERLIMIT;
}
}
int64_t
get_val(char *buf, int upper, enum sensor_type type)
{
double val;
int64_t rval = 0;
char *p;
if (buf == NULL) {
if (upper)
return (LLONG_MAX);
else
return (LLONG_MIN);
}
val = strtod(buf, &p);
if (buf == p)
err(1, "incorrect value: %s", buf);
switch (type) {
case SENSOR_TEMP:
switch (*p) {
case 'C':
printf("C");
rval = val * 1000 * 1000 + 273150000;
break;
case 'F':
printf("F");
rval = (val * 1000 * 1000 + 459670000) / 9 * 5;
break;
default:
errx(1, "unknown unit %s for temp sensor", p);
}
break;
case SENSOR_FANRPM:
rval = val;
break;
case SENSOR_VOLTS_DC:
case SENSOR_VOLTS_AC:
if (*p != 'V')
errx(1, "unknown unit %s for voltage sensor", p);
rval = val * 1000 * 1000;
break;
case SENSOR_PERCENT:
rval = val * 1000.0;
break;
case SENSOR_INDICATOR:
case SENSOR_INTEGER:
case SENSOR_DRIVE:
case SENSOR_ANGLE:
rval = val;
break;
case SENSOR_WATTS:
case SENSOR_AMPS:
case SENSOR_WATTHOUR:
case SENSOR_AMPHOUR:
case SENSOR_LUX:
case SENSOR_FREQ:
case SENSOR_ACCEL:
rval = val * 1000 * 1000;
break;
case SENSOR_TIMEDELTA:
rval = val * 1000 * 1000 * 1000;
break;
case SENSOR_HUMIDITY:
case SENSOR_DISTANCE:
case SENSOR_PRESSURE:
rval = val * 1000.0;
break;
default:
errx(1, "unsupported sensor type");
/* not reached */
}
free(buf);
return (rval);
}
/* ARGSUSED */
void
reparse_cfg(int signo)
{
reload = 1;
}
|