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