readconf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /* $Id: readconf.c,v 1.18 2003/01/18 09:55:37 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_MBUF:
  76. ts = "mbuf";
  77. ta = "";
  78. break;
  79. case MT_DEBUG:
  80. ts = "debug";
  81. ta = "";
  82. break;
  83. case MT_PROC:
  84. ts = "proc_";
  85. ta = args;
  86. break;
  87. default:
  88. warning("%s:%d: internal error: type (%d) unknown",
  89. __FILE__, __LINE__, type);
  90. return 0;
  91. }
  92. if ((snprintf(path, maxlen, "/%s%s.rrd", ts, ta)) >= maxlen)
  93. return 0;
  94. else
  95. return 1;
  96. }
  97. /* mux <host> (port|,| ) <number> */
  98. int
  99. read_mux(struct muxlist * mul, struct lex * l)
  100. {
  101. char muxname[_POSIX2_LINE_MAX];
  102. struct mux *mux;
  103. if (!SLIST_EMPTY(mul)) {
  104. warning("%s:%d: only one mux statement allowed",
  105. l->filename, l->cline);
  106. return 0;
  107. }
  108. lex_nexttoken(l);
  109. if (!getip(l->token)) {
  110. warning("%s:%d: could not resolve '%s'",
  111. l->filename, l->cline, l->token);
  112. return 0;
  113. }
  114. mux = add_mux(mul, SYMON_UNKMUX);
  115. mux->addr = xstrdup((const char *) &res_host);
  116. /* check for port statement */
  117. lex_nexttoken(l);
  118. if (l->op == LXT_PORT || l->op == LXT_COMMA)
  119. lex_nexttoken(l);
  120. if (l->type != LXY_NUMBER) {
  121. lex_ungettoken(l);
  122. mux->port = xstrdup(default_symux_port);
  123. }
  124. else {
  125. mux->port = xstrdup((const char *) l->token);
  126. }
  127. bzero(&muxname, sizeof(muxname));
  128. snprintf(&muxname[0], sizeof(muxname), "%s %s", mux->addr, mux->port);
  129. if (rename_mux(mul, mux, muxname) == NULL)
  130. fatal("%s:%d: internal error: dual mux", __FILE__, __LINE__);
  131. return 1;
  132. }
  133. /* source <host> { accept ... | write ... | datadir ... } */
  134. int
  135. read_source(struct sourcelist * sol, struct lex * l)
  136. {
  137. struct source *source;
  138. struct stream *stream;
  139. struct stat sb;
  140. char path[_POSIX2_LINE_MAX];
  141. char sn[_POSIX2_LINE_MAX];
  142. char sa[_POSIX2_LINE_MAX];
  143. int st;
  144. int pc;
  145. int fd;
  146. /* get hostname */
  147. lex_nexttoken(l);
  148. if (!getip(l->token)) {
  149. warning("%s:%d: could not resolve '%s'",
  150. l->filename, l->cline, l->token);
  151. return 0;
  152. }
  153. source = add_source(sol, res_host);
  154. EXPECT(l, LXT_BEGIN);
  155. while (lex_nexttoken(l)) {
  156. switch (l->op) {
  157. /* accept { cpu(x), ... } */
  158. case LXT_ACCEPT:
  159. EXPECT(l, LXT_BEGIN);
  160. while (lex_nexttoken(l) && l->op != LXT_END) {
  161. switch (l->op) {
  162. case LXT_CPU:
  163. case LXT_IF:
  164. case LXT_IO:
  165. case LXT_MEM:
  166. case LXT_PF:
  167. case LXT_MBUF:
  168. case LXT_DEBUG:
  169. case LXT_PROC:
  170. st = token2type(l->op);
  171. strncpy(&sn[0], l->token, _POSIX2_LINE_MAX);
  172. /* parse arg */
  173. lex_nexttoken(l);
  174. if (l->op == LXT_OPEN) {
  175. lex_nexttoken(l);
  176. if (l->op == LXT_CLOSE) {
  177. parse_error(l, "<stream argument>");
  178. return 0;
  179. }
  180. strncpy(&sa[0], l->token, _POSIX2_LINE_MAX);
  181. lex_nexttoken(l);
  182. if (l->op != LXT_CLOSE) {
  183. parse_error(l, ")");
  184. return 0;
  185. }
  186. }
  187. else {
  188. lex_ungettoken(l);
  189. sa[0] = '\0';
  190. }
  191. if ((stream = add_source_stream(source, st, sa)) == NULL) {
  192. warning("%s:%d: stream %s(%s) redefined",
  193. l->filename, l->cline, sn, sa);
  194. return 0;
  195. }
  196. break; /* LXT_CPU/IF/IO/MEM/PF/MBUF/DEBUG/PROC */
  197. case LXT_COMMA:
  198. break;
  199. default:
  200. parse_error(l, "{cpu|mem|if|io|pf|debug}");
  201. return 0;
  202. break;
  203. }
  204. }
  205. break; /* LXT_ACCEPT */
  206. /* datadir "path" */
  207. case LXT_DATADIR:
  208. lex_nexttoken(l);
  209. /* is path absolute */
  210. if (l->token && l->token[0] != '/') {
  211. warning("%s:%d: datadir path '%s' is not absolute",
  212. l->filename, l->cline, l->token);
  213. return 0;
  214. }
  215. /* make sure that directory exists */
  216. bzero(&sb, sizeof(struct stat));
  217. if (stat(l->token, &sb) == 0) {
  218. if (!(sb.st_mode & S_IFDIR)) {
  219. warning("%s:%d: datadir path '%s' is not a directory",
  220. l->filename, l->cline, l->token);
  221. return 0;
  222. }
  223. }
  224. else {
  225. warning("%s:%d: could not stat datadir path '%s'",
  226. l->filename, l->cline, l->token);
  227. return 0;
  228. }
  229. strncpy(&path[0], l->token, _POSIX2_LINE_MAX);
  230. path[_POSIX2_LINE_MAX - 1] = '\0';
  231. pc = strlen(path);
  232. if (path[pc - 1] == '/') {
  233. path[pc - 1] = '\0';
  234. pc--;
  235. }
  236. /* add path to empty streams */
  237. SLIST_FOREACH(stream, &source->sl, streams) {
  238. if (stream->file == NULL) {
  239. if (!(insert_filename(&path[pc],
  240. _POSIX2_LINE_MAX - pc,
  241. stream->type,
  242. stream->args))) {
  243. if (stream->args && strlen(stream->args)) {
  244. warning("%s:%d: failed to construct stream "
  245. "%s(%s) filename using datadir '%s'",
  246. l->filename, l->cline,
  247. type2str(stream->type),
  248. stream->args, l->token);
  249. }
  250. else {
  251. warning("%s:%d: failed to construct stream "
  252. "%s) filename using datadir '%s'",
  253. l->filename, l->cline,
  254. type2str(stream->type),
  255. l->token);
  256. }
  257. return 0;
  258. }
  259. /* try filename */
  260. if ((fd = open(path, O_RDWR | O_NONBLOCK, 0)) == -1) {
  261. warning("%s:%d: file '%s', guessed by datadir, cannot be opened",
  262. l->filename, l->cline, path);
  263. return 0;
  264. }
  265. else {
  266. close(fd);
  267. stream->file = xstrdup(path);
  268. }
  269. }
  270. }
  271. break; /* LXT_DATADIR */
  272. /* write cpu(0) in "filename" */
  273. case LXT_WRITE:
  274. lex_nexttoken(l);
  275. switch (l->op) {
  276. case LXT_CPU:
  277. case LXT_IF:
  278. case LXT_IO:
  279. case LXT_MEM:
  280. case LXT_PF:
  281. case LXT_MBUF:
  282. case LXT_DEBUG:
  283. case LXT_PROC:
  284. st = token2type(l->op);
  285. strncpy(&sn[0], l->token, _POSIX2_LINE_MAX);
  286. /* parse arg */
  287. lex_nexttoken(l);
  288. if (l->op == LXT_OPEN) {
  289. lex_nexttoken(l);
  290. if (l->op == LXT_CLOSE) {
  291. parse_error(l, "<stream argument>");
  292. return 0;
  293. }
  294. strncpy(&sa[0], l->token, _POSIX2_LINE_MAX);
  295. lex_nexttoken(l);
  296. if (l->op != LXT_CLOSE) {
  297. parse_error(l, ")");
  298. return 0;
  299. }
  300. }
  301. else {
  302. lex_ungettoken(l);
  303. sa[0] = '\0';
  304. }
  305. EXPECT(l, LXT_IN);
  306. lex_nexttoken(l);
  307. if ((stream = find_source_stream(source, st, sa)) == NULL) {
  308. if (strlen(sa)) {
  309. warning("%s:%d: stream %s(%s) is not accepted for %s",
  310. l->filename, l->cline, sn, sa, source->addr);
  311. return 0;
  312. }
  313. else {
  314. warning("%s:%d: stream %s is not accepted for %s",
  315. l->filename, l->cline, sn, source->addr);
  316. return 0;
  317. }
  318. }
  319. else {
  320. /* try filename */
  321. if ((fd = open(l->token, O_RDWR | O_NONBLOCK, 0)) == -1) {
  322. warning("%s:%d: file '%s' cannot be opened",
  323. l->filename, l->cline, l->token);
  324. return 0;
  325. }
  326. else {
  327. close(fd);
  328. if (stream->file != NULL) {
  329. warning("%s:%d: file '%s' overwrites previous definition '%s'",
  330. l->filename, l->cline, l->token, stream->file);
  331. xfree(stream->file);
  332. }
  333. stream->file = xstrdup(l->token);
  334. }
  335. }
  336. break; /* LXT_CPU/IF/IO/MEM/PF/MBUF/DEBUG/PROC */
  337. default:
  338. parse_error(l, "{cpu|mem|if|io|debug|proc}");
  339. return 0;
  340. break;
  341. }
  342. break; /* LXT_WRITE */
  343. case LXT_END:
  344. return 1;
  345. default:
  346. parse_error(l, "accept|write");
  347. return 0;
  348. }
  349. }
  350. warning("%s:%d: missing close brace on source statement",
  351. l->filename, l->cline);
  352. return 0;
  353. }
  354. /* Read symux.conf */
  355. int
  356. read_config_file(struct muxlist * mul, const char *filename)
  357. {
  358. struct lex *l;
  359. struct source *source;
  360. struct stream *stream;
  361. struct mux *mux;
  362. struct sourcelist sol;
  363. SLIST_INIT(mul);
  364. SLIST_INIT(&sol);
  365. if ((l = open_lex(filename)) == NULL)
  366. return 0;
  367. while (lex_nexttoken(l)) {
  368. /* expecting keyword now */
  369. switch (l->op) {
  370. case LXT_MUX:
  371. if (!read_mux(mul, l)) {
  372. free_sourcelist(&sol);
  373. return 0;
  374. }
  375. break;
  376. case LXT_SOURCE:
  377. if (!read_source(&sol, l)) {
  378. free_sourcelist(&sol);
  379. return 0;
  380. }
  381. break;
  382. default:
  383. parse_error(l, "mux|source");
  384. free_sourcelist(&sol);
  385. return 0;
  386. break;
  387. }
  388. }
  389. /* sanity checks */
  390. if (SLIST_EMPTY(mul)) {
  391. free_sourcelist(&sol);
  392. warning("%s: no mux statement seen",
  393. l->filename);
  394. return 0;
  395. }
  396. else {
  397. mux = SLIST_FIRST(mul);
  398. mux->sol = sol;
  399. if (strncmp(SYMON_UNKMUX, mux->name, sizeof(SYMON_UNKMUX)) == 0) {
  400. /* mux was not initialised for some reason */
  401. return 0;
  402. }
  403. }
  404. if (SLIST_EMPTY(&sol)) {
  405. warning("%s: no source section seen",
  406. l->filename);
  407. return 0;
  408. }
  409. else {
  410. SLIST_FOREACH(source, &sol, sources) {
  411. if (SLIST_EMPTY(&source->sl)) {
  412. warning("%s: no streams accepted for source '%s'",
  413. l->filename, source->addr);
  414. return 0;
  415. }
  416. else {
  417. SLIST_FOREACH(stream, &source->sl, streams) {
  418. if (stream->file == NULL) {
  419. warning("%s: no filename specified for stream '%s(%s)' in source '%s'",
  420. l->filename, type2str(stream->type), stream->args, source->addr);
  421. return 0;
  422. }
  423. }
  424. }
  425. }
  426. }
  427. close_lex(l);
  428. return 1;
  429. }