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