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