readconf.c 13 KB

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