readconf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /* $Id: readconf.c,v 1.28 2005/10/16 15:27:03 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_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)
  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. /* make sure that directory exists */
  241. bzero(&sb, sizeof(struct stat));
  242. if (stat(l->token, &sb) == 0) {
  243. if (!(sb.st_mode & S_IFDIR)) {
  244. warning("%.200s:%d: datadir path '%.200s' is not a directory",
  245. l->filename, l->cline, l->token);
  246. return 0;
  247. }
  248. } else {
  249. warning("%.200s:%d: could not stat datadir path '%.200s'",
  250. l->filename, l->cline, l->token);
  251. return 0;
  252. }
  253. strncpy(&path[0], l->token, _POSIX2_LINE_MAX);
  254. path[_POSIX2_LINE_MAX - 1] = '\0';
  255. pc = strlen(path);
  256. if (path[pc - 1] == '/') {
  257. path[pc - 1] = '\0';
  258. pc--;
  259. }
  260. /* add path to empty streams */
  261. SLIST_FOREACH(stream, &source->sl, streams) {
  262. if (stream->file == NULL) {
  263. if (!(insert_filename(&path[pc],
  264. _POSIX2_LINE_MAX - pc,
  265. stream->type,
  266. stream->arg))) {
  267. if (stream->arg && strlen(stream->arg)) {
  268. warning("%.200s:%d: failed to construct stream "
  269. "%.200s(%.200s) filename using datadir '%.200s'",
  270. l->filename, l->cline,
  271. type2str(stream->type),
  272. stream->arg, l->token);
  273. } else {
  274. warning("%.200s:%d: failed to construct stream "
  275. "%.200s) filename using datadir '%.200s'",
  276. l->filename, l->cline,
  277. type2str(stream->type),
  278. l->token);
  279. }
  280. return 0;
  281. }
  282. /* try filename */
  283. if ((fd = open(path, O_RDWR | O_NONBLOCK, 0)) == -1) {
  284. /* warn, but allow */
  285. warning("%.200s:%d: file '%.200s', guessed by datadir, cannot be opened",
  286. l->filename, l->cline, path);
  287. } else {
  288. close(fd);
  289. stream->file = xstrdup(path);
  290. }
  291. }
  292. }
  293. break; /* LXT_DATADIR */
  294. /* write cpu(0) in "filename" */
  295. case LXT_WRITE:
  296. lex_nexttoken(l);
  297. switch (l->op) {
  298. case LXT_CPU:
  299. case LXT_DF:
  300. case LXT_IF:
  301. case LXT_IO:
  302. case LXT_IO1:
  303. case LXT_MEM:
  304. case LXT_PF:
  305. case LXT_PFQ:
  306. case LXT_MBUF:
  307. case LXT_DEBUG:
  308. case LXT_PROC:
  309. case LXT_SENSOR:
  310. st = token2type(l->op);
  311. strncpy(&sn[0], l->token, _POSIX2_LINE_MAX);
  312. /* parse arg */
  313. lex_nexttoken(l);
  314. if (l->op == LXT_OPEN) {
  315. lex_nexttoken(l);
  316. if (l->op == LXT_CLOSE) {
  317. parse_error(l, "<stream argument>");
  318. return 0;
  319. }
  320. strncpy(&sa[0], l->token, _POSIX2_LINE_MAX);
  321. lex_nexttoken(l);
  322. if (l->op != LXT_CLOSE) {
  323. parse_error(l, ")");
  324. return 0;
  325. }
  326. } else {
  327. lex_ungettoken(l);
  328. sa[0] = '\0';
  329. }
  330. EXPECT(l, LXT_IN);
  331. lex_nexttoken(l);
  332. if ((stream = find_source_stream(source, st, sa)) == NULL) {
  333. if (strlen(sa)) {
  334. warning("%.200s:%d: stream %.200s(%.200s) is not accepted for %.200s",
  335. l->filename, l->cline, sn, sa, source->addr);
  336. return 0;
  337. } else {
  338. warning("%.200s:%d: stream %.200s is not accepted for %.200s",
  339. l->filename, l->cline, sn, source->addr);
  340. return 0;
  341. }
  342. } else {
  343. /* try filename */
  344. if ((fd = open(l->token, O_RDWR | O_NONBLOCK, 0)) == -1) {
  345. warning("%.200s:%d: file '%.200s' cannot be opened",
  346. l->filename, l->cline, l->token);
  347. return 0;
  348. } else {
  349. close(fd);
  350. if (stream->file != NULL) {
  351. warning("%.200s:%d: file '%.200s' overwrites previous definition '%.200s'",
  352. l->filename, l->cline, l->token, stream->file);
  353. xfree(stream->file);
  354. }
  355. stream->file = xstrdup(l->token);
  356. }
  357. }
  358. break; /* LXT_CPU/IF/IO/IO1/MEM/PF/PFQ/MBUF/DEBUG/PROC/SENSOR */
  359. default:
  360. parse_error(l, "{cpu|if|io|mem|pf|mbuf|debug|proc|sensor}");
  361. return 0;
  362. break;
  363. }
  364. break; /* LXT_WRITE */
  365. case LXT_END:
  366. return 1;
  367. default:
  368. parse_error(l, "accept|datadir|write");
  369. return 0;
  370. }
  371. }
  372. warning("%.200s:%d: missing close brace on source statement",
  373. l->filename, l->cline);
  374. return 0;
  375. }
  376. /* Read symux.conf */
  377. int
  378. read_config_file(struct muxlist * mul, const char *filename)
  379. {
  380. struct lex *l;
  381. struct source *source;
  382. struct stream *stream;
  383. struct mux *mux;
  384. struct sourcelist sol;
  385. SLIST_INIT(mul);
  386. SLIST_INIT(&sol);
  387. if ((l = open_lex(filename)) == NULL)
  388. return 0;
  389. while (lex_nexttoken(l)) {
  390. /* expecting keyword now */
  391. switch (l->op) {
  392. case LXT_MUX:
  393. if (!read_mux(mul, l)) {
  394. free_sourcelist(&sol);
  395. return 0;
  396. }
  397. break;
  398. case LXT_SOURCE:
  399. if (!read_source(&sol, l)) {
  400. free_sourcelist(&sol);
  401. return 0;
  402. }
  403. break;
  404. default:
  405. parse_error(l, "mux|source");
  406. free_sourcelist(&sol);
  407. return 0;
  408. break;
  409. }
  410. }
  411. /* sanity checks */
  412. if (SLIST_EMPTY(mul)) {
  413. free_sourcelist(&sol);
  414. warning("%.200s: no mux statement seen",
  415. l->filename);
  416. return 0;
  417. } else {
  418. mux = SLIST_FIRST(mul);
  419. mux->sol = sol;
  420. if (strncmp(SYMON_UNKMUX, mux->name, sizeof(SYMON_UNKMUX)) == 0) {
  421. /* mux was not initialised for some reason */
  422. return 0;
  423. }
  424. }
  425. if (SLIST_EMPTY(&sol)) {
  426. warning("%.200s: no source section seen",
  427. l->filename);
  428. return 0;
  429. } else {
  430. SLIST_FOREACH(source, &sol, sources) {
  431. if (SLIST_EMPTY(&source->sl)) {
  432. warning("%.200s: no streams accepted for source '%.200s'",
  433. l->filename, source->addr);
  434. return 0;
  435. } else {
  436. SLIST_FOREACH(stream, &source->sl, streams) {
  437. if (stream->file == NULL) {
  438. /* warn, but allow */
  439. warning("%.200s: no filename specified for stream '%.200s(%.200s)' in source '%.200s'",
  440. l->filename, type2str(stream->type), stream->arg, source->addr);
  441. }
  442. }
  443. }
  444. }
  445. }
  446. close_lex(l);
  447. return 1;
  448. }