Home | History | Annotate | Line # | Download | only in envstat
envstat.c revision 1.43
      1 /* $NetBSD: envstat.c,v 1.43 2007/08/29 16:55: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 
     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 (prop_dictionary_count(dict) == 0) {
    532 		warnx("no drivers registered");
    533 		goto out;
    534 	}
    535 
    536 	if (mydevname) {
    537 		obj = prop_dictionary_get(dict, mydevname);
    538 		if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
    539 			warnx("unknown device `%s'", mydevname);
    540 			rval = EINVAL;
    541 			goto out;
    542 		}
    543 
    544 		rval = find_sensors(obj);
    545 		if (rval)
    546 			goto out;
    547 	} else {
    548 		iter = prop_dictionary_iterator(dict);
    549 		if (iter == NULL) {
    550 			rval = EINVAL;
    551 			goto out;
    552 		}
    553 
    554 		/* iterate over the dictionary returned by the kernel */
    555 		while ((obj = prop_object_iterator_next(iter)) != NULL) {
    556 
    557 			array = prop_dictionary_get_keysym(dict, obj);
    558 			if (prop_object_type(array) != PROP_TYPE_ARRAY) {
    559 				warnx("no sensors found");
    560 				rval = EINVAL;
    561 				goto out;
    562 			}
    563 
    564 			dnp = prop_dictionary_keysym_cstring_nocopy(obj);
    565 
    566 			if (flags & ENVSYS_DFLAG) {
    567 				(void)printf("%s\n", dnp);
    568 			} else {
    569 				rval = find_sensors(array);
    570 				if (rval)
    571 					goto out;
    572 			}
    573 		}
    574 
    575 		prop_object_iterator_release(iter);
    576 	}
    577 
    578 	if (userreq == NULL)
    579 		if ((flags & ENVSYS_LFLAG) == 0)
    580 			print_sensors(gesen, gnelems);
    581 
    582 	if (interval)
    583 		(void)printf("\n");
    584 
    585 out:
    586 	if (gesen) {
    587 		free(gesen);
    588 		gesen = NULL;
    589 		gnelems = 0;
    590 		newsize = 0;
    591 	}
    592 	prop_object_release(dict);
    593 	return rval;
    594 }
    595 
    596 static int
    597 find_sensors(prop_array_t array)
    598 {
    599 	prop_object_iterator_t iter;
    600 	prop_object_t obj, obj1;
    601 	prop_string_t state, desc = NULL;
    602 	struct envsys_sensor *esen = NULL;
    603 	int rval = 0;
    604 	size_t oldsize;
    605 	char *str = NULL;
    606 
    607 	oldsize = newsize;
    608 	newsize += prop_array_count(array) * sizeof(*gesen);
    609 	esen = realloc(gesen, newsize);
    610 	if (esen == NULL) {
    611 		if (gesen)
    612 			free(gesen);
    613 		gesen = NULL;
    614 		rval = ENOMEM;
    615 		goto out;
    616 	}
    617 	gesen = esen;
    618 
    619 	iter = prop_array_iterator(array);
    620 	if (iter == NULL) {
    621 		rval = EINVAL;
    622 		goto out;
    623 	}
    624 
    625 	/* iterate over the array of dictionaries */
    626 	while ((obj = prop_object_iterator_next(iter)) != NULL) {
    627 
    628 		gesen[gnelems].visible = false;
    629 
    630 		/* check sensor's state */
    631 		state = prop_dictionary_get(obj, "state");
    632 
    633 		/* mark invalid sensors */
    634 		if (prop_string_equals_cstring(state, "invalid"))
    635 			gesen[gnelems].invalid = true;
    636 		else
    637 			gesen[gnelems].invalid = false;
    638 
    639 		/* description string */
    640 		desc = prop_dictionary_get(obj, "description");
    641 		if (desc != NULL) {
    642 			/* copy description */
    643 			(void)strlcpy(gesen[gnelems].desc,
    644 			    prop_string_cstring_nocopy(desc),
    645 		    	    sizeof(gesen[gnelems].desc));
    646 		} else
    647 			continue;
    648 
    649 		/* type string */
    650 		obj1  = prop_dictionary_get(obj, "type");
    651 		/* copy type */
    652 		(void)strlcpy(gesen[gnelems].type,
    653 		    prop_string_cstring_nocopy(obj1),
    654 		    sizeof(gesen[gnelems].type));
    655 
    656 		/* get current drive state string */
    657 		obj1 = prop_dictionary_get(obj, "drive-state");
    658 		if (obj1 != NULL)
    659 			(void)strlcpy(gesen[gnelems].drvstate,
    660 			    prop_string_cstring_nocopy(obj1),
    661 			    sizeof(gesen[gnelems].drvstate));
    662 
    663 		/* get current value */
    664 		obj1 = prop_dictionary_get(obj, "cur-value");
    665 		gesen[gnelems].cur_value = prop_number_integer_value(obj1);
    666 
    667 		/* get max value */
    668 		obj1 = prop_dictionary_get(obj, "max-value");
    669 		if (obj1 != NULL)
    670 			gesen[gnelems].max_value =
    671 			    prop_number_integer_value(obj1);
    672 		else
    673 			gesen[gnelems].max_value = 0;
    674 
    675 		/* get min value */
    676 		obj1 = prop_dictionary_get(obj, "min-value");
    677 		if (obj1 != NULL)
    678 			gesen[gnelems].min_value =
    679 			    prop_number_integer_value(obj1);
    680 		else
    681 			gesen[gnelems].min_value = 0;
    682 
    683 		/* get avg value */
    684 		obj1 = prop_dictionary_get(obj, "avg-value");
    685 		if (obj1 != NULL)
    686 			gesen[gnelems].avg_value =
    687 			    prop_number_integer_value(obj1);
    688 		else
    689 			gesen[gnelems].avg_value = 0;
    690 
    691 		/* get percentage flag */
    692 		obj1 = prop_dictionary_get(obj, "want-percentage");
    693 		if (obj1 != NULL)
    694 			gesen[gnelems].percentage = prop_bool_true(obj1);
    695 
    696 		/* get critical max value if available */
    697 		obj1 = prop_dictionary_get(obj, "critical-max-limit");
    698 		if (obj1 != NULL) {
    699 			gesen[gnelems].critmax_value =
    700 			    prop_number_integer_value(obj1);
    701 		} else
    702 			gesen[gnelems].critmax_value = 0;
    703 
    704 		/* get critical min value if available */
    705 		obj1 = prop_dictionary_get(obj, "critical-min-limit");
    706 		if (obj1 != NULL) {
    707 			gesen[gnelems].critmin_value =
    708 			    prop_number_integer_value(obj1);
    709 		} else
    710 			gesen[gnelems].critmin_value = 0;
    711 
    712 		/* get critical capacity value if available */
    713 		obj1 = prop_dictionary_get(obj, "critical-capacity");
    714 		if (obj1 != NULL) {
    715 			gesen[gnelems].critcap_value =
    716 			    prop_number_integer_value(obj1);
    717 		} else
    718 			gesen[gnelems].critcap_value = 0;
    719 
    720 		/* pass to the next struct and increase the counter */
    721 		gnelems++;
    722 
    723 		/* print sensor names if -l was given */
    724 		if (flags & ENVSYS_LFLAG) {
    725 			if (width)
    726 				(void)printf("%*s\n", width,
    727 				    prop_string_cstring_nocopy(desc));
    728 			else
    729 				(void)printf("%s\n",
    730 				    prop_string_cstring_nocopy(desc));
    731 		}
    732 	}
    733 
    734 	/* free memory */
    735 	prop_object_iterator_release(iter);
    736 
    737 	/*
    738 	 * if -s was specified, we need a way to mark if a sensor
    739 	 * was found.
    740 	 */
    741 	if (sensors) {
    742 		str = strdup(sensors);
    743 		if (str == NULL)
    744 			return ENOMEM;
    745 
    746 		rval = check_sensors(gesen, str, gnelems);
    747 		if (rval)
    748 			goto out;
    749 	}
    750 
    751 out:
    752 	if (str)
    753 		free(str);
    754 	return rval;
    755 }
    756 
    757 static int
    758 check_sensors(struct envsys_sensor *es, char *str, size_t nelems)
    759 {
    760 	int i;
    761 	char *sname;
    762 
    763 	sname = strtok(str, ",");
    764 	while (sname != NULL) {
    765 		for (i = 0; i < nelems; i++) {
    766 			if (strcmp(sname, es[i].desc) == 0) {
    767 				es[i].visible = true;
    768 				break;
    769 			}
    770 		}
    771 		if (i >= nelems) {
    772 			if (mydevname) {
    773 				warnx("unknown sensor `%s' for device `%s'",
    774 				    sname, mydevname);
    775 				return EINVAL;
    776 			} else {
    777 				warnx("unknown sensor `%s'", sname);
    778 				return EINVAL;
    779 			}
    780 		}
    781 		sname = strtok(NULL, ",");
    782 	}
    783 
    784 	/* check if all sensors were ok, and error out if not */
    785 	for (i = 0; i < nelems; i++) {
    786 		if (es[i].visible)
    787 			return 0;
    788 	}
    789 
    790 	warnx("no sensors selected to display");
    791 	return EINVAL;
    792 }
    793 
    794 static void
    795 print_sensors(struct envsys_sensor *es, size_t nelems)
    796 {
    797 	size_t maxlen = 0;
    798 	double temp = 0;
    799 	const char *invalid = "N/A";
    800 	const char *degrees = NULL;
    801 	int i;
    802 
    803 	/* find the longest description */
    804 	for (i = 0; i < nelems; i++) {
    805 		if (strlen(es[i].desc) > maxlen)
    806 			maxlen = strlen(es[i].desc);
    807 	}
    808 
    809 	if (width)
    810 		maxlen = width;
    811 
    812 	/* print the sensors */
    813 	for (i = 0; i < nelems; i++) {
    814 
    815 		/* skip sensors that were not marked as visible */
    816 		if (sensors && !es[i].visible)
    817 			continue;
    818 
    819 		(void)printf("%*.*s", (int)maxlen, (int)maxlen, es[i].desc);
    820 
    821 		if (es[i].invalid) {
    822 			(void)printf(": %10s\n", invalid);
    823 			continue;
    824 		}
    825 
    826 		if (strcmp(es[i].type, "Indicator") == 0) {
    827 
    828 			(void)printf(": %10s", es[i].cur_value ? "ON" : "OFF");
    829 
    830 /* converts the value to degC or degF */
    831 #define CONVERTTEMP(a, b, c)					\
    832 do {								\
    833 	if (b) 							\
    834 		(a) = ((b) / 1000000.0) - 273.15;		\
    835 	if (flags & ENVSYS_FFLAG) {				\
    836 		if (b)						\
    837 			(a) = (9.0 / 5.0) * (a) + 32.0;		\
    838 		(c) = "degF";					\
    839 	} else							\
    840 		(c) = "degC";					\
    841 } while (/* CONSTCOND */ 0)
    842 
    843 
    844 		/* temperatures */
    845 		} else if (strcmp(es[i].type, "Temperature") == 0) {
    846 
    847 			CONVERTTEMP(temp, es[i].cur_value, degrees);
    848 			(void)printf(": %10.3f %s", temp, degrees);
    849 
    850 			if (es[i].critmax_value || es[i].critmin_value)
    851 				(void)printf("  ");
    852 
    853 			if (es[i].critmax_value) {
    854 				CONVERTTEMP(temp, es[i].critmax_value, degrees);
    855 				(void)printf("max: %8.3f %s  ", temp, degrees);
    856 			}
    857 
    858 			if (es[i].critmin_value) {
    859 				CONVERTTEMP(temp, es[i].critmin_value, degrees);
    860 				(void)printf("min: %8.3f %s", temp, degrees);
    861 			}
    862 #undef CONVERTTEMP
    863 
    864 		/* fans */
    865 		} else if (strcmp(es[i].type, "Fan") == 0) {
    866 
    867 			(void)printf(": %10u RPM", es[i].cur_value);
    868 
    869 			if (es[i].critmax_value || es[i].critmin_value)
    870 				(void)printf("   ");
    871 			if (es[i].critmax_value)
    872 				(void)printf("max: %8u RPM   ",
    873 				    es[i].critmax_value);
    874 			if (es[i].critmin_value)
    875 				(void)printf("min: %8u RPM",
    876 				    es[i].critmin_value);
    877 
    878 		/* integers */
    879 		} else if (strcmp(es[i].type, "Integer") == 0) {
    880 
    881 			(void)printf(": %10d", es[i].cur_value);
    882 
    883 		/* drives */
    884 		} else if (strcmp(es[i].type, "Drive") == 0) {
    885 
    886 			(void)printf(": %10s", es[i].drvstate);
    887 
    888 		/* everything else */
    889 		} else {
    890 			const char *type;
    891 
    892 			if (strcmp(es[i].type, "Voltage DC") == 0)
    893 				type = "V";
    894 			else if (strcmp(es[i].type, "Voltage AC") == 0)
    895 				type = "VAC";
    896 			else if (strcmp(es[i].type, "Ampere") == 0)
    897 				type = "A";
    898 			else if (strcmp(es[i].type, "Watts") == 0)
    899 				type = "W";
    900 			else if (strcmp(es[i].type, "Ohms") == 0)
    901 				type = "Ohms";
    902 			else if (strcmp(es[i].type, "Watt hour") == 0)
    903 				type = "Wh";
    904 			else if (strcmp(es[i].type, "Ampere hour") == 0)
    905 				type = "Ah";
    906 			else
    907 				type = NULL;
    908 
    909 			(void)printf(": %10.3f %s",
    910 			    es[i].cur_value / 1000000.0, type);
    911 
    912 			if (es[i].percentage && es[i].max_value) {
    913 				(void)printf(" (%5.2f%%)",
    914 				    (es[i].cur_value * 100.0) /
    915 				    es[i].max_value);
    916 			}
    917 
    918 			if (es[i].critcap_value) {
    919 				(void)printf(" critical (%5.2f%%)",
    920 				    (es[i].critcap_value * 100.0) /
    921 				    es[i].max_value);
    922 			}
    923 
    924 			if (es[i].critmax_value || es[i].critmin_value)
    925 				(void)printf("     ");
    926 			if (es[i].critmax_value)
    927 				(void)printf("max: %8.3f %s     ",
    928 				    es[i].critmax_value / 1000000.0,
    929 				    type);
    930 			if (es[i].critmin_value)
    931 				(void)printf("min: %8.3f %s",
    932 				    es[i].critmin_value / 1000000.0,
    933 				    type);
    934 
    935 		}
    936 
    937 		(void)printf("\n");
    938 	}
    939 }
    940 
    941 static int
    942 usage(void)
    943 {
    944 	(void)fprintf(stderr, "Usage: %s [-Dflrx] ", getprogname());
    945 	(void)fprintf(stderr, "[-m ...] [-s s1,s2 ] [-w num] ");
    946 	(void)fprintf(stderr, "[-i num] [-d ...]\n");
    947 	exit(EXIT_FAILURE);
    948 	/* NOTREACHED */
    949 }
    950