envstat.c revision 1.40 1 /* $NetBSD: envstat.c,v 1.40 2007/07/21 11:11:10 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
64 /*
65 * Operation flags for -m.
66 */
67 #define USERF_SCRITICAL 0x00000001 /* set a critical limit */
68 #define USERF_RCRITICAL 0x00000002 /* remove a critical limit */
69 #define USERF_SCRITMAX 0x00000004 /* set a critical max limit */
70 #define USERF_RCRITMAX 0x00000008 /* remove a critical max limit */
71 #define USERF_SCRITMIN 0x00000010 /* set a critical min limit */
72 #define USERF_RCRITMIN 0x00000020 /* remove a critical min limit */
73 #define USERF_SRFACT 0x00000040 /* set a new rfact */
74 #define USERF_SDESCR 0x00000080 /* set a new description */
75
76 struct envsys_sensor {
77 bool invalid;
78 bool visible;
79 bool percentage;
80 int32_t cur_value;
81 int32_t max_value;
82 int32_t min_value;
83 int32_t avg_value;
84 int32_t critcap_value;
85 int32_t critmin_value;
86 int32_t critmax_value;
87 char desc[ENVSYS_DESCLEN];
88 char type[ENVSYS_DESCLEN];
89 char drvstate[ENVSYS_DESCLEN];
90 };
91
92 static int interval, flags, width;
93 static char *mydevname, *sensors, *userreq;
94 static struct envsys_sensor *gesen;
95 static size_t gnelems, newsize;
96
97 static int parse_dictionary(int);
98 static int send_dictionary(int);
99 static int find_sensors(prop_array_t);
100 static void print_sensors(struct envsys_sensor *, size_t);
101 static int check_sensors(struct envsys_sensor *, char *, size_t);
102 static int usage(void);
103
104
105 int main(int argc, char **argv)
106 {
107 prop_dictionary_t dict;
108 int c, fd, rval;
109 char *endptr;
110
111 rval = flags = interval = width = 0;
112 newsize = gnelems = 0;
113 gesen = NULL;
114
115 setprogname(argv[0]);
116
117 while ((c = getopt(argc, argv, "Dd:fi:lm:rs:w:x")) != -1) {
118 switch (c) {
119 case 'd': /* show sensors of a specific device */
120 mydevname = strdup(optarg);
121 if (mydevname == NULL)
122 err(ENOMEM, "out of memory");
123 break;
124 case 'i': /* wait time between intervals */
125 interval = strtoul(optarg, &endptr, 10);
126 if (*endptr != '\0')
127 errx(1, "interval must be an integer");
128 break;
129 case 'D': /* list registered devices */
130 flags |= ENVSYS_DFLAG;
131 break;
132 case 'f': /* display temperature in Farenheit */
133 flags |= ENVSYS_FFLAG;
134 break;
135 case 'l': /* list sensors */
136 flags |= ENVSYS_LFLAG;
137 break;
138 case 'w': /* width value for the lines */
139 width = strtoul(optarg, &endptr, 10);
140 if (*endptr != '\0')
141 errx(1, "width must be an integer");
142 break;
143 case 'x': /* print the dictionary in raw format */
144 flags |= ENVSYS_XFLAG;
145 break;
146 case 'r':
147 /*
148 * This flag doesn't do anything... it's only here for
149 * compatibility with the old implementation.
150 */
151 break;
152 case 's': /* only show specified sensors */
153 sensors = strdup(optarg);
154 if (sensors == NULL)
155 err(ENOMEM, "out of memory");
156 break;
157 case 'm':
158 userreq = strdup(optarg);
159 if (userreq == NULL)
160 err(ENOMEM, "out of memory");
161 break;
162 case '?':
163 default:
164 usage();
165 /* NOTREACHED */
166 }
167 }
168
169 if ((fd = open(_PATH_DEV_SYSMON, O_RDONLY)) == -1)
170 err(EXIT_FAILURE, "open");
171
172 if (!interval && (flags & ENVSYS_XFLAG)) {
173 rval = prop_dictionary_recv_ioctl(fd,
174 ENVSYS_GETDICTIONARY,
175 &dict);
176 if (rval) {
177 (void)printf("%s: %s\n", getprogname(),
178 strerror(rval));
179 goto out;
180 }
181 }
182
183 if (argc == 1) {
184 rval = parse_dictionary(fd);
185
186 } else if (userreq) {
187 if (!sensors || !mydevname) {
188 (void)fprintf(stderr, "%s: -m cannot be used without "
189 "-s and -d\n", getprogname());
190 return EINVAL;
191 }
192
193 rval = send_dictionary(fd);
194 goto out;
195
196 } else if (interval) {
197 for (;;) {
198 if (sensors && !mydevname) {
199 (void)fprintf(stderr, "%s: -s cannot be used "
200 "without -d\n", getprogname());
201 rval = EINVAL;
202 goto out;
203 }
204
205 rval = parse_dictionary(fd);
206 if (rval)
207 goto out;
208 (void)fflush(stdout);
209 (void)sleep(interval);
210 }
211
212 } else if (!interval) {
213 if (flags & ENVSYS_XFLAG)
214 (void)printf("%s", prop_dictionary_externalize(dict));
215 else {
216 if (sensors && !mydevname) {
217 (void)fprintf(stderr, "%s: -s cannot be used "
218 "without -d\n", getprogname());
219 rval = EINVAL;
220 goto out;
221 }
222 rval = parse_dictionary(fd);
223 }
224
225 } else
226 usage();
227
228 out:
229 if (sensors)
230 free(sensors);
231 if (userreq)
232 free(userreq);
233 if (mydevname)
234 free(mydevname);
235 if (gesen)
236 free(gesen);
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 (mydevname) {
530 obj = prop_dictionary_get(dict, mydevname);
531 if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
532 warnx("unknown device `%s'", mydevname);
533 rval = EINVAL;
534 goto out;
535 }
536
537 rval = find_sensors(obj);
538 if (rval)
539 goto out;
540 } else {
541 iter = prop_dictionary_iterator(dict);
542 if (iter == NULL) {
543 rval = EINVAL;
544 goto out;
545 }
546
547 /* iterate over the dictionary returned by the kernel */
548 while ((obj = prop_object_iterator_next(iter)) != NULL) {
549
550 array = prop_dictionary_get_keysym(dict, obj);
551 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
552 warnx("no sensors found");
553 rval = EINVAL;
554 goto out;
555 }
556
557 dnp = prop_dictionary_keysym_cstring_nocopy(obj);
558
559 if (flags & ENVSYS_DFLAG) {
560 (void)printf("%s\n", dnp);
561 } else {
562 rval = find_sensors(array);
563 if (rval)
564 goto out;
565 }
566 }
567
568 prop_object_iterator_release(iter);
569 }
570
571 if (userreq == NULL)
572 if ((flags & ENVSYS_LFLAG) == 0)
573 print_sensors(gesen, gnelems);
574
575 if (interval)
576 (void)printf("\n");
577
578 out:
579 if (gesen) {
580 free(gesen);
581 gesen = NULL;
582 gnelems = 0;
583 newsize = 0;
584 }
585 prop_object_release(dict);
586 return rval;
587 }
588
589 static int
590 find_sensors(prop_array_t array)
591 {
592 prop_object_iterator_t iter;
593 prop_object_t obj, obj1;
594 prop_string_t state, desc = NULL;
595 struct envsys_sensor *esen = NULL;
596 int rval = 0;
597 size_t oldsize;
598 char *str = NULL;
599
600 oldsize = newsize;
601 newsize += prop_array_count(array) * sizeof(*gesen);
602 esen = realloc(gesen, newsize);
603 if (esen == NULL) {
604 if (gesen)
605 free(gesen);
606 gesen = NULL;
607 rval = ENOMEM;
608 goto out;
609 }
610 gesen = esen;
611
612 iter = prop_array_iterator(array);
613 if (iter == NULL) {
614 rval = EINVAL;
615 goto out;
616 }
617
618 /* iterate over the array of dictionaries */
619 while ((obj = prop_object_iterator_next(iter)) != NULL) {
620
621 gesen[gnelems].visible = false;
622
623 /* check sensor's state */
624 state = prop_dictionary_get(obj, "state");
625
626 /* mark invalid sensors */
627 if (prop_string_equals_cstring(state, "invalid"))
628 gesen[gnelems].invalid = true;
629 else
630 gesen[gnelems].invalid = false;
631
632 /* description string */
633 desc = prop_dictionary_get(obj, "description");
634 if (desc != NULL) {
635 /* copy description */
636 (void)strlcpy(gesen[gnelems].desc,
637 prop_string_cstring_nocopy(desc),
638 sizeof(gesen[gnelems].desc));
639 } else
640 continue;
641
642 /* type string */
643 obj1 = prop_dictionary_get(obj, "type");
644 /* copy type */
645 (void)strlcpy(gesen[gnelems].type,
646 prop_string_cstring_nocopy(obj1),
647 sizeof(gesen[gnelems].type));
648
649 /* get current drive state string */
650 obj1 = prop_dictionary_get(obj, "drive-state");
651 if (obj1 != NULL)
652 (void)strlcpy(gesen[gnelems].drvstate,
653 prop_string_cstring_nocopy(obj1),
654 sizeof(gesen[gnelems].drvstate));
655
656 /* get current value */
657 obj1 = prop_dictionary_get(obj, "cur-value");
658 gesen[gnelems].cur_value = prop_number_integer_value(obj1);
659
660 /* get max value */
661 obj1 = prop_dictionary_get(obj, "max-value");
662 if (obj1 != NULL)
663 gesen[gnelems].max_value =
664 prop_number_integer_value(obj1);
665 else
666 gesen[gnelems].max_value = 0;
667
668 /* get min value */
669 obj1 = prop_dictionary_get(obj, "min-value");
670 if (obj1 != NULL)
671 gesen[gnelems].min_value =
672 prop_number_integer_value(obj1);
673 else
674 gesen[gnelems].min_value = 0;
675
676 /* get avg value */
677 obj1 = prop_dictionary_get(obj, "avg-value");
678 if (obj1 != NULL)
679 gesen[gnelems].avg_value =
680 prop_number_integer_value(obj1);
681 else
682 gesen[gnelems].avg_value = 0;
683
684 /* get percentage flag */
685 obj1 = prop_dictionary_get(obj, "want-percentage");
686 if (obj1 != NULL)
687 gesen[gnelems].percentage = prop_bool_true(obj1);
688
689 /* get critical max value if available */
690 obj1 = prop_dictionary_get(obj, "critical-max-limit");
691 if (obj1 != NULL) {
692 gesen[gnelems].critmax_value =
693 prop_number_integer_value(obj1);
694 } else
695 gesen[gnelems].critmax_value = 0;
696
697 /* get critical min value if available */
698 obj1 = prop_dictionary_get(obj, "critical-min-limit");
699 if (obj1 != NULL) {
700 gesen[gnelems].critmin_value =
701 prop_number_integer_value(obj1);
702 } else
703 gesen[gnelems].critmin_value = 0;
704
705 /* get critical capacity value if available */
706 obj1 = prop_dictionary_get(obj, "critical-capacity");
707 if (obj1 != NULL) {
708 gesen[gnelems].critcap_value =
709 prop_number_integer_value(obj1);
710 } else
711 gesen[gnelems].critcap_value = 0;
712
713 /* pass to the next struct and increase the counter */
714 gnelems++;
715
716 /* print sensor names if -l was given */
717 if (flags & ENVSYS_LFLAG) {
718 if (width)
719 (void)printf("%*s\n", width,
720 prop_string_cstring_nocopy(desc));
721 else
722 (void)printf("%s\n",
723 prop_string_cstring_nocopy(desc));
724 }
725 }
726
727 /* free memory */
728 prop_object_iterator_release(iter);
729
730 /*
731 * if -s was specified, we need a way to mark if a sensor
732 * was found.
733 */
734 if (sensors) {
735 str = strdup(sensors);
736 if (str == NULL)
737 return ENOMEM;
738
739 rval = check_sensors(gesen, str, gnelems);
740 if (rval)
741 goto out;
742 }
743
744 out:
745 if (str)
746 free(str);
747 return rval;
748 }
749
750 static int
751 check_sensors(struct envsys_sensor *es, char *str, size_t nelems)
752 {
753 int i;
754 char *sname;
755
756 sname = strtok(str, ",");
757 while (sname != NULL) {
758 for (i = 0; i < nelems; i++) {
759 if (strcmp(sname, es[i].desc) == 0) {
760 es[i].visible = true;
761 break;
762 }
763 }
764 if (i >= nelems) {
765 if (mydevname) {
766 warnx("unknown sensor `%s' for device `%s'",
767 sname, mydevname);
768 return EINVAL;
769 } else {
770 warnx("unknown sensor `%s'", sname);
771 return EINVAL;
772 }
773 }
774 sname = strtok(NULL, ",");
775 }
776
777 /* check if all sensors were ok, and error out if not */
778 for (i = 0; i < nelems; i++) {
779 if (es[i].visible)
780 return 0;
781 }
782
783 warnx("no sensors selected to display");
784 return EINVAL;
785 }
786
787 static void
788 print_sensors(struct envsys_sensor *es, size_t nelems)
789 {
790 size_t maxlen = 0;
791 double temp = 0;
792 const char *invalid = "N/A";
793 const char *degrees = NULL;
794 int i;
795
796 /* find the longest description */
797 for (i = 0; i < nelems; i++) {
798 if (strlen(es[i].desc) > maxlen)
799 maxlen = strlen(es[i].desc);
800 }
801
802 if (width)
803 maxlen = width;
804
805 /* print the sensors */
806 for (i = 0; i < nelems; i++) {
807
808 /* skip sensors that were not marked as visible */
809 if (sensors && !es[i].visible)
810 continue;
811
812 (void)printf("%*.*s", (int)maxlen, (int)maxlen, es[i].desc);
813
814 if (es[i].invalid) {
815 (void)printf(": %10s\n", invalid);
816 continue;
817 }
818
819 if (strcmp(es[i].type, "Indicator") == 0) {
820
821 (void)printf(": %10s", es[i].cur_value ? "ON" : "OFF");
822
823 /* converts the value to degC or degF */
824 #define CONVERTTEMP(a, b, c) \
825 do { \
826 (a) = ((b) / 1000000.0) - 273.15; \
827 if (flags & ENVSYS_FFLAG) { \
828 (a) = (9.0 / 5.0) * (a) + 32.0; \
829 (c) = "degF"; \
830 } else \
831 (c) = "degC"; \
832 } while (/* CONSTCOND */ 0)
833
834
835 /* temperatures */
836 } else if (strcmp(es[i].type, "Temperature") == 0) {
837
838 CONVERTTEMP(temp, es[i].cur_value, degrees);
839 (void)printf(": %10.3f %s", temp, degrees);
840
841 if (es[i].critmax_value || es[i].critmin_value)
842 (void)printf(" ");
843
844 if (es[i].critmax_value) {
845 CONVERTTEMP(temp, es[i].critmax_value, degrees);
846 (void)printf("max: %8.3f %s ", temp, degrees);
847 }
848
849 if (es[i].critmin_value) {
850 CONVERTTEMP(temp, es[i].critmin_value, degrees);
851 (void)printf("min: %8.3f %s", temp, degrees);
852 }
853 #undef CONVERTTEMP
854
855 /* fans */
856 } else if (strcmp(es[i].type, "Fan") == 0) {
857
858 (void)printf(": %10u RPM", es[i].cur_value);
859
860 if (es[i].critmax_value || es[i].critmin_value)
861 (void)printf(" ");
862 if (es[i].critmax_value)
863 (void)printf("max: %8u RPM ",
864 es[i].critmax_value);
865 if (es[i].critmin_value)
866 (void)printf("min: %8u RPM",
867 es[i].critmin_value);
868
869 /* integers */
870 } else if (strcmp(es[i].type, "Integer") == 0) {
871
872 (void)printf(": %10d", es[i].cur_value);
873
874 /* drives */
875 } else if (strcmp(es[i].type, "Drive") == 0) {
876
877 (void)printf(": %10s", es[i].drvstate);
878
879 /* everything else */
880 } else {
881 const char *type;
882
883 if (strcmp(es[i].type, "Voltage DC") == 0)
884 type = "V";
885 else if (strcmp(es[i].type, "Voltage AC") == 0)
886 type = "VAC";
887 else if (strcmp(es[i].type, "Ampere") == 0)
888 type = "A";
889 else if (strcmp(es[i].type, "Watts") == 0)
890 type = "W";
891 else if (strcmp(es[i].type, "Ohms") == 0)
892 type = "Ohms";
893 else if (strcmp(es[i].type, "Watt hour") == 0)
894 type = "Wh";
895 else if (strcmp(es[i].type, "Ampere hour") == 0)
896 type = "Ah";
897 else
898 type = NULL;
899
900 (void)printf(": %10.3f %s",
901 es[i].cur_value / 1000000.0, type);
902
903 if (es[i].percentage && es[i].max_value) {
904 (void)printf(" (%5.2f%%)",
905 (es[i].cur_value * 100.0) /
906 es[i].max_value);
907 }
908
909 if (es[i].critcap_value) {
910 (void)printf(" critical (%5.2f%%)",
911 (es[i].critcap_value * 100.0) /
912 es[i].max_value);
913 }
914
915 if (es[i].critmax_value || es[i].critmin_value)
916 (void)printf(" ");
917 if (es[i].critmax_value)
918 (void)printf("max: %8.3f %s ",
919 es[i].critmax_value / 1000000.0,
920 type);
921 if (es[i].critmin_value)
922 (void)printf("min: %8.3f %s",
923 es[i].critmin_value / 1000000.0,
924 type);
925
926 }
927
928 (void)printf("\n");
929 }
930 }
931
932 static int
933 usage(void)
934 {
935 (void)fprintf(stderr, "Usage: %s [-Dflrx] ", getprogname());
936 (void)fprintf(stderr, "[-m ...] [-s s1,s2 ] [-w num] ");
937 (void)fprintf(stderr, "[-i num] [-d ...]\n");
938 exit(EXIT_FAILURE);
939 /* NOTREACHED */
940 }
941