1 /* $NetBSD: config.c,v 1.6 2007/11/16 08:01:37 xtraeme 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.6 2007/11/16 08:01:37 xtraeme 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 if (!key || !value) 101 return; 102 103 if (!sensordict) { 104 sensordict = prop_dictionary_create(); 105 if (!sensordict) 106 err(EXIT_FAILURE, "cfdict"); 107 } 108 109 if (!prop_dictionary_set_cstring(sensordict, key, value)) 110 err(EXIT_FAILURE, "prop_dict_set_cstring"); 111 } 112 113 /* 114 * Marks sensor's dictionary to say that it's the last property 115 * and the dictionary should be added into the singly linked list. 116 */ 117 void 118 config_dict_mark(void) 119 { 120 struct sensor_block *sb; 121 122 sb = calloc(1, sizeof(*sb)); 123 if (!sb) 124 err(EXIT_FAILURE, "!sb"); 125 126 sb->dict = prop_dictionary_create(); 127 if (!sb->dict) 128 err(EXIT_FAILURE, "!sb->dict"); 129 130 sb->dict = prop_dictionary_copy(sensordict); 131 SLIST_INSERT_HEAD(&sensor_block_list, sb, sb_head); 132 config_dict_destroy(sensordict); 133 } 134 135 /* 136 * Only used for debugging purposses. 137 */ 138 void 139 config_dict_dump(prop_dictionary_t d) 140 { 141 char *buf; 142 143 buf = prop_dictionary_externalize(d); 144 (void)printf("%s", buf); 145 free(buf); 146 } 147 148 /* 149 * Returns the global dictionary. 150 */ 151 prop_dictionary_t 152 config_dict_parsed(void) 153 { 154 return cfdict; 155 } 156 157 /* 158 * To add device properties into the global array, for now only the 159 * 'refresh-timeout' property is accepted. 160 */ 161 void 162 config_dict_adddev_prop(const char *key, const char *value, int line) 163 { 164 prop_dictionary_t d = NULL; 165 uint64_t timo; 166 size_t len; 167 char *endptr, *tmp, *strval; 168 bool minutes, hours; 169 170 minutes = hours = false; 171 172 /* 173 * Check what was specified: seconds, minutes or hours. 174 */ 175 if ((tmp = strchr(value, 's'))) { 176 /* 177 * do nothing, by default the value will be sent as seconds. 178 */ 179 } else if ((tmp = strchr(value, 'm'))) { 180 minutes = true; 181 } else if ((tmp = strchr(value, 'h'))) { 182 hours = true; 183 } else 184 goto bad; 185 186 len = strlen(value); 187 strval = calloc(len, sizeof(*value)); 188 if (!strval) 189 err(EXIT_FAILURE, "calloc"); 190 191 (void)strlcpy(strval, value, len); 192 193 timo = strtoul(strval, &endptr, 10); 194 if (*endptr != '\0') { 195 free(strval); 196 goto bad; 197 } 198 199 free(strval); 200 201 if (!refreshdict) { 202 refreshdict = prop_dictionary_create(); 203 if (!refreshdict) 204 err(EXIT_FAILURE, "prop_dict_create refresh"); 205 } 206 207 d = prop_dictionary_create(); 208 if (!d) 209 err(EXIT_FAILURE, "prop_dict_create refresh 1"); 210 211 if (minutes) 212 timo *= 60; 213 else if (hours) { 214 /* 215 * Make sure the value is not too high... 216 */ 217 if (timo > 999) 218 goto bad; 219 timo *= 60 * 60; 220 } else { 221 /* 222 * 1 second is the lowest value allowed. 223 */ 224 if (timo < 1) 225 goto bad; 226 } 227 228 if (!prop_dictionary_set_uint64(d, key, timo)) 229 err(EXIT_FAILURE, "%s", key); 230 231 if (!prop_dictionary_set(refreshdict, "device-properties", d)) 232 err(EXIT_FAILURE, "device-properties %s", key); 233 234 prop_object_release(d); 235 return; 236 237 bad: 238 (void)printf("envstat: invalid value for the '%s' " 239 "property at line %d\n", key, line); 240 (void)printf("envstat: please fix the configuration file!\n"); 241 if (d) 242 prop_object_release(d); 243 244 exit(EXIT_FAILURE); 245 } 246 247 /* 248 * Destroys all objects from a dictionary. 249 */ 250 void 251 config_dict_destroy(prop_dictionary_t d) 252 { 253 prop_object_iterator_t iter; 254 prop_object_t obj; 255 256 iter = prop_dictionary_iterator(d); 257 if (!iter) 258 err(EXIT_FAILURE, "!iter"); 259 260 while ((obj = prop_object_iterator_next(iter)) != NULL) { 261 prop_dictionary_remove(d, 262 prop_dictionary_keysym_cstring_nocopy(obj)); 263 prop_object_iterator_reset(iter); 264 } 265 266 prop_object_iterator_release(iter); 267 } 268 269 /* 270 * Parses all properties on the device and adds the device 271 * into the singly linked list for devices and the global dictionary. 272 */ 273 void 274 config_devblock_add(const char *key, prop_dictionary_t kdict) 275 { 276 struct device_block *db; 277 struct sensor_block *sb; 278 prop_array_t array; 279 prop_object_iterator_t iter; 280 prop_dictionary_t sdict; 281 prop_object_t obj; 282 prop_string_t lindex; 283 const char *sensor; 284 bool sensor_found = false; 285 286 if (!key) 287 err(EXIT_FAILURE, "devblock !key"); 288 289 array = prop_dictionary_get(kdict, key); 290 if (!array) 291 config_errmsg(DEV_ERR, key, NULL); 292 293 SLIST_FOREACH(sb, &sensor_block_list, sb_head) { 294 /* get the index object value from configuration */ 295 lindex = prop_dictionary_get(sb->dict, "index"); 296 sensor = prop_string_cstring_nocopy(lindex); 297 298 iter = prop_array_iterator(array); 299 if (!iter) 300 err(EXIT_FAILURE, "prop_array_iterator devblock"); 301 302 /* 303 * Get the correct sensor's dictionary from kernel's 304 * dictionary. 305 */ 306 while ((sdict = prop_object_iterator_next(iter)) != NULL) { 307 obj = prop_dictionary_get(sdict, "index"); 308 if (prop_string_equals(lindex, obj)) { 309 sensor_found = true; 310 break; 311 } 312 } 313 314 if (!sensor_found) { 315 prop_object_iterator_release(iter); 316 config_errmsg(SENSOR_ERR, sensor, key); 317 } 318 319 config_devblock_check_sensorprops(sdict, sb->dict, sensor); 320 prop_object_iterator_release(iter); 321 } 322 323 db = calloc(1, sizeof(*db)); 324 if (!db) 325 err(EXIT_FAILURE, "calloc db"); 326 327 db->array = prop_array_create(); 328 if (!db->array) 329 err(EXIT_FAILURE, "prop_array_create devblock"); 330 331 /* 332 * Add all dictionaries into the array. 333 */ 334 SLIST_FOREACH(sb, &sensor_block_list, sb_head) 335 if (!prop_array_add(db->array, sb->dict)) 336 err(EXIT_FAILURE, "prop_array_add"); 337 338 /* 339 * Add the device-properties dictionary into the array. 340 */ 341 if (refreshdict) { 342 if (!prop_array_add(db->array, refreshdict)) 343 err(EXIT_FAILURE, "prop_array_add refreshdict"); 344 prop_object_release(refreshdict); 345 } 346 347 /* 348 * Add this device block into our list. 349 */ 350 db->dev_key = strdup(key); 351 SLIST_INSERT_HEAD(&device_block_list, db, db_head); 352 353 /* 354 * Remove all items in the list, but just decrement 355 * the refcnt in the dictionaries... they are in use. 356 */ 357 while (!SLIST_EMPTY(&sensor_block_list)) { 358 sb = SLIST_FIRST(&sensor_block_list); 359 SLIST_REMOVE_HEAD(&sensor_block_list, sb_head); 360 prop_object_release(sb->dict); 361 free(sb); 362 } 363 364 /* 365 * Now the properties on the array has been parsed, 366 * add it into the global dict. 367 */ 368 if (!cfdict) { 369 cfdict = prop_dictionary_create(); 370 if (!cfdict) 371 err(EXIT_FAILURE, "prop_dictionary_create cfdict"); 372 } 373 374 if (!prop_dictionary_set(cfdict, key, db->array)) 375 err(EXIT_FAILURE, "prop_dictionary_set db->array"); 376 377 } 378 379 /* 380 * Returns the dictionary that has 'sensor_key' in the 'dvname' 381 * array. 382 */ 383 prop_dictionary_t 384 config_devblock_getdict(const char *dvname, const char *sensor_key) 385 { 386 struct device_block *db; 387 prop_object_iterator_t iter; 388 prop_object_t obj, obj2; 389 390 if (!dvname || !sensor_key) 391 return NULL; 392 393 SLIST_FOREACH(db, &device_block_list, db_head) 394 if (strcmp(db->dev_key, dvname) == 0) 395 break; 396 397 if (!db) 398 return NULL; 399 400 iter = prop_array_iterator(db->array); 401 if (!iter) 402 return NULL; 403 404 while ((obj = prop_object_iterator_next(iter)) != NULL) { 405 obj2 = prop_dictionary_get(obj, "index"); 406 if (prop_string_equals_cstring(obj2, sensor_key)) 407 break; 408 } 409 410 prop_object_iterator_release(iter); 411 return obj; 412 } 413 414 /* 415 * Checks that all properties specified in the configuration file 416 * are valid and updates the objects with proper values. 417 */ 418 void 419 config_devblock_check_sensorprops(prop_dictionary_t ksdict, 420 prop_dictionary_t csdict, 421 const char *sensor) 422 { 423 prop_object_t obj, obj2, obj3; 424 char *strval, *endptr; 425 double val; 426 427 /* 428 * rfact property set? 429 */ 430 obj = prop_dictionary_get(csdict, "rfact"); 431 if (obj) { 432 obj2 = prop_dictionary_get(ksdict, "allow-rfact"); 433 if (prop_bool_true(obj2)) { 434 strval = prop_string_cstring(obj); 435 val = strtod(strval, &endptr); 436 if (*endptr != '\0') 437 config_errmsg(VALUE_ERR, "rfact", sensor); 438 439 if (!prop_dictionary_set_uint32(csdict, "rfact", val)) 440 err(EXIT_FAILURE, "dict_set rfact"); 441 } else 442 config_errmsg(PROP_ERR, "rfact", sensor); 443 } 444 445 /* 446 * critical-capacity property set? 447 */ 448 obj = prop_dictionary_get(csdict, "critical-capacity"); 449 if (obj) { 450 obj2 = prop_dictionary_get(ksdict, "want-percentage"); 451 obj3 = prop_dictionary_get(ksdict, "monitoring-supported"); 452 if (prop_bool_true(obj2) && prop_bool_true(obj3)) { 453 strval = prop_string_cstring(obj); 454 val = strtod(strval, &endptr); 455 if ((*endptr != '\0') || (val < 0 || val > 100)) 456 config_errmsg(VALUE_ERR, 457 "critical-capacity", 458 sensor); 459 /* 460 * Convert the value to a valid percentage. 461 */ 462 obj = prop_dictionary_get(ksdict, "max-value"); 463 val = (val / 100) * prop_number_integer_value(obj); 464 465 if (!prop_dictionary_set_uint32(csdict, 466 "critical-capacity", 467 val)) 468 err(EXIT_FAILURE, "dict_set critcap"); 469 } else 470 config_errmsg(PROP_ERR, "critical-capacity", sensor); 471 } 472 473 /* 474 * critical-max property set? 475 */ 476 obj = prop_dictionary_get(csdict, "critical-max"); 477 if (obj) { 478 obj2 = prop_dictionary_get(ksdict, "monitoring-supported"); 479 if (!prop_bool_true(obj2)) 480 config_errmsg(PROP_ERR, "critical-max", sensor); 481 482 strval = prop_string_cstring(obj); 483 obj = convert_val_to_pnumber(ksdict, "critical-max", 484 sensor, strval); 485 if (!prop_dictionary_set(csdict, "critical-max", obj)) 486 err(EXIT_FAILURE, "prop_dict_set cmax"); 487 } 488 489 /* 490 * critical-min property set? 491 */ 492 obj = prop_dictionary_get(csdict, "critical-min"); 493 if (obj) { 494 obj2 = prop_dictionary_get(ksdict, "monitoring-supported"); 495 if (!prop_bool_true(obj2)) 496 config_errmsg(PROP_ERR, "critical-min", sensor); 497 498 strval = prop_string_cstring(obj); 499 obj = convert_val_to_pnumber(ksdict, "critical-min", 500 sensor, strval); 501 if (!prop_dictionary_set(csdict, "critical-min", obj)) 502 err(EXIT_FAILURE, "prop_dict_set cmin"); 503 } 504 } 505 506 /* 507 * Conversions for critical-max and critical-min properties. 508 */ 509 prop_number_t 510 convert_val_to_pnumber(prop_dictionary_t kdict, const char *prop, 511 const char *sensor, char *value) 512 { 513 prop_object_t obj; 514 prop_number_t num; 515 double val, max, min; 516 char *strval, *tmp, *endptr; 517 bool celsius; 518 size_t len; 519 520 val = max = min = 0; 521 522 /* 523 * critical-max and critical-min are not allowed in 524 * battery sensors. 525 */ 526 obj = prop_dictionary_get(kdict, "want-percentage"); 527 if (prop_bool_true(obj)) 528 config_errmsg(PROP_ERR, prop, sensor); 529 530 /* 531 * Make the conversion for sensor's type. 532 */ 533 obj = prop_dictionary_get(kdict, "type"); 534 if (prop_string_equals_cstring(obj, "Temperature")) { 535 tmp = strchr(value, 'C'); 536 if (tmp) 537 celsius = true; 538 else { 539 tmp = strchr(value, 'F'); 540 if (!tmp) 541 config_errmsg(VALUE_ERR, prop, sensor); 542 543 celsius = false; 544 } 545 546 len = strlen(value); 547 strval = calloc(len, sizeof(*value)); 548 if (!strval) 549 err(EXIT_FAILURE, "calloc"); 550 551 (void)strlcpy(strval, value, len); 552 val = strtod(strval, &endptr); 553 if (*endptr != '\0') { 554 free(strval); 555 config_errmsg(VALUE_ERR, prop, sensor); 556 } 557 558 /* convert to fahrenheit */ 559 if (!celsius) 560 val = (val - 32.0) * (5.0 / 9.0); 561 562 /* convert to microKelvin */ 563 val = val * 1000000 + 273150000; 564 num = prop_number_create_unsigned_integer(val); 565 free(strval); 566 567 } else if (prop_string_equals_cstring(obj, "Fan")) { 568 /* no conversion */ 569 val = strtod(value, &endptr); 570 if (*endptr != '\0') 571 config_errmsg(VALUE_ERR, prop, sensor); 572 573 num = prop_number_create_unsigned_integer(val); 574 575 } else { 576 obj = prop_dictionary_get(kdict, "max-value"); 577 if (obj) 578 max = prop_number_integer_value(obj); 579 580 obj = prop_dictionary_get(kdict, "min-value"); 581 if (obj) 582 min = prop_number_integer_value(obj); 583 584 val = strtod(value, &endptr); 585 if (*endptr != '\0') 586 config_errmsg(VALUE_ERR, prop, sensor); 587 588 /* convert to m[V,W,Ohms] again */ 589 val *= 1000000.0; 590 591 /* 592 * trying to set a value higher than the max 593 * assigned? 594 */ 595 if (max && val > max) 596 config_errmsg(VALUE_ERR, prop, sensor); 597 598 /* 599 * trying to set a value lower than the min 600 * assigned? 601 */ 602 if (min && val < min) 603 config_errmsg(VALUE_ERR, prop, sensor); 604 605 num = prop_number_create_integer(val); 606 } 607 608 return num; 609 } 610