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