Home | History | Annotate | Line # | Download | only in envstat
envstat.c revision 1.44
      1 /* $NetBSD: envstat.c,v 1.44 2007/09/02 19:36:59 xtraeme Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Juan Romero Pardines.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by Juan Romero Pardines
     21  *      for the NetBSD Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * TODO:
     41  *
     42  * 	o Some checks should be added to ensure that the user does not
     43  * 	  set unwanted values for the critical limits.
     44  */
     45 
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <stdbool.h>
     49 #include <string.h>
     50 #include <unistd.h>
     51 #include <fcntl.h>
     52 #include <err.h>
     53 #include <errno.h>
     54 #include <prop/proplib.h>
     55 #include <sys/envsys.h>
     56 
     57 #define _PATH_DEV_SYSMON	"/dev/sysmon"
     58 
     59 #define ENVSYS_DFLAG	0x00000001	/* list registered devices */
     60 #define ENVSYS_FFLAG	0x00000002	/* show temp in farenheit */
     61 #define ENVSYS_LFLAG	0x00000004	/* list sensors */
     62 #define ENVSYS_XFLAG	0x00000008	/* externalize dictionary */
     63 
     64 /*
     65  * Operation flags for -m.
     66  */
     67 #define USERF_SCRITICAL 0x00000001	/* set a critical limit */
     68 #define USERF_RCRITICAL 0x00000002	/* remove a critical limit */
     69 #define USERF_SCRITMAX	0x00000004	/* set a critical max limit */
     70 #define USERF_RCRITMAX	0x00000008	/* remove a critical max limit */
     71 #define USERF_SCRITMIN	0x00000010	/* set a critical min limit */
     72 #define USERF_RCRITMIN	0x00000020	/* remove a critical min limit */
     73 #define USERF_SRFACT	0x00000040	/* set a new rfact */
     74 #define USERF_SDESCR	0x00000080	/* set a new description */
     75 
     76 struct envsys_sensor {
     77 	bool	invalid;
     78 	bool	visible;
     79 	bool	percentage;
     80 	int32_t	cur_value;
     81 	int32_t	max_value;
     82 	int32_t	min_value;
     83 	int32_t	avg_value;
     84 	int32_t critcap_value;
     85 	int32_t	critmin_value;
     86 	int32_t	critmax_value;
     87 	char	desc[ENVSYS_DESCLEN];
     88 	char	type[ENVSYS_DESCLEN];
     89 	char	drvstate[ENVSYS_DESCLEN];
     90 	char	genstr[ENVSYS_DESCLEN];
     91 };
     92 
     93 static int interval, flags, width;
     94 static char *mydevname, *sensors, *userreq;
     95 static struct envsys_sensor *gesen;
     96 static size_t gnelems, newsize;
     97 
     98 static int parse_dictionary(int);
     99 static int send_dictionary(int);
    100 static int find_sensors(prop_array_t);
    101 static void print_sensors(struct envsys_sensor *, size_t);
    102 static int check_sensors(struct envsys_sensor *, char *, size_t);
    103 static int usage(void);
    104 
    105 
    106 int main(int argc, char **argv)
    107 {
    108 	prop_dictionary_t dict;
    109 	int c, fd, rval;
    110 	char *buf, *endptr;
    111 
    112 	rval = flags = interval = width = 0;
    113 	newsize = gnelems = 0;
    114 	gesen = NULL;
    115 
    116 	setprogname(argv[0]);
    117 
    118 	while ((c = getopt(argc, argv, "Dd:fi:lm:rs:w:x")) != -1) {
    119 		switch (c) {
    120 		case 'd':	/* show sensors of a specific device */
    121 			mydevname = strdup(optarg);
    122 			if (mydevname == NULL)
    123 				err(ENOMEM, "out of memory");
    124 			break;
    125 		case 'i':	/* wait time between intervals */
    126 			interval = strtoul(optarg, &endptr, 10);
    127 			if (*endptr != '\0')
    128 				errx(1, "interval must be an integer");
    129 			break;
    130 		case 'D':	/* list registered devices */
    131 			flags |= ENVSYS_DFLAG;
    132 			break;
    133 		case 'f':	/* display temperature in Farenheit */
    134 			flags |= ENVSYS_FFLAG;
    135 			break;
    136 		case 'l':	/* list sensors */
    137 			flags |= ENVSYS_LFLAG;
    138 			break;
    139 		case 'w':	/* width value for the lines */
    140 			width = strtoul(optarg, &endptr, 10);
    141 			if (*endptr != '\0')
    142 				errx(1, "width must be an integer");
    143 			break;
    144 		case 'x':	/* print the dictionary in raw format */
    145 			flags |= ENVSYS_XFLAG;
    146 			break;
    147 		case 'r':
    148 			/*
    149 			 * This flag doesn't do anything... it's only here for
    150 			 * compatibility with the old implementation.
    151 			 */
    152 			break;
    153 		case 's':	/* only show specified sensors */
    154 			sensors = strdup(optarg);
    155 			if (sensors == NULL)
    156 				err(ENOMEM, "out of memory");
    157 			break;
    158 		case 'm':
    159 			userreq = strdup(optarg);
    160 			if (userreq == NULL)
    161 				err(ENOMEM, "out of memory");
    162 			break;
    163 		case '?':
    164 		default:
    165 			usage();
    166 			/* NOTREACHED */
    167 		}
    168 	}
    169 
    170 	if ((fd = open(_PATH_DEV_SYSMON, O_RDONLY)) == -1)
    171 		err(EXIT_FAILURE, "open");
    172 
    173 	if (!interval && (flags & ENVSYS_XFLAG)) {
    174 		rval = prop_dictionary_recv_ioctl(fd,
    175 		    			          ENVSYS_GETDICTIONARY,
    176 					          &dict);
    177 		if (rval) {
    178 			(void)printf("%s: %s\n", getprogname(),
    179 			    strerror(rval));
    180 			goto out;
    181 		}
    182 	}
    183 
    184 	if (argc == 1) {
    185 		rval = parse_dictionary(fd);
    186 
    187 	} else if (userreq) {
    188 		if (!sensors || !mydevname) {
    189 			(void)fprintf(stderr, "%s: -m cannot be used without "
    190 			    "-s and -d\n", getprogname());
    191 			return EINVAL;
    192 		}
    193 
    194 		rval = send_dictionary(fd);
    195 		goto out;
    196 
    197 	} else if (interval) {
    198 		for (;;) {
    199 			if (sensors && !mydevname) {
    200 				(void)fprintf(stderr, "%s: -s cannot be used "
    201 				    "without -d\n", getprogname());
    202 				rval = EINVAL;
    203 				goto out;
    204 			}
    205 
    206 			rval = parse_dictionary(fd);
    207 			if (rval)
    208 				goto out;
    209 			(void)fflush(stdout);
    210 			(void)sleep(interval);
    211 		}
    212 
    213 	} else if (!interval) {
    214 		if (flags & ENVSYS_XFLAG) {
    215 			buf = prop_dictionary_externalize(dict);
    216 			(void)printf("%s", buf);
    217 			free(buf);
    218 		} else {
    219 			if (sensors && !mydevname) {
    220 				(void)fprintf(stderr, "%s: -s cannot be used "
    221 				    "without -d\n", getprogname());
    222 				rval = EINVAL;
    223 				goto out;
    224 			}
    225 			rval = parse_dictionary(fd);
    226 		}
    227 
    228 	} else
    229 		usage();
    230 
    231 out:
    232 	if (sensors)
    233 		free(sensors);
    234 	if (userreq)
    235 		free(userreq);
    236 	if (mydevname)
    237 		free(mydevname);
    238 	if (gesen)
    239 		free(gesen);
    240 	(void)close(fd);
    241 	return rval;
    242 }
    243 
    244 static int
    245 send_dictionary(int fd)
    246 {
    247 	prop_dictionary_t dict, udict;
    248 	prop_object_t obj;
    249 	char *buf, *target, *endptr;
    250 	int error, i, uflag;
    251 	double val;
    252 
    253 	error = uflag = val = 0;
    254 
    255 	/*
    256 	 * part 1: kernel dictionary.
    257 	 *
    258 	 * This parts consists in parsing the kernel dictionary
    259 	 * to check for unknown device or sensor and we must
    260 	 * know what type of sensor are we trying to set
    261 	 * a critical condition.
    262 	 */
    263 	error = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
    264 	if (error)
    265 		return error;
    266 
    267 	if (mydevname) {
    268 		obj = prop_dictionary_get(dict, mydevname);
    269 		if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
    270 			warnx("unknown device `%s'", mydevname);
    271 			prop_object_release(dict);
    272 			return EINVAL;
    273 		}
    274 
    275 		if (find_sensors(obj)) {
    276 			prop_object_release(dict);
    277 			return EINVAL;
    278 		}
    279 	}
    280 
    281 	/* find the type for selected sensor */
    282 	for (i = 0; i < gnelems; i++)
    283 		if (strcmp(sensors, gesen[i].desc) == 0)
    284 			break;
    285 
    286 	/* we know the type of the sensor now, release kernel dict */
    287 	prop_object_release(dict);
    288 	/* we don't need the rdonly fd */
    289 	(void)close(fd);
    290 
    291 
    292 	/*
    293 	 * part 2: userland dictionary.
    294 	 *
    295 	 * This parts consists in setting the values provided
    296 	 * by the user and convert when necesssary to send
    297 	 * them to the kernel again.
    298 	 */
    299 	udict = prop_dictionary_create();
    300 
    301 #define MKPROP(var, str)						\
    302 do {									\
    303 	obj = prop_string_create_cstring_nocopy(var);			\
    304 	if (obj == NULL || !prop_dictionary_set(udict, (str), obj)) {	\
    305 		error = EINVAL;						\
    306 		goto out;						\
    307 	}								\
    308 } while (/* CONSTCOND */ 0)
    309 
    310 	/* create the driver-name object */
    311 	MKPROP(mydevname, "driver-name");
    312 	prop_object_release(obj);
    313 
    314 	/* create the sensor-name object */
    315 	MKPROP(sensors, "sensor-name");
    316 	prop_object_release(obj);
    317 
    318 #undef MKPROP
    319 
    320 	/*
    321 	 * parse the -m argument; we understand the following ways:
    322 	 *
    323 	 * 	-m critical/crit{max,min}=value
    324 	 * 	-m critical/crit{max,min}=remove
    325 	 * 	-m desc="BLAH"
    326 	 * 	-m rfact=value
    327 	 */
    328 	if (userreq) {
    329 		buf = strtok(userreq, "=");
    330 		target = strdup(buf);
    331 		if (target == NULL) {
    332 			error = ENOMEM;
    333 			goto out;
    334 		}
    335 
    336 		while (buf != NULL) {
    337 			/*
    338 			 * skip current string if it's the same
    339 			 * than target requested.
    340 			 */
    341 			if (strcmp(target, buf) == 0)
    342 				buf = strtok(NULL, "=");
    343 
    344 			/* check what target was requested */
    345 			if (strcmp(target, "desc") == 0) {
    346 				uflag |= USERF_SDESCR;
    347 				obj = prop_string_create_cstring_nocopy(buf);
    348 				break;
    349 #define SETNCHECKVAL(a, b)						\
    350 do {									\
    351 	if (strcmp(buf, "remove") == 0)					\
    352 		uflag |= (a);						\
    353 	else {								\
    354 		uflag |= (b);						\
    355 		val = strtod(buf, &endptr);				\
    356 		if (*endptr != '\0') {					\
    357 			(void)printf("%s: invalid value\n",		\
    358 			    getprogname());				\
    359 			error = EINVAL;					\
    360 			goto out;					\
    361 		}							\
    362 	}								\
    363 } while (/* CONSTCOND */ 0)
    364 
    365 			} else if (strcmp(target, "critical") == 0) {
    366 				SETNCHECKVAL(USERF_RCRITICAL, USERF_SCRITICAL);
    367 				break;
    368 			} else if (strcmp(target, "critmax") == 0) {
    369 				SETNCHECKVAL(USERF_RCRITMAX, USERF_SCRITMAX);
    370 				break;
    371 			} else if (strcmp(target, "critmin") == 0) {
    372 				SETNCHECKVAL(USERF_RCRITMIN, USERF_SCRITMIN);
    373 				break;
    374 			} else if (strcmp(target, "rfact") == 0) {
    375 				uflag |= USERF_SRFACT;
    376 				val = strtod(buf, &endptr);
    377 				if (*endptr != '\0') {
    378 					(void)printf("%s: invalid value\n",
    379 					    getprogname());
    380 					error = EINVAL;
    381 					goto out;
    382 				}
    383 				break;
    384 			} else {
    385 				(void)printf("%s: invalid target\n",
    386 				    getprogname());
    387 				error =  EINVAL;
    388 				goto out;
    389 			}
    390 		}
    391 		free(target);
    392 	}
    393 
    394 #undef SETNCHECKVAL
    395 
    396 	/* critical capacity for percentage sensors */
    397 	if (uflag & USERF_SCRITICAL) {
    398 		/* sanity check */
    399 		if (val < 0 || val > 100) {
    400 			(void)printf("%s: invalid value (0><100)\n",
    401 			    getprogname());
    402 			error = EINVAL;
    403 			goto out;
    404 		}
    405 
    406 		/* ok... convert the value */
    407 		val = (val / 100) * gesen[i].max_value;
    408 		obj = prop_number_create_unsigned_integer(val);
    409 	}
    410 
    411 	/*
    412 	 * conversions required to send a proper value to the kernel.
    413 	 */
    414 	if ((uflag & USERF_SCRITMAX) || (uflag & USERF_SCRITMIN)) {
    415 		/* temperatures */
    416 		if (strcmp(gesen[i].type, "Temperature") == 0) {
    417 			/* convert from farenheit to celsius */
    418 			if (flags & ENVSYS_FFLAG)
    419 				val = (val - 32.0) * (5.0 / 9.0);
    420 
    421 			/* convert to microKelvin */
    422 			val = val * 1000000 + 273150000;
    423 			/* printf("val=%d\n", (int)val); */
    424 			obj = prop_number_create_unsigned_integer(val);
    425 		/* fans */
    426 		} else if (strcmp(gesen[i].type, "Fan") == 0) {
    427 			if (val < 0 || val > 10000) {
    428 				error = EINVAL;
    429 				goto out;
    430 			}
    431 			/* printf("val=%d\n", (int)val); */
    432 			obj = prop_number_create_unsigned_integer(val);
    433 
    434 		/* volts, watts, ohms, etc */
    435 		} else {
    436 			/* convert to m[V,W,Ohms] again */
    437 			val *= 1000000.0;
    438 			/* printf("val=%5.0f\n", val); */
    439 			obj = prop_number_create_integer(val);
    440 		}
    441 	}
    442 
    443 #define SETPROP(str)							\
    444 do {									\
    445 	if (!prop_dictionary_set(udict, (str), obj)) {			\
    446 		error = EINVAL;						\
    447 		goto out;						\
    448 	}								\
    449 } while ( /*CONSTCOND*/ 0)
    450 
    451 	/* user wanted to set a new description */
    452 	if (uflag & USERF_SDESCR) {
    453 		SETPROP("new-description");
    454 
    455 	/* user wanted to set a new critical capacity */
    456 	} else if (uflag & USERF_SCRITICAL) {
    457 		SETPROP("critical-capacity");
    458 
    459 	} else if (uflag & USERF_RCRITICAL) {
    460 		obj = prop_bool_create(1);
    461 		SETPROP("remove-critical-cap");
    462 
    463 	/* user wanted to remove a critical min limit */
    464 	} else if (uflag & USERF_RCRITMIN) {
    465 		obj = prop_bool_create(1);
    466 		SETPROP("remove-cmin-limit");
    467 
    468 	/* user wanted to remove a critical max limit */
    469 	} else if (uflag & USERF_RCRITMAX) {
    470 		obj = prop_bool_create(1);
    471 		SETPROP("remove-cmax-limit");
    472 
    473 	/* user wanted to set a new critical min value */
    474 	} else if (uflag & USERF_SCRITMIN) {
    475 		SETPROP("critical-min-limit");
    476 
    477 	/* user wanted to set a new critical max value */
    478 	} else if (uflag & USERF_SCRITMAX) {
    479 		SETPROP("critical-max-limit");
    480 
    481 	/* user wanted to set a new rfact */
    482 	} else if (uflag & USERF_SRFACT) {
    483 		obj = prop_number_create_integer(val);
    484 		SETPROP("new-rfact");
    485 
    486 	} else {
    487 		(void)printf("%s: unknown operation\n", getprogname());
    488 		error = EINVAL;
    489 		goto out;
    490 	}
    491 
    492 #undef SETPROP
    493 
    494 	prop_object_release(obj);
    495 
    496 #ifdef DEBUG
    497 	printf("%s", prop_dictionary_externalize(udict));
    498 	return error;
    499 #endif
    500 
    501 	if ((fd = open(_PATH_DEV_SYSMON, O_RDWR)) == -1) {
    502 		error = errno;
    503 		warnx("%s", strerror(errno));
    504 		goto out;
    505 	}
    506 
    507 	/* all done? send our dictionary now */
    508 	error = prop_dictionary_send_ioctl(udict, fd, ENVSYS_SETDICTIONARY);
    509 
    510 	if (error)
    511 		(void)printf("%s: %s\n", getprogname(), strerror(error));
    512 out:
    513 	prop_object_release(udict);
    514 	return error;
    515 }
    516 
    517 static int
    518 parse_dictionary(int fd)
    519 {
    520 	prop_array_t array;
    521 	prop_dictionary_t dict;
    522 	prop_object_iterator_t iter;
    523 	prop_object_t obj;
    524 	const char *dnp = NULL;
    525 	int rval = 0;
    526 
    527 	/* receive dictionary from kernel */
    528 	rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
    529 	if (rval)
    530 		return rval;
    531 
    532 	if (prop_dictionary_count(dict) == 0) {
    533 		warnx("no drivers registered");
    534 		goto out;
    535 	}
    536 
    537 	if (mydevname) {
    538 		obj = prop_dictionary_get(dict, mydevname);
    539 		if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
    540 			warnx("unknown device `%s'", mydevname);
    541 			rval = EINVAL;
    542 			goto out;
    543 		}
    544 
    545 		rval = find_sensors(obj);
    546 		if (rval)
    547 			goto out;
    548 	} else {
    549 		iter = prop_dictionary_iterator(dict);
    550 		if (iter == NULL) {
    551 			rval = EINVAL;
    552 			goto out;
    553 		}
    554 
    555 		/* iterate over the dictionary returned by the kernel */
    556 		while ((obj = prop_object_iterator_next(iter)) != NULL) {
    557 
    558 			array = prop_dictionary_get_keysym(dict, obj);
    559 			if (prop_object_type(array) != PROP_TYPE_ARRAY) {
    560 				warnx("no sensors found");
    561 				rval = EINVAL;
    562 				goto out;
    563 			}
    564 
    565 			dnp = prop_dictionary_keysym_cstring_nocopy(obj);
    566 
    567 			if (flags & ENVSYS_DFLAG) {
    568 				(void)printf("%s\n", dnp);
    569 			} else {
    570 				rval = find_sensors(array);
    571 				if (rval)
    572 					goto out;
    573 			}
    574 		}
    575 
    576 		prop_object_iterator_release(iter);
    577 	}
    578 
    579 	if (userreq == NULL)
    580 		if ((flags & ENVSYS_LFLAG) == 0)
    581 			print_sensors(gesen, gnelems);
    582 
    583 	if (interval)
    584 		(void)printf("\n");
    585 
    586 out:
    587 	if (gesen) {
    588 		free(gesen);
    589 		gesen = NULL;
    590 		gnelems = 0;
    591 		newsize = 0;
    592 	}
    593 	prop_object_release(dict);
    594 	return rval;
    595 }
    596 
    597 static int
    598 find_sensors(prop_array_t array)
    599 {
    600 	prop_object_iterator_t iter;
    601 	prop_object_t obj, obj1;
    602 	prop_string_t state, desc = NULL;
    603 	struct envsys_sensor *esen = NULL;
    604 	int rval = 0;
    605 	size_t oldsize;
    606 	char *str = NULL;
    607 
    608 	oldsize = newsize;
    609 	newsize += prop_array_count(array) * sizeof(*gesen);
    610 	esen = realloc(gesen, newsize);
    611 	if (esen == NULL) {
    612 		if (gesen)
    613 			free(gesen);
    614 		gesen = NULL;
    615 		rval = ENOMEM;
    616 		goto out;
    617 	}
    618 	gesen = esen;
    619 
    620 	iter = prop_array_iterator(array);
    621 	if (iter == NULL) {
    622 		rval = EINVAL;
    623 		goto out;
    624 	}
    625 
    626 	/* iterate over the array of dictionaries */
    627 	while ((obj = prop_object_iterator_next(iter)) != NULL) {
    628 
    629 		gesen[gnelems].visible = false;
    630 
    631 		/* check sensor's state */
    632 		state = prop_dictionary_get(obj, "state");
    633 
    634 		/* mark invalid sensors */
    635 		if (prop_string_equals_cstring(state, "invalid"))
    636 			gesen[gnelems].invalid = true;
    637 		else
    638 			gesen[gnelems].invalid = false;
    639 
    640 		/* description string */
    641 		desc = prop_dictionary_get(obj, "description");
    642 		if (desc != NULL) {
    643 			/* copy description */
    644 			(void)strlcpy(gesen[gnelems].desc,
    645 			    prop_string_cstring_nocopy(desc),
    646 		    	    sizeof(gesen[gnelems].desc));
    647 		} else
    648 			continue;
    649 
    650 		/* type string */
    651 		obj1  = prop_dictionary_get(obj, "type");
    652 		/* copy type */
    653 		(void)strlcpy(gesen[gnelems].type,
    654 		    prop_string_cstring_nocopy(obj1),
    655 		    sizeof(gesen[gnelems].type));
    656 
    657 		/* get current drive state string */
    658 		obj1 = prop_dictionary_get(obj, "drive-state");
    659 		if (obj1 != NULL)
    660 			(void)strlcpy(gesen[gnelems].drvstate,
    661 			    prop_string_cstring_nocopy(obj1),
    662 			    sizeof(gesen[gnelems].drvstate));
    663 
    664 		/* get current generic state string */
    665 		obj1 = prop_dictionary_get(obj, "generic-state-string");
    666 		if (obj1 != NULL)
    667 			(void)strlcpy(gesen[gnelems].genstr,
    668 			    prop_string_cstring_nocopy(obj1),
    669 			    sizeof(gesen[gnelems].genstr));
    670 
    671 		/* get current value */
    672 		obj1 = prop_dictionary_get(obj, "cur-value");
    673 		gesen[gnelems].cur_value = prop_number_integer_value(obj1);
    674 
    675 		/* get max value */
    676 		obj1 = prop_dictionary_get(obj, "max-value");
    677 		if (obj1 != NULL)
    678 			gesen[gnelems].max_value =
    679 			    prop_number_integer_value(obj1);
    680 		else
    681 			gesen[gnelems].max_value = 0;
    682 
    683 		/* get min value */
    684 		obj1 = prop_dictionary_get(obj, "min-value");
    685 		if (obj1 != NULL)
    686 			gesen[gnelems].min_value =
    687 			    prop_number_integer_value(obj1);
    688 		else
    689 			gesen[gnelems].min_value = 0;
    690 
    691 		/* get avg value */
    692 		obj1 = prop_dictionary_get(obj, "avg-value");
    693 		if (obj1 != NULL)
    694 			gesen[gnelems].avg_value =
    695 			    prop_number_integer_value(obj1);
    696 		else
    697 			gesen[gnelems].avg_value = 0;
    698 
    699 		/* get percentage flag */
    700 		obj1 = prop_dictionary_get(obj, "want-percentage");
    701 		if (obj1 != NULL)
    702 			gesen[gnelems].percentage = prop_bool_true(obj1);
    703 
    704 		/* get critical max value if available */
    705 		obj1 = prop_dictionary_get(obj, "critical-max-limit");
    706 		if (obj1 != NULL) {
    707 			gesen[gnelems].critmax_value =
    708 			    prop_number_integer_value(obj1);
    709 		} else
    710 			gesen[gnelems].critmax_value = 0;
    711 
    712 		/* get critical min value if available */
    713 		obj1 = prop_dictionary_get(obj, "critical-min-limit");
    714 		if (obj1 != NULL) {
    715 			gesen[gnelems].critmin_value =
    716 			    prop_number_integer_value(obj1);
    717 		} else
    718 			gesen[gnelems].critmin_value = 0;
    719 
    720 		/* get critical capacity value if available */
    721 		obj1 = prop_dictionary_get(obj, "critical-capacity");
    722 		if (obj1 != NULL) {
    723 			gesen[gnelems].critcap_value =
    724 			    prop_number_integer_value(obj1);
    725 		} else
    726 			gesen[gnelems].critcap_value = 0;
    727 
    728 		/* pass to the next struct and increase the counter */
    729 		gnelems++;
    730 
    731 		/* print sensor names if -l was given */
    732 		if (flags & ENVSYS_LFLAG) {
    733 			if (width)
    734 				(void)printf("%*s\n", width,
    735 				    prop_string_cstring_nocopy(desc));
    736 			else
    737 				(void)printf("%s\n",
    738 				    prop_string_cstring_nocopy(desc));
    739 		}
    740 	}
    741 
    742 	/* free memory */
    743 	prop_object_iterator_release(iter);
    744 
    745 	/*
    746 	 * if -s was specified, we need a way to mark if a sensor
    747 	 * was found.
    748 	 */
    749 	if (sensors) {
    750 		str = strdup(sensors);
    751 		if (str == NULL)
    752 			return ENOMEM;
    753 
    754 		rval = check_sensors(gesen, str, gnelems);
    755 		if (rval)
    756 			goto out;
    757 	}
    758 
    759 out:
    760 	if (str)
    761 		free(str);
    762 	return rval;
    763 }
    764 
    765 static int
    766 check_sensors(struct envsys_sensor *es, char *str, size_t nelems)
    767 {
    768 	int i;
    769 	char *sname;
    770 
    771 	sname = strtok(str, ",");
    772 	while (sname != NULL) {
    773 		for (i = 0; i < nelems; i++) {
    774 			if (strcmp(sname, es[i].desc) == 0) {
    775 				es[i].visible = true;
    776 				break;
    777 			}
    778 		}
    779 		if (i >= nelems) {
    780 			if (mydevname) {
    781 				warnx("unknown sensor `%s' for device `%s'",
    782 				    sname, mydevname);
    783 				return EINVAL;
    784 			} else {
    785 				warnx("unknown sensor `%s'", sname);
    786 				return EINVAL;
    787 			}
    788 		}
    789 		sname = strtok(NULL, ",");
    790 	}
    791 
    792 	/* check if all sensors were ok, and error out if not */
    793 	for (i = 0; i < nelems; i++) {
    794 		if (es[i].visible)
    795 			return 0;
    796 	}
    797 
    798 	warnx("no sensors selected to display");
    799 	return EINVAL;
    800 }
    801 
    802 static void
    803 print_sensors(struct envsys_sensor *es, size_t nelems)
    804 {
    805 	size_t maxlen = 0;
    806 	double temp = 0;
    807 	const char *invalid = "N/A";
    808 	const char *degrees = NULL;
    809 	int i;
    810 
    811 	/* find the longest description */
    812 	for (i = 0; i < nelems; i++) {
    813 		if (strlen(es[i].desc) > maxlen)
    814 			maxlen = strlen(es[i].desc);
    815 	}
    816 
    817 	if (width)
    818 		maxlen = width;
    819 
    820 	/* print the sensors */
    821 	for (i = 0; i < nelems; i++) {
    822 
    823 		/* skip sensors that were not marked as visible */
    824 		if (sensors && !es[i].visible)
    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, "Generic string") == 0) {
    835 
    836 			(void)printf(": %10s", es[i].genstr);
    837 
    838 		} else 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 		/* everything else */
    901 		} else {
    902 			const char *type;
    903 
    904 			if (strcmp(es[i].type, "Voltage DC") == 0)
    905 				type = "V";
    906 			else if (strcmp(es[i].type, "Voltage AC") == 0)
    907 				type = "VAC";
    908 			else if (strcmp(es[i].type, "Ampere") == 0)
    909 				type = "A";
    910 			else if (strcmp(es[i].type, "Watts") == 0)
    911 				type = "W";
    912 			else if (strcmp(es[i].type, "Ohms") == 0)
    913 				type = "Ohms";
    914 			else if (strcmp(es[i].type, "Watt hour") == 0)
    915 				type = "Wh";
    916 			else if (strcmp(es[i].type, "Ampere hour") == 0)
    917 				type = "Ah";
    918 			else
    919 				type = NULL;
    920 
    921 			(void)printf(": %10.3f %s",
    922 			    es[i].cur_value / 1000000.0, type);
    923 
    924 			if (es[i].percentage && es[i].max_value) {
    925 				(void)printf(" (%5.2f%%)",
    926 				    (es[i].cur_value * 100.0) /
    927 				    es[i].max_value);
    928 			}
    929 
    930 			if (es[i].critcap_value) {
    931 				(void)printf(" critical (%5.2f%%)",
    932 				    (es[i].critcap_value * 100.0) /
    933 				    es[i].max_value);
    934 			}
    935 
    936 			if (es[i].critmax_value || es[i].critmin_value)
    937 				(void)printf("     ");
    938 			if (es[i].critmax_value)
    939 				(void)printf("max: %8.3f %s     ",
    940 				    es[i].critmax_value / 1000000.0,
    941 				    type);
    942 			if (es[i].critmin_value)
    943 				(void)printf("min: %8.3f %s",
    944 				    es[i].critmin_value / 1000000.0,
    945 				    type);
    946 
    947 		}
    948 
    949 		(void)printf("\n");
    950 	}
    951 }
    952 
    953 static int
    954 usage(void)
    955 {
    956 	(void)fprintf(stderr, "Usage: %s [-Dflrx] ", getprogname());
    957 	(void)fprintf(stderr, "[-m ...] [-s s1,s2 ] [-w num] ");
    958 	(void)fprintf(stderr, "[-i num] [-d ...]\n");
    959 	exit(EXIT_FAILURE);
    960 	/* NOTREACHED */
    961 }
    962