envstat.c revision 1.41 1 /* $NetBSD: envstat.c,v 1.41 2007/08/05 23:20:44 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 *buf, *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 buf = prop_dictionary_externalize(dict);
215 (void)printf("%s", buf);
216 free(buf);
217 } else {
218 if (sensors && !mydevname) {
219 (void)fprintf(stderr, "%s: -s cannot be used "
220 "without -d\n", getprogname());
221 rval = EINVAL;
222 goto out;
223 }
224 rval = parse_dictionary(fd);
225 }
226
227 } else
228 usage();
229
230 out:
231 if (sensors)
232 free(sensors);
233 if (userreq)
234 free(userreq);
235 if (mydevname)
236 free(mydevname);
237 if (gesen)
238 free(gesen);
239 (void)close(fd);
240 return rval;
241 }
242
243 static int
244 send_dictionary(int fd)
245 {
246 prop_dictionary_t dict, udict;
247 prop_object_t obj;
248 char *buf, *target, *endptr;
249 int error, i, uflag;
250 double val;
251
252 error = uflag = val = 0;
253
254 /*
255 * part 1: kernel dictionary.
256 *
257 * This parts consists in parsing the kernel dictionary
258 * to check for unknown device or sensor and we must
259 * know what type of sensor are we trying to set
260 * a critical condition.
261 */
262 error = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
263 if (error)
264 return error;
265
266 if (mydevname) {
267 obj = prop_dictionary_get(dict, mydevname);
268 if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
269 warnx("unknown device `%s'", mydevname);
270 prop_object_release(dict);
271 return EINVAL;
272 }
273
274 if (find_sensors(obj)) {
275 prop_object_release(dict);
276 return EINVAL;
277 }
278 }
279
280 /* find the type for selected sensor */
281 for (i = 0; i < gnelems; i++)
282 if (strcmp(sensors, gesen[i].desc) == 0)
283 break;
284
285 /* we know the type of the sensor now, release kernel dict */
286 prop_object_release(dict);
287 /* we don't need the rdonly fd */
288 (void)close(fd);
289
290
291 /*
292 * part 2: userland dictionary.
293 *
294 * This parts consists in setting the values provided
295 * by the user and convert when necesssary to send
296 * them to the kernel again.
297 */
298 udict = prop_dictionary_create();
299
300 #define MKPROP(var, str) \
301 do { \
302 obj = prop_string_create_cstring_nocopy(var); \
303 if (obj == NULL || !prop_dictionary_set(udict, (str), obj)) { \
304 error = EINVAL; \
305 goto out; \
306 } \
307 } while (/* CONSTCOND */ 0)
308
309 /* create the driver-name object */
310 MKPROP(mydevname, "driver-name");
311 prop_object_release(obj);
312
313 /* create the sensor-name object */
314 MKPROP(sensors, "sensor-name");
315 prop_object_release(obj);
316
317 #undef MKPROP
318
319 /*
320 * parse the -m argument; we understand the following ways:
321 *
322 * -m critical/crit{max,min}=value
323 * -m critical/crit{max,min}=remove
324 * -m desc="BLAH"
325 * -m rfact=value
326 */
327 if (userreq) {
328 buf = strtok(userreq, "=");
329 target = strdup(buf);
330 if (target == NULL) {
331 error = ENOMEM;
332 goto out;
333 }
334
335 while (buf != NULL) {
336 /*
337 * skip current string if it's the same
338 * than target requested.
339 */
340 if (strcmp(target, buf) == 0)
341 buf = strtok(NULL, "=");
342
343 /* check what target was requested */
344 if (strcmp(target, "desc") == 0) {
345 uflag |= USERF_SDESCR;
346 obj = prop_string_create_cstring_nocopy(buf);
347 break;
348 #define SETNCHECKVAL(a, b) \
349 do { \
350 if (strcmp(buf, "remove") == 0) \
351 uflag |= (a); \
352 else { \
353 uflag |= (b); \
354 val = strtod(buf, &endptr); \
355 if (*endptr != '\0') { \
356 (void)printf("%s: invalid value\n", \
357 getprogname()); \
358 error = EINVAL; \
359 goto out; \
360 } \
361 } \
362 } while (/* CONSTCOND */ 0)
363
364 } else if (strcmp(target, "critical") == 0) {
365 SETNCHECKVAL(USERF_RCRITICAL, USERF_SCRITICAL);
366 break;
367 } else if (strcmp(target, "critmax") == 0) {
368 SETNCHECKVAL(USERF_RCRITMAX, USERF_SCRITMAX);
369 break;
370 } else if (strcmp(target, "critmin") == 0) {
371 SETNCHECKVAL(USERF_RCRITMIN, USERF_SCRITMIN);
372 break;
373 } else if (strcmp(target, "rfact") == 0) {
374 uflag |= USERF_SRFACT;
375 val = strtod(buf, &endptr);
376 if (*endptr != '\0') {
377 (void)printf("%s: invalid value\n",
378 getprogname());
379 error = EINVAL;
380 goto out;
381 }
382 break;
383 } else {
384 (void)printf("%s: invalid target\n",
385 getprogname());
386 error = EINVAL;
387 goto out;
388 }
389 }
390 free(target);
391 }
392
393 #undef SETNCHECKVAL
394
395 /* critical capacity for percentage sensors */
396 if (uflag & USERF_SCRITICAL) {
397 /* sanity check */
398 if (val < 0 || val > 100) {
399 (void)printf("%s: invalid value (0><100)\n",
400 getprogname());
401 error = EINVAL;
402 goto out;
403 }
404
405 /* ok... convert the value */
406 val = (val / 100) * gesen[i].max_value;
407 obj = prop_number_create_unsigned_integer(val);
408 }
409
410 /*
411 * conversions required to send a proper value to the kernel.
412 */
413 if ((uflag & USERF_SCRITMAX) || (uflag & USERF_SCRITMIN)) {
414 /* temperatures */
415 if (strcmp(gesen[i].type, "Temperature") == 0) {
416 /* convert from farenheit to celsius */
417 if (flags & ENVSYS_FFLAG)
418 val = (val - 32.0) * (5.0 / 9.0);
419
420 /* convert to microKelvin */
421 val = val * 1000000 + 273150000;
422 /* printf("val=%d\n", (int)val); */
423 obj = prop_number_create_unsigned_integer(val);
424 /* fans */
425 } else if (strcmp(gesen[i].type, "Fan") == 0) {
426 if (val < 0 || val > 10000) {
427 error = EINVAL;
428 goto out;
429 }
430 /* printf("val=%d\n", (int)val); */
431 obj = prop_number_create_unsigned_integer(val);
432
433 /* volts, watts, ohms, etc */
434 } else {
435 /* convert to m[V,W,Ohms] again */
436 val *= 1000000.0;
437 /* printf("val=%5.0f\n", val); */
438 obj = prop_number_create_integer(val);
439 }
440 }
441
442 #define SETPROP(str) \
443 do { \
444 if (!prop_dictionary_set(udict, (str), obj)) { \
445 error = EINVAL; \
446 goto out; \
447 } \
448 } while ( /*CONSTCOND*/ 0)
449
450 /* user wanted to set a new description */
451 if (uflag & USERF_SDESCR) {
452 SETPROP("new-description");
453
454 /* user wanted to set a new critical capacity */
455 } else if (uflag & USERF_SCRITICAL) {
456 SETPROP("critical-capacity");
457
458 } else if (uflag & USERF_RCRITICAL) {
459 obj = prop_bool_create(1);
460 SETPROP("remove-critical-cap");
461
462 /* user wanted to remove a critical min limit */
463 } else if (uflag & USERF_RCRITMIN) {
464 obj = prop_bool_create(1);
465 SETPROP("remove-cmin-limit");
466
467 /* user wanted to remove a critical max limit */
468 } else if (uflag & USERF_RCRITMAX) {
469 obj = prop_bool_create(1);
470 SETPROP("remove-cmax-limit");
471
472 /* user wanted to set a new critical min value */
473 } else if (uflag & USERF_SCRITMIN) {
474 SETPROP("critical-min-limit");
475
476 /* user wanted to set a new critical max value */
477 } else if (uflag & USERF_SCRITMAX) {
478 SETPROP("critical-max-limit");
479
480 /* user wanted to set a new rfact */
481 } else if (uflag & USERF_SRFACT) {
482 obj = prop_number_create_integer(val);
483 SETPROP("new-rfact");
484
485 } else {
486 (void)printf("%s: unknown operation\n", getprogname());
487 error = EINVAL;
488 goto out;
489 }
490
491 #undef SETPROP
492
493 prop_object_release(obj);
494
495 #ifdef DEBUG
496 printf("%s", prop_dictionary_externalize(udict));
497 return error;
498 #endif
499
500 if ((fd = open(_PATH_DEV_SYSMON, O_RDWR)) == -1) {
501 error = errno;
502 warnx("%s", strerror(errno));
503 goto out;
504 }
505
506 /* all done? send our dictionary now */
507 error = prop_dictionary_send_ioctl(udict, fd, ENVSYS_SETDICTIONARY);
508
509 if (error)
510 (void)printf("%s: %s\n", getprogname(), strerror(error));
511 out:
512 prop_object_release(udict);
513 return error;
514 }
515
516 static int
517 parse_dictionary(int fd)
518 {
519 prop_array_t array;
520 prop_dictionary_t dict;
521 prop_object_iterator_t iter;
522 prop_object_t obj;
523 const char *dnp = NULL;
524 int rval = 0;
525
526 /* receive dictionary from kernel */
527 rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
528 if (rval)
529 return rval;
530
531 if (mydevname) {
532 obj = prop_dictionary_get(dict, mydevname);
533 if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
534 warnx("unknown device `%s'", mydevname);
535 rval = EINVAL;
536 goto out;
537 }
538
539 rval = find_sensors(obj);
540 if (rval)
541 goto out;
542 } else {
543 iter = prop_dictionary_iterator(dict);
544 if (iter == NULL) {
545 rval = EINVAL;
546 goto out;
547 }
548
549 /* iterate over the dictionary returned by the kernel */
550 while ((obj = prop_object_iterator_next(iter)) != NULL) {
551
552 array = prop_dictionary_get_keysym(dict, obj);
553 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
554 warnx("no sensors found");
555 rval = EINVAL;
556 goto out;
557 }
558
559 dnp = prop_dictionary_keysym_cstring_nocopy(obj);
560
561 if (flags & ENVSYS_DFLAG) {
562 (void)printf("%s\n", dnp);
563 } else {
564 rval = find_sensors(array);
565 if (rval)
566 goto out;
567 }
568 }
569
570 prop_object_iterator_release(iter);
571 }
572
573 if (userreq == NULL)
574 if ((flags & ENVSYS_LFLAG) == 0)
575 print_sensors(gesen, gnelems);
576
577 if (interval)
578 (void)printf("\n");
579
580 out:
581 if (gesen) {
582 free(gesen);
583 gesen = NULL;
584 gnelems = 0;
585 newsize = 0;
586 }
587 prop_object_release(dict);
588 return rval;
589 }
590
591 static int
592 find_sensors(prop_array_t array)
593 {
594 prop_object_iterator_t iter;
595 prop_object_t obj, obj1;
596 prop_string_t state, desc = NULL;
597 struct envsys_sensor *esen = NULL;
598 int rval = 0;
599 size_t oldsize;
600 char *str = NULL;
601
602 oldsize = newsize;
603 newsize += prop_array_count(array) * sizeof(*gesen);
604 esen = realloc(gesen, newsize);
605 if (esen == NULL) {
606 if (gesen)
607 free(gesen);
608 gesen = NULL;
609 rval = ENOMEM;
610 goto out;
611 }
612 gesen = esen;
613
614 iter = prop_array_iterator(array);
615 if (iter == NULL) {
616 rval = EINVAL;
617 goto out;
618 }
619
620 /* iterate over the array of dictionaries */
621 while ((obj = prop_object_iterator_next(iter)) != NULL) {
622
623 gesen[gnelems].visible = false;
624
625 /* check sensor's state */
626 state = prop_dictionary_get(obj, "state");
627
628 /* mark invalid sensors */
629 if (prop_string_equals_cstring(state, "invalid"))
630 gesen[gnelems].invalid = true;
631 else
632 gesen[gnelems].invalid = false;
633
634 /* description string */
635 desc = prop_dictionary_get(obj, "description");
636 if (desc != NULL) {
637 /* copy description */
638 (void)strlcpy(gesen[gnelems].desc,
639 prop_string_cstring_nocopy(desc),
640 sizeof(gesen[gnelems].desc));
641 } else
642 continue;
643
644 /* type string */
645 obj1 = prop_dictionary_get(obj, "type");
646 /* copy type */
647 (void)strlcpy(gesen[gnelems].type,
648 prop_string_cstring_nocopy(obj1),
649 sizeof(gesen[gnelems].type));
650
651 /* get current drive state string */
652 obj1 = prop_dictionary_get(obj, "drive-state");
653 if (obj1 != NULL)
654 (void)strlcpy(gesen[gnelems].drvstate,
655 prop_string_cstring_nocopy(obj1),
656 sizeof(gesen[gnelems].drvstate));
657
658 /* get current value */
659 obj1 = prop_dictionary_get(obj, "cur-value");
660 gesen[gnelems].cur_value = prop_number_integer_value(obj1);
661
662 /* get max value */
663 obj1 = prop_dictionary_get(obj, "max-value");
664 if (obj1 != NULL)
665 gesen[gnelems].max_value =
666 prop_number_integer_value(obj1);
667 else
668 gesen[gnelems].max_value = 0;
669
670 /* get min value */
671 obj1 = prop_dictionary_get(obj, "min-value");
672 if (obj1 != NULL)
673 gesen[gnelems].min_value =
674 prop_number_integer_value(obj1);
675 else
676 gesen[gnelems].min_value = 0;
677
678 /* get avg value */
679 obj1 = prop_dictionary_get(obj, "avg-value");
680 if (obj1 != NULL)
681 gesen[gnelems].avg_value =
682 prop_number_integer_value(obj1);
683 else
684 gesen[gnelems].avg_value = 0;
685
686 /* get percentage flag */
687 obj1 = prop_dictionary_get(obj, "want-percentage");
688 if (obj1 != NULL)
689 gesen[gnelems].percentage = prop_bool_true(obj1);
690
691 /* get critical max value if available */
692 obj1 = prop_dictionary_get(obj, "critical-max-limit");
693 if (obj1 != NULL) {
694 gesen[gnelems].critmax_value =
695 prop_number_integer_value(obj1);
696 } else
697 gesen[gnelems].critmax_value = 0;
698
699 /* get critical min value if available */
700 obj1 = prop_dictionary_get(obj, "critical-min-limit");
701 if (obj1 != NULL) {
702 gesen[gnelems].critmin_value =
703 prop_number_integer_value(obj1);
704 } else
705 gesen[gnelems].critmin_value = 0;
706
707 /* get critical capacity value if available */
708 obj1 = prop_dictionary_get(obj, "critical-capacity");
709 if (obj1 != NULL) {
710 gesen[gnelems].critcap_value =
711 prop_number_integer_value(obj1);
712 } else
713 gesen[gnelems].critcap_value = 0;
714
715 /* pass to the next struct and increase the counter */
716 gnelems++;
717
718 /* print sensor names if -l was given */
719 if (flags & ENVSYS_LFLAG) {
720 if (width)
721 (void)printf("%*s\n", width,
722 prop_string_cstring_nocopy(desc));
723 else
724 (void)printf("%s\n",
725 prop_string_cstring_nocopy(desc));
726 }
727 }
728
729 /* free memory */
730 prop_object_iterator_release(iter);
731
732 /*
733 * if -s was specified, we need a way to mark if a sensor
734 * was found.
735 */
736 if (sensors) {
737 str = strdup(sensors);
738 if (str == NULL)
739 return ENOMEM;
740
741 rval = check_sensors(gesen, str, gnelems);
742 if (rval)
743 goto out;
744 }
745
746 out:
747 if (str)
748 free(str);
749 return rval;
750 }
751
752 static int
753 check_sensors(struct envsys_sensor *es, char *str, size_t nelems)
754 {
755 int i;
756 char *sname;
757
758 sname = strtok(str, ",");
759 while (sname != NULL) {
760 for (i = 0; i < nelems; i++) {
761 if (strcmp(sname, es[i].desc) == 0) {
762 es[i].visible = true;
763 break;
764 }
765 }
766 if (i >= nelems) {
767 if (mydevname) {
768 warnx("unknown sensor `%s' for device `%s'",
769 sname, mydevname);
770 return EINVAL;
771 } else {
772 warnx("unknown sensor `%s'", sname);
773 return EINVAL;
774 }
775 }
776 sname = strtok(NULL, ",");
777 }
778
779 /* check if all sensors were ok, and error out if not */
780 for (i = 0; i < nelems; i++) {
781 if (es[i].visible)
782 return 0;
783 }
784
785 warnx("no sensors selected to display");
786 return EINVAL;
787 }
788
789 static void
790 print_sensors(struct envsys_sensor *es, size_t nelems)
791 {
792 size_t maxlen = 0;
793 double temp = 0;
794 const char *invalid = "N/A";
795 const char *degrees = NULL;
796 int i;
797
798 /* find the longest description */
799 for (i = 0; i < nelems; i++) {
800 if (strlen(es[i].desc) > maxlen)
801 maxlen = strlen(es[i].desc);
802 }
803
804 if (width)
805 maxlen = width;
806
807 /* print the sensors */
808 for (i = 0; i < nelems; i++) {
809
810 /* skip sensors that were not marked as visible */
811 if (sensors && !es[i].visible)
812 continue;
813
814 (void)printf("%*.*s", (int)maxlen, (int)maxlen, es[i].desc);
815
816 if (es[i].invalid) {
817 (void)printf(": %10s\n", invalid);
818 continue;
819 }
820
821 if (strcmp(es[i].type, "Indicator") == 0) {
822
823 (void)printf(": %10s", es[i].cur_value ? "ON" : "OFF");
824
825 /* converts the value to degC or degF */
826 #define CONVERTTEMP(a, b, c) \
827 do { \
828 (a) = ((b) / 1000000.0) - 273.15; \
829 if (flags & ENVSYS_FFLAG) { \
830 (a) = (9.0 / 5.0) * (a) + 32.0; \
831 (c) = "degF"; \
832 } else \
833 (c) = "degC"; \
834 } while (/* CONSTCOND */ 0)
835
836
837 /* temperatures */
838 } else if (strcmp(es[i].type, "Temperature") == 0) {
839
840 CONVERTTEMP(temp, es[i].cur_value, degrees);
841 (void)printf(": %10.3f %s", temp, degrees);
842
843 if (es[i].critmax_value || es[i].critmin_value)
844 (void)printf(" ");
845
846 if (es[i].critmax_value) {
847 CONVERTTEMP(temp, es[i].critmax_value, degrees);
848 (void)printf("max: %8.3f %s ", temp, degrees);
849 }
850
851 if (es[i].critmin_value) {
852 CONVERTTEMP(temp, es[i].critmin_value, degrees);
853 (void)printf("min: %8.3f %s", temp, degrees);
854 }
855 #undef CONVERTTEMP
856
857 /* fans */
858 } else if (strcmp(es[i].type, "Fan") == 0) {
859
860 (void)printf(": %10u RPM", es[i].cur_value);
861
862 if (es[i].critmax_value || es[i].critmin_value)
863 (void)printf(" ");
864 if (es[i].critmax_value)
865 (void)printf("max: %8u RPM ",
866 es[i].critmax_value);
867 if (es[i].critmin_value)
868 (void)printf("min: %8u RPM",
869 es[i].critmin_value);
870
871 /* integers */
872 } else if (strcmp(es[i].type, "Integer") == 0) {
873
874 (void)printf(": %10d", es[i].cur_value);
875
876 /* drives */
877 } else if (strcmp(es[i].type, "Drive") == 0) {
878
879 (void)printf(": %10s", es[i].drvstate);
880
881 /* everything else */
882 } else {
883 const char *type;
884
885 if (strcmp(es[i].type, "Voltage DC") == 0)
886 type = "V";
887 else if (strcmp(es[i].type, "Voltage AC") == 0)
888 type = "VAC";
889 else if (strcmp(es[i].type, "Ampere") == 0)
890 type = "A";
891 else if (strcmp(es[i].type, "Watts") == 0)
892 type = "W";
893 else if (strcmp(es[i].type, "Ohms") == 0)
894 type = "Ohms";
895 else if (strcmp(es[i].type, "Watt hour") == 0)
896 type = "Wh";
897 else if (strcmp(es[i].type, "Ampere hour") == 0)
898 type = "Ah";
899 else
900 type = NULL;
901
902 (void)printf(": %10.3f %s",
903 es[i].cur_value / 1000000.0, type);
904
905 if (es[i].percentage && es[i].max_value) {
906 (void)printf(" (%5.2f%%)",
907 (es[i].cur_value * 100.0) /
908 es[i].max_value);
909 }
910
911 if (es[i].critcap_value) {
912 (void)printf(" critical (%5.2f%%)",
913 (es[i].critcap_value * 100.0) /
914 es[i].max_value);
915 }
916
917 if (es[i].critmax_value || es[i].critmin_value)
918 (void)printf(" ");
919 if (es[i].critmax_value)
920 (void)printf("max: %8.3f %s ",
921 es[i].critmax_value / 1000000.0,
922 type);
923 if (es[i].critmin_value)
924 (void)printf("min: %8.3f %s",
925 es[i].critmin_value / 1000000.0,
926 type);
927
928 }
929
930 (void)printf("\n");
931 }
932 }
933
934 static int
935 usage(void)
936 {
937 (void)fprintf(stderr, "Usage: %s [-Dflrx] ", getprogname());
938 (void)fprintf(stderr, "[-m ...] [-s s1,s2 ] [-w num] ");
939 (void)fprintf(stderr, "[-i num] [-d ...]\n");
940 exit(EXIT_FAILURE);
941 /* NOTREACHED */
942 }
943