readconf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /* $Id: readconf.c,v 1.17 2002/12/15 14:28:10 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2002 Willem Dijkstra
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials provided
  15. * with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <sys/queue.h>
  32. #include <sys/stat.h>
  33. #include <string.h>
  34. #include <fcntl.h>
  35. #include <unistd.h>
  36. #include "data.h"
  37. #include "error.h"
  38. #include "lex.h"
  39. #include "net.h"
  40. #include "readconf.h"
  41. #include "xmalloc.h"
  42. __BEGIN_DECLS
  43. int read_mux(struct muxlist * mul, struct lex *);
  44. int read_source(struct sourcelist * sol, struct lex *);
  45. int insert_filename(char *, int, int, char *);
  46. __END_DECLS
  47. const char *default_symux_port = SYMUX_PORT;
  48. int
  49. insert_filename(char *path, int maxlen, int type, char *args)
  50. {
  51. char *ts;
  52. char *ta;
  53. ts = ta = NULL;
  54. switch (type) {
  55. case MT_CPU:
  56. ts = "cpu";
  57. ta = args;
  58. break;
  59. case MT_IF:
  60. ts = "if_";
  61. ta = args;
  62. break;
  63. case MT_IO:
  64. ts = "io_";
  65. ta = args;
  66. break;
  67. case MT_MEM:
  68. ts = "mem";
  69. ta = "";
  70. break;
  71. case MT_PF:
  72. ts = "pf";
  73. ta = "";
  74. break;
  75. case MT_DEBUG:
  76. ts = "debug";
  77. ta = "";
  78. break;
  79. case MT_PROC:
  80. ts = "proc_";
  81. ta = args;
  82. break;
  83. default:
  84. warning("%s:%d: internal error: type (%d) unknown",
  85. __FILE__, __LINE__, type);
  86. return 0;
  87. }
  88. if ((snprintf(path, maxlen, "/%s%s.rrd", ts, ta)) >= maxlen)
  89. return 0;
  90. else
  91. return 1;
  92. }
  93. /* mux <host> (port|,| ) <number> */
  94. int
  95. read_mux(struct muxlist * mul, struct lex * l)
  96. {
  97. char muxname[_POSIX2_LINE_MAX];
  98. struct mux *mux;
  99. if (!SLIST_EMPTY(mul)) {
  100. warning("%s:%d: only one mux statement allowed",
  101. l->filename, l->cline);
  102. return 0;
  103. }
  104. lex_nexttoken(l);
  105. if (!getip(l->token)) {
  106. warning("%s:%d: could not resolve '%s'",
  107. l->filename, l->cline, l->token);
  108. return 0;
  109. }
  110. mux = add_mux(mul, SYMON_UNKMUX);
  111. mux->addr = xstrdup((const char *) &res_host);
  112. /* check for port statement */
  113. lex_nexttoken(l);
  114. if (l->op == LXT_PORT || l->op == LXT_COMMA)
  115. lex_nexttoken(l);
  116. if (l->type != LXY_NUMBER) {
  117. lex_ungettoken(l);
  118. mux->port = xstrdup(default_symux_port);
  119. }
  120. else {
  121. mux->port = xstrdup((const char *) l->token);
  122. }
  123. bzero(&muxname, sizeof(muxname));
  124. snprintf(&muxname[0], sizeof(muxname), "%s %s", mux->addr, mux->port);
  125. if (rename_mux(mul, mux, muxname) == NULL)
  126. fatal("%s:%d: internal error: dual mux", __FILE__, __LINE__);
  127. return 1;
  128. }
  129. /* source <host> { accept ... | write ... | datadir ... } */
  130. int
  131. read_source(struct sourcelist * sol, struct lex * l)
  132. {
  133. struct source *source;
  134. struct stream *stream;
  135. struct stat sb;
  136. char path[_POSIX2_LINE_MAX];
  137. char sn[_POSIX2_LINE_MAX];
  138. char sa[_POSIX2_LINE_MAX];
  139. int st;
  140. int pc;
  141. int fd;
  142. /* get hostname */
  143. lex_nexttoken(l);
  144. if (!getip(l->token)) {
  145. warning("%s:%d: could not resolve '%s'",
  146. l->filename, l->cline, l->token);
  147. return 0;
  148. }
  149. source = add_source(sol, res_host);
  150. EXPECT(l, LXT_BEGIN);
  151. while (lex_nexttoken(l)) {
  152. switch (l->op) {
  153. /* accept { cpu(x), ... } */
  154. case LXT_ACCEPT:
  155. EXPECT(l, LXT_BEGIN);
  156. while (lex_nexttoken(l) && l->op != LXT_END) {
  157. switch (l->op) {
  158. case LXT_CPU:
  159. case LXT_IF:
  160. case LXT_IO:
  161. case LXT_MEM:
  162. case LXT_PF:
  163. case LXT_DEBUG:
  164. case LXT_PROC:
  165. st = token2type(l->op);
  166. strncpy(&sn[0], l->token, _POSIX2_LINE_MAX);
  167. /* parse arg */
  168. lex_nexttoken(l);
  169. if (l->op == LXT_OPEN) {
  170. lex_nexttoken(l);
  171. if (l->op == LXT_CLOSE) {
  172. parse_error(l, "<stream argument>");
  173. return 0;
  174. }
  175. strncpy(&sa[0], l->token, _POSIX2_LINE_MAX);
  176. lex_nexttoken(l);
  177. if (l->op != LXT_CLOSE) {
  178. parse_error(l, ")");
  179. return 0;
  180. }
  181. }
  182. else {
  183. lex_ungettoken(l);
  184. sa[0] = '\0';
  185. }
  186. if ((stream = add_source_stream(source, st, sa)) == NULL) {
  187. warning("%s:%d: stream %s(%s) redefined",
  188. l->filename, l->cline, sn, sa);
  189. return 0;
  190. }
  191. break; /* LXT_CPU/IF/IO/MEM/PF/DEBUG/PROC */
  192. case LXT_COMMA:
  193. break;
  194. default:
  195. parse_error(l, "{cpu|mem|if|io|pf|debug}");
  196. return 0;
  197. break;
  198. }
  199. }
  200. break; /* LXT_ACCEPT */
  201. /* datadir "path" */
  202. case LXT_DATADIR:
  203. lex_nexttoken(l);
  204. /* is path absolute */
  205. if (l->token && l->token[0] != '/') {
  206. warning("%s:%d: datadir path '%s' is not absolute",
  207. l->filename, l->cline, l->token);
  208. return 0;
  209. }
  210. /* make sure that directory exists */
  211. bzero(&sb, sizeof(struct stat));
  212. if (stat(l->token, &sb) == 0) {
  213. if (!(sb.st_mode & S_IFDIR)) {
  214. warning("%s:%d: datadir path '%s' is not a directory",
  215. l->filename, l->cline, l->token);
  216. return 0;
  217. }
  218. }
  219. else {
  220. warning("%s:%d: could not stat datadir path '%s'",
  221. l->filename, l->cline, l->token);
  222. return 0;
  223. }
  224. strncpy(&path[0], l->token, _POSIX2_LINE_MAX);
  225. path[_POSIX2_LINE_MAX - 1] = '\0';
  226. pc = strlen(path);
  227. if (path[pc - 1] == '/') {
  228. path[pc - 1] = '\0';
  229. pc--;
  230. }
  231. /* add path to empty streams */
  232. SLIST_FOREACH(stream, &source->sl, streams) {
  233. if (stream->file == NULL) {
  234. if (!(insert_filename(&path[pc],
  235. _POSIX2_LINE_MAX - pc,
  236. stream->type,
  237. stream->args))) {
  238. if (stream->args && strlen(stream->args)) {
  239. warning("%s:%d: failed to construct stream "
  240. "%s(%s) filename using datadir '%s'",
  241. l->filename, l->cline,
  242. type2str(stream->type),
  243. stream->args, l->token);
  244. }
  245. else {
  246. warning("%s:%d: failed to construct stream "
  247. "%s) filename using datadir '%s'",
  248. l->filename, l->cline,
  249. type2str(stream->type),
  250. l->token);
  251. }
  252. return 0;
  253. }
  254. /* try filename */
  255. if ((fd = open(path, O_RDWR | O_NONBLOCK, 0)) == -1) {
  256. warning("%s:%d: file '%s', guessed by datadir, cannot be opened",
  257. l->filename, l->cline, path);
  258. return 0;
  259. }
  260. else {
  261. close(fd);
  262. stream->file = xstrdup(path);
  263. }
  264. }
  265. }
  266. break; /* LXT_DATADIR */
  267. /* write cpu(0) in "filename" */
  268. case LXT_WRITE:
  269. lex_nexttoken(l);
  270. switch (l->op) {
  271. case LXT_CPU:
  272. case LXT_IF:
  273. case LXT_IO:
  274. case LXT_MEM:
  275. case LXT_PF:
  276. case LXT_DEBUG:
  277. case LXT_PROC:
  278. st = token2type(l->op);
  279. strncpy(&sn[0], l->token, _POSIX2_LINE_MAX);
  280. /* parse arg */
  281. lex_nexttoken(l);
  282. if (l->op == LXT_OPEN) {
  283. lex_nexttoken(l);
  284. if (l->op == LXT_CLOSE) {
  285. parse_error(l, "<stream argument>");
  286. return 0;
  287. }
  288. strncpy(&sa[0], l->token, _POSIX2_LINE_MAX);
  289. lex_nexttoken(l);
  290. if (l->op != LXT_CLOSE) {
  291. parse_error(l, ")");
  292. return 0;
  293. }
  294. }
  295. else {
  296. lex_ungettoken(l);
  297. sa[0] = '\0';
  298. }
  299. EXPECT(l, LXT_IN);
  300. lex_nexttoken(l);
  301. if ((stream = find_source_stream(source, st, sa)) == NULL) {
  302. if (strlen(sa)) {
  303. warning("%s:%d: stream %s(%s) is not accepted for %s",
  304. l->filename, l->cline, sn, sa, source->addr);
  305. return 0;
  306. }
  307. else {
  308. warning("%s:%d: stream %s is not accepted for %s",
  309. l->filename, l->cline, sn, source->addr);
  310. return 0;
  311. }
  312. }
  313. else {
  314. /* try filename */
  315. if ((fd = open(l->token, O_RDWR | O_NONBLOCK, 0)) == -1) {
  316. warning("%s:%d: file '%s' cannot be opened",
  317. l->filename, l->cline, l->token);
  318. return 0;
  319. }
  320. else {
  321. close(fd);
  322. if (stream->file != NULL) {
  323. warning("%s:%d: file '%s' overwrites previous definition '%s'",
  324. l->filename, l->cline, l->token, stream->file);
  325. xfree(stream->file);
  326. }
  327. stream->file = xstrdup(l->token);
  328. }
  329. }
  330. break; /* LXT_CPU/IF/IO/MEM/PF/DEBUG/PROC */
  331. default:
  332. parse_error(l, "{cpu|mem|if|io|debug|proc}");
  333. return 0;
  334. break;
  335. }
  336. break; /* LXT_WRITE */
  337. case LXT_END:
  338. return 1;
  339. default:
  340. parse_error(l, "accept|write");
  341. return 0;
  342. }
  343. }
  344. warning("%s:%d: missing close brace on source statement",
  345. l->filename, l->cline);
  346. return 0;
  347. }
  348. /* Read symux.conf */
  349. int
  350. read_config_file(struct muxlist * mul, const char *filename)
  351. {
  352. struct lex *l;
  353. struct source *source;
  354. struct stream *stream;
  355. struct mux *mux;
  356. struct sourcelist sol;
  357. SLIST_INIT(mul);
  358. SLIST_INIT(&sol);
  359. if ((l = open_lex(filename)) == NULL)
  360. return 0;
  361. while (lex_nexttoken(l)) {
  362. /* expecting keyword now */
  363. switch (l->op) {
  364. case LXT_MUX:
  365. if (!read_mux(mul, l)) {
  366. free_sourcelist(&sol);
  367. return 0;
  368. }
  369. break;
  370. case LXT_SOURCE:
  371. if (!read_source(&sol, l)) {
  372. free_sourcelist(&sol);
  373. return 0;
  374. }
  375. break;
  376. default:
  377. parse_error(l, "mux|source");
  378. free_sourcelist(&sol);
  379. return 0;
  380. break;
  381. }
  382. }
  383. /* sanity checks */
  384. if (SLIST_EMPTY(mul)) {
  385. free_sourcelist(&sol);
  386. warning("%s: no mux statement seen",
  387. l->filename);
  388. return 0;
  389. }
  390. else {
  391. mux = SLIST_FIRST(mul);
  392. mux->sol = sol;
  393. if (strncmp(SYMON_UNKMUX, mux->name, sizeof(SYMON_UNKMUX)) == 0) {
  394. /* mux was not initialised for some reason */
  395. return 0;
  396. }
  397. }
  398. if (SLIST_EMPTY(&sol)) {
  399. warning("%s: no source section seen",
  400. l->filename);
  401. return 0;
  402. }
  403. else {
  404. SLIST_FOREACH(source, &sol, sources) {
  405. if (SLIST_EMPTY(&source->sl)) {
  406. warning("%s: no streams accepted for source '%s'",
  407. l->filename, source->addr);
  408. return 0;
  409. }
  410. else {
  411. SLIST_FOREACH(stream, &source->sl, streams) {
  412. if (stream->file == NULL) {
  413. warning("%s: no filename specified for stream '%s(%s)' in source '%s'",
  414. l->filename, type2str(stream->type), stream->args, source->addr);
  415. return 0;
  416. }
  417. }
  418. }
  419. }
  420. }
  421. close_lex(l);
  422. return 1;
  423. }