Home | History | Annotate | Line # | Download | only in envstat
envstat.c revision 1.54
      1 /* $NetBSD: envstat.c,v 1.54 2007/09/25 14:20:49 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 	char 	dvname[ENVSYS_DESCLEN];
     93 };
     94 
     95 static int interval, flags, width;
     96 static char *mydevname, *sensors, *userreq;
     97 static struct envsys_sensor *gesen;
     98 static size_t gnelems, newsize;
     99 
    100 static int parse_dictionary(int);
    101 static int send_dictionary(int);
    102 static int find_sensors(prop_array_t, const char *);
    103 static void print_sensors(struct envsys_sensor *, size_t, const char *);
    104 static int check_sensors(struct envsys_sensor *, char *, size_t);
    105 static int usage(void);
    106 
    107 
    108 int main(int argc, char **argv)
    109 {
    110 	prop_dictionary_t dict;
    111 	int c, fd, rval;
    112 	char *buf, *endptr;
    113 
    114 	rval = flags = interval = width = 0;
    115 	newsize = gnelems = 0;
    116 	gesen = NULL;
    117 
    118 	setprogname(argv[0]);
    119 
    120 	while ((c = getopt(argc, argv, "Dd:fIi:lm:rs:w:x")) != -1) {
    121 		switch (c) {
    122 		case 'D':	/* list registered devices */
    123 			flags |= ENVSYS_DFLAG;
    124 			break;
    125 		case 'd':	/* show sensors of a specific device */
    126 			mydevname = strdup(optarg);
    127 			if (mydevname == NULL)
    128 				err(EXIT_FAILURE, "strdup");
    129 			break;
    130 		case 'f':	/* display temperature in Farenheit */
    131 			flags |= ENVSYS_FFLAG;
    132 			break;
    133 		case 'I':	/* Skips invalid sensors */
    134 			flags |= ENVSYS_IFLAG;
    135 			break;
    136 		case 'i':	/* wait time between intervals */
    137 			interval = strtoul(optarg, &endptr, 10);
    138 			if (*endptr != '\0')
    139 				errx(EXIT_FAILURE, "bad interval '%s'", optarg);
    140 			break;
    141 		case 'l':	/* list sensors */
    142 			flags |= ENVSYS_LFLAG;
    143 			break;
    144 		case 'm':
    145 			userreq = strdup(optarg);
    146 			if (userreq == NULL)
    147 				err(EXIT_FAILURE, "strdup");
    148 			break;
    149 		case 'r':
    150 			/*
    151 			 * This flag doesn't do anything... it's only here for
    152 			 * compatibility with the old implementation.
    153 			 */
    154 			break;
    155 		case 's':	/* only show specified sensors */
    156 			sensors = strdup(optarg);
    157 			if (sensors == NULL)
    158 				err(EXIT_FAILURE, "strdup");
    159 			break;
    160 		case 'w':	/* width value for the lines */
    161 			width = strtoul(optarg, &endptr, 10);
    162 			if (*endptr != '\0')
    163 				errx(EXIT_FAILURE, "bad width '%s'", optarg);
    164 			break;
    165 		case 'x':	/* print the dictionary in raw format */
    166 			flags |= ENVSYS_XFLAG;
    167 			break;
    168 		case '?':
    169 		default:
    170 			usage();
    171 			/* NOTREACHED */
    172 		}
    173 	}
    174 
    175 	argc -= optind;
    176 	argv += optind;
    177 
    178 	if (argc > 0)
    179 		usage();
    180 
    181 	if ((fd = open(_PATH_DEV_SYSMON, O_RDONLY)) == -1)
    182 		err(EXIT_FAILURE, "%s", _PATH_DEV_SYSMON);
    183 
    184 	if (flags & ENVSYS_XFLAG) {
    185 		rval = prop_dictionary_recv_ioctl(fd,
    186 						  ENVSYS_GETDICTIONARY,
    187 						  &dict);
    188 		if (rval)
    189 			errx(EXIT_FAILURE, "%s", strerror(rval));
    190 
    191 		buf = prop_dictionary_externalize(dict);
    192 		(void)printf("%s", buf);
    193 		free(buf);
    194 
    195 	} else if (userreq) {
    196 		if (!sensors || !mydevname)
    197 			errx(EXIT_FAILURE, "-m requires -s and -d");
    198 
    199 		rval = send_dictionary(fd);
    200 
    201 #define MISSING_FLAG() 							\
    202 do {									\
    203 	if (sensors && !mydevname)					\
    204 		errx(EXIT_FAILURE, "-s requires -d");	\
    205 } while (/* CONSTCOND */ 0)
    206 
    207 	} else if (interval) {
    208 		MISSING_FLAG();
    209 		for (;;) {
    210 			rval = parse_dictionary(fd);
    211 			if (rval)
    212 				break;
    213 
    214 			(void)fflush(stdout);
    215 			(void)sleep(interval);
    216 		}
    217 	} else {
    218 		MISSING_FLAG();
    219 		rval = parse_dictionary(fd);
    220 	}
    221 
    222 	if (sensors)
    223 		free(sensors);
    224 	if (userreq)
    225 		free(userreq);
    226 	if (mydevname)
    227 		free(mydevname);
    228 	(void)close(fd);
    229 
    230 	return (rval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
    231 }
    232 
    233 static int
    234 send_dictionary(int fd)
    235 {
    236 	prop_dictionary_t dict, udict;
    237 	prop_object_t obj;
    238 	char *buf, *target, *endptr;
    239 	int error, i, uflag;
    240 	double val;
    241 
    242 	error = uflag = val = 0;
    243 
    244 	/*
    245 	 * part 1: kernel dictionary.
    246 	 *
    247 	 * This part consists in parsing the kernel dictionary
    248 	 * to check for unknown devices or sensors. This is to
    249 	 * know what type of sensor are we trying to set a
    250 	 * a critical limit.
    251 	 */
    252 	error = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
    253 	if (error)
    254 		return error;
    255 
    256 	if (mydevname) {
    257 		obj = prop_dictionary_get(dict, mydevname);
    258 		if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
    259 			warnx("unknown device `%s'", mydevname);
    260 			prop_object_release(dict);
    261 			return EINVAL;
    262 		}
    263 
    264 		if (find_sensors(obj, mydevname)) {
    265 			prop_object_release(dict);
    266 			return EINVAL;
    267 		}
    268 	}
    269 
    270 	/* find the type for selected sensor */
    271 	for (i = 0; i < gnelems; i++)
    272 		if (strcmp(sensors, gesen[i].desc) == 0)
    273 			break;
    274 
    275 	/* we know the type of the sensor now, release kernel dict */
    276 	prop_object_release(dict);
    277 	/* we don't need the rdonly fd */
    278 	(void)close(fd);
    279 
    280 
    281 	/*
    282 	 * part 2: userland dictionary.
    283 	 *
    284 	 * This part consists in setting the values provided
    285 	 * by the user and convert when necesssary to send
    286 	 * them to the kernel again.
    287 	 */
    288 	udict = prop_dictionary_create();
    289 
    290 #define MKPROP(var, str)						\
    291 do {									\
    292 	obj = prop_string_create_cstring_nocopy(var);			\
    293 	if (obj == NULL || !prop_dictionary_set(udict, (str), obj)) {	\
    294 		error = EINVAL;						\
    295 		goto out;						\
    296 	}								\
    297 } while (/* CONSTCOND */ 0)
    298 
    299 	/* create the driver-name object */
    300 	MKPROP(mydevname, "driver-name");
    301 	prop_object_release(obj);
    302 
    303 	/* create the sensor-name object */
    304 	MKPROP(sensors, "sensor-name");
    305 	prop_object_release(obj);
    306 
    307 #undef MKPROP
    308 
    309 	/*
    310 	 * parse the -m argument; we understand the following ways:
    311 	 *
    312 	 * 	-m critical/crit{max,min}=value
    313 	 * 	-m critical/crit{max,min}=remove
    314 	 * 	-m desc="BLAH"
    315 	 * 	-m rfact=value
    316 	 */
    317 	if (userreq) {
    318 		buf = strtok(userreq, "=");
    319 		target = strdup(buf);
    320 		if (target == NULL) {
    321 			error = ENOMEM;
    322 			goto out;
    323 		}
    324 
    325 		while (buf != NULL) {
    326 			/*
    327 			 * skip current string if it's the same
    328 			 * than target requested.
    329 			 */
    330 			if (strcmp(target, buf) == 0)
    331 				buf = strtok(NULL, "=");
    332 
    333 			/* check what target was requested */
    334 			if (strcmp(target, "desc") == 0) {
    335 				uflag |= USERF_SDESCR;
    336 				obj = prop_string_create_cstring_nocopy(buf);
    337 				break;
    338 #define SETNCHECKVAL(a, b)						\
    339 do {									\
    340 	if (strcmp(buf, "remove") == 0)					\
    341 		uflag |= (a);						\
    342 	else {								\
    343 		uflag |= (b);						\
    344 		val = strtod(buf, &endptr);				\
    345 		if (*endptr != '\0') {					\
    346 			warnx("invalid value");				\
    347 			error = EINVAL;					\
    348 			goto out;					\
    349 		}							\
    350 	}								\
    351 } while (/* CONSTCOND */ 0)
    352 
    353 			} else if (strcmp(target, "critical") == 0) {
    354 				SETNCHECKVAL(USERF_RCRITICAL, USERF_SCRITICAL);
    355 				break;
    356 			} else if (strcmp(target, "critmax") == 0) {
    357 				SETNCHECKVAL(USERF_RCRITMAX, USERF_SCRITMAX);
    358 				break;
    359 			} else if (strcmp(target, "critmin") == 0) {
    360 				SETNCHECKVAL(USERF_RCRITMIN, USERF_SCRITMIN);
    361 				break;
    362 			} else if (strcmp(target, "rfact") == 0) {
    363 				uflag |= USERF_SRFACT;
    364 				val = strtod(buf, &endptr);
    365 				if (*endptr != '\0') {
    366 					warnx("invalid value");
    367 					error = EINVAL;
    368 					goto out;
    369 				}
    370 				break;
    371 			} else {
    372 				warnx("invalid target");
    373 				error =  EINVAL;
    374 				goto out;
    375 			}
    376 		}
    377 		free(target);
    378 	}
    379 
    380 #undef SETNCHECKVAL
    381 
    382 	/* critical capacity for percentage sensors */
    383 	if (uflag & USERF_SCRITICAL) {
    384 		/* sanity check */
    385 		if (val < 0 || val > 100) {
    386 			warnx("invalid value (0><100)");
    387 			error = EINVAL;
    388 			goto out;
    389 		}
    390 
    391 		/* ok... convert the value */
    392 		val = (val / 100) * gesen[i].max_value;
    393 		obj = prop_number_create_unsigned_integer(val);
    394 	}
    395 
    396 	/*
    397 	 * conversions required to send a proper value to the kernel.
    398 	 */
    399 	if ((uflag & USERF_SCRITMAX) || (uflag & USERF_SCRITMIN)) {
    400 		/* temperatures */
    401 		if (strcmp(gesen[i].type, "Temperature") == 0) {
    402 			/* convert from farenheit to celsius */
    403 			if (flags & ENVSYS_FFLAG)
    404 				val = (val - 32.0) * (5.0 / 9.0);
    405 
    406 			/* convert to microKelvin */
    407 			val = val * 1000000 + 273150000;
    408 			/* printf("val=%d\n", (int)val); */
    409 			obj = prop_number_create_unsigned_integer(val);
    410 		/* fans */
    411 		} else if (strcmp(gesen[i].type, "Fan") == 0) {
    412 			if (val < 0 || val > 10000) {
    413 				error = EINVAL;
    414 				goto out;
    415 			}
    416 			/* printf("val=%d\n", (int)val); */
    417 			obj = prop_number_create_unsigned_integer(val);
    418 
    419 		/* volts, watts, ohms, etc */
    420 		} else {
    421 			/* convert to m[V,W,Ohms] again */
    422 			val *= 1000000.0;
    423 			/* printf("val=%5.0f\n", val); */
    424 			obj = prop_number_create_integer(val);
    425 		}
    426 	}
    427 
    428 #define SETPROP(str)							\
    429 do {									\
    430 	if (!prop_dictionary_set(udict, (str), obj)) {			\
    431 		error = EINVAL;						\
    432 		goto out;						\
    433 	}								\
    434 } while ( /*CONSTCOND*/ 0)
    435 
    436 	/* user wanted to set a new description */
    437 	if (uflag & USERF_SDESCR) {
    438 		SETPROP("new-description");
    439 
    440 	/* user wanted to set a new critical capacity */
    441 	} else if (uflag & USERF_SCRITICAL) {
    442 		SETPROP("critical-capacity");
    443 
    444 	} else if (uflag & USERF_RCRITICAL) {
    445 		obj = prop_bool_create(1);
    446 		SETPROP("remove-critical-cap");
    447 
    448 	/* user wanted to remove a critical min limit */
    449 	} else if (uflag & USERF_RCRITMIN) {
    450 		obj = prop_bool_create(1);
    451 		SETPROP("remove-cmin-limit");
    452 
    453 	/* user wanted to remove a critical max limit */
    454 	} else if (uflag & USERF_RCRITMAX) {
    455 		obj = prop_bool_create(1);
    456 		SETPROP("remove-cmax-limit");
    457 
    458 	/* user wanted to set a new critical min value */
    459 	} else if (uflag & USERF_SCRITMIN) {
    460 		SETPROP("critical-min-limit");
    461 
    462 	/* user wanted to set a new critical max value */
    463 	} else if (uflag & USERF_SCRITMAX) {
    464 		SETPROP("critical-max-limit");
    465 
    466 	/* user wanted to set a new rfact */
    467 	} else if (uflag & USERF_SRFACT) {
    468 		obj = prop_number_create_integer(val);
    469 		SETPROP("new-rfact");
    470 
    471 	} else {
    472 		warnx("unknown operation");
    473 		error = EINVAL;
    474 		goto out;
    475 	}
    476 
    477 #undef SETPROP
    478 
    479 	prop_object_release(obj);
    480 
    481 #ifdef DEBUG
    482 	printf("%s", prop_dictionary_externalize(udict));
    483 	return error;
    484 #endif
    485 
    486 	if ((fd = open(_PATH_DEV_SYSMON, O_RDWR)) == -1) {
    487 		error = errno;
    488 		warn("%s", _PATH_DEV_SYSMON);
    489 		goto out;
    490 	}
    491 
    492 	/* all done? send our dictionary now */
    493 	error = prop_dictionary_send_ioctl(udict, fd, ENVSYS_SETDICTIONARY);
    494 
    495 	if (error)
    496 		warnx("%s", strerror(error));
    497 out:
    498 	prop_object_release(udict);
    499 	return error;
    500 }
    501 
    502 static int
    503 parse_dictionary(int fd)
    504 {
    505 	prop_array_t array;
    506 	prop_dictionary_t dict;
    507 	prop_object_iterator_t iter;
    508 	prop_object_t obj;
    509 	const char *dnp = NULL;
    510 	int rval = 0;
    511 
    512 	/* receive dictionary from kernel */
    513 	rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
    514 	if (rval)
    515 		return rval;
    516 
    517 	if (prop_dictionary_count(dict) == 0) {
    518 		warnx("no drivers registered");
    519 		goto out;
    520 	}
    521 
    522 	if (mydevname) {
    523 		obj = prop_dictionary_get(dict, mydevname);
    524 		if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
    525 			warnx("unknown device `%s'", mydevname);
    526 			rval = EINVAL;
    527 			goto out;
    528 		}
    529 
    530 		rval = find_sensors(obj, mydevname);
    531 		if (rval)
    532 			goto out;
    533 
    534 		if (userreq == NULL) {
    535 			if ((flags & ENVSYS_LFLAG) == 0)
    536 				print_sensors(gesen, gnelems, mydevname);
    537 			if (interval)
    538 				(void)printf("\n");
    539 		}
    540 	} else {
    541 		iter = prop_dictionary_iterator(dict);
    542 		if (iter == NULL) {
    543 			rval = EINVAL;
    544 			goto out;
    545 		}
    546 
    547 		/* iterate over the dictionary returned by the kernel */
    548 		while ((obj = prop_object_iterator_next(iter)) != NULL) {
    549 
    550 			array = prop_dictionary_get_keysym(dict, obj);
    551 			if (prop_object_type(array) != PROP_TYPE_ARRAY) {
    552 				warnx("no sensors found");
    553 				rval = EINVAL;
    554 				goto out;
    555 			}
    556 
    557 			dnp = prop_dictionary_keysym_cstring_nocopy(obj);
    558 
    559 			if (flags & ENVSYS_DFLAG) {
    560 				(void)printf("%s\n", dnp);
    561 				continue;
    562 			} else {
    563 				(void)printf("[%s]\n", dnp);
    564 				rval = find_sensors(array, dnp);
    565 				if (rval)
    566 					goto out;
    567 			}
    568 
    569 			if (userreq == NULL) {
    570 				if ((flags & ENVSYS_LFLAG) == 0)
    571 					print_sensors(gesen, gnelems, dnp);
    572 				if (interval)
    573 					(void)printf("\n");
    574 			}
    575 		}
    576 
    577 		prop_object_iterator_release(iter);
    578 	}
    579 
    580 out:
    581 	if (gesen) {
    582 		free(gesen);
    583 		gesen = NULL;
    584 		gnelems = 0;
    585 		newsize = 0;
    586 	}
    587 	prop_object_release(dict);
    588 	return rval;
    589 }
    590 
    591 static int
    592 find_sensors(prop_array_t array, const char *dvname)
    593 {
    594 	prop_object_iterator_t iter;
    595 	prop_object_t obj, obj1;
    596 	prop_string_t state, desc = NULL;
    597 	struct envsys_sensor *esen = NULL;
    598 	int rval = 0;
    599 	char *str = NULL;
    600 
    601 	newsize += prop_array_count(array) * sizeof(*gesen);
    602 	esen = realloc(gesen, newsize);
    603 	if (esen == NULL) {
    604 		if (gesen)
    605 			free(gesen);
    606 		gesen = NULL;
    607 		rval = ENOMEM;
    608 		goto out;
    609 	}
    610 	gesen = esen;
    611 
    612 	iter = prop_array_iterator(array);
    613 	if (iter == NULL) {
    614 		rval = EINVAL;
    615 		goto out;
    616 	}
    617 
    618 	/* iterate over the array of dictionaries */
    619 	while ((obj = prop_object_iterator_next(iter)) != NULL) {
    620 
    621 		/* copy device name */
    622 		(void)strlcpy(gesen[gnelems].dvname, dvname,
    623 		    sizeof(gesen[gnelems].dvname));
    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, const char *dvname)
    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 		/* skip sensors that don't belong to device 'dvname' */
    819 		if (strcmp(es[i].dvname, dvname))
    820 			continue;
    821 
    822 		/* skip sensors that were not marked as visible */
    823 		if (sensors && !es[i].visible)
    824 			continue;
    825 
    826 		/* Do not print invalid sensors if -I is set */
    827 		if ((flags & ENVSYS_IFLAG) && es[i].invalid)
    828 			continue;
    829 
    830 		(void)printf("%s%*.*s", mydevname ? "" : "  ", (int)maxlen,
    831 		    (int)maxlen, es[i].desc);
    832 
    833 		if (es[i].invalid) {
    834 			(void)printf(": %10s\n", invalid);
    835 			continue;
    836 		}
    837 
    838 		if (strcmp(es[i].type, "Indicator") == 0) {
    839 
    840 			(void)printf(": %10s", es[i].cur_value ? "ON" : "OFF");
    841 
    842 /* converts the value to degC or degF */
    843 #define CONVERTTEMP(a, b, c)					\
    844 do {								\
    845 	if (b) 							\
    846 		(a) = ((b) / 1000000.0) - 273.15;		\
    847 	if (flags & ENVSYS_FFLAG) {				\
    848 		if (b)						\
    849 			(a) = (9.0 / 5.0) * (a) + 32.0;		\
    850 		(c) = "degF";					\
    851 	} else							\
    852 		(c) = "degC";					\
    853 } while (/* CONSTCOND */ 0)
    854 
    855 
    856 		/* temperatures */
    857 		} else if (strcmp(es[i].type, "Temperature") == 0) {
    858 
    859 			CONVERTTEMP(temp, es[i].cur_value, degrees);
    860 			(void)printf(": %10.3f %s", temp, degrees);
    861 
    862 			if (es[i].critmax_value || es[i].critmin_value)
    863 				(void)printf("  ");
    864 
    865 			if (es[i].critmax_value) {
    866 				CONVERTTEMP(temp, es[i].critmax_value, degrees);
    867 				(void)printf("max: %8.3f %s  ", temp, degrees);
    868 			}
    869 
    870 			if (es[i].critmin_value) {
    871 				CONVERTTEMP(temp, es[i].critmin_value, degrees);
    872 				(void)printf("min: %8.3f %s", temp, degrees);
    873 			}
    874 #undef CONVERTTEMP
    875 
    876 		/* fans */
    877 		} else if (strcmp(es[i].type, "Fan") == 0) {
    878 
    879 			(void)printf(": %10u RPM", es[i].cur_value);
    880 
    881 			if (es[i].critmax_value || es[i].critmin_value)
    882 				(void)printf("   ");
    883 			if (es[i].critmax_value)
    884 				(void)printf("max: %8u RPM   ",
    885 				    es[i].critmax_value);
    886 			if (es[i].critmin_value)
    887 				(void)printf("min: %8u RPM",
    888 				    es[i].critmin_value);
    889 
    890 		/* integers */
    891 		} else if (strcmp(es[i].type, "Integer") == 0) {
    892 
    893 			(void)printf(": %10d", es[i].cur_value);
    894 
    895 		/* drives  */
    896 		} else if (strcmp(es[i].type, "Drive") == 0) {
    897 
    898 			(void)printf(": %10s", es[i].drvstate);
    899 
    900 		/* Battery state */
    901 		} else if (strcmp(es[i].type, "Battery state") == 0) {
    902 
    903 			(void)printf(": %10s", es[i].battstate);
    904 
    905 		/* everything else */
    906 		} else {
    907 			const char *type;
    908 
    909 			if (strcmp(es[i].type, "Voltage DC") == 0)
    910 				type = "V";
    911 			else if (strcmp(es[i].type, "Voltage AC") == 0)
    912 				type = "VAC";
    913 			else if (strcmp(es[i].type, "Ampere") == 0)
    914 				type = "A";
    915 			else if (strcmp(es[i].type, "Watts") == 0)
    916 				type = "W";
    917 			else if (strcmp(es[i].type, "Ohms") == 0)
    918 				type = "Ohms";
    919 			else if (strcmp(es[i].type, "Watt hour") == 0)
    920 				type = "Wh";
    921 			else if (strcmp(es[i].type, "Ampere hour") == 0)
    922 				type = "Ah";
    923 			else
    924 				type = NULL;
    925 
    926 			(void)printf(": %10.3f %s",
    927 			    es[i].cur_value / 1000000.0, type);
    928 
    929 			if (es[i].percentage && es[i].max_value) {
    930 				(void)printf(" (%5.2f%%)",
    931 				    (es[i].cur_value * 100.0) /
    932 				    es[i].max_value);
    933 			}
    934 
    935 			if (es[i].critcap_value) {
    936 				(void)printf(" critical (%5.2f%%)",
    937 				    (es[i].critcap_value * 100.0) /
    938 				    es[i].max_value);
    939 			}
    940 
    941 			if (es[i].critmax_value || es[i].critmin_value)
    942 				(void)printf("     ");
    943 			if (es[i].critmax_value)
    944 				(void)printf("max: %8.3f %s     ",
    945 				    es[i].critmax_value / 1000000.0,
    946 				    type);
    947 			if (es[i].critmin_value)
    948 				(void)printf("min: %8.3f %s",
    949 				    es[i].critmin_value / 1000000.0,
    950 				    type);
    951 
    952 		}
    953 
    954 		(void)printf("\n");
    955 	}
    956 }
    957 
    958 static int
    959 usage(void)
    960 {
    961 	(void)fprintf(stderr, "Usage: %s [-DfIlrx] ", getprogname());
    962 	(void)fprintf(stderr, "[-d device] [-i interval] ");
    963 	(void)fprintf(stderr, "[-m key=value] [-s sensor,...] [-w width]\n");
    964 	exit(EXIT_FAILURE);
    965 	/* NOTREACHED */
    966 }
    967