1 /* $NetBSD: config.c,v 1.13 2020/06/07 00:51:48 thorpej 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.13 2020/06/07 00:51:48 thorpej 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 __dead 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_string(sensordict, key, value)) 111 err(EXIT_FAILURE, "prop_dict_set_string"); 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_value(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_value(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_string(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 const char *strval; 429 char *endptr; 430 double val; 431 432 /* 433 * rfact property set? 434 */ 435 obj = prop_dictionary_get(csdict, "rfact"); 436 if (obj) { 437 obj2 = prop_dictionary_get(ksdict, "allow-rfact"); 438 if (prop_bool_true(obj2)) { 439 strval = prop_string_value(obj); 440 val = strtod(strval, &endptr); 441 if (*endptr != '\0') 442 config_errmsg(VALUE_ERR, "rfact", sensor); 443 444 if (!prop_dictionary_set_uint32(csdict, "rfact", val)) 445 err(EXIT_FAILURE, "dict_set rfact"); 446 } else 447 config_errmsg(PROP_ERR, "rfact", sensor); 448 } 449 450 /* 451 * critical-capacity property set? 452 */ 453 obj = prop_dictionary_get(csdict, "critical-capacity"); 454 if (obj) { 455 obj2 = prop_dictionary_get(ksdict, "want-percentage"); 456 obj3 = prop_dictionary_get(ksdict, "monitoring-supported"); 457 if (prop_bool_true(obj2) && prop_bool_true(obj3)) { 458 strval = prop_string_value(obj); 459 val = strtod(strval, &endptr); 460 if ((*endptr != '\0') || (val < 0 || val > 100)) 461 config_errmsg(VALUE_ERR, 462 "critical-capacity", 463 sensor); 464 /* 465 * Convert the value to a valid percentage. 466 */ 467 obj = prop_dictionary_get(ksdict, "max-value"); 468 val = (val / 100) * prop_number_signed_value(obj); 469 470 if (!prop_dictionary_set_uint32(csdict, 471 "critical-capacity", 472 val)) 473 err(EXIT_FAILURE, "dict_set critcap"); 474 } else 475 config_errmsg(PROP_ERR, "critical-capacity", sensor); 476 } 477 478 /* 479 * warning-capacity property set? 480 */ 481 obj = prop_dictionary_get(csdict, "warning-capacity"); 482 if (obj) { 483 obj2 = prop_dictionary_get(ksdict, "want-percentage"); 484 obj3 = prop_dictionary_get(ksdict, "monitoring-supported"); 485 if (prop_bool_true(obj2) && prop_bool_true(obj3)) { 486 strval = prop_string_value(obj); 487 val = strtod(strval, &endptr); 488 if ((*endptr != '\0') || (val < 0 || val > 100)) 489 config_errmsg(VALUE_ERR, 490 "warning-capacity", 491 sensor); 492 /* 493 * Convert the value to a valid percentage. 494 */ 495 obj = prop_dictionary_get(ksdict, "max-value"); 496 val = (val / 100) * prop_number_signed_value(obj); 497 498 if (!prop_dictionary_set_uint32(csdict, 499 "warning-capacity", 500 val)) 501 err(EXIT_FAILURE, "dict_set warncap"); 502 } else 503 config_errmsg(PROP_ERR, "warning-capacity", sensor); 504 } 505 506 /* 507 * high-capacity property set? 508 */ 509 obj = prop_dictionary_get(csdict, "high-capacity"); 510 if (obj) { 511 obj2 = prop_dictionary_get(ksdict, "want-percentage"); 512 obj3 = prop_dictionary_get(ksdict, "monitoring-supported"); 513 if (prop_bool_true(obj2) && prop_bool_true(obj3)) { 514 strval = prop_string_value(obj); 515 val = strtod(strval, &endptr); 516 if ((*endptr != '\0') || (val < 0 || val > 100)) 517 config_errmsg(VALUE_ERR, 518 "high-capacity", 519 sensor); 520 /* 521 * Convert the value to a valid percentage. 522 */ 523 obj = prop_dictionary_get(ksdict, "max-value"); 524 val = (val / 100) * prop_number_signed_value(obj); 525 526 if (!prop_dictionary_set_uint32(csdict, 527 "high-capacity", 528 val)) 529 err(EXIT_FAILURE, "dict_set highcap"); 530 } else 531 config_errmsg(PROP_ERR, "high-capacity", sensor); 532 } 533 534 /* 535 * maximum-capacity property set? 536 */ 537 obj = prop_dictionary_get(csdict, "maximum-capacity"); 538 if (obj) { 539 obj2 = prop_dictionary_get(ksdict, "want-percentage"); 540 obj3 = prop_dictionary_get(ksdict, "monitoring-supported"); 541 if (prop_bool_true(obj2) && prop_bool_true(obj3)) { 542 strval = prop_string_value(obj); 543 val = strtod(strval, &endptr); 544 if ((*endptr != '\0') || (val < 0 || val > 100)) 545 config_errmsg(VALUE_ERR, 546 "maximum-capacity", 547 sensor); 548 /* 549 * Convert the value to a valid percentage. 550 */ 551 obj = prop_dictionary_get(ksdict, "max-value"); 552 val = (val / 100) * prop_number_signed_value(obj); 553 554 if (!prop_dictionary_set_uint32(csdict, 555 "maximum-capacity", 556 val)) 557 err(EXIT_FAILURE, "dict_set maxcap"); 558 } else 559 config_errmsg(PROP_ERR, "maximum-capacity", sensor); 560 } 561 562 /* 563 * critical-max property set? 564 */ 565 obj = prop_dictionary_get(csdict, "critical-max"); 566 if (obj) { 567 obj2 = prop_dictionary_get(ksdict, "monitoring-supported"); 568 if (!prop_bool_true(obj2)) 569 config_errmsg(PROP_ERR, "critical-max", sensor); 570 571 strval = prop_string_value(obj); 572 obj = convert_val_to_pnumber(ksdict, "critical-max", 573 sensor, strval); 574 if (!prop_dictionary_set(csdict, "critical-max", obj)) 575 err(EXIT_FAILURE, "prop_dict_set cmax"); 576 } 577 578 /* 579 * critical-min property set? 580 */ 581 obj = prop_dictionary_get(csdict, "critical-min"); 582 if (obj) { 583 obj2 = prop_dictionary_get(ksdict, "monitoring-supported"); 584 if (!prop_bool_true(obj2)) 585 config_errmsg(PROP_ERR, "critical-min", sensor); 586 587 strval = prop_string_value(obj); 588 obj = convert_val_to_pnumber(ksdict, "critical-min", 589 sensor, strval); 590 if (!prop_dictionary_set(csdict, "critical-min", obj)) 591 err(EXIT_FAILURE, "prop_dict_set cmin"); 592 } 593 594 /* 595 * warning-max property set? 596 */ 597 obj = prop_dictionary_get(csdict, "warning-max"); 598 if (obj) { 599 obj2 = prop_dictionary_get(ksdict, "monitoring-supported"); 600 if (!prop_bool_true(obj2)) 601 config_errmsg(PROP_ERR, "warning-max", sensor); 602 603 strval = prop_string_value(obj); 604 obj = convert_val_to_pnumber(ksdict, "warning-max", 605 sensor, strval); 606 if (!prop_dictionary_set(csdict, "warning-max", obj)) 607 err(EXIT_FAILURE, "prop_dict_set wmax"); 608 } 609 /* 610 * warning-min property set? 611 */ 612 obj = prop_dictionary_get(csdict, "warning-min"); 613 if (obj) { 614 obj2 = prop_dictionary_get(ksdict, "monitoring-supported"); 615 if (!prop_bool_true(obj2)) 616 config_errmsg(PROP_ERR, "warning-min", sensor); 617 618 strval = prop_string_value(obj); 619 obj = convert_val_to_pnumber(ksdict, "warning-min", 620 sensor, strval); 621 if (!prop_dictionary_set(csdict, "warning-min", obj)) 622 err(EXIT_FAILURE, "prop_dict_set wmin"); 623 } 624 } 625 626 /* 627 * Conversions for {critical,warning}-{max,min} properties. 628 */ 629 prop_number_t 630 convert_val_to_pnumber(prop_dictionary_t kdict, const char *prop, 631 const char *sensor, const char *value) 632 { 633 prop_object_t obj; 634 prop_number_t num; 635 double val, max, min; 636 char *strval, *tmp, *endptr; 637 bool celsius; 638 size_t len; 639 640 val = max = min = 0; 641 642 /* 643 * Not allowed in battery sensors. 644 */ 645 obj = prop_dictionary_get(kdict, "type"); 646 if (prop_string_equals_string(obj, "Battery capacity")) 647 config_errmsg(PROP_ERR, prop, sensor); 648 649 /* 650 * Make the conversion for sensor's type. 651 */ 652 if (prop_string_equals_string(obj, "Temperature")) { 653 tmp = strchr(value, 'C'); 654 if (tmp) 655 celsius = true; 656 else { 657 tmp = strchr(value, 'F'); 658 if (!tmp) 659 config_errmsg(VALUE_ERR, prop, sensor); 660 661 celsius = false; 662 } 663 664 len = strlen(value); 665 strval = calloc(len, sizeof(*value)); 666 if (!strval) 667 err(EXIT_FAILURE, "calloc"); 668 669 (void)strlcpy(strval, value, len); 670 val = strtod(strval, &endptr); 671 if (*endptr != '\0') { 672 free(strval); 673 config_errmsg(VALUE_ERR, prop, sensor); 674 } 675 676 /* convert to fahrenheit */ 677 if (!celsius) 678 val = (val - 32.0) * (5.0 / 9.0); 679 680 /* convert to microKelvin */ 681 val = val * 1000000 + 273150000; 682 num = prop_number_create_unsigned(val); 683 free(strval); 684 685 } else if (prop_string_equals_string(obj, "Fan") || 686 prop_string_equals_string(obj, "Integer")) { 687 /* no conversion */ 688 val = strtod(value, &endptr); 689 if (*endptr != '\0') 690 config_errmsg(VALUE_ERR, prop, sensor); 691 692 num = prop_number_create_unsigned(val); 693 694 } else { 695 obj = prop_dictionary_get(kdict, "max-value"); 696 if (obj) 697 max = prop_number_signed_value(obj); 698 699 obj = prop_dictionary_get(kdict, "min-value"); 700 if (obj) 701 min = prop_number_signed_value(obj); 702 703 val = strtod(value, &endptr); 704 if (*endptr != '\0') 705 config_errmsg(VALUE_ERR, prop, sensor); 706 707 /* convert to m[V,W,Ohms] again */ 708 val *= 1000000.0; 709 710 /* 711 * trying to set a value higher than the max 712 * assigned? 713 */ 714 if (max && val > max) 715 config_errmsg(VALUE_ERR, prop, sensor); 716 717 /* 718 * trying to set a value lower than the min 719 * assigned? 720 */ 721 if (min && val < min) 722 config_errmsg(VALUE_ERR, prop, sensor); 723 724 num = prop_number_create_signed(val); 725 } 726 727 return num; 728 } 729