| 1 |
/* |
|---|
| 2 |
* Copyright (c) 2007, OmniTI Computer Consulting, Inc. |
|---|
| 3 |
* All rights reserved. |
|---|
| 4 |
* |
|---|
| 5 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 6 |
* modification, are permitted provided that the following conditions are |
|---|
| 7 |
* met: |
|---|
| 8 |
* |
|---|
| 9 |
* * Redistributions of source code must retain the above copyright |
|---|
| 10 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 11 |
* * Redistributions in binary form must reproduce the above |
|---|
| 12 |
* copyright notice, this list of conditions and the following |
|---|
| 13 |
* disclaimer in the documentation and/or other materials provided |
|---|
| 14 |
* with the distribution. |
|---|
| 15 |
* * Neither the name OmniTI Computer Consulting, Inc. nor the names |
|---|
| 16 |
* of its contributors may be used to endorse or promote products |
|---|
| 17 |
* derived from this software without specific prior written |
|---|
| 18 |
* permission. |
|---|
| 19 |
* |
|---|
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 24 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 31 |
*/ |
|---|
| 32 |
|
|---|
| 33 |
#include "noit_defines.h" |
|---|
| 34 |
|
|---|
| 35 |
#include <stdio.h> |
|---|
| 36 |
#include <unistd.h> |
|---|
| 37 |
#include <fcntl.h> |
|---|
| 38 |
#include <errno.h> |
|---|
| 39 |
#include <assert.h> |
|---|
| 40 |
#include <libxml/parser.h> |
|---|
| 41 |
#include <libxml/tree.h> |
|---|
| 42 |
#include <libxml/xpath.h> |
|---|
| 43 |
#include <zlib.h> |
|---|
| 44 |
|
|---|
| 45 |
#include "noit_conf.h" |
|---|
| 46 |
#include "noit_check.h" |
|---|
| 47 |
#include "noit_console.h" |
|---|
| 48 |
#include "utils/noit_hash.h" |
|---|
| 49 |
#include "utils/noit_log.h" |
|---|
| 50 |
#include "utils/noit_b64.h" |
|---|
| 51 |
|
|---|
| 52 |
/* tmp hash impl, replace this with something nice */ |
|---|
| 53 |
static noit_hash_table _tmp_config = NOIT_HASH_EMPTY; |
|---|
| 54 |
static xmlDocPtr master_config = NULL; |
|---|
| 55 |
static char master_config_file[PATH_MAX] = ""; |
|---|
| 56 |
static xmlXPathContextPtr xpath_ctxt = NULL; |
|---|
| 57 |
|
|---|
| 58 |
/* This is used to notice config changes and journal the config out |
|---|
| 59 |
* using a user-specified function. It supports allowing multiple config |
|---|
| 60 |
* changed to coalesce so you don't write out 1000 changes in a few seconds. |
|---|
| 61 |
*/ |
|---|
| 62 |
static u_int32_t __config_gen = 0; |
|---|
| 63 |
static u_int32_t __config_coalesce = 0; |
|---|
| 64 |
static u_int32_t __config_coalesce_time = 0; |
|---|
| 65 |
void noit_conf_coalesce_changes(u_int32_t seconds) { |
|---|
| 66 |
__config_coalesce_time = seconds; |
|---|
| 67 |
} |
|---|
| 68 |
void noit_conf_mark_changed() { |
|---|
| 69 |
/* increment the change counter -- in case anyone cares */ |
|---|
| 70 |
__config_gen++; |
|---|
| 71 |
/* reset the coalesce counter. It is decremented each second and |
|---|
| 72 |
* the journal function fires on a transition from 1 => 0 |
|---|
| 73 |
*/ |
|---|
| 74 |
__config_coalesce = __config_coalesce_time; |
|---|
| 75 |
} |
|---|
| 76 |
struct recurrent_journaler { |
|---|
| 77 |
int (*journal_config)(void *); |
|---|
| 78 |
void *jc_closure; |
|---|
| 79 |
}; |
|---|
| 80 |
static int |
|---|
| 81 |
noit_conf_watch_config_and_journal(eventer_t e, int mask, void *closure, |
|---|
| 82 |
struct timeval *now) { |
|---|
| 83 |
struct recurrent_journaler *rj = closure; |
|---|
| 84 |
eventer_t newe; |
|---|
| 85 |
|
|---|
| 86 |
if(__config_coalesce == 1) |
|---|
| 87 |
rj->journal_config(rj->jc_closure); |
|---|
| 88 |
if(__config_coalesce > 0) |
|---|
| 89 |
__config_coalesce--; |
|---|
| 90 |
|
|---|
| 91 |
/* Schedule the same event to fire a second form now */ |
|---|
| 92 |
newe = eventer_alloc(); |
|---|
| 93 |
gettimeofday(&newe->whence, NULL); |
|---|
| 94 |
newe->whence.tv_sec += 1; |
|---|
| 95 |
newe->mask = EVENTER_TIMER; |
|---|
| 96 |
newe->callback = noit_conf_watch_config_and_journal; |
|---|
| 97 |
newe->closure = closure; |
|---|
| 98 |
eventer_add(newe); |
|---|
| 99 |
return 0; |
|---|
| 100 |
} |
|---|
| 101 |
void |
|---|
| 102 |
noit_conf_watch_and_journal_watchdog(int (*f)(void *), void *c) { |
|---|
| 103 |
struct recurrent_journaler *rj; |
|---|
| 104 |
struct timeval __now; |
|---|
| 105 |
rj = calloc(1, sizeof(*rj)); |
|---|
| 106 |
rj->journal_config = f; |
|---|
| 107 |
rj->jc_closure = c; |
|---|
| 108 |
gettimeofday(&__now, NULL); |
|---|
| 109 |
noit_conf_watch_config_and_journal(NULL, EVENTER_TIMER, rj, &__now); |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
static noit_hash_table _compiled_fallback = NOIT_HASH_EMPTY; |
|---|
| 113 |
static struct { |
|---|
| 114 |
const char *key; |
|---|
| 115 |
const char *val; |
|---|
| 116 |
} config_info[] = { |
|---|
| 117 |
/* |
|---|
| 118 |
* These are compile-time fallbacks to be used in the event |
|---|
| 119 |
* that the current running config does not have values for |
|---|
| 120 |
* these config paths. |
|---|
| 121 |
* |
|---|
| 122 |
* PLEASE: keep them alphabetically sorted. |
|---|
| 123 |
*/ |
|---|
| 124 |
{ "/%s/eventer/@implementation", DEFAULT_EVENTER }, |
|---|
| 125 |
{ "/%s/modules/@directory", MODULES_DIR }, |
|---|
| 126 |
|
|---|
| 127 |
{ NULL, NULL } |
|---|
| 128 |
}; |
|---|
| 129 |
|
|---|
| 130 |
void noit_conf_xml_error_func(void *ctx, const char *format, ...) { |
|---|
| 131 |
struct timeval __now; |
|---|
| 132 |
noit_log_stream_t ls = ctx; |
|---|
| 133 |
va_list arg; |
|---|
| 134 |
if(!ls) return; |
|---|
| 135 |
va_start(arg, format); |
|---|
| 136 |
gettimeofday(&__now, NULL); |
|---|
| 137 |
noit_vlog(ls, &__now, __FILE__, __LINE__, format, arg); |
|---|
| 138 |
va_end(arg); |
|---|
| 139 |
} |
|---|
| 140 |
void noit_conf_xml_error_ext_func(void *ctx, xmlErrorPtr err) { |
|---|
| 141 |
struct timeval __now; |
|---|
| 142 |
noit_log_stream_t ls = ctx; |
|---|
| 143 |
if(!ls) return; |
|---|
| 144 |
gettimeofday(&__now, NULL); |
|---|
| 145 |
if(err->file) |
|---|
| 146 |
noit_log(ls, &__now, err->file, err->line, |
|---|
| 147 |
"XML error [%d/%d] in %s on line %d %s\n", |
|---|
| 148 |
err->domain, err->code, err->file, err->line, err->message); |
|---|
| 149 |
else |
|---|
| 150 |
noit_log(ls, &__now, err->file, err->line, |
|---|
| 151 |
"XML error [%d/%d] %s\n", |
|---|
| 152 |
err->domain, err->code, err->message); |
|---|
| 153 |
} |
|---|
| 154 |
void noit_conf_init(const char *toplevel) { |
|---|
| 155 |
int i; |
|---|
| 156 |
char keystr[256]; |
|---|
| 157 |
xmlSetGenericErrorFunc(noit_error, noit_conf_xml_error_func); |
|---|
| 158 |
xmlSetStructuredErrorFunc(noit_error, noit_conf_xml_error_ext_func); |
|---|
| 159 |
for(i = 0; config_info[i].key != NULL; i++) { |
|---|
| 160 |
snprintf(keystr, sizeof(keystr), config_info[i].key, toplevel); |
|---|
| 161 |
noit_hash_store(&_compiled_fallback, |
|---|
| 162 |
strdup(keystr), strlen(keystr), |
|---|
| 163 |
(void *)strdup(config_info[i].val)); |
|---|
| 164 |
} |
|---|
| 165 |
xmlKeepBlanksDefault(0); |
|---|
| 166 |
xmlInitParser(); |
|---|
| 167 |
xmlXPathInit(); |
|---|
| 168 |
} |
|---|
| 169 |
|
|---|
| 170 |
int noit_conf_load(const char *path) { |
|---|
| 171 |
xmlDocPtr new_config; |
|---|
| 172 |
new_config = xmlParseFile(path); |
|---|
| 173 |
if(new_config) { |
|---|
| 174 |
if(master_config) xmlFreeDoc(master_config); |
|---|
| 175 |
if(xpath_ctxt) xmlXPathFreeContext(xpath_ctxt); |
|---|
| 176 |
|
|---|
| 177 |
master_config = new_config; |
|---|
| 178 |
xpath_ctxt = xmlXPathNewContext(master_config); |
|---|
| 179 |
if(path != master_config_file) |
|---|
| 180 |
if(realpath(path, master_config_file) == NULL) |
|---|
| 181 |
noitL(noit_error, "realpath failed: %s\n", strerror(errno)); |
|---|
| 182 |
noit_conf_mark_changed(); |
|---|
| 183 |
return 0; |
|---|
| 184 |
} |
|---|
| 185 |
return -1; |
|---|
| 186 |
} |
|---|
| 187 |
|
|---|
| 188 |
char *noit_conf_config_filename() { |
|---|
| 189 |
return strdup(master_config_file); |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
int noit_conf_xml_xpath(xmlDocPtr *mc, xmlXPathContextPtr *xp) { |
|---|
| 193 |
if(mc) *mc = master_config; |
|---|
| 194 |
if(xp) *xp = xpath_ctxt; |
|---|
| 195 |
return 0; |
|---|
| 196 |
} |
|---|
| 197 |
int noit_conf_save(const char *path) { |
|---|
| 198 |
return -1; |
|---|
| 199 |
} |
|---|
| 200 |
|
|---|
| 201 |
void noit_conf_get_elements_into_hash(noit_conf_section_t section, |
|---|
| 202 |
const char *path, |
|---|
| 203 |
noit_hash_table *table) { |
|---|
| 204 |
int i, cnt; |
|---|
| 205 |
xmlXPathObjectPtr pobj = NULL; |
|---|
| 206 |
xmlXPathContextPtr current_ctxt; |
|---|
| 207 |
xmlNodePtr current_node = (xmlNodePtr)section; |
|---|
| 208 |
xmlNodePtr node; |
|---|
| 209 |
|
|---|
| 210 |
current_ctxt = xpath_ctxt; |
|---|
| 211 |
if(current_node) { |
|---|
| 212 |
current_ctxt = xmlXPathNewContext(master_config); |
|---|
| 213 |
current_ctxt->node = current_node; |
|---|
| 214 |
} |
|---|
| 215 |
pobj = xmlXPathEval((xmlChar *)path, current_ctxt); |
|---|
| 216 |
if(!pobj) goto out; |
|---|
| 217 |
if(pobj->type != XPATH_NODESET) goto out; |
|---|
| 218 |
if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto out; |
|---|
| 219 |
cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); |
|---|
| 220 |
for(i=0; i<cnt; i++) { |
|---|
| 221 |
char *value; |
|---|
| 222 |
node = xmlXPathNodeSetItem(pobj->nodesetval, i); |
|---|
| 223 |
value = (char *)xmlXPathCastNodeToString(node); |
|---|
| 224 |
noit_hash_replace(table, |
|---|
| 225 |
strdup((char *)node->name), strlen((char *)node->name), |
|---|
| 226 |
strdup(value), free, free); |
|---|
| 227 |
xmlFree(value); |
|---|
| 228 |
} |
|---|
| 229 |
out: |
|---|
| 230 |
if(pobj) xmlXPathFreeObject(pobj); |
|---|
| 231 |
if(current_ctxt && current_ctxt != xpath_ctxt) |
|---|
| 232 |
xmlXPathFreeContext(current_ctxt); |
|---|
| 233 |
} |
|---|
| 234 |
void noit_conf_get_into_hash(noit_conf_section_t section, |
|---|
| 235 |
const char *path, |
|---|
| 236 |
noit_hash_table *table) { |
|---|
| 237 |
int cnt; |
|---|
| 238 |
xmlXPathObjectPtr pobj = NULL; |
|---|
| 239 |
xmlXPathContextPtr current_ctxt; |
|---|
| 240 |
xmlNodePtr current_node = (xmlNodePtr)section; |
|---|
| 241 |
xmlNodePtr node, parent_node; |
|---|
| 242 |
char xpath_expr[1024]; |
|---|
| 243 |
char *inheritid; |
|---|
| 244 |
|
|---|
| 245 |
current_ctxt = xpath_ctxt; |
|---|
| 246 |
if(current_node) { |
|---|
| 247 |
current_ctxt = xmlXPathNewContext(master_config); |
|---|
| 248 |
current_ctxt->node = current_node; |
|---|
| 249 |
} |
|---|
| 250 |
if(path[0] == '/') |
|---|
| 251 |
strlcpy(xpath_expr, path, sizeof(xpath_expr)); |
|---|
| 252 |
else |
|---|
| 253 |
snprintf(xpath_expr, sizeof(xpath_expr), |
|---|
| 254 |
"ancestor-or-self::node()/%s", path); |
|---|
| 255 |
pobj = xmlXPathEval((xmlChar *)xpath_expr, current_ctxt); |
|---|
| 256 |
if(!pobj) goto out; |
|---|
| 257 |
if(pobj->type != XPATH_NODESET) goto out; |
|---|
| 258 |
if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto out; |
|---|
| 259 |
cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); |
|---|
| 260 |
/* These are in the order of root to leaf |
|---|
| 261 |
* We want to recurse... apply: |
|---|
| 262 |
* 1. our parent's config |
|---|
| 263 |
* 2. our "inherit" config if it exists. |
|---|
| 264 |
* 3. our config. |
|---|
| 265 |
*/ |
|---|
| 266 |
node = xmlXPathNodeSetItem(pobj->nodesetval, cnt-1); |
|---|
| 267 |
/* 1. */ |
|---|
| 268 |
if(cnt > 1) { |
|---|
| 269 |
parent_node = xmlXPathNodeSetItem(pobj->nodesetval, cnt-2); |
|---|
| 270 |
if(parent_node != current_node) |
|---|
| 271 |
noit_conf_get_into_hash(parent_node, (const char *)node->name, table); |
|---|
| 272 |
} |
|---|
| 273 |
/* 2. */ |
|---|
| 274 |
inheritid = (char *)xmlGetProp(node, (xmlChar *)"inherit"); |
|---|
| 275 |
if(inheritid) { |
|---|
| 276 |
snprintf(xpath_expr, sizeof(xpath_expr), "//*[@id=\"%s\"]", inheritid); |
|---|
| 277 |
noit_conf_get_into_hash(NULL, xpath_expr, table); |
|---|
| 278 |
} |
|---|
| 279 |
/* 3. */ |
|---|
| 280 |
noit_conf_get_elements_into_hash(node, "*", table); |
|---|
| 281 |
|
|---|
| 282 |
out: |
|---|
| 283 |
if(pobj) xmlXPathFreeObject(pobj); |
|---|
| 284 |
if(current_ctxt && current_ctxt != xpath_ctxt) |
|---|
| 285 |
xmlXPathFreeContext(current_ctxt); |
|---|
| 286 |
} |
|---|
| 287 |
noit_hash_table *noit_conf_get_hash(noit_conf_section_t section, |
|---|
| 288 |
const char *path) { |
|---|
| 289 |
noit_hash_table *table = NULL; |
|---|
| 290 |
|
|---|
| 291 |
table = calloc(1, sizeof(*table)); |
|---|
| 292 |
noit_conf_get_into_hash(section, path, table); |
|---|
| 293 |
return table; |
|---|
| 294 |
} |
|---|
| 295 |
noit_conf_section_t noit_conf_get_section(noit_conf_section_t section, |
|---|
| 296 |
const char *path) { |
|---|
| 297 |
noit_conf_section_t subsection = NULL; |
|---|
| 298 |
xmlXPathObjectPtr pobj = NULL; |
|---|
| 299 |
xmlXPathContextPtr current_ctxt; |
|---|
| 300 |
xmlNodePtr current_node = (xmlNodePtr)section; |
|---|
| 301 |
|
|---|
| 302 |
current_ctxt = xpath_ctxt; |
|---|
| 303 |
if(current_node) { |
|---|
| 304 |
current_ctxt = xmlXPathNewContext(master_config); |
|---|
| 305 |
current_ctxt->node = current_node; |
|---|
| 306 |
} |
|---|
| 307 |
pobj = xmlXPathEval((xmlChar *)path, current_ctxt); |
|---|
| 308 |
if(!pobj) goto out; |
|---|
| 309 |
if(pobj->type != XPATH_NODESET) goto out; |
|---|
| 310 |
if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto out; |
|---|
| 311 |
subsection = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0); |
|---|
| 312 |
out: |
|---|
| 313 |
if(pobj) xmlXPathFreeObject(pobj); |
|---|
| 314 |
if(current_ctxt && current_ctxt != xpath_ctxt) |
|---|
| 315 |
xmlXPathFreeContext(current_ctxt); |
|---|
| 316 |
return subsection; |
|---|
| 317 |
} |
|---|
| 318 |
noit_conf_section_t *noit_conf_get_sections(noit_conf_section_t section, |
|---|
| 319 |
const char *path, |
|---|
| 320 |
int *cnt) { |
|---|
| 321 |
int i; |
|---|
| 322 |
noit_conf_section_t *sections = NULL; |
|---|
| 323 |
xmlXPathObjectPtr pobj = NULL; |
|---|
| 324 |
xmlXPathContextPtr current_ctxt; |
|---|
| 325 |
xmlNodePtr current_node = (xmlNodePtr)section; |
|---|
| 326 |
|
|---|
| 327 |
*cnt = 0; |
|---|
| 328 |
current_ctxt = xpath_ctxt; |
|---|
| 329 |
if(current_node) { |
|---|
| 330 |
current_ctxt = xmlXPathNewContext(master_config); |
|---|
| 331 |
current_ctxt->node = current_node; |
|---|
| 332 |
} |
|---|
| 333 |
pobj = xmlXPathEval((xmlChar *)path, current_ctxt); |
|---|
| 334 |
if(!pobj) goto out; |
|---|
| 335 |
if(pobj->type != XPATH_NODESET) goto out; |
|---|
| 336 |
if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto out; |
|---|
| 337 |
*cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); |
|---|
| 338 |
sections = calloc(*cnt, sizeof(*sections)); |
|---|
| 339 |
for(i=0; i<*cnt; i++) |
|---|
| 340 |
sections[i] = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i); |
|---|
| 341 |
out: |
|---|
| 342 |
if(pobj) xmlXPathFreeObject(pobj); |
|---|
| 343 |
if(current_ctxt && current_ctxt != xpath_ctxt) |
|---|
| 344 |
xmlXPathFreeContext(current_ctxt); |
|---|
| 345 |
return sections; |
|---|
| 346 |
} |
|---|
| 347 |
int _noit_conf_get_string(noit_conf_section_t section, xmlNodePtr *vnode, |
|---|
| 348 |
const char *path, char **value) { |
|---|
| 349 |
const char *str; |
|---|
| 350 |
int i, rv = 1; |
|---|
| 351 |
xmlXPathObjectPtr pobj = NULL; |
|---|
| 352 |
xmlXPathContextPtr current_ctxt; |
|---|
| 353 |
xmlNodePtr current_node = (xmlNodePtr)section; |
|---|
| 354 |
|
|---|
| 355 |
current_ctxt = xpath_ctxt; |
|---|
| 356 |
if(current_node) { |
|---|
| 357 |
current_ctxt = xmlXPathNewContext(master_config); |
|---|
| 358 |
current_ctxt->node = current_node; |
|---|
| 359 |
} |
|---|
| 360 |
pobj = xmlXPathEval((xmlChar *)path, current_ctxt); |
|---|
| 361 |
if(pobj) { |
|---|
| 362 |
xmlNodePtr node; |
|---|
| 363 |
switch(pobj->type) { |
|---|
| 364 |
case XPATH_NODESET: |
|---|
| 365 |
if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto fallback; |
|---|
| 366 |
i = xmlXPathNodeSetGetLength(pobj->nodesetval); |
|---|
| 367 |
node = xmlXPathNodeSetItem(pobj->nodesetval, i-1); |
|---|
| 368 |
if(vnode) *vnode = node; |
|---|
| 369 |
*value = (char *)xmlXPathCastNodeToString(node); |
|---|
| 370 |
break; |
|---|
| 371 |
default: |
|---|
| 372 |
*value = (char *)xmlXPathCastToString(pobj); |
|---|
| 373 |
} |
|---|
| 374 |
goto found; |
|---|
| 375 |
} |
|---|
| 376 |
fallback: |
|---|
| 377 |
if(noit_hash_retr_str(&_compiled_fallback, |
|---|
| 378 |
path, strlen(path), &str)) { |
|---|
| 379 |
*value = (char *)xmlStrdup((xmlChar *)str); |
|---|
| 380 |
goto found; |
|---|
| 381 |
} |
|---|
| 382 |
rv = 0; |
|---|
| 383 |
found: |
|---|
| 384 |
if(pobj) xmlXPathFreeObject(pobj); |
|---|
| 385 |
if(current_ctxt && current_ctxt != xpath_ctxt) |
|---|
| 386 |
xmlXPathFreeContext(current_ctxt); |
|---|
| 387 |
return rv; |
|---|
| 388 |
} |
|---|
| 389 |
int noit_conf_get_uuid(noit_conf_section_t section, |
|---|
| 390 |
const char *path, uuid_t out) { |
|---|
| 391 |
char *str; |
|---|
| 392 |
if(_noit_conf_get_string(section,NULL,path,&str)) { |
|---|
| 393 |
if(uuid_parse(str, out) == 0) return 1; |
|---|
| 394 |
return 0; |
|---|
| 395 |
} |
|---|
| 396 |
return 0; |
|---|
| 397 |
} |
|---|
| 398 |
int noit_conf_get_string(noit_conf_section_t section, |
|---|
| 399 |
const char *path, char **value) { |
|---|
| 400 |
char *str; |
|---|
| 401 |
if(_noit_conf_get_string(section,NULL,path,&str)) { |
|---|
| 402 |
*value = strdup(str); |
|---|
| 403 |
xmlFree(str); |
|---|
| 404 |
return 1; |
|---|
| 405 |
} |
|---|
| 406 |
return 0; |
|---|
| 407 |
} |
|---|
| 408 |
int noit_conf_get_stringbuf(noit_conf_section_t section, |
|---|
| 409 |
const char *path, char *buf, int len) { |
|---|
| 410 |
char *str; |
|---|
| 411 |
if(_noit_conf_get_string(section,NULL,path,&str)) { |
|---|
| 412 |
strlcpy(buf, str, len); |
|---|
| 413 |
xmlFree(str); |
|---|
| 414 |
return 1; |
|---|
| 415 |
} |
|---|
| 416 |
return 0; |
|---|
| 417 |
} |
|---|
| 418 |
int noit_conf_set_string(noit_conf_section_t section, |
|---|
| 419 |
const char *path, const char *value) { |
|---|
| 420 |
noit_hash_replace(&_tmp_config, |
|---|
| 421 |
strdup(path), strlen(path), (void *)strdup(value), |
|---|
| 422 |
free, free); |
|---|
| 423 |
return 1; |
|---|
| 424 |
} |
|---|
| 425 |
int noit_conf_get_int(noit_conf_section_t section, |
|---|
| 426 |
const char *path, int *value) { |
|---|
| 427 |
char *str; |
|---|
| 428 |
long longval; |
|---|
| 429 |
if(_noit_conf_get_string(section,NULL,path,&str)) { |
|---|
| 430 |
int base = 10; |
|---|
| 431 |
if(str[0] == '0') { |
|---|
| 432 |
if(str[1] == 'x') base = 16; |
|---|
| 433 |
else base = 8; |
|---|
| 434 |
} |
|---|
| 435 |
longval = strtol(str, NULL, base); |
|---|
| 436 |
xmlFree(str); |
|---|
| 437 |
*value = (int)longval; |
|---|
| 438 |
return 1; |
|---|
| 439 |
} |
|---|
| 440 |
return 0; |
|---|
| 441 |
} |
|---|
| 442 |
int noit_conf_set_int(noit_conf_section_t section, |
|---|
| 443 |
const char *path, int value) { |
|---|
| 444 |
char buffer[32]; |
|---|
| 445 |
snprintf(buffer, 32, "%d", value); |
|---|
| 446 |
return noit_conf_set_string(section,path,buffer); |
|---|
| 447 |
} |
|---|
| 448 |
int noit_conf_get_float(noit_conf_section_t section, |
|---|
| 449 |
const char *path, float *value) { |
|---|
| 450 |
char *str; |
|---|
| 451 |
if(_noit_conf_get_string(section,NULL,path,&str)) { |
|---|
| 452 |
*value = atof(str); |
|---|
| 453 |
xmlFree(str); |
|---|
| 454 |
return 1; |
|---|
| 455 |
} |
|---|
| 456 |
return 0; |
|---|
| 457 |
} |
|---|
| 458 |
int noit_conf_set_float(noit_conf_section_t section, |
|---|
| 459 |
const char *path, float value) { |
|---|
| 460 |
char buffer[32]; |
|---|
| 461 |
snprintf(buffer, 32, "%f", value); |
|---|
| 462 |
return noit_conf_set_string(section,path,buffer); |
|---|
| 463 |
} |
|---|
| 464 |
int noit_conf_get_boolean(noit_conf_section_t section, |
|---|
| 465 |
const char *path, noit_boolean *value) { |
|---|
| 466 |
char *str; |
|---|
| 467 |
if(_noit_conf_get_string(section,NULL,path,&str)) { |
|---|
| 468 |
if(!strcasecmp(str, "true") || |
|---|
| 469 |
!strcasecmp(str, "on")) *value = noit_true; |
|---|
| 470 |
else *value = noit_false; |
|---|
| 471 |
xmlFree(str); |
|---|
| 472 |
return 1; |
|---|
| 473 |
} |
|---|
| 474 |
return 0; |
|---|
| 475 |
} |
|---|
| 476 |
int noit_conf_set_boolean(noit_conf_section_t section, |
|---|
| 477 |
const char *path, noit_boolean value) { |
|---|
| 478 |
if(value == noit_true) |
|---|
| 479 |
return noit_conf_set_string(section,path,"true"); |
|---|
| 480 |
return noit_conf_set_string(section,path,"false"); |
|---|
| 481 |
} |
|---|
| 482 |
|
|---|
| 483 |
struct config_line_vstr { |
|---|
| 484 |
char *buff; |
|---|
| 485 |
int raw_len; |
|---|
| 486 |
int len; |
|---|
| 487 |
int allocd; |
|---|
| 488 |
enum { CONFIG_RAW = 0, CONFIG_COMPRESSED, CONFIG_B64 } target, encoded; |
|---|
| 489 |
}; |
|---|
| 490 |
static int |
|---|
| 491 |
noit_config_log_write_xml(void *vstr, const char *buffer, int len) { |
|---|
| 492 |
struct config_line_vstr *clv = vstr; |
|---|
| 493 |
assert(clv->encoded == CONFIG_RAW); |
|---|
| 494 |
if(!clv->buff) { |
|---|
| 495 |
clv->allocd = 8192; |
|---|
| 496 |
clv->buff = malloc(clv->allocd); |
|---|
| 497 |
} |
|---|
| 498 |
while(len + clv->len > clv->allocd) { |
|---|
| 499 |
char *newbuff; |
|---|
| 500 |
int newsize = clv->allocd; |
|---|
| 501 |
newsize <<= 1; |
|---|
| 502 |
newbuff = realloc(clv->buff, newsize); |
|---|
| 503 |
if(!newbuff) { |
|---|
| 504 |
return -1; |
|---|
| 505 |
} |
|---|
| 506 |
clv->allocd = newsize; |
|---|
| 507 |
clv->buff = newbuff; |
|---|
| 508 |
} |
|---|
| 509 |
memcpy(clv->buff + clv->len, buffer, len); |
|---|
| 510 |
clv->len += len; |
|---|
| 511 |
return len; |
|---|
| 512 |
} |
|---|
| 513 |
static int |
|---|
| 514 |
noit_config_log_close_xml(void *vstr) { |
|---|
| 515 |
struct config_line_vstr *clv = vstr; |
|---|
| 516 |
uLong initial_dlen, dlen; |
|---|
| 517 |
char *compbuff, *b64buff; |
|---|
| 518 |
|
|---|
| 519 |
if(clv->buff == NULL) { |
|---|
| 520 |
clv->encoded = clv->target; |
|---|
| 521 |
return 0; |
|---|
| 522 |
} |
|---|
| 523 |
clv->raw_len = clv->len; |
|---|
| 524 |
assert(clv->encoded == CONFIG_RAW); |
|---|
| 525 |
if(clv->encoded == clv->target) return 0; |
|---|
| 526 |
|
|---|
| 527 |
/* Compress */ |
|---|
| 528 |
initial_dlen = dlen = compressBound(clv->len); |
|---|
| 529 |
compbuff = malloc(initial_dlen); |
|---|
| 530 |
if(!compbuff) return -1; |
|---|
| 531 |
if(Z_OK != compress2((Bytef *)compbuff, &dlen, |
|---|
| 532 |
(Bytef *)clv->buff, clv->len, 9)) { |
|---|
| 533 |
noitL(noit_error, "Error compressing config for transmission.\n"); |
|---|
| 534 |
free(compbuff); |
|---|
| 535 |
return -1; |
|---|
| 536 |
} |
|---|
| 537 |
free(clv->buff); |
|---|
| 538 |
clv->buff = compbuff; |
|---|
| 539 |
clv->allocd = initial_dlen; |
|---|
| 540 |
clv->len = dlen; |
|---|
| 541 |
clv->encoded = CONFIG_COMPRESSED; |
|---|
| 542 |
if(clv->encoded == clv->target) return 0; |
|---|
| 543 |
|
|---|
| 544 |
/* Encode */ |
|---|
| 545 |
initial_dlen = ((clv->len + 2) / 3) * 4; |
|---|
| 546 |
b64buff = malloc(initial_dlen); |
|---|
| 547 |
dlen = noit_b64_encode((unsigned char *)clv->buff, clv->len, |
|---|
| 548 |
b64buff, initial_dlen); |
|---|
| 549 |
if(dlen == 0) { |
|---|
| 550 |
free(b64buff); |
|---|
| 551 |
return -1; |
|---|
| 552 |
} |
|---|
| 553 |
free(clv->buff); |
|---|
| 554 |
clv->buff = b64buff; |
|---|
| 555 |
clv->allocd = initial_dlen; |
|---|
| 556 |
clv->len = dlen; |
|---|
| 557 |
clv->encoded = CONFIG_B64; |
|---|
| 558 |
if(clv->encoded == clv->target) return 0; |
|---|
| 559 |
return -1; |
|---|
| 560 |
} |
|---|
| 561 |
|
|---|
| 562 |
int |
|---|
| 563 |
noit_conf_reload(noit_console_closure_t ncct, |
|---|
| 564 |
int argc, char **argv, |
|---|
| 565 |
noit_console_state_t *state, void *closure) { |
|---|
| 566 |
if(noit_conf_load(master_config_file)) { |
|---|
| 567 |
nc_printf(ncct, "error loading config\n"); |
|---|
| 568 |
return -1; |
|---|
| 569 |
} |
|---|
| 570 |
return 0; |
|---|
| 571 |
} |
|---|
| 572 |
int |
|---|
| 573 |
noit_conf_write_terminal(noit_console_closure_t ncct, |
|---|
| 574 |
int argc, char **argv, |
|---|
| 575 |
noit_console_state_t *state, void *closure) { |
|---|
| 576 |
xmlOutputBufferPtr out; |
|---|
| 577 |
xmlCharEncodingHandlerPtr enc; |
|---|
| 578 |
enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8); |
|---|
| 579 |
out = xmlOutputBufferCreateIO(noit_console_write_xml, |
|---|
| 580 |
noit_console_close_xml, |
|---|
| 581 |
ncct, enc); |
|---|
| 582 |
xmlSaveFormatFileTo(out, master_config, "utf8", 1); |
|---|
| 583 |
return 0; |
|---|
| 584 |
} |
|---|
| 585 |
int |
|---|
| 586 |
noit_conf_write_file(noit_console_closure_t ncct, |
|---|
| 587 |
int argc, char **argv, |
|---|
| 588 |
noit_console_state_t *state, void *closure) { |
|---|
| 589 |
int fd, len; |
|---|
| 590 |
char master_file_tmp[PATH_MAX]; |
|---|
| 591 |
xmlOutputBufferPtr out; |
|---|
| 592 |
xmlCharEncodingHandlerPtr enc; |
|---|
| 593 |
|
|---|
| 594 |
snprintf(master_file_tmp, sizeof(master_file_tmp), |
|---|
| 595 |
"%s.tmp", master_config_file); |
|---|
| 596 |
unlink(master_file_tmp); |
|---|
| 597 |
fd = open(master_file_tmp, O_CREAT|O_EXCL|O_WRONLY, 0640); |
|---|
| 598 |
if(fd < 0) { |
|---|
| 599 |
nc_printf(ncct, "Failed to open tmp file: %s\n", strerror(errno)); |
|---|
| 600 |
return -1; |
|---|
| 601 |
} |
|---|
| 602 |
enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8); |
|---|
| 603 |
out = xmlOutputBufferCreateFd(fd, enc); |
|---|
| 604 |
if(!out) { |
|---|
| 605 |
close(fd); |
|---|
| 606 |
unlink(master_file_tmp); |
|---|
| 607 |
nc_printf(ncct, "internal error: OutputBufferCreate failed\n"); |
|---|
| 608 |
return -1; |
|---|
| 609 |
} |
|---|
| 610 |
len = xmlSaveFormatFileTo(out, master_config, "utf8", 1); |
|---|
| 611 |
close(fd); |
|---|
| 612 |
if(len <= 0) { |
|---|
| 613 |
nc_printf(ncct, "internal error: writing to tmp file failed.\n"); |
|---|
| 614 |
return -1; |
|---|
| 615 |
} |
|---|
| 616 |
if(rename(master_file_tmp, master_config_file) != 0) { |
|---|
| 617 |
nc_printf(ncct, "Failed to replace file: %s\n", strerror(errno)); |
|---|
| 618 |
return -1; |
|---|
| 619 |
} |
|---|
| 620 |
nc_printf(ncct, "%d bytes written.\n", len); |
|---|
| 621 |
return 0; |
|---|
| 622 |
} |
|---|
| 623 |
char * |
|---|
| 624 |
noit_conf_xml_in_mem(size_t *len) { |
|---|
| 625 |
struct config_line_vstr *clv; |
|---|
| 626 |
xmlOutputBufferPtr out; |
|---|
| 627 |
xmlCharEncodingHandlerPtr enc; |
|---|
| 628 |
char *rv; |
|---|
| 629 |
|
|---|
| 630 |
clv = calloc(1, sizeof(*clv)); |
|---|
| 631 |
clv->target = CONFIG_RAW; |
|---|
| 632 |
enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8); |
|---|
| 633 |
out = xmlOutputBufferCreateIO(noit_config_log_write_xml, |
|---|
| 634 |
noit_config_log_close_xml, |
|---|
| 635 |
clv, enc); |
|---|
| 636 |
xmlSaveFormatFileTo(out, master_config, "utf8", 1); |
|---|
| 637 |
if(clv->encoded != CONFIG_RAW) { |
|---|
| 638 |
noitL(noit_error, "Error logging configuration\n"); |
|---|
| 639 |
if(clv->buff) free(clv->buff); |
|---|
| 640 |
free(clv); |
|---|
| 641 |
return NULL; |
|---|
| 642 |
} |
|---|
| 643 |
rv = clv->buff; |
|---|
| 644 |
*len = clv->len; |
|---|
| 645 |
free(clv); |
|---|
| 646 |
return rv; |
|---|
| 647 |
} |
|---|
| 648 |
|
|---|
| 649 |
int |
|---|
| 650 |
noit_conf_write_log() { |
|---|
| 651 |
static u_int32_t last_write_gen = 0; |
|---|
| 652 |
static noit_log_stream_t config_log = NULL; |
|---|
| 653 |
struct timeval __now; |
|---|
| 654 |
xmlOutputBufferPtr out; |
|---|
| 655 |
xmlCharEncodingHandlerPtr enc; |
|---|
| 656 |
struct config_line_vstr *clv; |
|---|
| 657 |
SETUP_LOG(config, return -1); |
|---|
| 658 |
|
|---|
| 659 |
/* We know we haven't changed */ |
|---|
| 660 |
if(last_write_gen == __config_gen) return 0; |
|---|
| 661 |
|
|---|
| 662 |
gettimeofday(&__now, NULL); |
|---|
| 663 |
clv = calloc(1, sizeof(*clv)); |
|---|
| 664 |
clv->target = CONFIG_B64; |
|---|
| 665 |
enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8); |
|---|
| 666 |
out = xmlOutputBufferCreateIO(noit_config_log_write_xml, |
|---|
| 667 |
noit_config_log_close_xml, |
|---|
| 668 |
clv, enc); |
|---|
| 669 |
xmlSaveFormatFileTo(out, master_config, "utf8", 1); |
|---|
| 670 |
if(clv->encoded != CONFIG_B64) { |
|---|
| 671 |
noitL(noit_error, "Error logging configuration\n"); |
|---|
| 672 |
if(clv->buff) free(clv->buff); |
|---|
| 673 |
free(clv); |
|---|
| 674 |
return -1; |
|---|
| 675 |
} |
|---|
| 676 |
noitL(config_log, "n\t%lu.%03lu\t%d\t%.*s\n", |
|---|
| 677 |
__now.tv_sec, __now.tv_usec / 1000UL, clv->raw_len, |
|---|
| 678 |
clv->len, clv->buff); |
|---|
| 679 |
free(clv->buff); |
|---|
| 680 |
free(clv); |
|---|
| 681 |
last_write_gen = __config_gen; |
|---|
| 682 |
return 0; |
|---|
| 683 |
} |
|---|
| 684 |
|
|---|
| 685 |
void |
|---|
| 686 |
noit_conf_log_init(const char *toplevel) { |
|---|
| 687 |
int i, cnt = 0, o, ocnt = 0; |
|---|
| 688 |
noit_conf_section_t *log_configs, *outlets; |
|---|
| 689 |
char path[256]; |
|---|
| 690 |
|
|---|
| 691 |
snprintf(path, sizeof(path), "/%s/logs//log", toplevel); |
|---|
| 692 |
log_configs = noit_conf_get_sections(NULL, path, &cnt); |
|---|
| 693 |
noitL(noit_stderr, "Found %d %s stanzas\n", cnt, path); |
|---|
| 694 |
for(i=0; i<cnt; i++) { |
|---|
| 695 |
noit_log_stream_t ls; |
|---|
| 696 |
char name[256], type[256], path[256]; |
|---|
| 697 |
noit_hash_table *config; |
|---|
| 698 |
noit_boolean disabled; |
|---|
| 699 |
noit_boolean debug; |
|---|
| 700 |
|
|---|
| 701 |
if(!noit_conf_get_stringbuf(log_configs[i], |
|---|
| 702 |
"ancestor-or-self::node()/@name", |
|---|
| 703 |
name, sizeof(name))) { |
|---|
| 704 |
noitL(noit_error, "log section %d does not have a name attribute\n", i+1); |
|---|
| 705 |
exit(-1); |
|---|
| 706 |
} |
|---|
| 707 |
if(!noit_conf_get_stringbuf(log_configs[i], |
|---|
| 708 |
"ancestor-or-self::node()/@type", |
|---|
| 709 |
type, sizeof(type))) { |
|---|
| 710 |
type[0] = '\0'; |
|---|
| 711 |
} |
|---|
| 712 |
if(!noit_conf_get_stringbuf(log_configs[i], |
|---|
| 713 |
"ancestor-or-self::node()/@path", |
|---|
| 714 |
path, sizeof(path))) { |
|---|
| 715 |
path[0] = '\0'; |
|---|
| 716 |
} |
|---|
| 717 |
config = noit_conf_get_hash(log_configs[i], |
|---|
| 718 |
"ancestor-or-self::node()/config/*"); |
|---|
| 719 |
ls = noit_log_stream_new(name, type[0] ? type : NULL, |
|---|
| 720 |
path[0] ? path : NULL, NULL, config); |
|---|
| 721 |
if(!ls) { |
|---|
| 722 |
fprintf(stderr, "Error configuring log: %s[%s:%s]\n", name, type, path); |
|---|
| 723 |
exit(-1); |
|---|
| 724 |
} |
|---|
| 725 |
|
|---|
| 726 |
if(noit_conf_get_boolean(log_configs[i], |
|---|
| 727 |
"ancestor-or-self::node()/@disabled", |
|---|
| 728 |
&disabled) && disabled) |
|---|
| 729 |
ls->enabled = 0; |
|---|
| 730 |
|
|---|
| 731 |
if(noit_conf_get_boolean(log_configs[i], |
|---|
| 732 |
"ancestor-or-self::node()/@debug", |
|---|
| 733 |
&debug) && debug) |
|---|
| 734 |
ls->debug = 1; |
|---|
| 735 |
|
|---|
| 736 |
outlets = noit_conf_get_sections(log_configs[i], |
|---|
| 737 |
"ancestor-or-self::node()/outlet", &ocnt); |
|---|
| 738 |
noitL(noit_debug, "Found %d outlets for log '%s'\n", ocnt, name); |
|---|
| 739 |
|
|---|
| 740 |
for(o=0; o<ocnt; o++) { |
|---|
| 741 |
noit_log_stream_t outlet; |
|---|
| 742 |
char oname[256]; |
|---|
| 743 |
noit_conf_get_stringbuf(outlets[o], "@name", |
|---|
| 744 |
oname, sizeof(oname)); |
|---|
| 745 |
outlet = noit_log_stream_find(oname); |
|---|
| 746 |
if(!outlet) { |
|---|
| 747 |
fprintf(stderr, "Cannot find outlet '%s' for %s[%s:%s]\n", oname, |
|---|
| 748 |
name, type, path); |
|---|
| 749 |
exit(-1); |
|---|
| 750 |
} |
|---|
| 751 |
else |
|---|
| 752 |
noit_log_stream_add_stream(ls, outlet); |
|---|
| 753 |
} |
|---|
| 754 |
if(outlets) free(outlets); |
|---|
| 755 |
} |
|---|
| 756 |
if(log_configs) free(log_configs); |
|---|
| 757 |
} |
|---|