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