envstat.c revision 1.50 1 /* $NetBSD: envstat.c,v 1.50 2007/09/10 17:39:14 xtraeme Exp $ */
2
3 /*-
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Juan Romero Pardines.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Juan Romero Pardines
21 * for the NetBSD Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * TODO:
41 *
42 * o Some checks should be added to ensure that the user does not
43 * set unwanted values for the critical limits.
44 */
45
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <stdbool.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <fcntl.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <prop/proplib.h>
55 #include <sys/envsys.h>
56
57 #define _PATH_DEV_SYSMON "/dev/sysmon"
58
59 #define ENVSYS_DFLAG 0x00000001 /* list registered devices */
60 #define ENVSYS_FFLAG 0x00000002 /* show temp in farenheit */
61 #define ENVSYS_LFLAG 0x00000004 /* list sensors */
62 #define ENVSYS_XFLAG 0x00000008 /* externalize dictionary */
63 #define ENVSYS_IFLAG 0x00000010 /* skips invalid sensors */
64
65 /*
66 * Operation flags for -m.
67 */
68 #define USERF_SCRITICAL 0x00000001 /* set a critical limit */
69 #define USERF_RCRITICAL 0x00000002 /* remove a critical limit */
70 #define USERF_SCRITMAX 0x00000004 /* set a critical max limit */
71 #define USERF_RCRITMAX 0x00000008 /* remove a critical max limit */
72 #define USERF_SCRITMIN 0x00000010 /* set a critical min limit */
73 #define USERF_RCRITMIN 0x00000020 /* remove a critical min limit */
74 #define USERF_SRFACT 0x00000040 /* set a new rfact */
75 #define USERF_SDESCR 0x00000080 /* set a new description */
76
77 struct envsys_sensor {
78 bool invalid;
79 bool visible;
80 bool percentage;
81 int32_t cur_value;
82 int32_t max_value;
83 int32_t min_value;
84 int32_t avg_value;
85 int32_t critcap_value;
86 int32_t critmin_value;
87 int32_t critmax_value;
88 char desc[ENVSYS_DESCLEN];
89 char type[ENVSYS_DESCLEN];
90 char drvstate[ENVSYS_DESCLEN];
91 char battstate[ENVSYS_DESCLEN];
92 };
93
94 static int interval, flags, width;
95 static char *mydevname, *sensors, *userreq;
96 static struct envsys_sensor *gesen;
97 static size_t gnelems, newsize;
98
99 static int parse_dictionary(int);
100 static int send_dictionary(int);
101 static int find_sensors(prop_array_t);
102 static void print_sensors(struct envsys_sensor *, size_t);
103 static int check_sensors(struct envsys_sensor *, char *, size_t);
104 static int usage(void);
105
106
107 int main(int argc, char **argv)
108 {
109 prop_dictionary_t dict;
110 int c, fd, rval;
111 char *buf, *endptr;
112
113 rval = flags = interval = width = 0;
114 newsize = gnelems = 0;
115 gesen = NULL;
116
117 setprogname(argv[0]);
118
119 while ((c = getopt(argc, argv, "DId:fi:lm:rs:w:x")) != -1) {
120 switch (c) {
121 case 'D': /* list registered devices */
122 flags |= ENVSYS_DFLAG;
123 break;
124 case 'I': /* Skips invalid sensors */
125 flags |= ENVSYS_IFLAG;
126 break;
127 case 'd': /* show sensors of a specific device */
128 mydevname = strdup(optarg);
129 if (mydevname == NULL)
130 err(ENOMEM, "out of memory");
131 break;
132 case 'f': /* display temperature in Farenheit */
133 flags |= ENVSYS_FFLAG;
134 break;
135 case 'i': /* wait time between intervals */
136 interval = strtoul(optarg, &endptr, 10);
137 if (*endptr != '\0')
138 errx(1, "interval must be an integer");
139 break;
140 case 'l': /* list sensors */
141 flags |= ENVSYS_LFLAG;
142 break;
143 case 'm':
144 userreq = strdup(optarg);
145 if (userreq == NULL)
146 err(ENOMEM, "out of memory");
147 break;
148 case 'r':
149 /*
150 * This flag doesn't do anything... it's only here for
151 * compatibility with the old implementation.
152 */
153 break;
154 case 's': /* only show specified sensors */
155 sensors = strdup(optarg);
156 if (sensors == NULL)
157 err(ENOMEM, "out of memory");
158 break;
159 case 'w': /* width value for the lines */
160 width = strtoul(optarg, &endptr, 10);
161 if (*endptr != '\0')
162 errx(1, "width must be an integer");
163 break;
164 case 'x': /* print the dictionary in raw format */
165 flags |= ENVSYS_XFLAG;
166 break;
167 case '?':
168 default:
169 usage();
170 /* NOTREACHED */
171 }
172 }
173
174 argc -= optind;
175 argv += optind;
176
177 if (argc > 0)
178 usage();
179
180 if ((fd = open(_PATH_DEV_SYSMON, O_RDONLY)) == -1)
181 err(EXIT_FAILURE, "open");
182
183 if (flags & ENVSYS_XFLAG) {
184 rval = prop_dictionary_recv_ioctl(fd,
185 ENVSYS_GETDICTIONARY,
186 &dict);
187 if (rval) {
188 (void)printf("%s: %s\n", getprogname(),
189 strerror(rval));
190 goto out;
191 }
192 buf = prop_dictionary_externalize(dict);
193 (void)printf("%s", buf);
194 free(buf);
195
196 } else if (userreq) {
197 if (!sensors || !mydevname) {
198 (void)fprintf(stderr, "%s: -m cannot be used "
199 "without -s and -d\n", getprogname());
200 rval = EINVAL;
201 goto out;
202 }
203 rval = send_dictionary(fd);
204 goto out;
205
206 #define MISSING_FLAG() \
207 do { \
208 if (sensors && !mydevname) { \
209 (void)fprintf(stderr, "%s: -s cannot be used " \
210 "without -d\n", getprogname()); \
211 rval = EINVAL; \
212 goto out; \
213 } \
214 } while (/* CONSTCOND */ 0)
215
216 } else if (interval) {
217 for (;;) {
218 MISSING_FLAG();
219 rval = parse_dictionary(fd);
220 if (rval)
221 goto out;
222 (void)fflush(stdout);
223 (void)sleep(interval);
224 }
225 } else {
226 MISSING_FLAG();
227 rval = parse_dictionary(fd);
228 }
229
230 out:
231 if (sensors)
232 free(sensors);
233 if (userreq)
234 free(userreq);
235 if (mydevname)
236 free(mydevname);
237 (void)close(fd);
238 return rval;
239 }
240
241 static int
242 send_dictionary(int fd)
243 {
244 prop_dictionary_t dict, udict;
245 prop_object_t obj;
246 char *buf, *target, *endptr;
247 int error, i, uflag;
248 double val;
249
250 error = uflag = val = 0;
251
252 /*
253 * part 1: kernel dictionary.
254 *
255 * This parts consists in parsing the kernel dictionary
256 * to check for unknown device or sensor and we must
257 * know what type of sensor are we trying to set
258 * a critical condition.
259 */
260 error = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
261 if (error)
262 return error;
263
264 if (mydevname) {
265 obj = prop_dictionary_get(dict, mydevname);
266 if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
267 warnx("unknown device `%s'", mydevname);
268 prop_object_release(dict);
269 return EINVAL;
270 }
271
272 if (find_sensors(obj)) {
273 prop_object_release(dict);
274 return EINVAL;
275 }
276 }
277
278 /* find the type for selected sensor */
279 for (i = 0; i < gnelems; i++)
280 if (strcmp(sensors, gesen[i].desc) == 0)
281 break;
282
283 /* we know the type of the sensor now, release kernel dict */
284 prop_object_release(dict);
285 /* we don't need the rdonly fd */
286 (void)close(fd);
287
288
289 /*
290 * part 2: userland dictionary.
291 *
292 * This parts consists in setting the values provided
293 * by the user and convert when necesssary to send
294 * them to the kernel again.
295 */
296 udict = prop_dictionary_create();
297
298 #define MKPROP(var, str) \
299 do { \
300 obj = prop_string_create_cstring_nocopy(var); \
301 if (obj == NULL || !prop_dictionary_set(udict, (str), obj)) { \
302 error = EINVAL; \
303 goto out; \
304 } \
305 } while (/* CONSTCOND */ 0)
306
307 /* create the driver-name object */
308 MKPROP(mydevname, "driver-name");
309 prop_object_release(obj);
310
311 /* create the sensor-name object */
312 MKPROP(sensors, "sensor-name");
313 prop_object_release(obj);
314
315 #undef MKPROP
316
317 /*
318 * parse the -m argument; we understand the following ways:
319 *
320 * -m critical/crit{max,min}=value
321 * -m critical/crit{max,min}=remove
322 * -m desc="BLAH"
323 * -m rfact=value
324 */
325 if (userreq) {
326 buf = strtok(userreq, "=");
327 target = strdup(buf);
328 if (target == NULL) {
329 error = ENOMEM;
330 goto out;
331 }
332
333 while (buf != NULL) {
334 /*
335 * skip current string if it's the same
336 * than target requested.
337 */
338 if (strcmp(target, buf) == 0)
339 buf = strtok(NULL, "=");
340
341 /* check what target was requested */
342 if (strcmp(target, "desc") == 0) {
343 uflag |= USERF_SDESCR;
344 obj = prop_string_create_cstring_nocopy(buf);
345 break;
346 #define SETNCHECKVAL(a, b) \
347 do { \
348 if (strcmp(buf, "remove") == 0) \
349 uflag |= (a); \
350 else { \
351 uflag |= (b); \
352 val = strtod(buf, &endptr); \
353 if (*endptr != '\0') { \
354 (void)printf("%s: invalid value\n", \
355 getprogname()); \
356 error = EINVAL; \
357 goto out; \
358 } \
359 } \
360 } while (/* CONSTCOND */ 0)
361
362 } else if (strcmp(target, "critical") == 0) {
363 SETNCHECKVAL(USERF_RCRITICAL, USERF_SCRITICAL);
364 break;
365 } else if (strcmp(target, "critmax") == 0) {
366 SETNCHECKVAL(USERF_RCRITMAX, USERF_SCRITMAX);
367 break;
368 } else if (strcmp(target, "critmin") == 0) {
369 SETNCHECKVAL(USERF_RCRITMIN, USERF_SCRITMIN);
370 break;
371 } else if (strcmp(target, "rfact") == 0) {
372 uflag |= USERF_SRFACT;
373 val = strtod(buf, &endptr);
374 if (*endptr != '\0') {
375 (void)printf("%s: invalid value\n",
376 getprogname());
377 error = EINVAL;
378 goto out;
379 }
380 break;
381 } else {
382 (void)printf("%s: invalid target\n",
383 getprogname());
384 error = EINVAL;
385 goto out;
386 }
387 }
388 free(target);
389 }
390
391 #undef SETNCHECKVAL
392
393 /* critical capacity for percentage sensors */
394 if (uflag & USERF_SCRITICAL) {
395 /* sanity check */
396 if (val < 0 || val > 100) {
397 (void)printf("%s: invalid value (0><100)\n",
398 getprogname());
399 error = EINVAL;
400 goto out;
401 }
402
403 /* ok... convert the value */
404 val = (val / 100) * gesen[i].max_value;
405 obj = prop_number_create_unsigned_integer(val);
406 }
407
408 /*
409 * conversions required to send a proper value to the kernel.
410 */
411 if ((uflag & USERF_SCRITMAX) || (uflag & USERF_SCRITMIN)) {
412 /* temperatures */
413 if (strcmp(gesen[i].type, "Temperature") == 0) {
414 /* convert from farenheit to celsius */
415 if (flags & ENVSYS_FFLAG)
416 val = (val - 32.0) * (5.0 / 9.0);
417
418 /* convert to microKelvin */
419 val = val * 1000000 + 273150000;
420 /* printf("val=%d\n", (int)val); */
421 obj = prop_number_create_unsigned_integer(val);
422 /* fans */
423 } else if (strcmp(gesen[i].type, "Fan") == 0) {
424 if (val < 0 || val > 10000) {
425 error = EINVAL;
426 goto out;
427 }
428 /* printf("val=%d\n", (int)val); */
429 obj = prop_number_create_unsigned_integer(val);
430
431 /* volts, watts, ohms, etc */
432 } else {
433 /* convert to m[V,W,Ohms] again */
434 val *= 1000000.0;
435 /* printf("val=%5.0f\n", val); */
436 obj = prop_number_create_integer(val);
437 }
438 }
439
440 #define SETPROP(str) \
441 do { \
442 if (!prop_dictionary_set(udict, (str), obj)) { \
443 error = EINVAL; \
444 goto out; \
445 } \
446 } while ( /*CONSTCOND*/ 0)
447
448 /* user wanted to set a new description */
449 if (uflag & USERF_SDESCR) {
450 SETPROP("new-description");
451
452 /* user wanted to set a new critical capacity */
453 } else if (uflag & USERF_SCRITICAL) {
454 SETPROP("critical-capacity");
455
456 } else if (uflag & USERF_RCRITICAL) {
457 obj = prop_bool_create(1);
458 SETPROP("remove-critical-cap");
459
460 /* user wanted to remove a critical min limit */
461 } else if (uflag & USERF_RCRITMIN) {
462 obj = prop_bool_create(1);
463 SETPROP("remove-cmin-limit");
464
465 /* user wanted to remove a critical max limit */
466 } else if (uflag & USERF_RCRITMAX) {
467 obj = prop_bool_create(1);
468 SETPROP("remove-cmax-limit");
469
470 /* user wanted to set a new critical min value */
471 } else if (uflag & USERF_SCRITMIN) {
472 SETPROP("critical-min-limit");
473
474 /* user wanted to set a new critical max value */
475 } else if (uflag & USERF_SCRITMAX) {
476 SETPROP("critical-max-limit");
477
478 /* user wanted to set a new rfact */
479 } else if (uflag & USERF_SRFACT) {
480 obj = prop_number_create_integer(val);
481 SETPROP("new-rfact");
482
483 } else {
484 (void)printf("%s: unknown operation\n", getprogname());
485 error = EINVAL;
486 goto out;
487 }
488
489 #undef SETPROP
490
491 prop_object_release(obj);
492
493 #ifdef DEBUG
494 printf("%s", prop_dictionary_externalize(udict));
495 return error;
496 #endif
497
498 if ((fd = open(_PATH_DEV_SYSMON, O_RDWR)) == -1) {
499 error = errno;
500 warnx("%s", strerror(errno));
501 goto out;
502 }
503
504 /* all done? send our dictionary now */
505 error = prop_dictionary_send_ioctl(udict, fd, ENVSYS_SETDICTIONARY);
506
507 if (error)
508 (void)printf("%s: %s\n", getprogname(), strerror(error));
509 out:
510 prop_object_release(udict);
511 return error;
512 }
513
514 static int
515 parse_dictionary(int fd)
516 {
517 prop_array_t array;
518 prop_dictionary_t dict;
519 prop_object_iterator_t iter;
520 prop_object_t obj;
521 const char *dnp = NULL;
522 int rval = 0;
523
524 /* receive dictionary from kernel */
525 rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
526 if (rval)
527 return rval;
528
529 if (prop_dictionary_count(dict) == 0) {
530 warnx("no drivers registered");
531 goto out;
532 }
533
534 if (mydevname) {
535 obj = prop_dictionary_get(dict, mydevname);
536 if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
537 warnx("unknown device `%s'", mydevname);
538 rval = EINVAL;
539 goto out;
540 }
541
542 rval = find_sensors(obj);
543 if (rval)
544 goto out;
545 } else {
546 iter = prop_dictionary_iterator(dict);
547 if (iter == NULL) {
548 rval = EINVAL;
549 goto out;
550 }
551
552 /* iterate over the dictionary returned by the kernel */
553 while ((obj = prop_object_iterator_next(iter)) != NULL) {
554
555 array = prop_dictionary_get_keysym(dict, obj);
556 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
557 warnx("no sensors found");
558 rval = EINVAL;
559 goto out;
560 }
561
562 dnp = prop_dictionary_keysym_cstring_nocopy(obj);
563
564 if (flags & ENVSYS_DFLAG) {
565 (void)printf("%s\n", dnp);
566 } else {
567 rval = find_sensors(array);
568 if (rval)
569 goto out;
570 }
571 }
572
573 prop_object_iterator_release(iter);
574 }
575
576 if (userreq == NULL)
577 if ((flags & ENVSYS_LFLAG) == 0)
578 print_sensors(gesen, gnelems);
579
580 if (interval)
581 (void)printf("\n");
582
583 out:
584 if (gesen) {
585 free(gesen);
586 gesen = NULL;
587 gnelems = 0;
588 newsize = 0;
589 }
590 prop_object_release(dict);
591 return rval;
592 }
593
594 static int
595 find_sensors(prop_array_t array)
596 {
597 prop_object_iterator_t iter;
598 prop_object_t obj, obj1;
599 prop_string_t state, desc = NULL;
600 struct envsys_sensor *esen = NULL;
601 int rval = 0;
602 char *str = NULL;
603
604 newsize += prop_array_count(array) * sizeof(*gesen);
605 esen = realloc(gesen, newsize);
606 if (esen == NULL) {
607 if (gesen)
608 free(gesen);
609 gesen = NULL;
610 rval = ENOMEM;
611 goto out;
612 }
613 gesen = esen;
614
615 iter = prop_array_iterator(array);
616 if (iter == NULL) {
617 rval = EINVAL;
618 goto out;
619 }
620
621 /* iterate over the array of dictionaries */
622 while ((obj = prop_object_iterator_next(iter)) != NULL) {
623
624 gesen[gnelems].visible = false;
625
626 /* check sensor's state */
627 state = prop_dictionary_get(obj, "state");
628
629 /* mark invalid sensors */
630 if (prop_string_equals_cstring(state, "invalid"))
631 gesen[gnelems].invalid = true;
632 else
633 gesen[gnelems].invalid = false;
634
635 /* description string */
636 desc = prop_dictionary_get(obj, "description");
637 if (desc != NULL) {
638 /* copy description */
639 (void)strlcpy(gesen[gnelems].desc,
640 prop_string_cstring_nocopy(desc),
641 sizeof(gesen[gnelems].desc));
642 } else
643 continue;
644
645 /* type string */
646 obj1 = prop_dictionary_get(obj, "type");
647 /* copy type */
648 (void)strlcpy(gesen[gnelems].type,
649 prop_string_cstring_nocopy(obj1),
650 sizeof(gesen[gnelems].type));
651
652 /* get current drive state string */
653 obj1 = prop_dictionary_get(obj, "drive-state");
654 if (obj1 != NULL)
655 (void)strlcpy(gesen[gnelems].drvstate,
656 prop_string_cstring_nocopy(obj1),
657 sizeof(gesen[gnelems].drvstate));
658
659 /* get current battery state string */
660 obj1 = prop_dictionary_get(obj, "battery-state");
661 if (obj1 != NULL)
662 (void)strlcpy(gesen[gnelems].battstate,
663 prop_string_cstring_nocopy(obj1),
664 sizeof(gesen[gnelems].battstate));
665
666 /* get current value */
667 obj1 = prop_dictionary_get(obj, "cur-value");
668 gesen[gnelems].cur_value = prop_number_integer_value(obj1);
669
670 /* get max value */
671 obj1 = prop_dictionary_get(obj, "max-value");
672 if (obj1 != NULL)
673 gesen[gnelems].max_value =
674 prop_number_integer_value(obj1);
675 else
676 gesen[gnelems].max_value = 0;
677
678 /* get min value */
679 obj1 = prop_dictionary_get(obj, "min-value");
680 if (obj1 != NULL)
681 gesen[gnelems].min_value =
682 prop_number_integer_value(obj1);
683 else
684 gesen[gnelems].min_value = 0;
685
686 /* get avg value */
687 obj1 = prop_dictionary_get(obj, "avg-value");
688 if (obj1 != NULL)
689 gesen[gnelems].avg_value =
690 prop_number_integer_value(obj1);
691 else
692 gesen[gnelems].avg_value = 0;
693
694 /* get percentage flag */
695 obj1 = prop_dictionary_get(obj, "want-percentage");
696 if (obj1 != NULL)
697 gesen[gnelems].percentage = prop_bool_true(obj1);
698
699 /* get critical max value if available */
700 obj1 = prop_dictionary_get(obj, "critical-max-limit");
701 if (obj1 != NULL) {
702 gesen[gnelems].critmax_value =
703 prop_number_integer_value(obj1);
704 } else
705 gesen[gnelems].critmax_value = 0;
706
707 /* get critical min value if available */
708 obj1 = prop_dictionary_get(obj, "critical-min-limit");
709 if (obj1 != NULL) {
710 gesen[gnelems].critmin_value =
711 prop_number_integer_value(obj1);
712 } else
713 gesen[gnelems].critmin_value = 0;
714
715 /* get critical capacity value if available */
716 obj1 = prop_dictionary_get(obj, "critical-capacity");
717 if (obj1 != NULL) {
718 gesen[gnelems].critcap_value =
719 prop_number_integer_value(obj1);
720 } else
721 gesen[gnelems].critcap_value = 0;
722
723 /* pass to the next struct and increase the counter */
724 gnelems++;
725
726 /* print sensor names if -l was given */
727 if (flags & ENVSYS_LFLAG) {
728 if (width)
729 (void)printf("%*s\n", width,
730 prop_string_cstring_nocopy(desc));
731 else
732 (void)printf("%s\n",
733 prop_string_cstring_nocopy(desc));
734 }
735 }
736
737 /* free memory */
738 prop_object_iterator_release(iter);
739
740 /*
741 * if -s was specified, we need a way to mark if a sensor
742 * was found.
743 */
744 if (sensors) {
745 str = strdup(sensors);
746 if (str == NULL)
747 return ENOMEM;
748
749 rval = check_sensors(gesen, str, gnelems);
750 if (rval)
751 goto out;
752 }
753
754 out:
755 if (str)
756 free(str);
757 return rval;
758 }
759
760 static int
761 check_sensors(struct envsys_sensor *es, char *str, size_t nelems)
762 {
763 int i;
764 char *sname;
765
766 sname = strtok(str, ",");
767 while (sname != NULL) {
768 for (i = 0; i < nelems; i++) {
769 if (strcmp(sname, es[i].desc) == 0) {
770 es[i].visible = true;
771 break;
772 }
773 }
774 if (i >= nelems) {
775 if (mydevname) {
776 warnx("unknown sensor `%s' for device `%s'",
777 sname, mydevname);
778 return EINVAL;
779 } else {
780 warnx("unknown sensor `%s'", sname);
781 return EINVAL;
782 }
783 }
784 sname = strtok(NULL, ",");
785 }
786
787 /* check if all sensors were ok, and error out if not */
788 for (i = 0; i < nelems; i++) {
789 if (es[i].visible)
790 return 0;
791 }
792
793 warnx("no sensors selected to display");
794 return EINVAL;
795 }
796
797 static void
798 print_sensors(struct envsys_sensor *es, size_t nelems)
799 {
800 size_t maxlen = 0;
801 double temp = 0;
802 const char *invalid = "N/A";
803 const char *degrees = NULL;
804 int i;
805
806 /* find the longest description */
807 for (i = 0; i < nelems; i++) {
808 if (strlen(es[i].desc) > maxlen)
809 maxlen = strlen(es[i].desc);
810 }
811
812 if (width)
813 maxlen = width;
814
815 /* print the sensors */
816 for (i = 0; i < nelems; i++) {
817
818 /* skip sensors that were not marked as visible */
819 if (sensors && !es[i].visible)
820 continue;
821
822 /* Do not print invalid sensors if -I is set */
823 if ((flags & ENVSYS_IFLAG) && es[i].invalid)
824 continue;
825
826 (void)printf("%*.*s", (int)maxlen, (int)maxlen, es[i].desc);
827
828 if (es[i].invalid) {
829 (void)printf(": %10s\n", invalid);
830 continue;
831 }
832
833 if (strcmp(es[i].type, "Indicator") == 0) {
834
835 (void)printf(": %10s", es[i].cur_value ? "ON" : "OFF");
836
837 /* converts the value to degC or degF */
838 #define CONVERTTEMP(a, b, c) \
839 do { \
840 if (b) \
841 (a) = ((b) / 1000000.0) - 273.15; \
842 if (flags & ENVSYS_FFLAG) { \
843 if (b) \
844 (a) = (9.0 / 5.0) * (a) + 32.0; \
845 (c) = "degF"; \
846 } else \
847 (c) = "degC"; \
848 } while (/* CONSTCOND */ 0)
849
850
851 /* temperatures */
852 } else if (strcmp(es[i].type, "Temperature") == 0) {
853
854 CONVERTTEMP(temp, es[i].cur_value, degrees);
855 (void)printf(": %10.3f %s", temp, degrees);
856
857 if (es[i].critmax_value || es[i].critmin_value)
858 (void)printf(" ");
859
860 if (es[i].critmax_value) {
861 CONVERTTEMP(temp, es[i].critmax_value, degrees);
862 (void)printf("max: %8.3f %s ", temp, degrees);
863 }
864
865 if (es[i].critmin_value) {
866 CONVERTTEMP(temp, es[i].critmin_value, degrees);
867 (void)printf("min: %8.3f %s", temp, degrees);
868 }
869 #undef CONVERTTEMP
870
871 /* fans */
872 } else if (strcmp(es[i].type, "Fan") == 0) {
873
874 (void)printf(": %10u RPM", es[i].cur_value);
875
876 if (es[i].critmax_value || es[i].critmin_value)
877 (void)printf(" ");
878 if (es[i].critmax_value)
879 (void)printf("max: %8u RPM ",
880 es[i].critmax_value);
881 if (es[i].critmin_value)
882 (void)printf("min: %8u RPM",
883 es[i].critmin_value);
884
885 /* integers */
886 } else if (strcmp(es[i].type, "Integer") == 0) {
887
888 (void)printf(": %10d", es[i].cur_value);
889
890 /* drives */
891 } else if (strcmp(es[i].type, "Drive") == 0) {
892
893 (void)printf(": %10s", es[i].drvstate);
894
895 /* Battery state */
896 } else if (strcmp(es[i].type, "Battery state") == 0) {
897
898 (void)printf(": %10s", es[i].battstate);
899
900 /* everything else */
901 } else {
902 const char *type;
903
904 if (strcmp(es[i].type, "Voltage DC") == 0)
905 type = "V";
906 else if (strcmp(es[i].type, "Voltage AC") == 0)
907 type = "VAC";
908 else if (strcmp(es[i].type, "Ampere") == 0)
909 type = "A";
910 else if (strcmp(es[i].type, "Watts") == 0)
911 type = "W";
912 else if (strcmp(es[i].type, "Ohms") == 0)
913 type = "Ohms";
914 else if (strcmp(es[i].type, "Watt hour") == 0)
915 type = "Wh";
916 else if (strcmp(es[i].type, "Ampere hour") == 0)
917 type = "Ah";
918 else
919 type = NULL;
920
921 (void)printf(": %10.3f %s",
922 es[i].cur_value / 1000000.0, type);
923
924 if (es[i].percentage && es[i].max_value) {
925 (void)printf(" (%5.2f%%)",
926 (es[i].cur_value * 100.0) /
927 es[i].max_value);
928 }
929
930 if (es[i].critcap_value) {
931 (void)printf(" critical (%5.2f%%)",
932 (es[i].critcap_value * 100.0) /
933 es[i].max_value);
934 }
935
936 if (es[i].critmax_value || es[i].critmin_value)
937 (void)printf(" ");
938 if (es[i].critmax_value)
939 (void)printf("max: %8.3f %s ",
940 es[i].critmax_value / 1000000.0,
941 type);
942 if (es[i].critmin_value)
943 (void)printf("min: %8.3f %s",
944 es[i].critmin_value / 1000000.0,
945 type);
946
947 }
948
949 (void)printf("\n");
950 }
951 }
952
953 static int
954 usage(void)
955 {
956 (void)fprintf(stderr, "Usage: %s [-DIflrx] ", getprogname());
957 (void)fprintf(stderr, "[-m ...] [-s s1,s2 ] [-w num] ");
958 (void)fprintf(stderr, "[-i num] [-d ...]\n");
959 exit(EXIT_FAILURE);
960 /* NOTREACHED */
961 }
962