readconf.c 17 KB

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