root/src/noit_conf.c

Revision 07ad2653e890ccb2dabfb826b6396a54666afe20, 18.9 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

support writing out the config

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  */
5
6 #include "noit_defines.h"
7
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <errno.h>
12 #include <assert.h>
13 #include <libxml/parser.h>
14 #include <libxml/tree.h>
15 #include <libxml/xpath.h>
16
17 #include "noit_conf.h"
18 #include "noit_check.h"
19 #include "noit_console.h"
20 #include "utils/noit_hash.h"
21 #include "utils/noit_log.h"
22
23 /* tmp hash impl, replace this with something nice */
24 static noit_hash_table _tmp_config = NOIT_HASH_EMPTY;
25 static xmlDocPtr master_config = NULL;
26 static char master_config_file[PATH_MAX] = "";
27 static xmlXPathContextPtr xpath_ctxt = NULL;
28
29 static noit_hash_table _compiled_fallback = NOIT_HASH_EMPTY;
30 static struct {
31   const char *key;
32   const char *val;
33 } config_info[] = {
34   /*
35    * These are compile-time fallbacks to be used in the event
36    * that the current running config does not have values for
37    * these config paths.
38    *
39    * PLEASE: keep them alphabetically sorted.
40    */
41   { "/noit/modules/directory", MODULES_DIR },
42
43   { NULL, NULL }
44 };
45
46 static void register_console_config_commands();
47
48 void noit_conf_init() {
49   int i;
50   for(i = 0; config_info[i].key != NULL; i++) {
51     noit_hash_store(&_compiled_fallback,
52                     strdup(config_info[i].key), strlen(config_info[i].key),
53                     (void *)strdup(config_info[i].val));
54   }
55   xmlInitParser();
56   xmlXPathInit();
57   register_console_config_commands();
58 }
59
60 int noit_conf_load(const char *path) {
61   xmlDocPtr new_config;
62   new_config = xmlParseFile(path);
63   if(new_config) {
64     if(master_config) xmlFreeDoc(master_config);
65     if(xpath_ctxt) xmlXPathFreeContext(xpath_ctxt);
66
67     master_config = new_config;
68     xpath_ctxt = xmlXPathNewContext(master_config);
69     realpath(path, master_config_file);
70     return 0;
71   }
72   return -1;
73 }
74 int noit_conf_save(const char *path) {
75   return -1;
76 }
77
78 noit_hash_table *noit_conf_get_hash(noit_conf_section_t section,
79                                     const char *path) {
80   int i, cnt;
81   noit_hash_table *table = NULL;
82   xmlXPathObjectPtr pobj = NULL;
83   xmlXPathContextPtr current_ctxt;
84   xmlNodePtr current_node = (xmlNodePtr)section;
85   xmlNodePtr node;
86
87   current_ctxt = xpath_ctxt;
88   if(current_node) {
89     current_ctxt = xmlXPathNewContext(master_config);
90     current_ctxt->node = current_node;
91   }
92   pobj = xmlXPathEval((xmlChar *)path, current_ctxt);
93   if(!pobj) goto out;
94   if(pobj->type != XPATH_NODESET) goto out;
95   if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto out;
96   table = calloc(1, sizeof(*table));
97   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
98   for(i=0; i<cnt; i++) {
99     char *value;
100     node = xmlXPathNodeSetItem(pobj->nodesetval, i);
101     value = (char *)xmlXPathCastNodeToString(node);
102     noit_hash_replace(table,
103                       strdup((char *)node->name), strlen((char *)node->name),
104                       strdup(value), free, free);
105   }
106  out:
107   if(pobj) xmlXPathFreeObject(pobj);
108   if(current_ctxt && current_ctxt != xpath_ctxt)
109     xmlXPathFreeContext(current_ctxt);
110   return table;
111 }
112 noit_conf_section_t noit_conf_get_section(noit_conf_section_t section,
113                                           const char *path) {
114   noit_conf_section_t subsection = NULL;
115   xmlXPathObjectPtr pobj = NULL;
116   xmlXPathContextPtr current_ctxt;
117   xmlNodePtr current_node = (xmlNodePtr)section;
118
119   current_ctxt = xpath_ctxt;
120   if(current_node) {
121     current_ctxt = xmlXPathNewContext(master_config);
122     current_ctxt->node = current_node;
123   }
124   pobj = xmlXPathEval((xmlChar *)path, current_ctxt);
125   if(!pobj) goto out;
126   if(pobj->type != XPATH_NODESET) goto out;
127   if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto out;
128   subsection = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0);
129  out:
130   if(pobj) xmlXPathFreeObject(pobj);
131   if(current_ctxt && current_ctxt != xpath_ctxt)
132     xmlXPathFreeContext(current_ctxt);
133   return subsection;
134 }
135 noit_conf_section_t *noit_conf_get_sections(noit_conf_section_t section,
136                                             const char *path,
137                                             int *cnt) {
138   int i;
139   noit_conf_section_t *sections = NULL;
140   xmlXPathObjectPtr pobj = NULL;
141   xmlXPathContextPtr current_ctxt;
142   xmlNodePtr current_node = (xmlNodePtr)section;
143
144   *cnt = 0;
145   current_ctxt = xpath_ctxt;
146   if(current_node) {
147     current_ctxt = xmlXPathNewContext(master_config);
148     current_ctxt->node = current_node;
149   }
150   pobj = xmlXPathEval((xmlChar *)path, current_ctxt);
151   if(!pobj) goto out;
152   if(pobj->type != XPATH_NODESET) goto out;
153   if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto out;
154   *cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
155   sections = calloc(*cnt, sizeof(*sections));
156   for(i=0; i<*cnt; i++)
157     sections[i] = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i);
158  out:
159   if(pobj) xmlXPathFreeObject(pobj);
160   if(current_ctxt && current_ctxt != xpath_ctxt)
161     xmlXPathFreeContext(current_ctxt);
162   return sections;
163 }
164 int _noit_conf_get_string(noit_conf_section_t section,
165                           const char *path, char **value) {
166   char *str;
167   int i;
168   xmlXPathObjectPtr pobj;
169   xmlXPathContextPtr current_ctxt;
170   xmlNodePtr current_node = (xmlNodePtr)section;
171
172   current_ctxt = xpath_ctxt;
173   if(current_node) {
174     current_ctxt = xmlXPathNewContext(master_config);
175     current_ctxt->node = current_node;
176   }
177   pobj = xmlXPathEval((xmlChar *)path, current_ctxt);
178   if(pobj) {
179     xmlNodePtr node;
180     switch(pobj->type) {
181       case XPATH_NODESET:
182         if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) return 0;
183         i = xmlXPathNodeSetGetLength(pobj->nodesetval);
184         node = xmlXPathNodeSetItem(pobj->nodesetval, i-1);
185         *value = (char *)xmlXPathCastNodeToString(node);
186         break;
187       default:
188         *value = (char *)xmlXPathCastToString(pobj);
189     }
190     goto found;
191   }
192   if(noit_hash_retrieve(&_compiled_fallback,
193                         path, strlen(path), (void **)&str)) {
194     *value = str;
195     goto found;
196   }
197   return 0;
198  found:
199   if(current_ctxt && current_ctxt != xpath_ctxt)
200     xmlXPathFreeContext(current_ctxt);
201   return 1;
202 }
203 int noit_conf_get_string(noit_conf_section_t section,
204                          const char *path, char **value) {
205   char *str;
206   if(_noit_conf_get_string(section,path,&str)) {
207     *value = strdup(str);
208     return 1;
209   }
210   return 0;
211 }
212 int noit_conf_get_stringbuf(noit_conf_section_t section,
213                             const char *path, char *buf, int len) {
214   char *str;
215   if(_noit_conf_get_string(section,path,&str)) {
216     strlcpy(buf, str, len);
217     return 1;
218   }
219   return 0;
220 }
221 int noit_conf_set_string(noit_conf_section_t section,
222                          const char *path, const char *value) {
223   noit_hash_replace(&_tmp_config,
224                     strdup(path), strlen(path), (void *)strdup(value),
225                     free, free);
226   return 1;
227 }
228 int noit_conf_get_int(noit_conf_section_t section,
229                       const char *path, int *value) {
230   char *str;
231   long longval;
232   if(noit_conf_get_string(section,path,&str)) {
233     int base = 10;
234     if(str[0] == '0') {
235       if(str[1] == 'x') base = 16;
236       else base = 8;
237     }
238     longval = strtol(str, NULL, base);
239     free(str);
240     *value = (int)longval;
241     return 1;
242   }
243   return 0;
244 }
245 int noit_conf_set_int(noit_conf_section_t section,
246                       const char *path, int value) {
247   char buffer[32];
248   snprintf(buffer, 32, "%d", value);
249   return noit_conf_set_string(section,path,buffer);
250 }
251 int noit_conf_get_float(noit_conf_section_t section,
252                         const char *path, float *value) {
253   char *str;
254   if(noit_conf_get_string(section,path,&str)) {
255     *value = atof(str);
256     free(str);
257     return 1;
258   }
259   return 0;
260 }
261 int noit_conf_set_float(noit_conf_section_t section,
262                         const char *path, float value) {
263   char buffer[32];
264   snprintf(buffer, 32, "%f", value);
265   return noit_conf_set_string(section,path,buffer);
266 }
267 int noit_conf_get_boolean(noit_conf_section_t section,
268                           const char *path, noit_conf_boolean *value) {
269   char *str;
270   if(noit_conf_get_string(section,path,&str)) {
271     if(!strcasecmp(str, "true")) *value = noit_true;
272     else *value = noit_false;
273     free(str);
274     return 1;
275   }
276   return 0;
277 }
278 int noit_conf_set_boolean(noit_conf_section_t section,
279                           const char *path, noit_conf_boolean value) {
280   if(value == noit_true)
281     return noit_conf_set_string(section,path,"true");
282   return noit_conf_set_string(section,path,"false");
283 }
284
285 static void
286 conf_t_userdata_free(void *data) {
287   noit_conf_t_userdata_t *info = data;
288   if(info) {
289     if(info->path) free(info->path);
290     free(info);
291   }
292 }
293 static int
294 noit_console_state_conf_terminal(noit_console_closure_t ncct,
295                                  int argc, char **argv,
296                                  noit_console_state_t *state, void *closure) {
297   noit_conf_t_userdata_t *info;
298   if(argc) {
299     nc_printf(ncct, "extra arguments not expected.\n");
300     return -1;
301   }
302   info = calloc(1, sizeof(*info));
303   info->path = strdup("/");
304   noit_console_userdata_set(ncct, NOIT_CONF_T_USERDATA, info,
305                             conf_t_userdata_free);
306   noit_console_state_push_state(ncct, state);
307   noit_console_state_init(ncct);
308   return 0;
309 }
310
311 static int
312 noit_console_config_cd(noit_console_closure_t ncct,
313                        int argc, char **argv,
314                        noit_console_state_t *state, void *closure) {
315   const char *err = "internal error";
316   char *path, xpath[1024];
317   noit_conf_t_userdata_t *info;
318   xmlXPathObjectPtr pobj = NULL;
319   xmlXPathContextPtr current_ctxt;
320   xmlNodePtr node = NULL;
321
322   if(argc != 1) {
323     nc_printf(ncct, "requires one argument\n");
324     return -1;
325   }
326   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
327   if(argv[0][0] == '/')
328     snprintf(xpath, sizeof(xpath), "/noit%s", argv[0]);
329   else {
330     snprintf(xpath, sizeof(xpath), "/noit%s/%s", info->path, argv[0]);
331   }
332   if(xpath[strlen(xpath)-1] == '/') xpath[strlen(xpath)-1] = '\0';
333
334   current_ctxt = xpath_ctxt;
335   pobj = xmlXPathEval((xmlChar *)xpath, current_ctxt);
336   if(!pobj || pobj->type != XPATH_NODESET ||
337      xmlXPathNodeSetIsEmpty(pobj->nodesetval)) {
338     err = "no such section";
339     goto bad;
340   }
341   if(xmlXPathNodeSetGetLength(pobj->nodesetval) > 1) {
342     err = "ambiguous section";
343     goto bad;
344   }
345
346   node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0);
347   path = (char *)xmlGetNodePath(node);
348   if(strncmp(path, "/noit/", strlen("/noit/")) && strcmp(path, "/noit")) {
349     err = "new path outside out tree";
350     goto bad;
351   }
352   free(info->path);
353   if(!strcmp(path, "/noit"))
354     info->path = strdup("/");
355   else
356     info->path = strdup((char *)xmlGetNodePath(node) + strlen("/noit"));
357   if(pobj) xmlXPathFreeObject(pobj);
358   return 0;
359  bad:
360   if(pobj) xmlXPathFreeObject(pobj);
361   nc_printf(ncct, "%s\n", err);
362   return -1;
363 }
364 static int
365 noit_console_config_show(noit_console_closure_t ncct,
366                          int argc, char **argv,
367                          noit_console_state_t *state, void *closure) {
368   int i, cnt, titled = 0, cliplen = 0;
369   const char *path, *basepath = NULL;
370   char xpath[1024];
371   noit_conf_t_userdata_t *info = NULL;
372   xmlXPathObjectPtr pobj = NULL;
373   xmlXPathContextPtr current_ctxt;
374   xmlNodePtr node;
375
376   if(argc > 1) {
377     nc_printf(ncct, "too many arguments\n");
378     return -1;
379   }
380
381   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
382   if(info) path = basepath = info->path;
383   if(!info && argc == 0) {
384     nc_printf(ncct, "argument required when not in configuration mode\n");
385     return -1;
386   }
387
388   if(argc == 1) path = argv[0];
389   if(!basepath) basepath = path;
390
391   /* { / } is a special case */
392   if(!strcmp(basepath, "/")) basepath = "";
393   if(!strcmp(path, "/")) path = "";
394
395   if(!master_config) {
396     nc_printf(ncct, "no config\n");
397     return -1;
398   }
399
400   /* { / } is the only path that will end with a /
401    * in XPath { / / * } means something _entirely different than { / * }
402    * Ever notice how it is hard to describe xpath in C comments?
403    */
404   /* We don't want to show the root node */
405   cliplen = strlen("/noit/");
406
407   /* If we are in configuration mode
408    * and we are without an argument or the argument is absolute,
409    * clip the current path off */
410   if(info && (argc == 0 || path[0] != '/')) cliplen += strlen(basepath);
411   if(!path[0] || path[0] == '/') /* base only, or absolute path requested */
412     snprintf(xpath, sizeof(xpath), "/noit%s/@*", path);
413   else
414     snprintf(xpath, sizeof(xpath), "/noit%s/%s/@*", basepath, path);
415
416   current_ctxt = xpath_ctxt;
417   pobj = xmlXPathEval((xmlChar *)xpath, current_ctxt);
418   if(!pobj || pobj->type != XPATH_NODESET) {
419     nc_printf(ncct, "no such object\n");
420     goto bad;
421   }
422   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
423   titled = 0;
424   for(i=0; i<cnt; i++) {
425     node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i);
426     if(!strcmp((char *)node->name, "check")) continue;
427     if(node->children && node->children == xmlGetLastChild(node) &&
428       xmlNodeIsText(node->children)) {
429       if(!titled++) nc_printf(ncct, "== Section Settings ==\n");
430       nc_printf(ncct, "%s: %s\n", xmlGetNodePath(node) + cliplen,
431                 xmlXPathCastNodeToString(node->children));
432     }
433   }
434   xmlXPathFreeObject(pobj);
435
436   /* _shorten string_ turning last { / @ * } to { / * } */
437   strlcpy(xpath + strlen(xpath) - 2, "*", 2);
438 nc_printf(ncct, "Looking up path '%s'\n", xpath);
439   pobj = xmlXPathEval((xmlChar *)xpath, current_ctxt);
440   if(!pobj || pobj->type != XPATH_NODESET) {
441     nc_printf(ncct, "no such object\n");
442     goto bad;
443   }
444   cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
445   titled = 0;
446   for(i=0; i<cnt; i++) {
447     node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i);
448     if(!strcmp((char *)node->name, "check")) continue;
449     if(!(node->children && node->children == xmlGetLastChild(node) &&
450          xmlNodeIsText(node->children))) {
451       if(!titled++) nc_printf(ncct, "== Subsections ==\n");
452       nc_printf(ncct, "%s\n", xmlGetNodePath(node) + cliplen);
453     }
454   }
455
456   titled = 0;
457   for(i=0; i<cnt; i++) {
458     node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i);
459     if(!strcmp((char *)node->name, "check")) {
460       int busted = 1;
461       xmlAttr *attr;
462       char *uuid_str = "undefined";;
463
464       if(!titled++) nc_printf(ncct, "== Checks ==\n");
465
466       for(attr=node->properties; attr; attr = attr->next) {
467         if(!strcmp((char *)attr->name, "uuid"))
468           uuid_str = (char *)xmlXPathCastNodeToString(attr->children);
469       }
470       if(uuid_str) {
471         uuid_t checkid;
472         nc_printf(ncct, "check[@uuid=\"%s\"] ", uuid_str);
473         if(uuid_parse(uuid_str, checkid) == 0) {
474           noit_check_t *check;
475           check = noit_poller_lookup(checkid);
476           if(check) {
477             busted = 0;
478             nc_printf(ncct, "%s`%s", check->target, check->name);
479           }
480         }
481       }
482       else
483         nc_printf(ncct, "%s ", xmlGetNodePath(node) + cliplen);
484       if(busted) nc_printf(ncct, "[check not in running system]");
485       nc_write(ncct, "\n", 1);
486     }
487   }
488   xmlXPathFreeObject(pobj);
489   return 0;
490  bad:
491   if(pobj) xmlXPathFreeObject(pobj);
492   return -1;
493 }
494
495 static char *
496 conf_t_prompt(EditLine *el) {
497   noit_console_closure_t ncct;
498   noit_conf_t_userdata_t *info;
499   static char *tl = "noit(conf)# ";
500   static char *pfmt = "noit(conf:%s%s)# ";
501   int path_len, max_len;
502
503   el_get(el, EL_USERDATA, (void *)&ncct);
504   if(!ncct) return tl;
505   info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA);
506   if(!info) return tl;
507
508   path_len = strlen(info->path);
509   max_len = strlen(pfmt) - 4 /* %s%s */ - 1 /* \0 */;
510   if(path_len > max_len)
511     snprintf(info->prompt, sizeof(info->prompt),
512              pfmt, "...", info->path + max_len - 3 /* ... */);
513   else
514     snprintf(info->prompt, sizeof(info->prompt), pfmt, "", info->path);
515   return info->prompt;
516 }
517
518 static int
519 noit_console_write_xml(void *vncct, const char *buffer, int len) {
520   noit_console_closure_t ncct = vncct;
521   return nc_write(ncct, buffer, len);
522 }
523 static int
524 noit_console_close_xml(void *vncct) {
525   return 0;
526 }
527 static int
528 noit_conf_write_terminal(noit_console_closure_t ncct,
529                          int argc, char **argv,
530                          noit_console_state_t *state, void *closure) {
531   xmlOutputBufferPtr out;
532   xmlCharEncodingHandlerPtr enc;
533   enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8);
534   out = xmlOutputBufferCreateIO(noit_console_write_xml,
535                                 noit_console_close_xml,
536                                 ncct, enc);
537   xmlSaveFileTo(out, master_config, "utf8");
538   return 0;
539 }
540 static int
541 noit_conf_write_file(noit_console_closure_t ncct,
542                      int argc, char **argv,
543                      noit_console_state_t *state, void *closure) {
544   int fd, len;
545   char master_file_tmp[PATH_MAX];
546   xmlOutputBufferPtr out;
547   xmlCharEncodingHandlerPtr enc;
548
549   snprintf(master_file_tmp, sizeof(master_file_tmp),
550            "%s.tmp", master_config_file);
551   unlink(master_file_tmp);
552   fd = open(master_file_tmp, O_CREAT|O_EXCL|O_WRONLY, 0640);
553   if(fd < 0) {
554     nc_printf(ncct, "Failed to open tmp file: %s\n", strerror(errno));
555     return -1;
556   }
557   enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8);
558   out = xmlOutputBufferCreateFd(fd, enc);
559   if(!out) {
560     close(fd);
561     unlink(master_file_tmp);
562     nc_printf(ncct, "internal error: OutputBufferCreate failed\n");
563     return -1;
564   }
565   len = xmlSaveFileTo(out, master_config, "utf8");
566   close(fd);
567   if(len <= 0) {
568     nc_printf(ncct, "internal error: writing to tmp file failed.\n");
569     return -1;
570   }
571   if(rename(master_file_tmp, master_config_file) != 0) {
572     nc_printf(ncct, "Failed to replace file: %s\n", strerror(errno));
573     return -1;
574   }
575   nc_printf(ncct, "%d bytes written.\n", len);
576   return 0;
577 }
578
579 static
580 void register_console_config_commands() {
581   noit_console_state_t *tl, *_conf_state, *_conf_t_state,
582                        *_write_state;
583
584   tl = noit_console_state_initial();
585
586   _write_state = calloc(1, sizeof(*_write_state));
587   noit_console_state_add_cmd(_write_state,
588     NCSCMD("terminal", noit_conf_write_terminal, NULL, NULL));
589   noit_console_state_add_cmd(_write_state,
590     NCSCMD("file", noit_conf_write_file, NULL, NULL));
591   /* write mememory?  It's to a file, but I like router syntax */
592   noit_console_state_add_cmd(_write_state,
593     NCSCMD("memory", noit_conf_write_file, NULL, NULL));
594
595   _conf_t_state = calloc(1, sizeof(*_conf_t_state));
596   _conf_t_state->console_prompt_function = conf_t_prompt;
597   noit_console_state_add_cmd(_conf_t_state, &console_command_exit);
598   noit_console_state_add_cmd(_conf_t_state,
599     NCSCMD("ls", noit_console_config_show, NULL, NULL));
600   noit_console_state_add_cmd(_conf_t_state,
601     NCSCMD("cd", noit_console_config_cd, NULL, NULL));
602   noit_console_state_add_cmd(_conf_t_state,
603     NCSCMD("write", noit_console_state_delegate, _write_state, NULL));
604
605   _conf_state = calloc(1, sizeof(*_conf_state));
606   noit_console_state_add_cmd(_conf_state,
607     NCSCMD("terminal", noit_console_state_conf_terminal, _conf_t_state, NULL));
608
609   noit_console_state_add_cmd(tl,
610     NCSCMD("configure", noit_console_state_delegate, _conf_state, NULL));
611   noit_console_state_add_cmd(tl,
612     NCSCMD("write", noit_console_state_delegate, _write_state, NULL));
613 }
Note: See TracBrowser for help on using the browser.