1 /* $NetBSD: config.c,v 1.5 2007/10/09 08:00:46 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.5 2007/10/09 08:00:46 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; 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("error: "); 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("please fix the configuration file!\n"); 91 exit(EXIT_FAILURE); 92 } 93 94 /* 95 * Adds a property into the global dictionary 'cfdict'. 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 * Adds the last property into the dictionary and puts it into 115 * the singly linked list for future use. 116 */ 117 void 118 config_dict_mark(const char *key) 119 { 120 struct sensor_block *sb; 121 122 if (!key) 123 err(EXIT_FAILURE, "!key"); 124 125 sb = calloc(1, sizeof(*sb)); 126 if (!sb) 127 err(EXIT_FAILURE, "!sb"); 128 129 sb->dict = prop_dictionary_create(); 130 if (!sb->dict) 131 err(EXIT_FAILURE, "!sb->dict"); 132 133 sb->dict = prop_dictionary_copy(sensordict); 134 SLIST_INSERT_HEAD(&sensor_block_list, sb, sb_head); 135 config_dict_destroy(sensordict); 136 } 137 138 /* 139 * Only used for debugging purposses. 140 */ 141 void 142 config_dict_dump(prop_dictionary_t d) 143 { 144 char *buf; 145 146 buf = prop_dictionary_externalize(d); 147 (void)printf("%s", buf); 148 free(buf); 149 } 150 151 /* 152 * Returns the global dictionary. 153 */ 154 prop_dictionary_t 155 config_dict_parsed(void) 156 { 157 return cfdict; 158 } 159 160 /* 161 * Destroys all objects from a dictionary. 162 */ 163 void 164 config_dict_destroy(prop_dictionary_t d) 165 { 166 prop_object_iterator_t iter; 167 prop_object_t obj; 168 169 iter = prop_dictionary_iterator(d); 170 if (!iter) 171 err(EXIT_FAILURE, "!iter"); 172 173 while ((obj = prop_object_iterator_next(iter)) != NULL) { 174 prop_dictionary_remove(d, 175 prop_dictionary_keysym_cstring_nocopy(obj)); 176 prop_object_iterator_reset(iter); 177 } 178 179 prop_object_iterator_release(iter); 180 } 181 182 /* 183 * Parses all properties on the device and adds the device 184 * into the singly linked list for devices and the global dictionary. 185 */ 186 void 187 config_devblock_add(const char *key, prop_dictionary_t kdict) 188 { 189 struct device_block *db; 190 struct sensor_block *sb; 191 prop_array_t array; 192 prop_object_iterator_t iter; 193 prop_dictionary_t sdict; 194 prop_object_t obj; 195 prop_string_t lindex; 196 const char *sensor; 197 bool sensor_found = false; 198 199 if (!key) 200 err(EXIT_FAILURE, "devblock !key"); 201 202 array = prop_dictionary_get(kdict, key); 203 if (!array) 204 config_errmsg(DEV_ERR, key, NULL); 205 206 SLIST_FOREACH(sb, &sensor_block_list, sb_head) { 207 /* get the index object value from configuration */ 208 lindex = prop_dictionary_get(sb->dict, "index"); 209 sensor = prop_string_cstring_nocopy(lindex); 210 211 iter = prop_array_iterator(array); 212 if (!iter) 213 err(EXIT_FAILURE, "prop_array_iterator devblock"); 214 215 /* 216 * Get the correct sensor's dictionary from kernel's 217 * dictionary. 218 */ 219 while ((sdict = prop_object_iterator_next(iter)) != NULL) { 220 obj = prop_dictionary_get(sdict, "index"); 221 if (prop_string_equals(lindex, obj)) { 222 sensor_found = true; 223 break; 224 } 225 } 226 227 if (!sensor_found) { 228 prop_object_iterator_release(iter); 229 config_errmsg(SENSOR_ERR, sensor, key); 230 } 231 232 config_devblock_check_sensorprops(sdict, sb->dict, sensor); 233 prop_object_iterator_release(iter); 234 } 235 236 db = calloc(1, sizeof(*db)); 237 if (!db) 238 err(EXIT_FAILURE, "calloc db"); 239 240 db->array = prop_array_create(); 241 if (!db->array) 242 err(EXIT_FAILURE, "prop_array_create devblock"); 243 244 /* 245 * Add all dictionaries into the array. 246 */ 247 SLIST_FOREACH(sb, &sensor_block_list, sb_head) 248 if (!prop_array_add(db->array, sb->dict)) 249 err(EXIT_FAILURE, "prop_array_add"); 250 251 /* 252 * Add this device block into our list. 253 */ 254 db->dev_key = strdup(key); 255 SLIST_INSERT_HEAD(&device_block_list, db, db_head); 256 257 /* 258 * Remove all items in the list, but just decrement 259 * the refcnt in the dictionaries... they are in use. 260 */ 261 while (!SLIST_EMPTY(&sensor_block_list)) { 262 sb = SLIST_FIRST(&sensor_block_list); 263 SLIST_REMOVE_HEAD(&sensor_block_list, sb_head); 264 prop_object_release(sb->dict); 265 free(sb); 266 } 267 268 /* 269 * Now the properties on the array has been parsed, 270 * add it into the global dict. 271 */ 272 if (!cfdict) { 273 cfdict = prop_dictionary_create(); 274 if (!cfdict) 275 err(EXIT_FAILURE, "prop_dictionary_create cfdict"); 276 } 277 278 if (!prop_dictionary_set(cfdict, key, db->array)) 279 err(EXIT_FAILURE, "prop_dictionary_set db->array"); 280 281 } 282 283 /* 284 * Returns the dictionary that has 'sensor_key' in the 'dvname' 285 * array. 286 */ 287 prop_dictionary_t 288 config_devblock_getdict(const char *dvname, const char *sensor_key) 289 { 290 struct device_block *db; 291 prop_object_iterator_t iter; 292 prop_object_t obj, obj2; 293 294 if (!dvname || !sensor_key) 295 return NULL; 296 297 SLIST_FOREACH(db, &device_block_list, db_head) 298 if (strcmp(db->dev_key, dvname) == 0) 299 break; 300 301 if (!db) 302 return NULL; 303 304 iter = prop_array_iterator(db->array); 305 if (!iter) 306 return NULL; 307 308 while ((obj = prop_object_iterator_next(iter)) != NULL) { 309 obj2 = prop_dictionary_get(obj, "index"); 310 if (prop_string_equals_cstring(obj2, sensor_key)) 311 break; 312 } 313 314 prop_object_iterator_release(iter); 315 return obj; 316 } 317 318 /* 319 * Checks that all properties specified in the configuration file 320 * are valid and updates the objects with proper values. 321 */ 322 void 323 config_devblock_check_sensorprops(prop_dictionary_t ksdict, 324 prop_dictionary_t csdict, 325 const char *sensor) 326 { 327 prop_object_t obj, obj2, obj3; 328 char *strval, *endptr; 329 double val; 330 331 /* 332 * rfact property set? 333 */ 334 obj = prop_dictionary_get(csdict, "rfact"); 335 if (obj) { 336 obj2 = prop_dictionary_get(ksdict, "allow-rfact"); 337 if (prop_bool_true(obj2)) { 338 strval = prop_string_cstring(obj); 339 val = strtod(strval, &endptr); 340 if (*endptr != '\0') 341 config_errmsg(VALUE_ERR, "rfact", sensor); 342 343 if (!prop_dictionary_set_uint32(csdict, "rfact", val)) 344 err(EXIT_FAILURE, "dict_set rfact"); 345 } else 346 config_errmsg(PROP_ERR, "rfact", sensor); 347 } 348 349 /* 350 * critical-capacity property set? 351 */ 352 obj = prop_dictionary_get(csdict, "critical-capacity"); 353 if (obj) { 354 obj2 = prop_dictionary_get(ksdict, "want-percentage"); 355 obj3 = prop_dictionary_get(ksdict, "monitoring-supported"); 356 if (prop_bool_true(obj2) && prop_bool_true(obj3)) { 357 strval = prop_string_cstring(obj); 358 val = strtod(strval, &endptr); 359 if ((*endptr != '\0') || (val < 0 || val > 100)) 360 config_errmsg(VALUE_ERR, 361 "critical-capacity", 362 sensor); 363 /* 364 * Convert the value to a valid percentage. 365 */ 366 obj = prop_dictionary_get(ksdict, "max-value"); 367 val = (val / 100) * prop_number_integer_value(obj); 368 369 if (!prop_dictionary_set_uint32(csdict, 370 "critical-capacity", 371 val)) 372 err(EXIT_FAILURE, "dict_set critcap"); 373 } else 374 config_errmsg(PROP_ERR, 375 "critical-capacity", sensor); 376 } 377 378 /* 379 * critical-max property set? 380 */ 381 obj = prop_dictionary_get(csdict, "critical-max"); 382 if (obj) { 383 obj2 = prop_dictionary_get(ksdict, "monitoring-supported"); 384 if (!prop_bool_true(obj2)) 385 config_errmsg(PROP_ERR, "critical-max", sensor); 386 387 strval = prop_string_cstring(obj); 388 obj = convert_val_to_pnumber(ksdict, "critical-max", 389 sensor, strval); 390 if (!prop_dictionary_set(csdict, "critical-max", obj)) 391 err(EXIT_FAILURE, "prop_dict_set cmax"); 392 } 393 394 /* 395 * critical-min property set? 396 */ 397 obj = prop_dictionary_get(csdict, "critical-min"); 398 if (obj) { 399 obj2 = prop_dictionary_get(ksdict, "monitoring-supported"); 400 if (!prop_bool_true(obj2)) 401 config_errmsg(PROP_ERR, "critical-min", sensor); 402 403 strval = prop_string_cstring(obj); 404 obj = convert_val_to_pnumber(ksdict, "critical-min", 405 sensor, strval); 406 if (!prop_dictionary_set(csdict, "critical-min", obj)) 407 err(EXIT_FAILURE, "prop_dict_set cmin"); 408 } 409 } 410 411 /* 412 * Conversions for critical-max and critical-min properties. 413 */ 414 prop_number_t 415 convert_val_to_pnumber(prop_dictionary_t kdict, const char *prop, 416 const char *sensor, char *value) 417 { 418 prop_object_t obj; 419 prop_number_t num; 420 double val, max, min; 421 char *strval, *tmp, *endptr; 422 bool celsius; 423 size_t len; 424 425 val = max = min = 0; 426 427 /* 428 * critical-max and critical-min are not allowed in 429 * battery sensors. 430 */ 431 obj = prop_dictionary_get(kdict, "want-percentage"); 432 if (prop_bool_true(obj)) 433 config_errmsg(PROP_ERR, prop, sensor); 434 435 /* 436 * Make the conversion for sensor's type. 437 */ 438 obj = prop_dictionary_get(kdict, "type"); 439 if (prop_string_equals_cstring(obj, "Temperature")) { 440 tmp = strchr(value, 'C'); 441 if (tmp) 442 celsius = true; 443 else { 444 tmp = strchr(value, 'F'); 445 if (!tmp) 446 config_errmsg(VALUE_ERR, prop, sensor); 447 448 celsius = false; 449 } 450 451 len = strlen(value); 452 strval = calloc(len, sizeof(*value)); 453 if (!strval) 454 err(EXIT_FAILURE, "calloc"); 455 456 (void)strlcpy(strval, value, len); 457 val = strtod(strval, &endptr); 458 if (*endptr != '\0') 459 config_errmsg(VALUE_ERR, prop, sensor); 460 461 /* convert to fahrenheit */ 462 if (!celsius) 463 val = (val - 32.0) * (5.0 / 9.0); 464 465 /* convert to microKelvin */ 466 val = val * 1000000 + 273150000; 467 num = prop_number_create_unsigned_integer(val); 468 469 } else if (prop_string_equals_cstring(obj, "Fan")) { 470 /* no conversion */ 471 val = strtod(value, &endptr); 472 if (*endptr != '\0') 473 config_errmsg(VALUE_ERR, prop, sensor); 474 475 num = prop_number_create_unsigned_integer(val); 476 477 } else { 478 obj = prop_dictionary_get(kdict, "max-value"); 479 if (obj) 480 max = prop_number_integer_value(obj); 481 482 obj = prop_dictionary_get(kdict, "min-value"); 483 if (obj) 484 min = prop_number_integer_value(obj); 485 486 val = strtod(value, &endptr); 487 if (*endptr != '\0') 488 config_errmsg(VALUE_ERR, prop, sensor); 489 490 /* convert to m[V,W,Ohms] again */ 491 val *= 1000000.0; 492 493 /* 494 * trying to set a value higher than the max 495 * assigned? 496 */ 497 if (max && val > max) 498 config_errmsg(VALUE_ERR, prop, sensor); 499 500 /* 501 * trying to set a value lower than the min 502 * assigned? 503 */ 504 if (min && val < min) 505 config_errmsg(VALUE_ERR, prop, sensor); 506 507 num = prop_number_create_integer(val); 508 } 509 510 return num; 511 } 512