readconf.c 16 KB

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