Home | History | Annotate | Line # | Download | only in envstat
envstat.c revision 1.2.4.1
      1  1.2.4.1  bouyer /*	$NetBSD: envstat.c,v 1.2.4.1 2000/07/30 17:58:24 bouyer Exp $ */
      2      1.1    groo 
      3      1.1    groo /*-
      4      1.1    groo  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5      1.1    groo  * All rights reserved.
      6      1.1    groo  *
      7      1.1    groo  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1    groo  * by Bill Squier.
      9      1.1    groo  *
     10      1.1    groo  * Redistribution and use in source and binary forms, with or without
     11      1.1    groo  * modification, are permitted provided that the following conditions
     12      1.1    groo  * are met:
     13      1.1    groo  * 1. Redistributions of source code must retain the above copyright
     14      1.1    groo  *    notice, this list of conditions and the following disclaimer.
     15      1.1    groo  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1    groo  *    notice, this list of conditions and the following disclaimer in the
     17      1.1    groo  *    documentation and/or other materials provided with the distribution.
     18      1.1    groo  * 3. All advertising materials mentioning features or use of this software
     19      1.1    groo  *    must display the following acknowledgement:
     20      1.1    groo  *        This product includes software developed by the NetBSD
     21      1.1    groo  *        Foundation, Inc. and its contributors.
     22      1.1    groo  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.1    groo  *    contributors may be used to endorse or promote products derived
     24      1.1    groo  *    from this software without specific prior written permission.
     25      1.1    groo  *
     26      1.1    groo  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.1    groo  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.1    groo  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.1    groo  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.1    groo  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.1    groo  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.1    groo  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.1    groo  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.1    groo  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.1    groo  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.1    groo  * POSSIBILITY OF SUCH DAMAGE.
     37      1.1    groo  */
     38      1.1    groo 
     39      1.1    groo #include <sys/cdefs.h>
     40      1.1    groo #ifndef lint
     41  1.2.4.1  bouyer __RCSID("$NetBSD: envstat.c,v 1.2.4.1 2000/07/30 17:58:24 bouyer Exp $");
     42      1.1    groo #endif
     43      1.1    groo 
     44      1.1    groo #include <fcntl.h>
     45      1.1    groo #include <stdio.h>
     46      1.1    groo #include <stdlib.h>
     47      1.1    groo #include <string.h>
     48      1.1    groo #include <unistd.h>
     49  1.2.4.1  bouyer #include <err.h>
     50      1.1    groo 
     51      1.1    groo #include <sys/envsys.h>
     52      1.1    groo #include <sys/ioctl.h>
     53      1.1    groo 
     54      1.1    groo extern char *__progname;
     55  1.2.4.1  bouyer const char E_CANTENUM[] = "cannot enumerate sensors";
     56  1.2.4.1  bouyer 
     57  1.2.4.1  bouyer #define	_PATH_SYSMON	"/dev/sysmon"
     58      1.1    groo 
     59      1.1    groo int main __P((int, char **));
     60      1.1    groo void listsensors __P((envsys_basic_info_t *, int));
     61      1.1    groo int numsensors __P((int));
     62      1.1    groo int fillsensors __P((int, envsys_tre_data_t *, envsys_basic_info_t *, int));
     63      1.1    groo int longestname __P((envsys_basic_info_t *, int));
     64      1.1    groo int marksensors __P((envsys_basic_info_t *, int *, char *, int));
     65      1.1    groo int strtosnum __P((envsys_basic_info_t *, const char *, int));
     66      1.1    groo void header __P((unsigned, int, envsys_basic_info_t *, const int * const,
     67      1.1    groo 		 int));
     68      1.1    groo void values __P((unsigned, int, envsys_tre_data_t *, const int * const, int));
     69      1.1    groo void usage __P((void));
     70      1.1    groo 
     71      1.1    groo 
     72      1.1    groo int
     73      1.1    groo main(argc, argv)
     74      1.1    groo 	int argc;
     75      1.1    groo 	char **argv;
     76      1.1    groo {
     77      1.1    groo 	int c, fd, ns, ls, celsius;
     78      1.1    groo 	unsigned int interval, width, headrep, headcnt;
     79      1.1    groo 	envsys_tre_data_t *etds;
     80      1.1    groo 	envsys_basic_info_t *ebis;
     81      1.1    groo 	int *cetds;
     82      1.1    groo 	char *sensors;
     83  1.2.4.1  bouyer 	const char *dev;
     84      1.1    groo 
     85      1.1    groo 	fd = -1;
     86      1.1    groo 	ls = 0;
     87      1.1    groo 	celsius = 1;
     88      1.1    groo 	interval = 0;
     89      1.1    groo 	width = 0;
     90      1.1    groo 	sensors = NULL;
     91      1.1    groo 	headrep = 22;
     92      1.1    groo 
     93      1.1    groo 	while ((c = getopt(argc, argv, "cfi:ln:s:w:")) != -1) {
     94      1.1    groo 		switch(c) {
     95      1.1    groo 		case 'i':	/* wait time between displays */
     96      1.1    groo 			interval = atoi(optarg);
     97      1.1    groo 			break;
     98      1.1    groo 		case 'w':	/* minimum column width */
     99      1.1    groo 			width = atoi(optarg);
    100      1.1    groo 			break;
    101      1.1    groo 		case 'l':	/* list sensor names */
    102      1.1    groo 			ls = 1;
    103      1.1    groo 			break;
    104      1.1    groo 		case 'n':	/* repeat header every headrep lines */
    105      1.1    groo 			headrep = atoi(optarg);
    106      1.1    groo 			break;
    107      1.1    groo 		case 's':	/* restrict display to named sensors */
    108      1.1    groo 			sensors = (char *)malloc(strlen(optarg) + 1);
    109      1.1    groo 			if (sensors == NULL)
    110      1.1    groo 				exit(1);
    111      1.1    groo 			strcpy(sensors, optarg);
    112      1.1    groo 			break;
    113      1.1    groo 		case 'f':	/* display temp in degF */
    114      1.1    groo 			celsius = 0;
    115      1.1    groo 			break;
    116      1.1    groo 		case '?':
    117      1.1    groo 		default:
    118      1.1    groo 			usage();
    119      1.1    groo 			/* NOTREACHED */
    120      1.1    groo 		}
    121      1.1    groo 	}
    122      1.1    groo 
    123  1.2.4.1  bouyer 	if (optind < argc)
    124  1.2.4.1  bouyer 		dev = argv[optind];
    125  1.2.4.1  bouyer 	else
    126  1.2.4.1  bouyer 		dev = _PATH_SYSMON;
    127      1.1    groo 
    128  1.2.4.1  bouyer 	if ((fd = open(dev, O_RDONLY)) == -1)
    129  1.2.4.1  bouyer 		err(1, "unable to open %s", dev);
    130      1.1    groo 
    131      1.1    groo 	/*
    132      1.1    groo 	 * Determine number of sensors, allocate and fill arrays with
    133      1.1    groo 	 * initial information.  Determine column width
    134      1.1    groo 	 */
    135  1.2.4.1  bouyer 	if ((ns = numsensors(fd)) <= 0)
    136  1.2.4.1  bouyer 		errx(1, E_CANTENUM);
    137  1.2.4.1  bouyer 
    138      1.1    groo 	cetds = (int *)malloc(ns * sizeof(int));
    139      1.1    groo 	etds = (envsys_tre_data_t *)malloc(ns * sizeof(envsys_tre_data_t));
    140      1.1    groo 	ebis = (envsys_basic_info_t *)malloc(ns * sizeof(envsys_basic_info_t));
    141      1.1    groo 
    142  1.2.4.1  bouyer 	if ((cetds == NULL) || (etds == NULL) || (ebis == NULL))
    143  1.2.4.1  bouyer 		errx(1, "cannot allocate memory");
    144      1.1    groo 
    145  1.2.4.1  bouyer 	if (fillsensors(fd, etds, ebis, ns) == -1)
    146  1.2.4.1  bouyer 		errx(1, E_CANTENUM);
    147      1.1    groo 
    148      1.1    groo 	if (ls) {
    149      1.1    groo 		listsensors(ebis, ns);
    150      1.1    groo 		exit(0);
    151      1.1    groo 	}
    152      1.1    groo 
    153      1.1    groo 
    154      1.1    groo #define MAX(x, y)  (((x) > (y)) ? (x) : (y))
    155      1.1    groo 	if (!width) {
    156      1.1    groo 		width = longestname(ebis, ns);
    157      1.1    groo 		width = MAX(((79 - ns) / ns), width);
    158      1.1    groo 	}
    159      1.1    groo 
    160      1.1    groo 	/* Mark which sensor numbers are to be displayed */
    161      1.1    groo 	if (marksensors(ebis, cetds, sensors, ns) == -1)
    162      1.1    groo 		exit(1);
    163      1.1    groo 
    164      1.1    groo 	/* If we didn't specify an interval value, print the sensors once */
    165      1.1    groo 	if (!interval) {
    166      1.1    groo 		if (headrep)
    167      1.1    groo 			header(width, celsius, ebis, cetds, ns);
    168      1.1    groo 		values(width, celsius, etds, cetds, ns);
    169      1.1    groo 
    170      1.1    groo 		exit (0);
    171      1.1    groo 	}
    172      1.1    groo 
    173      1.1    groo 	headcnt = 0;
    174      1.1    groo 	if (headrep)
    175      1.1    groo 		header(width, celsius, ebis, cetds, ns);
    176      1.1    groo 
    177      1.1    groo         for (;;) {
    178      1.1    groo 		values(width, celsius, etds, cetds, ns);
    179      1.1    groo 		if (headrep && (++headcnt == headrep)) {
    180      1.1    groo 			headcnt = 0;
    181      1.1    groo 			header(width, celsius, ebis, cetds, ns);
    182      1.1    groo 		}
    183      1.1    groo 
    184      1.1    groo 		sleep(interval);
    185      1.1    groo 
    186  1.2.4.1  bouyer 		if (fillsensors(fd, etds, ebis, ns) == -1)
    187  1.2.4.1  bouyer 			errx(1, E_CANTENUM);
    188      1.1    groo 	}
    189      1.1    groo 
    190      1.1    groo 	/* NOTREACHED */
    191      1.1    groo 	return (0);
    192      1.1    groo }
    193      1.1    groo 
    194      1.1    groo 
    195      1.1    groo static const char *unit_str[] = {"degC", "RPM", "VAC", "Vcc", "Ohms", "Watts",
    196      1.1    groo 				 "Amps", "Ukn"};
    197      1.1    groo 
    198      1.1    groo /*
    199      1.1    groo  * pre:  cetds[i] != 0 iff sensor i should appear in the output
    200      1.1    groo  * post: a column header line is displayed on stdout
    201      1.1    groo  */
    202      1.1    groo void
    203      1.1    groo header(width, celsius, ebis, cetds, ns)
    204      1.1    groo 	unsigned width;
    205      1.1    groo 	int celsius;
    206      1.1    groo 	envsys_basic_info_t *ebis;
    207      1.1    groo 	const int * const cetds;
    208      1.1    groo 	int ns;
    209      1.1    groo {
    210      1.1    groo 	int i;
    211      1.1    groo 	const char *s;
    212      1.1    groo 
    213      1.1    groo 	/* sensor names */
    214      1.1    groo 	for (i = 0; i < ns; ++i)
    215      1.1    groo 		if (cetds[i])
    216      1.1    groo 			printf(" %*.*s", (int)width, (int)width, ebis[i].desc);
    217      1.1    groo 
    218      1.1    groo 	printf("\n");
    219      1.1    groo 
    220      1.1    groo 	/* units */
    221      1.1    groo 	for (i = 0; i < ns; ++i)
    222      1.1    groo 		if (cetds[i]) {
    223      1.1    groo 			if ((ebis[i].units == ENVSYS_STEMP) &&
    224      1.1    groo 			    !celsius)
    225      1.1    groo 				s = "degF";
    226      1.1    groo 			else if (ebis[i].units > 6)
    227      1.1    groo 				s = unit_str[7];
    228      1.1    groo 			else
    229      1.1    groo 				s = unit_str[ebis[i].units];
    230      1.1    groo 
    231      1.1    groo 			printf(" %*.*s", (int)width, (int)width, s);
    232      1.1    groo 		}
    233      1.1    groo 	printf("\n");
    234      1.1    groo }
    235      1.1    groo 
    236      1.1    groo void
    237      1.1    groo values(width, celsius, etds, cetds, ns)
    238      1.1    groo 	unsigned width;
    239      1.1    groo 	int celsius;
    240      1.1    groo 	envsys_tre_data_t *etds;
    241      1.1    groo 	const int * const cetds;
    242      1.1    groo 	int ns;
    243      1.1    groo {
    244      1.1    groo 	int i;
    245      1.1    groo 	double temp;
    246      1.1    groo 
    247      1.1    groo 	for (i = 0; i < ns; ++i)
    248      1.1    groo 		if (cetds[i]) {
    249      1.1    groo 
    250      1.1    groo 			/* * sensors without valid data */
    251      1.1    groo 			if ((etds[i].validflags & ENVSYS_FCURVALID) == 0) {
    252      1.1    groo 				printf(" %*.*s", (int)width, (int)width, "*");
    253      1.1    groo 				continue;
    254      1.1    groo 			}
    255      1.1    groo 
    256      1.1    groo 			switch(etds[i].units) {
    257      1.1    groo 			case ENVSYS_STEMP:
    258      1.1    groo 				temp = (etds[i].cur.data_us / 1000000.0) -
    259      1.1    groo 				    273.15;
    260      1.1    groo 				if (!celsius)
    261      1.1    groo 					temp = (9.0 / 5.0) * temp + 32.0;
    262      1.1    groo 				printf(" %*.2f", width, temp);
    263      1.1    groo 				break;
    264      1.1    groo 			case ENVSYS_SFANRPM:
    265      1.1    groo 				printf(" %*u", width, etds[i].cur.data_us);
    266      1.1    groo 				break;
    267      1.1    groo 			default:
    268      1.1    groo 				printf(" %*.2f", width, etds[i].cur.data_s /
    269      1.1    groo 				       1000000.0);
    270      1.1    groo 				break;
    271      1.1    groo 			}
    272      1.1    groo 		}
    273      1.1    groo 	printf("\n");
    274      1.1    groo }
    275      1.1    groo 
    276      1.1    groo 
    277      1.1    groo /*
    278      1.1    groo  * post: displays usage on stderr
    279      1.1    groo  */
    280      1.1    groo void
    281      1.1    groo usage()
    282      1.1    groo {
    283      1.1    groo 	fprintf(stderr, "usage: %s [-c] [-s s1,s2,...]", __progname);
    284      1.1    groo 	fprintf(stderr, " [-i interval] [-n headrep] [-w width] device\n");
    285      1.1    groo 	fprintf(stderr, "       envstat -l device\n");
    286      1.1    groo 	exit(1);
    287      1.1    groo }
    288      1.1    groo 
    289      1.1    groo 
    290      1.1    groo /*
    291      1.1    groo  * post: a list of sensor names supported by the device is displayed on stdout
    292      1.1    groo  */
    293      1.1    groo void
    294      1.1    groo listsensors(ebis, ns)
    295      1.1    groo 	envsys_basic_info_t *ebis;
    296      1.1    groo 	int ns;
    297      1.1    groo {
    298      1.1    groo 	int i;
    299      1.1    groo 
    300      1.1    groo 	for (i = 0; i < ns; ++i)
    301      1.1    groo 		if (ebis[i].validflags & ENVSYS_FVALID)
    302      1.1    groo 			printf("%s\n", ebis[i].desc);
    303      1.1    groo }
    304      1.1    groo 
    305      1.1    groo 
    306      1.1    groo /*
    307      1.1    groo  * pre:  fd contains a valid file descriptor of an envsys(4) supporting device
    308      1.1    groo  * post: returns the number of valid sensors provided by the device
    309      1.1    groo  *       or -1 on error
    310      1.1    groo  */
    311      1.1    groo int
    312      1.1    groo numsensors(fd)
    313      1.1    groo 	int fd;
    314      1.1    groo {
    315      1.1    groo 	int count = 0, valid = 1;
    316      1.1    groo 	envsys_tre_data_t etd;
    317      1.1    groo 	etd.sensor = 0;
    318      1.1    groo 
    319      1.1    groo 	while (valid) {
    320      1.1    groo 		if (ioctl(fd, ENVSYS_GTREDATA, &etd) == -1) {
    321      1.1    groo 			fprintf(stderr, E_CANTENUM);
    322      1.1    groo 			exit(1);
    323      1.1    groo 		}
    324      1.1    groo 
    325      1.1    groo 		valid = etd.validflags & ENVSYS_FVALID;
    326      1.1    groo 		if (valid)
    327      1.1    groo 			++count;
    328      1.1    groo 
    329      1.1    groo 		++etd.sensor;
    330      1.1    groo 	}
    331      1.1    groo 
    332      1.1    groo 	return count;
    333      1.1    groo }
    334      1.1    groo 
    335      1.1    groo /*
    336      1.1    groo  * pre:  fd contains a valid file descriptor of an envsys(4) supporting device
    337      1.1    groo  *       && ns is the number of sensors
    338      1.1    groo  *       && etds and ebis are arrays of sufficient size
    339      1.1    groo  * post: returns 0 and etds and ebis arrays are filled with sensor info
    340      1.1    groo  *       or returns -1 on failure
    341      1.1    groo  */
    342      1.1    groo int
    343      1.1    groo fillsensors(fd, etds, ebis, ns)
    344      1.1    groo 	int fd;
    345      1.1    groo 	envsys_tre_data_t *etds;
    346      1.1    groo 	envsys_basic_info_t *ebis;
    347      1.1    groo 	int ns;
    348      1.1    groo {
    349      1.1    groo 	int i;
    350      1.1    groo 
    351      1.1    groo 	for (i = 0; i < ns; ++i) {
    352      1.1    groo 		ebis[i].sensor = i;
    353      1.1    groo 		if (ioctl(fd, ENVSYS_GTREINFO, &ebis[i]) == -1)
    354      1.1    groo 			return -1;
    355      1.1    groo 
    356      1.1    groo 		etds[i].sensor = i;
    357      1.1    groo 		if (ioctl(fd, ENVSYS_GTREDATA, &etds[i]) == -1)
    358      1.1    groo 			return -1;
    359      1.1    groo 	}
    360      1.1    groo 
    361      1.1    groo 	return 0;
    362      1.1    groo }
    363      1.1    groo 
    364      1.1    groo 
    365      1.1    groo /*
    366      1.1    groo  * post: returns the strlen() of the longest sensor name
    367      1.1    groo  */
    368      1.1    groo int
    369      1.1    groo longestname(ebis, ns)
    370      1.1    groo 	envsys_basic_info_t *ebis;
    371      1.1    groo 	int ns;
    372      1.1    groo {
    373      1.1    groo 	int i, maxlen, cur;
    374      1.1    groo 
    375      1.1    groo 	maxlen = 0;
    376      1.1    groo 
    377      1.1    groo 	for (i = 0; i < ns; ++i) {
    378      1.1    groo 		cur = strlen(ebis[i].desc);
    379      1.1    groo 		if (cur > maxlen)
    380      1.1    groo 			maxlen = cur;
    381      1.1    groo 	}
    382      1.1    groo 
    383      1.1    groo 	return maxlen;
    384      1.1    groo }
    385      1.1    groo 
    386      1.1    groo /*
    387      1.1    groo  * post: returns 0 and cetds[i] != 0 iff sensor i should appear in the output
    388      1.1    groo  *       or returns -1
    389      1.1    groo  */
    390      1.1    groo int
    391      1.1    groo marksensors(ebis, cetds, sensors, ns)
    392      1.1    groo 	envsys_basic_info_t *ebis;
    393      1.1    groo 	int *cetds;
    394      1.1    groo 	char *sensors;
    395      1.1    groo 	int ns;
    396      1.1    groo {
    397      1.1    groo 	int i;
    398      1.1    groo 	char *s;
    399      1.1    groo 
    400      1.1    groo 	if (sensors == NULL) {
    401      1.1    groo 		/* No sensors specified, include them all */
    402      1.1    groo 		for (i = 0; i < ns; ++i)
    403      1.1    groo 			cetds[i] = 1;
    404      1.1    groo 
    405      1.1    groo 		return 0;
    406      1.1    groo 	}
    407      1.1    groo 
    408      1.1    groo 	/* Assume no sensors in display */
    409      1.1    groo 	memset(cetds, 0, ns * sizeof(int));
    410      1.1    groo 
    411      1.1    groo 	s = strtok(sensors, ",");
    412      1.1    groo 	while (s != NULL) {
    413      1.1    groo 		if ((i = strtosnum(ebis, s, ns)) != -1)
    414      1.1    groo 			cetds[i] = 1;
    415      1.1    groo 		else {
    416      1.1    groo 			fprintf(stderr, "envstat: unknown sensor %s\n", s);
    417      1.1    groo 			return (-1);
    418      1.1    groo 		}
    419      1.1    groo 
    420      1.1    groo 		s = strtok(NULL, ",");
    421      1.1    groo 	}
    422      1.1    groo 
    423      1.1    groo 	/* Check if we have at least one sensor selected for output */
    424      1.1    groo 	for (i = 0; i < ns; ++i)
    425      1.1    groo 		if (cetds[i] == 1)
    426      1.1    groo 			return (0);
    427      1.1    groo 
    428      1.1    groo 	fprintf(stderr, "envstat: no sensors selected for display\n");
    429      1.1    groo 	return (-1);
    430      1.1    groo }
    431      1.1    groo 
    432      1.1    groo 
    433      1.1    groo /*
    434      1.1    groo  * returns -1 if s is not a valid sensor name for the device
    435      1.1    groo  *       or the sensor number of a sensor which has that name
    436      1.1    groo  */
    437      1.1    groo int
    438      1.1    groo strtosnum(ebis, s, ns)
    439      1.1    groo 	envsys_basic_info_t *ebis;
    440      1.1    groo 	const char *s;
    441      1.1    groo 	int ns;
    442      1.1    groo {
    443      1.1    groo 	int i;
    444      1.1    groo 
    445      1.1    groo 	for (i = 0; i < ns; ++i) {
    446      1.1    groo 		if((ebis[i].validflags & ENVSYS_FVALID) &&
    447      1.1    groo 		   !strcmp(s, ebis[i].desc))
    448      1.1    groo 			return ebis[i].sensor;
    449      1.1    groo 	}
    450      1.1    groo 
    451      1.1    groo 	return -1;
    452      1.1    groo }
    453