Home | History | Annotate | Line # | Download | only in envstat
envstat.c revision 1.29
      1 /* $NetBSD: envstat.c,v 1.29 2007/07/05 14:08: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 
     64 /*
     65  * Operation flags for -m.
     66  */
     67 #define USERF_SCRITICAL 0x00000001	/* set a critical limit */
     68 #define USERF_RCRITICAL 0x00000002	/* remove a critical limit */
     69 #define USERF_SCRITMAX	0x00000004	/* set a critical max limit */
     70 #define USERF_RCRITMAX	0x00000008	/* remove a critical max limit */
     71 #define USERF_SCRITMIN	0x00000010	/* set a critical min limit */
     72 #define USERF_RCRITMIN	0x00000020	/* remove a critical min limit */
     73 #define USERF_SRFACT	0x00000040	/* set a new rfact */
     74 #define USERF_SDESCR	0x00000080	/* set a new description */
     75 
     76 struct envsys_sensor {
     77 	bool	invalid;
     78 	bool	visible;
     79 	bool	percentage;
     80 	int32_t	cur_value;
     81 	int32_t	max_value;
     82 	int32_t	min_value;
     83 	int32_t	avg_value;
     84 	int32_t critcap_value;
     85 	int32_t	critmin_value;
     86 	int32_t	critmax_value;
     87 	char	desc[ENVSYS_DESCLEN];
     88 	char	type[ENVSYS_DESCLEN];
     89 	char	drvstate[ENVSYS_DESCLEN];
     90 };
     91 
     92 static int interval, flags, width;
     93 static char *mydevname, *sensors, *userreq;
     94 static struct envsys_sensor *gesen;
     95 static size_t gnelems;
     96 
     97 static int parse_dictionary(int);
     98 static int send_dictionary(int);
     99 static int find_sensors(prop_array_t);
    100 static void print_sensors(struct envsys_sensor *, size_t);
    101 static int check_sensors(struct envsys_sensor *, char *, size_t);
    102 static int usage(void);
    103 
    104 
    105 int main(int argc, char **argv)
    106 {
    107 	prop_dictionary_t dict;
    108 	int c, fd, rval;
    109 	char *endptr;
    110 
    111 	rval = flags = interval = width = 0;
    112 
    113 	setprogname(argv[0]);
    114 
    115 	while ((c = getopt(argc, argv, "Dd:fi:lm:s:w:x")) != -1) {
    116 		switch (c) {
    117 		case 'd':	/* show sensors of a specific device */
    118 			mydevname = strdup(optarg);
    119 			if (mydevname == NULL)
    120 				err(ENOMEM, "out of memory");
    121 			break;
    122 		case 'i':	/* wait time between intervals */
    123 			interval = strtoul(optarg, &endptr, 10);
    124 			if (*endptr != '\0')
    125 				errx(1, "interval must be an integer");
    126 			break;
    127 		case 'D':	/* list registered devices */
    128 			flags |= ENVSYS_DFLAG;
    129 			break;
    130 		case 'f':	/* display temperature in Farenheit */
    131 			flags |= ENVSYS_FFLAG;
    132 			break;
    133 		case 'l':	/* list sensors */
    134 			flags |= ENVSYS_LFLAG;
    135 			break;
    136 		case 'w':	/* width value for the lines */
    137 			width = strtoul(optarg, &endptr, 10);
    138 			if (*endptr != '\0')
    139 				errx(1, "width must be an integer");
    140 			break;
    141 		case 'x':	/* print the dictionary in raw format */
    142 			flags |= ENVSYS_XFLAG;
    143 			break;
    144 		case 's':	/* only show specified sensors */
    145 			sensors = strdup(optarg);
    146 			if (sensors == NULL)
    147 				err(ENOMEM, "out of memory");
    148 			break;
    149 		case 'm':
    150 			userreq = strdup(optarg);
    151 			if (userreq == NULL)
    152 				err(ENOMEM, "out of memory");
    153 			break;
    154 		case '?':
    155 		default:
    156 			usage();
    157 			/* NOTREACHED */
    158 		}
    159 	}
    160 
    161 	if ((fd = open(_PATH_DEV_SYSMON, O_RDONLY)) == -1)
    162 		err(EXIT_FAILURE, "open");
    163 
    164 	if (!interval && (flags & ENVSYS_XFLAG)) {
    165 		if (prop_dictionary_recv_ioctl(fd,
    166 		    			       ENVSYS_GETDICTIONARY,
    167 					       &dict)) {
    168 			(void)close(fd);
    169 			err(EINVAL, "recv_ioctl");
    170 		}
    171 	}
    172 
    173 	if (argc == 1) {
    174 		rval = parse_dictionary(fd);
    175 
    176 	} else if (userreq) {
    177 		if (!sensors || !mydevname) {
    178 			(void)fprintf(stderr, "%s: -m cannot be used without "
    179 			    "-s and -d\n", getprogname());
    180 			return EINVAL;
    181 		}
    182 
    183 		rval = send_dictionary(fd);
    184 		goto out;
    185 
    186 	} else if (interval) {
    187 		for (;;) {
    188 			rval = parse_dictionary(fd);
    189 			if (rval)
    190 				goto out;
    191 			(void)fflush(stdout);
    192 			(void)sleep(interval);
    193 		}
    194 
    195 	} else if (!interval) {
    196 		if (flags & ENVSYS_XFLAG)
    197 			(void)printf("%s", prop_dictionary_externalize(dict));
    198 		else
    199 			rval = parse_dictionary(fd);
    200 
    201 	} else
    202 		usage();
    203 
    204 out:
    205 	if (sensors)
    206 		free(sensors);
    207 	if (userreq)
    208 		free(userreq);
    209 	if (mydevname)
    210 		free(mydevname);
    211 	(void)close(fd);
    212 	return rval;
    213 }
    214 
    215 static int
    216 send_dictionary(int fd)
    217 {
    218 	prop_dictionary_t dict, udict;
    219 	prop_object_t obj;
    220 	char *buf, *target, *endptr;
    221 	int error, i, uflag;
    222 	double val;
    223 
    224 	error = uflag = val = 0;
    225 
    226 	/*
    227 	 * part 1: kernel dictionary.
    228 	 *
    229 	 * This parts consists in parsing the kernel dictionary
    230 	 * to check for unknown device or sensor and we must
    231 	 * know what type of sensor are we trying to set
    232 	 * a critical condition.
    233 	 */
    234 	if (prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict))
    235 		return EINVAL;
    236 
    237 	if (mydevname) {
    238 		obj = prop_dictionary_get(dict, mydevname);
    239 		if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
    240 			warnx("unknown device `%s'", mydevname);
    241 			prop_object_release(dict);
    242 			return EINVAL;
    243 		}
    244 
    245 		if (find_sensors(obj)) {
    246 			prop_object_release(dict);
    247 			return EINVAL;
    248 		}
    249 	}
    250 
    251 	/* find the type for selected sensor */
    252 	for (i = 0; i < gnelems; i++)
    253 		if (strcmp(sensors, gesen[i].desc) == 0)
    254 			break;
    255 
    256 	/* we know the type of the sensor now, release kernel dict */
    257 	prop_object_release(dict);
    258 
    259 	/*
    260 	 * part 2: userland dictionary.
    261 	 *
    262 	 * This parts consists in setting the values provided
    263 	 * by the user and convert when necesssary to send
    264 	 * them to the kernel again.
    265 	 */
    266 	udict = prop_dictionary_create();
    267 
    268 	/* create the driver-name object */
    269 	obj = prop_string_create_cstring_nocopy(mydevname);
    270 	if (obj == NULL || !prop_dictionary_set(udict, "driver-name", obj)) {
    271 		error = EINVAL;
    272 		goto out;
    273 	}
    274 
    275 	prop_object_release(obj);
    276 
    277 	/* create the sensor-name object */
    278 	obj = prop_string_create_cstring_nocopy(sensors);
    279 	if (obj == NULL || !prop_dictionary_set(udict, "sensor-name", obj)) {
    280 		error = EINVAL;
    281 		goto out;
    282 	}
    283 
    284 	prop_object_release(obj);
    285 	/*
    286 	 * parse the -m argument; we understand the following ways:
    287 	 *
    288 	 * 	-m critical/crit{max,min}=value
    289 	 * 	-m critical/crit{max,min}=remove
    290 	 * 	-m desc="BLAH"
    291 	 * 	-m rfact=value
    292 	 */
    293 	if (userreq) {
    294 		buf = strtok(userreq, "=");
    295 		target = strdup(buf);
    296 		if (target == NULL) {
    297 			error = ENOMEM;
    298 			goto out;
    299 		}
    300 
    301 		while (buf != NULL) {
    302 			/*
    303 			 * skip current string if it's the same
    304 			 * than target requested.
    305 			 */
    306 			if (strcmp(target, buf) == 0)
    307 				buf = strtok(NULL, "=");
    308 
    309 			/* check what target was requested */
    310 			if (strcmp(target, "desc") == 0) {
    311 				uflag |= USERF_SDESCR;
    312 				obj = prop_string_create_cstring_nocopy(buf);
    313 				break;
    314 #define SETNCHECKVAL(a, b)						\
    315 do {									\
    316 	if (strcmp(buf, "remove") == 0)					\
    317 		uflag |= (a);						\
    318 	else {								\
    319 		uflag |= (b);						\
    320 		val = strtod(buf, &endptr);				\
    321 		if (*endptr != '\0') {					\
    322 			(void)printf("%s: invalid value\n",		\
    323 			    getprogname());				\
    324 			error = EINVAL;					\
    325 			goto out;					\
    326 		}							\
    327 	}								\
    328 } while (/* CONSTCOND */ 0)
    329 
    330 			} else if (strcmp(target, "critical") == 0) {
    331 				SETNCHECKVAL(USERF_RCRITICAL, USERF_SCRITICAL);
    332 				break;
    333 			} else if (strcmp(target, "critmax") == 0) {
    334 				SETNCHECKVAL(USERF_RCRITMAX, USERF_SCRITMAX);
    335 				break;
    336 			} else if (strcmp(target, "critmin") == 0) {
    337 				SETNCHECKVAL(USERF_RCRITMIN, USERF_SCRITMIN);
    338 				break;
    339 			} else if (strcmp(target, "rfact") == 0) {
    340 				uflag |= USERF_SRFACT;
    341 				val = strtod(buf, &endptr);
    342 				if (*endptr != '\0') {
    343 					(void)printf("%s: invalid value\n",
    344 					    getprogname());
    345 					error = EINVAL;
    346 					goto out;
    347 				}
    348 				break;
    349 			} else {
    350 				(void)printf("%s: invalid target\n",
    351 				    getprogname());
    352 				error =  EINVAL;
    353 				goto out;
    354 			}
    355 		}
    356 		free(target);
    357 	}
    358 
    359 	/* critical capacity for percentage sensors */
    360 	if (uflag & USERF_SCRITICAL) {
    361 		/* sanity check */
    362 		if (val < 0 || val > 100) {
    363 			(void)printf("%s: invalid value (0><100)\n",
    364 			    getprogname());
    365 			error = EINVAL;
    366 			goto out;
    367 		}
    368 
    369 		/* ok... convert the value */
    370 		val = (val / 100) * gesen[i].max_value;
    371 		obj = prop_number_create_unsigned_integer(val);
    372 	}
    373 
    374 	/*
    375 	 * conversions required to send a proper value to the kernel.
    376 	 */
    377 	if ((uflag & USERF_SCRITMAX) || (uflag & USERF_SCRITMIN)) {
    378 		/* temperatures */
    379 		if (strcmp(gesen[i].type, "Temperature") == 0) {
    380 			/* convert from farenheit to celsius */
    381 			if (flags & ENVSYS_FFLAG)
    382 				val = (val - 32.0) * (5.0 / 9.0);
    383 
    384 			/* convert to microKelvin */
    385 			val = val * 1000000 + 273150000;
    386 			/* printf("val=%d\n", (int)val); */
    387 			obj = prop_number_create_unsigned_integer(val);
    388 		/* fans */
    389 		} else if (strcmp(gesen[i].type, "Fan") == 0) {
    390 			if (val < 0 || val > 10000) {
    391 				error = EINVAL;
    392 				goto out;
    393 			}
    394 			/* printf("val=%d\n", (int)val); */
    395 			obj = prop_number_create_unsigned_integer(val);
    396 
    397 		/* volts, watts, ohms, etc */
    398 		} else {
    399 			/* convert to m[V,W,Ohms] again */
    400 			val *= 1000000.0;
    401 			/* printf("val=%5.0f\n", val); */
    402 			obj = prop_number_create_integer(val);
    403 		}
    404 	}
    405 
    406 	/* user wanted to set a new description */
    407 	if (uflag & USERF_SDESCR) {
    408 		if (!prop_dictionary_set(udict, "new-description", obj)) {
    409 			error = EINVAL;
    410 			goto out;
    411 		}
    412 
    413 	/* user wanted to set a new critical capacity */
    414 	} else if (uflag & USERF_SCRITICAL) {
    415 		if (!prop_dictionary_set(udict, "critical-capacity", obj)) {
    416 			error = EINVAL;
    417 			goto out;
    418 		}
    419 
    420 	} else if (uflag & USERF_RCRITICAL) {
    421 		obj = prop_bool_create(1);
    422 		if (!prop_dictionary_set(udict, "remove-critical-cap", obj)) {
    423 			error = EINVAL;
    424 			goto out;
    425 		}
    426 
    427 	/* user wanted to remove a critical min limit */
    428 	} else if (uflag & USERF_RCRITMIN) {
    429 		obj = prop_bool_create(1);
    430 		if (!prop_dictionary_set(udict, "remove-cmin-limit", obj)) {
    431 			error = EINVAL;
    432 			goto out;
    433 		}
    434 
    435 	/* user wanted to remove a critical max limit */
    436 	} else if (uflag & USERF_RCRITMAX) {
    437 		obj = prop_bool_create(1);
    438 		if (!prop_dictionary_set(udict, "remove-cmax-limit", obj)) {
    439 			error = EINVAL;
    440 			goto out;
    441 		}
    442 
    443 	/* user wanted to set a new critical min value */
    444 	} else if (uflag & USERF_SCRITMIN) {
    445 		if (!prop_dictionary_set(udict, "critical-min-limit", obj)) {
    446 			error = EINVAL;
    447 			goto out;
    448 		}
    449 
    450 	/* user wanted to set a new critical max value */
    451 	} else if (uflag & USERF_SCRITMAX) {
    452 		if (!prop_dictionary_set(udict, "critical-max-limit", obj)) {
    453 			error = EINVAL;
    454 			goto out;
    455 		}
    456 
    457 	/* user wanted to set a new rfact */
    458 	} else if (uflag & USERF_SRFACT) {
    459 		obj = prop_number_create_integer(val);
    460 		if (!prop_dictionary_set(udict, "new-rfact", obj)) {
    461 			error = EINVAL;
    462 			goto out;
    463 		}
    464 
    465 	} else {
    466 		(void)printf("%s: unknown operation\n", getprogname());
    467 		error = EINVAL;
    468 		goto out;
    469 	}
    470 
    471 	prop_object_release(obj);
    472 
    473 #if 0
    474 	printf("%s", prop_dictionary_externalize(udict));
    475 	return error;
    476 #endif
    477 
    478 	/* all done? send our dictionary now */
    479 	error = prop_dictionary_send_ioctl(udict, fd, ENVSYS_SETDICTIONARY);
    480 
    481 	if (error)
    482 		(void)printf("%s: %s\n", getprogname(), strerror(error));
    483 out:
    484 	prop_object_release(udict);
    485 	return error;
    486 }
    487 
    488 static int
    489 parse_dictionary(int fd)
    490 {
    491 	prop_array_t array;
    492 	prop_dictionary_t dict;
    493 	prop_object_iterator_t iter;
    494 	prop_object_t obj;
    495 	const char *dnp = NULL;
    496 	int rval = 0;
    497 
    498 	/* receive dictionary from kernel */
    499 	if (prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict))
    500 		return EINVAL;
    501 
    502 	if (mydevname) {
    503 		obj = prop_dictionary_get(dict, mydevname);
    504 		if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
    505 			warnx("unknown device `%s'", mydevname);
    506 			rval = EINVAL;
    507 			goto out;
    508 		}
    509 
    510 		rval = find_sensors(obj);
    511 		if (rval)
    512 			goto out;
    513 
    514 		if (interval)
    515 			(void)printf("\n");
    516 
    517 	} else {
    518 		iter = prop_dictionary_iterator(dict);
    519 		if (iter == NULL) {
    520 			rval = EINVAL;
    521 			goto out;
    522 		}
    523 
    524 		/* iterate over the dictionary returned by the kernel */
    525 		while ((obj = prop_object_iterator_next(iter)) != NULL) {
    526 
    527 			array = prop_dictionary_get_keysym(dict, obj);
    528 			if (prop_object_type(array) != PROP_TYPE_ARRAY) {
    529 				warnx("no sensors found");
    530 				rval = EINVAL;
    531 				goto out;
    532 			}
    533 
    534 			dnp = prop_dictionary_keysym_cstring_nocopy(obj);
    535 
    536 			if (flags & ENVSYS_DFLAG) {
    537 				(void)printf("%s", dnp);
    538 			} else {
    539 				rval = find_sensors(array);
    540 				if (rval)
    541 					goto out;
    542 			}
    543 			if (prop_dictionary_count(dict) > 1 || interval)
    544 				(void)printf("\n");
    545 		}
    546 		prop_object_iterator_release(iter);
    547 		if ((flags & ENVSYS_DFLAG) && prop_dictionary_count(dict) == 1)
    548 			(void)printf("\n");
    549 	}
    550 
    551 out:
    552 	prop_object_release(dict);
    553 	return rval;
    554 }
    555 
    556 static int
    557 find_sensors(prop_array_t array)
    558 {
    559 	prop_object_iterator_t iter;
    560 	prop_object_t obj, obj1;
    561 	prop_string_t state, desc = NULL;
    562 	struct envsys_sensor *esen;
    563 	int i, rval;
    564 	size_t nelems = 0;
    565 	char *str = NULL;
    566 
    567 	i = rval = 0;
    568 
    569 	esen = (struct envsys_sensor *)calloc(prop_array_count(array),
    570 	    sizeof(struct envsys_sensor));
    571 	if (esen == NULL)
    572 		return ENOMEM;
    573 
    574 	iter = prop_array_iterator(array);
    575 	if (iter == NULL) {
    576 		rval = EINVAL;
    577 		goto out;
    578 	}
    579 
    580 	/* iterate over the array of dictionaries */
    581 	while ((obj = prop_object_iterator_next(iter)) != NULL) {
    582 		/* check sensor's state */
    583 		state = prop_dictionary_get(obj, "state");
    584 
    585 		/* mark invalid sensors */
    586 		if (prop_string_equals_cstring(state, "invalid"))
    587 			esen[i].invalid = true;
    588 
    589 		/* description string */
    590 		desc = prop_dictionary_get(obj, "description");
    591 		/* copy description */
    592 		(void)strlcpy(esen[i].desc, prop_string_cstring_nocopy(desc),
    593 		    sizeof(esen[i].desc));
    594 
    595 		/* type string */
    596 		obj1  = prop_dictionary_get(obj, "type");
    597 		/* copy type */
    598 		(void)strlcpy(esen[i].type, prop_string_cstring_nocopy(obj1),
    599 		    sizeof(esen[i].type));
    600 
    601 		/* get current drive state string */
    602 		obj1 = prop_dictionary_get(obj, "drive-state");
    603 		if (obj1 != NULL)
    604 			(void)strlcpy(esen[i].drvstate,
    605 			    prop_string_cstring_nocopy(obj1),
    606 			    sizeof(esen[i].drvstate));
    607 
    608 		/* get current value */
    609 		obj1 = prop_dictionary_get(obj, "cur-value");
    610 		esen[i].cur_value = prop_number_integer_value(obj1);
    611 
    612 		/* get max value */
    613 		obj1 = prop_dictionary_get(obj, "max-value");
    614 		if (obj1 != NULL)
    615 			esen[i].max_value = prop_number_integer_value(obj1);
    616 
    617 		/* get min value */
    618 		obj1 = prop_dictionary_get(obj, "min-value");
    619 		if (obj1 != NULL)
    620 			esen[i].min_value = prop_number_integer_value(obj1);
    621 
    622 		/* get avg value */
    623 		obj1 = prop_dictionary_get(obj, "avg-value");
    624 		if (obj1 != NULL)
    625 			esen[i].avg_value = prop_number_integer_value(obj1);
    626 
    627 		/* get percentage flag */
    628 		obj1 = prop_dictionary_get(obj, "want-percentage");
    629 		if (obj1 != NULL)
    630 			esen[i].percentage = prop_bool_true(obj1);
    631 
    632 		/* get critical max value if available */
    633 		obj1 = prop_dictionary_get(obj, "critical-max-limit");
    634 		if (obj1 != NULL) {
    635 			esen[i].critmax_value =
    636 			    prop_number_integer_value(obj1);
    637 		}
    638 
    639 		/* get critical min value if available */
    640 		obj1 = prop_dictionary_get(obj, "critical-min-limit");
    641 		if (obj1 != NULL) {
    642 			esen[i].critmin_value =
    643 			    prop_number_integer_value(obj1);
    644 		}
    645 
    646 		/* get critical capacity value if available */
    647 		obj1 = prop_dictionary_get(obj, "critical-capacity");
    648 		if (obj1 != NULL) {
    649 			esen[i].critcap_value =
    650 			    prop_number_integer_value(obj1);
    651 		}
    652 
    653 		/* pass to the next struct */
    654 		i++;
    655 		nelems++;
    656 
    657 		/* print sensor names if -l was given */
    658 		if (flags & ENVSYS_LFLAG) {
    659 			if (width)
    660 				(void)printf("%*s\n", width,
    661 				    prop_string_cstring_nocopy(desc));
    662 			else
    663 				(void)printf("%s\n",
    664 				    prop_string_cstring_nocopy(desc));
    665 		}
    666 	}
    667 
    668 	/* free memory */
    669 	prop_object_iterator_release(iter);
    670 
    671 	/*
    672 	 * if -s was specified, we need a way to mark if a sensor
    673 	 * was found.
    674 	 */
    675 	if (sensors) {
    676 		str = strdup(sensors);
    677 		if (str == NULL)
    678 			return ENOMEM;
    679 
    680 		rval = check_sensors(esen, str, nelems);
    681 		if (rval)
    682 			goto out;
    683 
    684 		free(str);
    685 	}
    686 
    687 	gesen = esen;
    688 	gnelems = nelems;
    689 
    690 	if (userreq == NULL) {
    691 		if ((flags & ENVSYS_LFLAG) == 0)
    692 			print_sensors(esen, nelems);
    693 	}
    694 
    695 out:
    696 	free(esen);
    697 	return rval;
    698 }
    699 
    700 static int
    701 check_sensors(struct envsys_sensor *es, char *str, size_t nelems)
    702 {
    703 	int i;
    704 	char *sname;
    705 
    706 	sname = strtok(str, ",");
    707 	while (sname != NULL) {
    708 		for (i = 0; i < nelems; i++) {
    709 			if (strcmp(sname, es[i].desc) == 0) {
    710 				es[i].visible = true;
    711 				break;
    712 			}
    713 		}
    714 		if (i >= nelems) {
    715 			if (mydevname) {
    716 				warnx("unknown sensor `%s' for device `%s'",
    717 				    sname, mydevname);
    718 				return EINVAL;
    719 			} else {
    720 				warnx("unknown sensor `%s'", sname);
    721 				return EINVAL;
    722 			}
    723 		}
    724 		sname = strtok(NULL, ",");
    725 	}
    726 
    727 	/* check if all sensors were ok, and error out if not */
    728 	for (i = 0; i < nelems; i++) {
    729 		if (es[i].visible)
    730 			return 0;
    731 	}
    732 
    733 	warnx("no sensors selected to display");
    734 	return EINVAL;
    735 }
    736 
    737 static void
    738 print_sensors(struct envsys_sensor *es, size_t nelems)
    739 {
    740 	size_t maxlen = 0;
    741 	double temp = 0;
    742 	const char *invalid = "N/A";
    743 	const char *degrees = NULL;
    744 	int i;
    745 
    746 	/* find the longest description */
    747 	for (i = 0; i < nelems; ++i) {
    748 		if (strlen(es[i].desc) > maxlen)
    749 			maxlen = strlen(es[i].desc);
    750 	}
    751 
    752 	if (width)
    753 		maxlen = width;
    754 
    755 	/* print the sensors */
    756 	for (i = 0; i < nelems; ++i) {
    757 
    758 		/* skip sensors that were not marked as visible */
    759 		if (sensors && !es[i].visible)
    760 			continue;
    761 
    762 		/* we have a winner... */
    763 		(void)printf("%*.*s", (int)maxlen, (int)maxlen, es[i].desc);
    764 
    765 		if (es[i].invalid) {
    766 			(void)printf(": %10s\n", invalid);
    767 			continue;
    768 		}
    769 
    770 		if (strcmp(es[i].type, "Indicator") == 0) {
    771 
    772 			(void)printf(": %10s", es[i].cur_value ? "ON" : "OFF");
    773 
    774 /* converts the value to degC or degF */
    775 #define CONVERTTEMP(a, b, c)					\
    776 do {								\
    777 	(a) = ((b) / 1000000.0) - 273.15;			\
    778 	if (flags & ENVSYS_FFLAG) {				\
    779 		(a) = (9.0 / 5.0) * (a) + 32.0;			\
    780 		(c) = "degF";					\
    781 	} else							\
    782 		(c) = "degC";					\
    783 } while (/* CONSTCOND */ 0)
    784 
    785 
    786 		/* temperatures */
    787 		} else if (strcmp(es[i].type, "Temperature") == 0) {
    788 
    789 			CONVERTTEMP(temp, es[i].cur_value, degrees);
    790 			(void)printf(": %10.3f %s", temp, degrees);
    791 
    792 			if (es[i].critmax_value || es[i].critmin_value)
    793 				(void)printf("  ");
    794 
    795 			if (es[i].critmax_value) {
    796 				CONVERTTEMP(temp, es[i].critmax_value, degrees);
    797 				(void)printf("max: %8.3f %s  ", temp, degrees);
    798 			}
    799 
    800 			if (es[i].critmin_value) {
    801 				CONVERTTEMP(temp, es[i].critmin_value, degrees);
    802 				(void)printf("min: %8.3f %s", temp, degrees);
    803 			}
    804 
    805 		/* fans */
    806 		} else if (strcmp(es[i].type, "Fan") == 0) {
    807 
    808 			(void)printf(": %10u RPM", es[i].cur_value);
    809 
    810 			if (es[i].critmax_value || es[i].critmin_value)
    811 				(void)printf("   ");
    812 			if (es[i].critmax_value)
    813 				(void)printf("max: %8u RPM   ",
    814 				    es[i].critmax_value);
    815 			if (es[i].critmin_value)
    816 				(void)printf("min: %8u RPM",
    817 				    es[i].critmin_value);
    818 
    819 		/* integers */
    820 		} else if (strcmp(es[i].type, "Integer") == 0) {
    821 
    822 			(void)printf(": %10d", es[i].cur_value);
    823 
    824 		/* drives */
    825 		} else if (strcmp(es[i].type, "Drive") == 0) {
    826 
    827 			(void)printf(": %s", es[i].drvstate);
    828 
    829 		/* everything else */
    830 		} else {
    831 			const char *type;
    832 
    833 			if (strcmp(es[i].type, "Voltage DC") == 0)
    834 				type = "V";
    835 			else if (strcmp(es[i].type, "Voltage AC") == 0)
    836 				type = "VAC";
    837 			else if (strcmp(es[i].type, "Ampere") == 0)
    838 				type = "A";
    839 			else if (strcmp(es[i].type, "Watts") == 0)
    840 				type = "W";
    841 			else if (strcmp(es[i].type, "Ohms") == 0)
    842 				type = "Ohms";
    843 			else if (strcmp(es[i].type, "Watt hour") == 0)
    844 				type = "Wh";
    845 			else if (strcmp(es[i].type, "Ampere hour") == 0)
    846 				type = "Ah";
    847 			else
    848 				type = NULL;
    849 
    850 			(void)printf(": %10.3f %s",
    851 			    es[i].cur_value / 1000000.0, type);
    852 
    853 			if (es[i].percentage && es[i].max_value) {
    854 				(void)printf(" (%5.2f%%)",
    855 				    (es[i].cur_value * 100.0) /
    856 				    es[i].max_value);
    857 			}
    858 
    859 			if (es[i].critcap_value) {
    860 				(void)printf(" critical (%5.2f%%)",
    861 				    (es[i].critcap_value * 100.0) /
    862 				    es[i].max_value);
    863 			}
    864 
    865 			if (es[i].critmax_value || es[i].critmin_value)
    866 				(void)printf("     ");
    867 			if (es[i].critmax_value)
    868 				(void)printf("max: %8.3f %s     ",
    869 				    es[i].critmax_value / 1000000.0,
    870 				    type);
    871 			if (es[i].critmin_value)
    872 				(void)printf("min: %8.3f %s",
    873 				    es[i].critmin_value / 1000000.0,
    874 				    type);
    875 
    876 		}
    877 
    878 		(void)printf("\n");
    879 	}
    880 }
    881 
    882 static int
    883 usage(void)
    884 {
    885 	(void)fprintf(stderr, "Usage: %s [-Dflx] ", getprogname());
    886 	(void)fprintf(stderr, "[-m ...] [-s s1,s2 ] [-w num] ");
    887 	(void)fprintf(stderr, "[-i num] [-d ...]\n");
    888 	exit(EXIT_FAILURE);
    889 	/* NOTREACHED */
    890 }
    891