readconf.c 12 KB

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