readconf.c 16 KB

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