1 /* $NetBSD: config.c,v 1.10 2010/10/05 05:03:49 pgoyette Exp $ */ 2 3 /*- 4 * Copyright (c) 2007 Juan Romero Pardines. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 #ifndef lint 30 __RCSID("$NetBSD: config.c,v 1.10 2010/10/05 05:03:49 pgoyette Exp $"); 31 #endif /* not lint */ 32 33 #include <stdio.h> 34 #include <string.h> 35 #include <stdlib.h> 36 #include <err.h> 37 #include <errno.h> 38 #include <sys/queue.h> 39 #include <prop/proplib.h> 40 41 #include "envstat.h" 42 43 /* 44 * Singly linked list for dictionaries that store properties 45 * in a sensor. 46 */ 47 static SLIST_HEAD(, sensor_block) sensor_block_list = 48 SLIST_HEAD_INITIALIZER(&sensor_block_list); 49 50 /* 51 * Singly linked list for devices that store a proplib array 52 * device with a device name. 53 */ 54 static SLIST_HEAD(, device_block) device_block_list = 55 SLIST_HEAD_INITIALIZER(&device_block_list); 56 57 enum { 58 VALUE_ERR, 59 PROP_ERR, 60 SENSOR_ERR, 61 DEV_ERR 62 }; 63 64 static prop_dictionary_t cfdict, sensordict, refreshdict; 65 static void config_errmsg(int, const char *, const char *); 66 67 static void 68 config_errmsg(int lvl, const char *key, const char *key2) 69 { 70 (void)printf("envstat: "); 71 72 switch (lvl) { 73 case VALUE_ERR: 74 (void)printf("invalid value for '%s' in `%s'\n", 75 key, key2); 76 break; 77 case PROP_ERR: 78 (void)printf("the '%s' property is not allowed " 79 "in `%s'\n", key, key2); 80 break; 81 case SENSOR_ERR: 82 (void)printf("'%s' is not a valid sensor in the " 83 "`%s' device\n", key, key2); 84 break; 85 case DEV_ERR: 86 (void)printf("device `%s' doesn't exist\n", key); 87 break; 88 } 89 90 (void)printf("envstat: please fix the configuration file!\n"); 91 exit(EXIT_FAILURE); 92 } 93 94 /* 95 * Adds a property into a temporary dictionary. 96 */ 97 void 98 config_dict_add_prop(const char *key, char *value) 99 { 100 101 if (!key || !value) 102 return; 103 104 if (!sensordict) { 105 sensordict = prop_dictionary_create(); 106 if (!sensordict) 107 err(EXIT_FAILURE, "sensordict"); 108 } 109 110 if (!prop_dictionary_set_cstring(sensordict, key, value)) 111 err(EXIT_FAILURE, "prop_dict_set_cstring"); 112 } 113 114 /* 115 * Marks sensor's dictionary to say that it's the last property 116 * and the dictionary should be added into the singly linked list. 117 */ 118 void 119 config_dict_mark(void) 120 { 121 struct sensor_block *sb; 122 123 sb = calloc(1, sizeof(*sb)); 124 if (!sb) 125 err(EXIT_FAILURE, "!sb"); 126 127 sb->dict = prop_dictionary_create(); 128 if (!sb->dict) 129 err(EXIT_FAILURE, "!sb->dict"); 130 131 sb->dict = prop_dictionary_copy(sensordict); 132 SLIST_INSERT_HEAD(&sensor_block_list, sb, sb_head); 133 config_dict_destroy(sensordict); 134 } 135 136 /* 137 * Only used for debugging purposses. 138 */ 139 void 140 config_dict_dump(prop_dictionary_t d) 141 { 142 char *buf; 143 144 buf = prop_dictionary_externalize(d); 145 (void)printf("%s", buf); 146 free(buf); 147 } 148 149 /* 150 * Returns the global dictionary. 151 */ 152 prop_dictionary_t 153 config_dict_parsed(void) 154 { 155 return cfdict; 156 } 157 158 /* 159 * To add device properties into the global array, for now only the 160 * 'refresh-timeout' property is accepted. 161 */ 162 void 163 config_dict_adddev_prop(const char *key, const char *value, int line) 164 { 165 prop_dictionary_t d = NULL; 166 uint64_t timo; 167 size_t len; 168 char *endptr, *tmp, *strval; 169 bool minutes, hours; 170 171 minutes = hours = false; 172 173 /* 174 * Check what was specified: seconds, minutes or hours. 175 */ 176 if ((tmp = strchr(value, 's'))) { 177 /* 178 * do nothing, by default the value will be sent as seconds. 179 */ 180 } else if ((tmp = strchr(value, 'm'))) { 181 minutes = true; 182 } else if ((tmp = strchr(value, 'h'))) { 183 hours = true; 184 } else 185 goto bad; 186 187 len = strlen(value); 188 strval = calloc(len, sizeof(*value)); 189 if (!strval) 190 err(EXIT_FAILURE, "calloc"); 191 192 (void)strlcpy(strval, value, len); 193 194 timo = strtoul(strval, &endptr, 10); 195 if (*endptr != '\0') { 196 free(strval); 197 goto bad; 198 } 199 200 free(strval); 201 202 refreshdict = prop_dictionary_create(); 203 if (!refreshdict) 204 err(EXIT_FAILURE, "prop_dict_create refresh"); 205 206 d = prop_dictionary_create(); 207 if (!d) 208 err(EXIT_FAILURE, "prop_dict_create refresh 1"); 209 210 if (minutes) 211 timo *= 60; 212 else if (hours) { 213 /* 214 * Make sure the value is not too high... 215 */ 216 if (timo > 999) 217 goto bad; 218 timo *= 60 * 60; 219 } else { 220 /* 221 * 1 second is the lowest value allowed. 222 */ 223 if (timo < 1) 224 goto bad; 225 } 226 227 if (!prop_dictionary_set_uint64(d, key, timo)) 228 err(EXIT_FAILURE, "%s", key); 229 230 if (!prop_dictionary_set(refreshdict, "device-properties", d)) 231 err(EXIT_FAILURE, "device-properties %s", key); 232 233 prop_object_release(d); 234 return; 235 236 bad: 237 (void)printf("envstat: invalid value for the '%s' " 238 "property at line %d\n", key, line); 239 (void)printf("envstat: please fix the configuration file!\n"); 240 if (d) 241 prop_object_release(d); 242 243 exit(EXIT_FAILURE); 244 } 245 246 /* 247 * Destroys all objects from a dictionary. 248 */ 249 void 250 config_dict_destroy(prop_dictionary_t d) 251 { 252 prop_object_iterator_t iter; 253 prop_object_t obj; 254 255 iter = prop_dictionary_iterator(d); 256 if (!iter) 257 err(EXIT_FAILURE, "!iter"); 258 259 while ((obj = prop_object_iterator_next(iter)) != NULL) { 260 prop_dictionary_remove(d, 261 prop_dictionary_keysym_cstring_nocopy(obj)); 262 prop_object_iterator_reset(iter); 263 } 264 265 prop_object_iterator_release(iter); 266 } 267 268 /* 269 * Parses all properties on the device and adds the device 270 * into the singly linked list for devices and the global dictionary. 271 */ 272 void 273 config_devblock_add(const char *key, prop_dictionary_t kdict) 274 { 275 struct device_block *db; 276 struct sensor_block *sb; 277 prop_array_t array; 278 prop_object_iterator_t iter; 279 prop_dictionary_t sdict; 280 prop_object_t obj; 281 prop_string_t lindex; 282 const char *sensor; 283 bool sensor_found = false; 284 285 if (!key) 286 err(EXIT_FAILURE, "devblock !key"); 287 288 array = prop_dictionary_get(kdict, key); 289 if (!array) 290 config_errmsg(DEV_ERR, key, NULL); 291 292 SLIST_FOREACH(sb, &sensor_block_list, sb_head) { 293 /* get the index object value from configuration */ 294 lindex = prop_dictionary_get(sb->dict, "index"); 295 sensor = prop_string_cstring_nocopy(lindex); 296 297 iter = prop_array_iterator(array); 298 if (!iter) 299 err(EXIT_FAILURE, "prop_array_iterator devblock"); 300 301 /* 302 * Get the correct sensor's dictionary from kernel's 303 * dictionary. 304 */ 305 while ((sdict = prop_object_iterator_next(iter)) != NULL) { 306 obj = prop_dictionary_get(sdict, "index"); 307 if (prop_string_equals(lindex, obj)) { 308 sensor_found = true; 309 break; 310 } 311 } 312 313 if (!sensor_found) { 314 prop_object_iterator_release(iter); 315 config_errmsg(SENSOR_ERR, sensor, key); 316 } 317 318 config_devblock_check_sensorprops(sdict, sb->dict, sensor); 319 prop_object_iterator_release(iter); 320 } 321 322 db = calloc(1, sizeof(*db)); 323 if (!db) 324 err(EXIT_FAILURE, "calloc db"); 325 326 db->array = prop_array_create(); 327 if (!db->array) 328 err(EXIT_FAILURE, "prop_array_create devblock"); 329 330 /* 331 * Add all dictionaries into the array. 332 */ 333 SLIST_FOREACH(sb, &sensor_block_list, sb_head) 334 if (!prop_array_add(db->array, sb->dict)) 335 err(EXIT_FAILURE, "prop_array_add"); 336 337 /* 338 * Add the device-properties dictionary into the array. 339 */ 340 if (refreshdict) { 341 if (!prop_array_add(db->array, refreshdict)) 342 err(EXIT_FAILURE, "prop_array_add refreshdict"); 343 prop_object_release(refreshdict); 344 } 345 346 /* 347 * Add this device block into our list. 348 */ 349 db->dev_key = strdup(key); 350 SLIST_INSERT_HEAD(&device_block_list, db, db_head); 351 352 /* 353 * Remove all items in the list, but just decrement 354 * the refcnt in the dictionaries... they are in use. 355 */ 356 while (!SLIST_EMPTY(&sensor_block_list)) { 357 sb = SLIST_FIRST(&sensor_block_list); 358 SLIST_REMOVE_HEAD(&sensor_block_list, sb_head); 359 prop_object_release(sb->dict); 360 free(sb); 361 } 362 363 /* 364 * Now the properties on the array has been parsed, 365 * add it into the global dict. 366 */ 367 if (!cfdict) { 368 cfdict = prop_dictionary_create(); 369 if (!cfdict) 370 err(EXIT_FAILURE, "prop_dictionary_create cfdict"); 371 } 372 373 if (!prop_dictionary_set(cfdict, key, db->array)) 374 err(EXIT_FAILURE, "prop_dictionary_set db->array"); 375 376 /* 377 * refreshdict must be NULLed to avoid false positives in 378 * next matches. 379 */ 380 refreshdict = NULL; 381 } 382 383 /* 384 * Returns the dictionary that has 'sensor_key' in the 'dvname' 385 * array. 386 */ 387 prop_dictionary_t 388 config_devblock_getdict(const char *dvname, const char *sensor_key) 389 { 390 struct device_block *db; 391 prop_object_iterator_t iter; 392 prop_object_t obj, obj2; 393 394 if (!dvname || !sensor_key) 395 return NULL; 396 397 SLIST_FOREACH(db, &device_block_list, db_head) 398 if (strcmp(db->dev_key, dvname) == 0) 399 break; 400 401 if (!db) 402 return NULL; 403 404 iter = prop_array_iterator(db->array); 405 if (!iter) 406 return NULL; 407 408 while ((obj = prop_object_iterator_next(iter)) != NULL) { 409 obj2 = prop_dictionary_get(obj, "index"); 410 if (prop_string_equals_cstring(obj2, sensor_key)) 411 break; 412 } 413 414 prop_object_iterator_release(iter); 415 return obj; 416 } 417 418 /* 419 * Checks that all properties specified in the configuration file 420 * are valid and updates the objects with proper values. 421 */ 422 void 423 config_devblock_check_sensorprops(prop_dictionary_t ksdict, 424 prop_dictionary_t csdict, 425 const char *sensor) 426 { 427 prop_object_t obj, obj2, obj3; 428 char *strval, *endptr; 429 double val; 430 431 /* 432 * rfact property set? 433 */ 434 obj = prop_dictionary_get(csdict, "rfact"); 435 if (obj) { 436 obj2 = prop_dictionary_get(ksdict, "allow-rfact"); 437 if (prop_bool_true(obj2)) { 438 strval = prop_string_cstring(obj); 439 val = strtod(strval, &endptr); 440 if (*endptr != '\0') 441 config_errmsg(VALUE_ERR, "rfact", sensor); 442 443 if (!prop_dictionary_set_uint32(csdict, "rfact", val)) 444 err(EXIT_FAILURE, "dict_set rfact"); 445 } else 446 config_errmsg(PROP_ERR, "rfact", sensor); 447 } 448 449 /* 450 * critical-capacity property set? 451 */ 452 obj = prop_dictionary_get(csdict, "critical-capacity"); 453 if (obj) { 454 obj2 = prop_dictionary_get(ksdict, "want-percentage"); 455 obj3 = prop_dictionary_get(ksdict, "monitoring-supported"); 456 if (prop_bool_true(obj2) && prop_bool_true(obj3)) { 457 strval = prop_string_cstring(obj); 458 val = strtod(strval, &endptr); 459 if ((*endptr != '\0') || (val < 0 || val > 100)) 460 config_errmsg(VALUE_ERR, 461 "critical-capacity", 462 sensor); 463 /* 464 * Convert the value to a valid percentage. 465 */ 466 obj = prop_dictionary_get(ksdict, "max-value"); 467 val = (val / 100) * prop_number_integer_value(obj); 468 469 if (!prop_dictionary_set_uint32(csdict, 470 "critical-capacity", 471 val)) 472 err(EXIT_FAILURE, "dict_set critcap"); 473 } else 474 config_errmsg(PROP_ERR, "critical-capacity", sensor); 475 } 476 477 /* 478 * warning-capacity property set? 479 */ 480 obj = prop_dictionary_get(csdict, "warning-capacity"); 481 if (obj) { 482 obj2 = prop_dictionary_get(ksdict, "want-percentage"); 483 obj3 = prop_dictionary_get(ksdict, "monitoring-supported"); 484 if (prop_bool_true(obj2) && prop_bool_true(obj3)) { 485 strval = prop_string_cstring(obj); 486 val = strtod(strval, &endptr); 487 if ((*endptr != '\0') || (val < 0 || val > 100)) 488 config_errmsg(VALUE_ERR, 489 "warning-capacity", 490 sensor); 491 /* 492 * Convert the value to a valid percentage. 493 */ 494 obj = prop_dictionary_get(ksdict, "max-value"); 495 val = (val / 100) * prop_number_integer_value(obj); 496 497 if (!prop_dictionary_set_uint32(csdict, 498 "warning-capacity", 499 val)) 500 err(EXIT_FAILURE, "dict_set warncap"); 501 } else 502 config_errmsg(PROP_ERR, "warning-capacity", sensor); 503 } 504 505 /* 506 * high-capacity property set? 507 */ 508 obj = prop_dictionary_get(csdict, "high-capacity"); 509 if (obj) { 510 obj2 = prop_dictionary_get(ksdict, "want-percentage"); 511 obj3 = prop_dictionary_get(ksdict, "monitoring-supported"); 512 if (prop_bool_true(obj2) && prop_bool_true(obj3)) { 513 strval = prop_string_cstring(obj); 514 val = strtod(strval, &endptr); 515 if ((*endptr != '\0') || (val < 0 || val > 100)) 516 config_errmsg(VALUE_ERR, 517 "high-capacity", 518 sensor); 519 /* 520 * Convert the value to a valid percentage. 521 */ 522 obj = prop_dictionary_get(ksdict, "max-value"); 523 val = (val / 100) * prop_number_integer_value(obj); 524 525 if (!prop_dictionary_set_uint32(csdict, 526 "high-capacity", 527 val)) 528 err(EXIT_FAILURE, "dict_set highcap"); 529 } else 530 config_errmsg(PROP_ERR, "high-capacity", sensor); 531 } 532 533 /* 534 * maximum-capacity property set? 535 */ 536 obj = prop_dictionary_get(csdict, "maximum-capacity"); 537 if (obj) { 538 obj2 = prop_dictionary_get(ksdict, "want-percentage"); 539 obj3 = prop_dictionary_get(ksdict, "monitoring-supported"); 540 if (prop_bool_true(obj2) && prop_bool_true(obj3)) { 541 strval = prop_string_cstring(obj); 542 val = strtod(strval, &endptr); 543 if ((*endptr != '\0') || (val < 0 || val > 100)) 544 config_errmsg(VALUE_ERR, 545 "maximum-capacity", 546 sensor); 547 /* 548 * Convert the value to a valid percentage. 549 */ 550 obj = prop_dictionary_get(ksdict, "max-value"); 551 val = (val / 100) * prop_number_integer_value(obj); 552 553 if (!prop_dictionary_set_uint32(csdict, 554 "maximum-capacity", 555 val)) 556 err(EXIT_FAILURE, "dict_set maxcap"); 557 } else 558 config_errmsg(PROP_ERR, "maximum-capacity", sensor); 559 } 560 561 /* 562 * critical-max property set? 563 */ 564 obj = prop_dictionary_get(csdict, "critical-max"); 565 if (obj) { 566 obj2 = prop_dictionary_get(ksdict, "monitoring-supported"); 567 if (!prop_bool_true(obj2)) 568 config_errmsg(PROP_ERR, "critical-max", sensor); 569 570 strval = prop_string_cstring(obj); 571 obj = convert_val_to_pnumber(ksdict, "critical-max", 572 sensor, strval); 573 if (!prop_dictionary_set(csdict, "critical-max", obj)) 574 err(EXIT_FAILURE, "prop_dict_set cmax"); 575 } 576 577 /* 578 * critical-min property set? 579 */ 580 obj = prop_dictionary_get(csdict, "critical-min"); 581 if (obj) { 582 obj2 = prop_dictionary_get(ksdict, "monitoring-supported"); 583 if (!prop_bool_true(obj2)) 584 config_errmsg(PROP_ERR, "critical-min", sensor); 585 586 strval = prop_string_cstring(obj); 587 obj = convert_val_to_pnumber(ksdict, "critical-min", 588 sensor, strval); 589 if (!prop_dictionary_set(csdict, "critical-min", obj)) 590 err(EXIT_FAILURE, "prop_dict_set cmin"); 591 } 592 593 /* 594 * warning-max property set? 595 */ 596 obj = prop_dictionary_get(csdict, "warning-max"); 597 if (obj) { 598 obj2 = prop_dictionary_get(ksdict, "monitoring-supported"); 599 if (!prop_bool_true(obj2)) 600 config_errmsg(PROP_ERR, "warning-max", sensor); 601 602 strval = prop_string_cstring(obj); 603 obj = convert_val_to_pnumber(ksdict, "warning-max", 604 sensor, strval); 605 if (!prop_dictionary_set(csdict, "warning-max", obj)) 606 err(EXIT_FAILURE, "prop_dict_set wmax"); 607 } 608 /* 609 * warning-min property set? 610 */ 611 obj = prop_dictionary_get(csdict, "warning-min"); 612 if (obj) { 613 obj2 = prop_dictionary_get(ksdict, "monitoring-supported"); 614 if (!prop_bool_true(obj2)) 615 config_errmsg(PROP_ERR, "warning-min", sensor); 616 617 strval = prop_string_cstring(obj); 618 obj = convert_val_to_pnumber(ksdict, "warning-min", 619 sensor, strval); 620 if (!prop_dictionary_set(csdict, "warning-min", obj)) 621 err(EXIT_FAILURE, "prop_dict_set wmin"); 622 } 623 } 624 625 /* 626 * Conversions for critical-max and critical-min properties. 627 */ 628 prop_number_t 629 convert_val_to_pnumber(prop_dictionary_t kdict, const char *prop, 630 const char *sensor, char *value) 631 { 632 prop_object_t obj; 633 prop_number_t num; 634 double val, max, min; 635 char *strval, *tmp, *endptr; 636 bool celsius; 637 size_t len; 638 639 val = max = min = 0; 640 641 /* 642 * critical-max and critical-min are not allowed in 643 * battery sensors. 644 */ 645 obj = prop_dictionary_get(kdict, "want-percentage"); 646 if (prop_bool_true(obj)) 647 config_errmsg(PROP_ERR, prop, sensor); 648 649 /* 650 * Make the conversion for sensor's type. 651 */ 652 obj = prop_dictionary_get(kdict, "type"); 653 if (prop_string_equals_cstring(obj, "Temperature")) { 654 tmp = strchr(value, 'C'); 655 if (tmp) 656 celsius = true; 657 else { 658 tmp = strchr(value, 'F'); 659 if (!tmp) 660 config_errmsg(VALUE_ERR, prop, sensor); 661 662 celsius = false; 663 } 664 665 len = strlen(value); 666 strval = calloc(len, sizeof(*value)); 667 if (!strval) 668 err(EXIT_FAILURE, "calloc"); 669 670 (void)strlcpy(strval, value, len); 671 val = strtod(strval, &endptr); 672 if (*endptr != '\0') { 673 free(strval); 674 config_errmsg(VALUE_ERR, prop, sensor); 675 } 676 677 /* convert to fahrenheit */ 678 if (!celsius) 679 val = (val - 32.0) * (5.0 / 9.0); 680 681 /* convert to microKelvin */ 682 val = val * 1000000 + 273150000; 683 num = prop_number_create_unsigned_integer(val); 684 free(strval); 685 686 } else if (prop_string_equals_cstring(obj, "Fan") || 687 prop_string_equals_cstring(obj, "Integer")) { 688 /* no conversion */ 689 val = strtod(value, &endptr); 690 if (*endptr != '\0') 691 config_errmsg(VALUE_ERR, prop, sensor); 692 693 num = prop_number_create_unsigned_integer(val); 694 695 } else { 696 obj = prop_dictionary_get(kdict, "max-value"); 697 if (obj) 698 max = prop_number_integer_value(obj); 699 700 obj = prop_dictionary_get(kdict, "min-value"); 701 if (obj) 702 min = prop_number_integer_value(obj); 703 704 val = strtod(value, &endptr); 705 if (*endptr != '\0') 706 config_errmsg(VALUE_ERR, prop, sensor); 707 708 /* convert to m[V,W,Ohms] again */ 709 val *= 1000000.0; 710 711 /* 712 * trying to set a value higher than the max 713 * assigned? 714 */ 715 if (max && val > max) 716 config_errmsg(VALUE_ERR, prop, sensor); 717 718 /* 719 * trying to set a value lower than the min 720 * assigned? 721 */ 722 if (min && val < min) 723 config_errmsg(VALUE_ERR, prop, sensor); 724 725 num = prop_number_create_integer(val); 726 } 727 728 return num; 729 } 730