readconf.c 11 KB

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