forked from circonus-labs/reconnoiter
/
noit_conf.c
1939 lines (1827 loc) · 61.2 KB
/
noit_conf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2007, OmniTI Computer Consulting, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name OmniTI Computer Consulting, Inc. nor the names
* of its contributors may be used to endorse or promote products
* derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "noit_defines.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <assert.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <zlib.h>
#include "noit_conf.h"
#include "noit_check.h"
#include "noit_console.h"
#include "noit_xml.h"
#include "utils/noit_hash.h"
#include "utils/noit_log.h"
#include "utils/noit_b64.h"
/* tmp hash impl, replace this with something nice */
static noit_log_stream_t xml_debug = NULL;
#define XML2LOG(log) do { \
xmlSetGenericErrorFunc(log, noit_conf_xml_error_func); \
xmlSetStructuredErrorFunc(log, noit_conf_xml_error_ext_func); \
} while(0)
#define XML2CONSOLE(ncct) do { \
xmlSetGenericErrorFunc(ncct, noit_conf_xml_console_error_func); \
xmlSetStructuredErrorFunc(ncct, noit_conf_xml_console_error_ext_func); \
} while(0)
static noit_hash_table _tmp_config = NOIT_HASH_EMPTY;
static xmlDocPtr master_config = NULL;
static int config_include_cnt = -1;
static int backingstore_include_cnt = -1;
static struct {
xmlNodePtr insertion_point;
xmlNodePtr old_children;
xmlDocPtr doc;
xmlNodePtr root;
} *config_include_nodes = NULL,
*backingstore_include_nodes = NULL;
typedef struct noit_xml_userdata {
char *name;
char *path;
u_int64_t dirty_time;
struct noit_xml_userdata *freelist;
} noit_xml_userdata_t;
static noit_xml_userdata_t *backingstore_freelist = NULL;
static u_int64_t last_config_flush = 0;
#define is_stopnode_name(n) ((n) && \
(!strcmp((char *)(n), "check") || \
!strcmp((char *)(n), "config") || \
!strcmp((char *)(n), "filterset")))
#define is_stopnode(node) ((node) && is_stopnode_name((node)->name))
static char *root_node_name = NULL;
static char master_config_file[PATH_MAX] = "";
static xmlXPathContextPtr xpath_ctxt = NULL;
/* This is used to notice config changes and journal the config out
* using a user-specified function. It supports allowing multiple config
* changed to coalesce so you don't write out 1000 changes in a few seconds.
*/
static u_int32_t __config_gen = 0;
static u_int32_t __config_coalesce = 0;
static u_int32_t __config_coalesce_time = 0;
static u_int64_t max_gen_count = 0;
void noit_conf_coalesce_changes(u_int32_t seconds) {
__config_coalesce_time = seconds;
}
void noit_conf_mark_changed() {
/* increment the change counter -- in case anyone cares */
__config_gen++;
/* reset the coalesce counter. It is decremented each second and
* the journal function fires on a transition from 1 => 0
*/
__config_coalesce = __config_coalesce_time;
}
struct recurrent_journaler {
int (*journal_config)(void *);
void *jc_closure;
};
static void
noit_xml_userdata_free(noit_xml_userdata_t *n) {
if(n->name) free(n->name);
if(n->path) free(n->path);
}
void
noit_conf_set_namespace(const char *ns) {
xmlNsPtr nsptr;
xmlNodePtr root;
root = xmlDocGetRootElement(master_config);
nsptr = xmlSearchNs(master_config, root, (xmlChar *)ns);
if(!nsptr) {
char url[128];
snprintf(url, sizeof(url), "noit://module/%s", ns);
nsptr = xmlNewNs(root, (xmlChar *)url, (xmlChar *)ns);
}
}
static int
noit_conf_watch_config_and_journal(eventer_t e, int mask, void *closure,
struct timeval *now) {
struct recurrent_journaler *rj = closure;
eventer_t newe;
if(__config_coalesce == 1)
rj->journal_config(rj->jc_closure);
if(__config_coalesce > 0)
__config_coalesce--;
/* Schedule the same event to fire a second form now */
newe = eventer_alloc();
gettimeofday(&newe->whence, NULL);
newe->whence.tv_sec += 1;
newe->mask = EVENTER_TIMER;
newe->callback = noit_conf_watch_config_and_journal;
newe->closure = closure;
eventer_add(newe);
return 0;
}
void
noit_conf_watch_and_journal_watchdog(int (*f)(void *), void *c) {
static int callbacknamed = 0;
struct recurrent_journaler *rj;
struct timeval __now;
if(!callbacknamed) {
callbacknamed = 1;
eventer_name_callback("noit_conf_watch_config_and_journal",
noit_conf_watch_config_and_journal);
}
rj = calloc(1, sizeof(*rj));
rj->journal_config = f;
rj->jc_closure = c;
gettimeofday(&__now, NULL);
noit_conf_watch_config_and_journal(NULL, EVENTER_TIMER, rj, &__now);
}
static noit_hash_table _compiled_fallback = NOIT_HASH_EMPTY;
static struct {
const char *key;
const char *val;
} config_info[] = {
/*
* These are compile-time fallbacks to be used in the event
* that the current running config does not have values for
* these config paths.
*
* PLEASE: keep them alphabetically sorted.
*/
{ "/%s/eventer/@implementation", DEFAULT_EVENTER },
{ "/%s/modules/@directory", MODULES_DIR },
{ NULL, NULL }
};
void noit_conf_xml_console_error_func(void *ctx, const char *format, ...) {
noit_console_closure_t ncct = ctx;
va_list arg;
if(!ncct) return;
va_start(arg, format);
nc_vprintf(ncct, format, arg);
va_end(arg);
}
void noit_conf_xml_console_error_ext_func(void *ctx, xmlErrorPtr err) {
noit_console_closure_t ncct = ctx;
if(!ctx) return;
if(err->file)
nc_printf(ncct, "XML error [%d/%d] in %s on line %d %s\n",
err->domain, err->code, err->file, err->line, err->message);
else
nc_printf(ncct, "XML error [%d/%d] %s\n",
err->domain, err->code, err->message);
}
void noit_conf_xml_error_func(void *ctx, const char *format, ...) {
struct timeval __now;
noit_log_stream_t ls = ctx;
va_list arg;
if(!ls) return;
va_start(arg, format);
gettimeofday(&__now, NULL);
noit_vlog(ls, &__now, __FILE__, __LINE__, format, arg);
va_end(arg);
}
void noit_conf_xml_error_ext_func(void *ctx, xmlErrorPtr err) {
struct timeval __now;
noit_log_stream_t ls = ctx;
if(!ls) return;
gettimeofday(&__now, NULL);
if(err->file)
noit_log(ls, &__now, err->file, err->line,
"XML error [%d/%d] in %s on line %d %s\n",
err->domain, err->code, err->file, err->line, err->message);
else
noit_log(ls, &__now, err->file, err->line,
"XML error [%d/%d] %s\n",
err->domain, err->code, err->message);
}
DECLARE_CHECKER(name)
void noit_conf_init(const char *toplevel) {
int i;
char keystr[256];
xml_debug = noit_log_stream_find("debug/xml");
COMPILE_CHECKER(name, "^[-_\\.:/a-zA-Z0-9]+$");
XML2LOG(noit_error);
for(i = 0; config_info[i].key != NULL; i++) {
snprintf(keystr, sizeof(keystr), config_info[i].key, toplevel);
noit_hash_store(&_compiled_fallback,
strdup(keystr), strlen(keystr),
(void *)strdup(config_info[i].val));
}
xmlKeepBlanksDefault(0);
xmlInitParser();
xmlXPathInit();
}
void
noit_conf_magic_separate(xmlDocPtr doc) {
assert(config_include_cnt != -1);
if(config_include_nodes) {
int i;
for(i=0; i<config_include_cnt; i++) {
if(config_include_nodes[i].doc) {
xmlNodePtr n;
for(n=config_include_nodes[i].insertion_point->children;
n; n = n->next)
n->parent = config_include_nodes[i].root;
config_include_nodes[i].insertion_point->children =
config_include_nodes[i].old_children;
xmlFreeDoc(config_include_nodes[i].doc);
}
}
free(config_include_nodes);
}
config_include_nodes = NULL;
config_include_cnt = -1;
if(backingstore_include_nodes) {
int i;
for(i=0; i<backingstore_include_cnt; i++) {
if(backingstore_include_nodes[i].doc) {
xmlNodePtr n;
for(n=backingstore_include_nodes[i].insertion_point->children;
n; n = n->next) {
n->parent = backingstore_include_nodes[i].root;
n->parent->last = n;
}
backingstore_include_nodes[i].insertion_point->children =
backingstore_include_nodes[i].old_children;
for(n=backingstore_include_nodes[i].insertion_point->children;
n; n = n->next) {
n->parent->last = n; /* sets it to the last child */
}
xmlFreeDoc(backingstore_include_nodes[i].doc);
}
}
free(backingstore_include_nodes);
}
backingstore_include_nodes = NULL;
backingstore_include_cnt = -1;
}
void
noit_conf_kansas_city_shuffle_redo(xmlDocPtr doc) {
if(config_include_nodes) {
int i;
for(i=0; i<config_include_cnt; i++) {
if(config_include_nodes[i].doc) {
xmlNodePtr n;
config_include_nodes[i].insertion_point->children =
config_include_nodes[i].root->children;
for(n=config_include_nodes[i].insertion_point->children;
n; n = n->next)
n->parent = config_include_nodes[i].insertion_point;
}
}
}
}
void
noit_conf_kansas_city_shuffle_undo(xmlDocPtr doc) {
if(config_include_nodes) {
int i;
for(i=0; i<config_include_cnt; i++) {
if(config_include_nodes[i].doc) {
xmlNodePtr n;
for(n=config_include_nodes[i].insertion_point->children;
n; n = n->next)
n->parent = config_include_nodes[i].root;
config_include_nodes[i].insertion_point->children =
config_include_nodes[i].old_children;
}
}
}
}
static u_int64_t
usec_now() {
u_int64_t usec;
struct timeval tv;
gettimeofday(&tv, NULL);
usec = tv.tv_sec * 1000000UL;
usec += tv.tv_usec;
return usec;
}
void
noit_conf_backingstore_remove(noit_conf_section_t vnode) {
xmlNodePtr node = vnode;
noit_xml_userdata_t *subctx = node->_private;
if(subctx) {
noitL(noit_debug, "marking %s for removal\n", subctx->path);
if(!backingstore_freelist) backingstore_freelist = subctx;
else {
noit_xml_userdata_t *fl = backingstore_freelist;
while(fl->freelist) fl = fl->freelist;
fl->freelist = subctx;
}
node->_private = NULL;
}
/* If we're deleted, we'll mark the parent as dirty */
if(node->parent) noit_conf_backingstore_dirty(node->parent);
}
void
noit_conf_backingstore_dirty(noit_conf_section_t vnode) {
xmlNodePtr node = vnode;
noit_xml_userdata_t *subctx = node->_private;
if(subctx) {
subctx->dirty_time = usec_now();
return;
}
if(node->parent) noit_conf_backingstore_dirty(node->parent);
}
int
noit_conf_backingstore_write(noit_xml_userdata_t *ctx, noit_boolean skip,
xmlAttrPtr attrs, xmlNodePtr node) {
int failure = 0;
char newpath[PATH_MAX];
xmlNodePtr n;
snprintf(newpath, sizeof(newpath), "%s/.attrs", ctx->path);
if(attrs) {
xmlDocPtr tmpdoc;
xmlNodePtr tmpnode;
noitL(noit_debug, " **> %s\n", newpath);
tmpdoc = xmlNewDoc((xmlChar *)"1.0");
tmpnode = xmlNewNode(NULL, ctx->name ? (xmlChar *)ctx->name : (xmlChar *)"stub");
xmlDocSetRootElement(tmpdoc, tmpnode);
tmpnode->properties = attrs;
failure = noit_xmlSaveToFile(tmpdoc, newpath);
tmpnode->properties = NULL;
xmlFreeDoc(tmpdoc);
if(failure) return -1;
}
else if(!skip) {
unlink(newpath);
}
for(n = node; n; n = n->next) {
int leaf;
noit_xml_userdata_t *subctx;
subctx = n->_private;
leaf = is_stopnode(n);
if(!subctx) { /* This has never been written out */
subctx = calloc(1, sizeof(*subctx));
subctx->name = strdup((char *)n->name);
snprintf(newpath, sizeof(newpath), "%s/%s#%llu", ctx->path, n->name, ++max_gen_count);
if(leaf) strlcat(newpath, ".xml", sizeof(newpath));
subctx->path = strdup(newpath);
subctx->dirty_time = usec_now();
n->_private = subctx;
noitL(noit_debug, " !!> %s\n", subctx->path);
}
if(leaf) {
xmlDocPtr tmpdoc;
xmlNodePtr tmpnode;
if(subctx->dirty_time > last_config_flush) {
tmpdoc = xmlNewDoc((xmlChar *)"1.0");
tmpnode = xmlNewNode(NULL, n->name);
xmlDocSetRootElement(tmpdoc, tmpnode);
tmpnode->properties = n->properties;
tmpnode->children = n->children;
failure = noit_xmlSaveToFile(tmpdoc, subctx->path);
tmpnode->properties = NULL;
tmpnode->children = NULL;
xmlFreeDoc(tmpdoc);
noitL(noit_debug, " ==> %s\n", subctx->path);
if(failure) return -1;
}
}
else {
noit_boolean skip_attrs;
skip_attrs = leaf || (subctx->dirty_time <= last_config_flush);
noitL(noit_debug, " --> %s\n", subctx->path);
if(noit_conf_backingstore_write(subctx, skip_attrs, skip_attrs ? NULL : n->properties, n->children))
return -1;
}
}
return 0;
}
void
noit_conf_shatter_write(xmlDocPtr doc) {
if(backingstore_freelist) {
noit_xml_userdata_t *fl, *last;
for(fl = backingstore_freelist; fl; ) {
last = fl;
fl = fl->freelist;
/* If it is a file, we'll unlink it, otherwise,
* we need to delete the attributes and the directory.
*/
if(unlink(last->path)) {
char attrpath[PATH_MAX];
snprintf(attrpath, sizeof(attrpath), "%s/.attrs", last->path);
unlink(attrpath);
if(rmdir(last->path) && errno != ENOENT) {
/* This shouldn't happen, but if it does we risk
* leaving a mess. Don't do that.
*/
noitL(noit_error, "backingstore mess %s: %s\n",
last->path, strerror(errno));
}
}
noit_xml_userdata_free(last);
}
backingstore_freelist = NULL;
}
if(backingstore_include_nodes) {
int i;
for(i=0; i<backingstore_include_cnt; i++) {
if(backingstore_include_nodes[i].doc) {
xmlNodePtr n;
noit_xml_userdata_t *what = backingstore_include_nodes[i].doc->_private;
for(n=backingstore_include_nodes[i].insertion_point->children;
n; n = n->next) {
n->parent = backingstore_include_nodes[i].root;
n->parent->last = n;
}
backingstore_include_nodes[i].root->children =
backingstore_include_nodes[i].insertion_point->children;
backingstore_include_nodes[i].insertion_point->children =
backingstore_include_nodes[i].old_children;
for(n=backingstore_include_nodes[i].insertion_point->children;
n; n = n->next) {
n->parent->last = n; /* sets it to the last child */
}
noit_conf_backingstore_write(what, noit_false, NULL, backingstore_include_nodes[i].root->children);
}
}
last_config_flush = usec_now();
}
}
void
noit_conf_shatter_postwrite(xmlDocPtr doc) {
if(backingstore_include_nodes) {
int i;
for(i=0; i<backingstore_include_cnt; i++) {
if(backingstore_include_nodes[i].doc) {
xmlNodePtr n;
backingstore_include_nodes[i].insertion_point->children =
backingstore_include_nodes[i].root->children;
for(n=backingstore_include_nodes[i].insertion_point->children;
n; n = n->next) {
n->parent = backingstore_include_nodes[i].insertion_point;
n->parent->last = n;
}
}
}
}
}
int
noit_conf_read_into_node(xmlNodePtr node, const char *path) {
DIR *dirroot;
struct dirent *de, *entry;
char filepath[PATH_MAX];
xmlDocPtr doc;
xmlNodePtr root = NULL;
xmlAttrPtr a;
struct stat sb;
int size, rv;
noitL(noit_debug, "read backing store: %s\n", path);
snprintf(filepath, sizeof(filepath), "%s/.attrs", path);
while((rv = stat(filepath, &sb)) < 0 && errno == EINTR);
if(rv == 0) {
doc = xmlParseFile(filepath);
if(doc) root = xmlDocGetRootElement(doc);
if(doc && root) {
node->properties = root->properties;
for(a = node->properties; a; a = a->next) {
a->parent = node;
a->doc = node->doc;
}
root->properties = NULL;
xmlFreeDoc(doc);
doc = NULL;
}
}
#ifdef _PC_NAME_MAX
size = pathconf(path, _PC_NAME_MAX);
#endif
size = MAX(size, PATH_MAX + 128);
de = alloca(size);
dirroot = opendir(path);
if(!dirroot) return -1;
while(portable_readdir_r(dirroot, de, &entry) == 0 && entry != NULL) {
noit_xml_userdata_t *udata;
char name[PATH_MAX];
char *sep;
xmlNodePtr child;
u_int64_t gen;
sep = strchr(entry->d_name, '#');
if(!sep) continue;
snprintf(filepath, sizeof(filepath), "%s/%s", path, entry->d_name);
while((rv = stat(filepath, &sb)) < 0 && errno == EINTR);
if(rv == 0) {
strlcpy(name, entry->d_name, sizeof(name));
name[sep - entry->d_name] = '\0';
gen = strtoull(sep+1, NULL, 10);
if(gen > max_gen_count) max_gen_count = gen;
if(S_ISDIR(sb.st_mode)) {
noitL(noit_debug, "<DIR< %s\n", entry->d_name);
child = xmlNewNode(NULL, (xmlChar *)name);
noit_conf_read_into_node(child, filepath);
udata = calloc(1, sizeof(*udata));
udata->name = strdup(name);
udata->path = strdup(filepath);
child->_private = udata;
xmlAddChild(node, child);
}
else if(S_ISREG(sb.st_mode)) {
xmlDocPtr cdoc;
xmlNodePtr cnode = NULL;
noitL(noit_debug, "<FILE< %s\n", entry->d_name);
cdoc = xmlParseFile(filepath);
if(cdoc) {
cnode = xmlDocGetRootElement(cdoc);
xmlDocSetRootElement(cdoc, xmlNewNode(NULL, (xmlChar *)"dummy"));
if(cnode) {
udata = calloc(1, sizeof(*udata));
udata->name = strdup(name);
udata->path = strdup(filepath);
cnode->_private = udata;
xmlAddChild(node, cnode);
}
xmlFreeDoc(cdoc);
}
}
}
}
closedir(dirroot);
return 0;
}
xmlDocPtr
noit_conf_read_backing_store(const char *path) {
xmlDocPtr doc;
xmlNodePtr root;
noit_xml_userdata_t *what;
doc = xmlNewDoc((xmlChar *)"1.0");
what = calloc(1, sizeof(*what));
what->path = strdup(path);
doc->_private = what;
root = xmlNewNode(NULL, (xmlChar *)"stub");
xmlDocSetRootElement(doc, root);
noit_conf_read_into_node(root, path);
return doc;
}
int
noit_conf_magic_mix(const char *parentfile, xmlDocPtr doc) {
xmlXPathContextPtr mix_ctxt = NULL;
xmlXPathObjectPtr pobj = NULL;
xmlNodePtr node;
int i, cnt, rv = 0;
assert(config_include_cnt == -1);
assert(backingstore_include_cnt == -1);
backingstore_include_cnt = 0;
mix_ctxt = xmlXPathNewContext(doc);
pobj = xmlXPathEval((xmlChar *)"//*[@backingstore]", mix_ctxt);
if(!pobj) goto includes;
if(pobj->type != XPATH_NODESET) goto includes;
if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto includes;
cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
if(cnt > 0)
backingstore_include_nodes = calloc(cnt, sizeof(*backingstore_include_nodes));
for(i=0; i<cnt; i++) {
char *path, *infile;
node = xmlXPathNodeSetItem(pobj->nodesetval, i);
path = (char *)xmlGetProp(node, (xmlChar *)"backingstore");
if(!path) continue;
if(*path == '/') infile = strdup(path);
else {
char *cp;
infile = malloc(PATH_MAX);
strlcpy(infile, parentfile, PATH_MAX);
for(cp = infile + strlen(infile) - 1; cp > infile; cp--) {
if(*cp == '/') { *cp = '\0'; break; }
else *cp = '\0';
}
strlcat(infile, "/", PATH_MAX);
strlcat(infile, path, PATH_MAX);
}
xmlFree(path);
backingstore_include_nodes[i].doc = noit_conf_read_backing_store(infile);
if(backingstore_include_nodes[i].doc) {
xmlNodePtr n, lchild;
backingstore_include_nodes[i].insertion_point = node;
backingstore_include_nodes[i].root = xmlDocGetRootElement(backingstore_include_nodes[i].doc);
/* for backing store, they are permanently reattached under the backing store.
* so for any children, we need to glue them into the new parent document.
*/
lchild = backingstore_include_nodes[i].root->children;
while(lchild && lchild->next) lchild = lchild->next;
if(lchild) {
lchild->next = node->children;
if(node->children) node->children->prev = lchild;
}
else
backingstore_include_nodes[i].root->children = node->children;
for(n=node->children; n; n = n->next) {
n->parent = backingstore_include_nodes[i].root; /* this gets mapped right back, just for clarity */
n->doc = backingstore_include_nodes[i].doc;
}
backingstore_include_nodes[i].old_children = NULL;
node->children = backingstore_include_nodes[i].root->children;
for(n=node->children; n; n = n->next) {
n->parent = backingstore_include_nodes[i].insertion_point;
n->parent->last = n;
}
}
else {
noitL(noit_error, "Could not load: '%s'\n", infile);
rv = -1;
}
free(infile);
}
mix_ctxt = xmlXPathNewContext(doc);
backingstore_include_cnt = cnt;
noitL(noit_debug, "Processed %d backing stores.\n", backingstore_include_cnt);
includes:
if(pobj) xmlXPathFreeObject(pobj);
config_include_cnt = 0;
pobj = xmlXPathEval((xmlChar *)"//include[@file]", mix_ctxt);
if(!pobj) goto out;
if(pobj->type != XPATH_NODESET) goto out;
if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto out;
cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
if(cnt > 0)
config_include_nodes = calloc(cnt, sizeof(*config_include_nodes));
for(i=0; i<cnt; i++) {
char *path, *infile;
node = xmlXPathNodeSetItem(pobj->nodesetval, i);
path = (char *)xmlGetProp(node, (xmlChar *)"file");
if(!path) continue;
if(*path == '/') infile = strdup(path);
else {
char *cp;
infile = malloc(PATH_MAX);
strlcpy(infile, parentfile, PATH_MAX);
for(cp = infile + strlen(infile) - 1; cp > infile; cp--) {
if(*cp == '/') { *cp = '\0'; break; }
else *cp = '\0';
}
strlcat(infile, "/", PATH_MAX);
strlcat(infile, path, PATH_MAX);
}
xmlFree(path);
config_include_nodes[i].doc = xmlParseFile(infile);
if(config_include_nodes[i].doc) {
xmlNodePtr n;
config_include_nodes[i].insertion_point = node;
config_include_nodes[i].root = xmlDocGetRootElement(config_include_nodes[i].doc);
config_include_nodes[i].old_children = node->children;
node->children = config_include_nodes[i].root->children;
for(n=node->children; n; n = n->next)
n->parent = config_include_nodes[i].insertion_point;
}
else {
noitL(noit_error, "Could not load: '%s'\n", infile);
rv = -1;
}
free(infile);
}
config_include_cnt = cnt;
noitL(noit_debug, "Processed %d includes\n", config_include_cnt);
out:
if(pobj) xmlXPathFreeObject(pobj);
if(mix_ctxt) xmlXPathFreeContext(mix_ctxt);
return rv;
}
static int noit_conf_load_internal(const char *path) {
int rv = 0;
xmlDocPtr new_config;
xmlNodePtr root;
new_config = xmlParseFile(path);
if(new_config) {
root = xmlDocGetRootElement(new_config);
if(root_node_name) free(root_node_name);
root_node_name = strdup((char *)root->name);
if(master_config) {
/* separate all includes */
noit_conf_magic_separate(master_config);
xmlFreeDoc(master_config);
}
if(xpath_ctxt) xmlXPathFreeContext(xpath_ctxt);
master_config = new_config;
/* mixin all the includes */
if(noit_conf_magic_mix(path, master_config)) rv = -1;
xpath_ctxt = xmlXPathNewContext(master_config);
if(path != master_config_file)
if(realpath(path, master_config_file) == NULL)
noitL(noit_error, "realpath failed: %s\n", strerror(errno));
noit_conf_mark_changed();
return rv;
}
rv = -1;
return rv;
}
int noit_conf_load(const char *path) {
int rv;
XML2LOG(noit_error);
rv = noit_conf_load_internal(path);
XML2LOG(xml_debug);
return rv;
}
char *noit_conf_config_filename() {
return strdup(master_config_file);
}
int noit_conf_xml_xpath(xmlDocPtr *mc, xmlXPathContextPtr *xp) {
if(mc) *mc = master_config;
if(xp) *xp = xpath_ctxt;
return 0;
}
int noit_conf_save(const char *path) {
return -1;
}
void noit_conf_get_elements_into_hash(noit_conf_section_t section,
const char *path,
noit_hash_table *table,
const char *namespace) {
int i, cnt;
xmlXPathObjectPtr pobj = NULL;
xmlXPathContextPtr current_ctxt;
xmlNodePtr current_node = (xmlNodePtr)section;
xmlNodePtr node;
current_ctxt = xpath_ctxt;
if(current_node) {
current_ctxt = xmlXPathNewContext(master_config);
current_ctxt->node = current_node;
}
pobj = xmlXPathEval((xmlChar *)path, current_ctxt);
if(!pobj) goto out;
if(pobj->type != XPATH_NODESET) goto out;
if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto out;
cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
for(i=0; i<cnt; i++) {
char *value;
node = xmlXPathNodeSetItem(pobj->nodesetval, i);
if(namespace && node->ns && !strcmp((char *)node->ns->prefix, namespace)) {
const xmlChar *name = node->name;
if(!strcmp((char *)name, "value")) {
name = xmlGetProp(node, (xmlChar *)"name");
if(!name) name = node->name;
}
value = (char *)xmlXPathCastNodeToString(node);
noit_hash_replace(table,
strdup((char *)name), strlen((char *)name),
strdup(value), free, free);
xmlFree(value);
}
else if(!namespace && !node->ns) {
const xmlChar *name = node->name;
if(!strcmp((char *)name, "value")) {
name = xmlGetProp(node, (xmlChar *)"name");
if(!name) name = node->name;
}
value = (char *)xmlXPathCastNodeToString(node);
noit_hash_replace(table,
strdup((char *)name), strlen((char *)name),
strdup(value), free, free);
xmlFree(value);
}
}
out:
if(pobj) xmlXPathFreeObject(pobj);
if(current_ctxt && current_ctxt != xpath_ctxt)
xmlXPathFreeContext(current_ctxt);
}
void noit_conf_get_into_hash(noit_conf_section_t section,
const char *path,
noit_hash_table *table,
const char *namespace) {
unsigned int cnt;
xmlXPathObjectPtr pobj = NULL;
xmlXPathContextPtr current_ctxt;
xmlNodePtr current_node = (xmlNodePtr)section;
xmlNodePtr node, parent_node;
char xpath_expr[1024];
char *inheritid;
current_ctxt = xpath_ctxt;
if(current_node) {
current_ctxt = xmlXPathNewContext(master_config);
current_ctxt->node = current_node;
}
if(path[0] == '/')
strlcpy(xpath_expr, path, sizeof(xpath_expr));
else
snprintf(xpath_expr, sizeof(xpath_expr),
"ancestor-or-self::node()/%s", path);
pobj = xmlXPathEval((xmlChar *)xpath_expr, current_ctxt);
if(!pobj) goto out;
if(pobj->type != XPATH_NODESET) goto out;
if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto out;
cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
/* These are in the order of root to leaf
* We want to recurse... apply:
* 1. our parent's config
* 2. our "inherit" config if it exists.
* 3. our config.
*/
node = xmlXPathNodeSetItem(pobj->nodesetval, cnt-1);
/* 1. */
if(cnt > 1 && node) {
parent_node = xmlXPathNodeSetItem(pobj->nodesetval, cnt-2);
if(parent_node != current_node)
noit_conf_get_into_hash(parent_node, (const char *)node->name, table, namespace);
}
/* 2. */
inheritid = (char *)xmlGetProp(node, (xmlChar *)"inherit");
if(inheritid) {
snprintf(xpath_expr, sizeof(xpath_expr), "//*[@id=\"%s\"]", inheritid);
noit_conf_get_into_hash(NULL, xpath_expr, table, namespace);
xmlFree(inheritid);
}
/* 3. */
noit_conf_get_elements_into_hash(node, "*", table, namespace);
out:
if(pobj) xmlXPathFreeObject(pobj);
if(current_ctxt && current_ctxt != xpath_ctxt)
xmlXPathFreeContext(current_ctxt);
}
noit_hash_table *noit_conf_get_namespaced_hash(noit_conf_section_t section,
const char *path, const char *ns) {
noit_hash_table *table = NULL;
table = calloc(1, sizeof(*table));
noit_conf_get_into_hash(section, path, table, ns);
if(table->size == 0) {
noit_hash_destroy(table, free, free);
free(table);
table = NULL;
}
return table;
}
noit_hash_table *noit_conf_get_hash(noit_conf_section_t section,
const char *path) {
noit_hash_table *table = NULL;
table = calloc(1, sizeof(*table));
noit_conf_get_into_hash(section, path, table, NULL);
return table;
}
noit_conf_section_t noit_conf_get_section(noit_conf_section_t section,
const char *path) {
noit_conf_section_t subsection = NULL;
xmlXPathObjectPtr pobj = NULL;
xmlXPathContextPtr current_ctxt;
xmlNodePtr current_node = (xmlNodePtr)section;
current_ctxt = xpath_ctxt;
if(current_node) {
current_ctxt = xmlXPathNewContext(master_config);
current_ctxt->node = current_node;
}
pobj = xmlXPathEval((xmlChar *)path, current_ctxt);
if(!pobj) goto out;
if(pobj->type != XPATH_NODESET) goto out;
if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto out;
subsection = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0);
out:
if(pobj) xmlXPathFreeObject(pobj);
if(current_ctxt && current_ctxt != xpath_ctxt)
xmlXPathFreeContext(current_ctxt);
return subsection;
}
noit_conf_section_t *noit_conf_get_sections(noit_conf_section_t section,
const char *path,
int *cnt) {
int i;
noit_conf_section_t *sections = NULL;
xmlXPathObjectPtr pobj = NULL;
xmlXPathContextPtr current_ctxt;
xmlNodePtr current_node = (xmlNodePtr)section;
*cnt = 0;
current_ctxt = xpath_ctxt;
if(current_node) {
current_ctxt = xmlXPathNewContext(master_config);
current_ctxt->node = current_node;
}
pobj = xmlXPathEval((xmlChar *)path, current_ctxt);
if(!pobj) goto out;
if(pobj->type != XPATH_NODESET) goto out;
if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto out;
*cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
sections = calloc(*cnt, sizeof(*sections));
for(i=0; i<*cnt; i++)
sections[i] = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i);
out:
if(pobj) xmlXPathFreeObject(pobj);
if(current_ctxt && current_ctxt != xpath_ctxt)
xmlXPathFreeContext(current_ctxt);
return sections;
}
int _noit_conf_get_string(noit_conf_section_t section, xmlNodePtr *vnode,
const char *path, char **value) {
const char *str;
int rv = 1;
unsigned int i;
xmlXPathObjectPtr pobj = NULL;
xmlXPathContextPtr current_ctxt;
xmlNodePtr current_node = (xmlNodePtr)section;
current_ctxt = xpath_ctxt;
if(current_node) {
current_ctxt = xmlXPathNewContext(master_config);
current_ctxt->node = current_node;
}
pobj = xmlXPathEval((xmlChar *)path, current_ctxt);
if(pobj) {
xmlNodePtr node;
switch(pobj->type) {
case XPATH_NODESET:
if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto fallback;
i = xmlXPathNodeSetGetLength(pobj->nodesetval);
node = xmlXPathNodeSetItem(pobj->nodesetval, i-1);
if(vnode) *vnode = node;
*value = (char *)xmlXPathCastNodeToString(node);
break;
default:
*value = (char *)xmlXPathCastToString(pobj);
}
goto found;