readconf.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* $Id: readconf.c,v 1.11 2002/08/11 19:54:48 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2002 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 <stdarg.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <assert.h>
  36. #include <limits.h>
  37. #include <fcntl.h>
  38. #include <unistd.h>
  39. #include "xmalloc.h"
  40. #include "lex.h"
  41. #include "error.h"
  42. #include "net.h"
  43. #include "data.h"
  44. __BEGIN_DECLS
  45. int read_mux(struct muxlist *mul, struct lex *);
  46. int read_source(struct sourcelist *sol, struct lex *);
  47. __END_DECLS
  48. /* mux <host> (port|:|,| ) <number> */
  49. int
  50. read_mux(struct muxlist *mul, struct lex *l)
  51. {
  52. struct mux *m;
  53. if (! SLIST_EMPTY(mul)) {
  54. warning("%s:%d: only one mux statement allowed",
  55. l->filename, l->cline);
  56. return 0;
  57. }
  58. lex_nexttoken(l);
  59. if (!lookup(l->token)) {
  60. warning("%s:%d: could not resolve '%s'",
  61. l->filename, l->cline, l->token );
  62. return 0;
  63. }
  64. m = add_mux(mul, lookup_address);
  65. m->ip = lookup_ip;
  66. /* check for port statement */
  67. lex_nexttoken(l);
  68. if (l->op == LXT_PORT || l->op == LXT_COLON || l->op == LXT_COMMA)
  69. lex_nexttoken(l);
  70. else {
  71. if (l->type != LXY_NUMBER) {
  72. lex_ungettoken(l);
  73. m->port = MONMUX_PORT;
  74. return 1;
  75. }
  76. }
  77. if (l->type != LXY_NUMBER) {
  78. parse_error(l, "<number>");
  79. return 0;
  80. }
  81. m->port = l->value;
  82. return 1;
  83. }
  84. /* source <host> { accept ... | write ... } */
  85. int
  86. read_source(struct sourcelist *sol, struct lex *l)
  87. {
  88. struct source *source;
  89. struct stream *stream;
  90. char sn[_POSIX2_LINE_MAX];
  91. char sa[_POSIX2_LINE_MAX];
  92. int st;
  93. /* get hostname */
  94. lex_nexttoken(l);
  95. if (!lookup(l->token)) {
  96. warning("%s:%d: could not resolve '%s'",
  97. l->filename, l->cline, l->token );
  98. return 0;
  99. }
  100. source = add_source(sol, lookup_address);
  101. source->ip = lookup_ip;
  102. EXPECT(l, LXT_BEGIN);
  103. while (lex_nexttoken(l)) {
  104. switch (l->op) {
  105. /* accept { cpu(x), ... } */
  106. case LXT_ACCEPT:
  107. EXPECT(l, LXT_BEGIN);
  108. while (lex_nexttoken(l) && l->op != LXT_END) {
  109. switch (l->op) {
  110. case LXT_CPU:
  111. case LXT_IF:
  112. case LXT_IO:
  113. case LXT_MEM:
  114. st = token2type(l->op);
  115. strncpy(&sn[0], l->token, _POSIX2_LINE_MAX);
  116. /* parse arg */
  117. lex_nexttoken(l);
  118. if (l->op == LXT_OPEN) {
  119. lex_nexttoken(l);
  120. if (l->op == LXT_CLOSE) {
  121. parse_error(l, "<stream argument>");
  122. return 0;
  123. }
  124. strncpy(&sa[0], l->token, _POSIX2_LINE_MAX);
  125. lex_nexttoken(l);
  126. if (l->op != LXT_CLOSE) {
  127. parse_error(l, ")");
  128. return 0;
  129. }
  130. } else {
  131. lex_ungettoken(l);
  132. sa[0]='\0';
  133. }
  134. if ((stream = add_source_stream(source, st, sa)) == NULL) {
  135. warning("%s:%d: stream %s(%s) redefined",
  136. l->filename, l->cline, sn, sa);
  137. return 0;
  138. }
  139. break; /* LXT_CPU/LXT_IF/LXT_IO/LXT_MEM */
  140. case LXT_COMMA:
  141. break;
  142. default:
  143. parse_error(l, "{cpu|mem|if|io}");
  144. return 0;
  145. break;
  146. }
  147. }
  148. break; /* LXT_ACCEPT */
  149. /* write cpu(0) in "filename" */
  150. case LXT_WRITE:
  151. lex_nexttoken(l);
  152. switch (l->op) {
  153. case LXT_CPU:
  154. case LXT_IF:
  155. case LXT_IO:
  156. case LXT_MEM:
  157. st = token2type(l->op);
  158. strncpy(&sn[0], l->token, _POSIX2_LINE_MAX);
  159. /* parse arg */
  160. lex_nexttoken(l);
  161. if (l->op == LXT_OPEN) {
  162. lex_nexttoken(l);
  163. if (l->op == LXT_CLOSE) {
  164. parse_error(l, "<stream argument>");
  165. return 0;
  166. }
  167. strncpy(&sa[0], l->token, _POSIX2_LINE_MAX);
  168. lex_nexttoken(l);
  169. if (l->op != LXT_CLOSE) {
  170. parse_error(l, ")");
  171. return 0;
  172. }
  173. } else {
  174. lex_ungettoken(l);
  175. sa[0]='\0';
  176. }
  177. EXPECT(l, LXT_IN);
  178. lex_nexttoken(l);
  179. if ((stream = find_source_stream(source, st, sa)) == NULL) {
  180. if (strlen(sa)) {
  181. warning("%s:%d: stream %s(%s) is not accepted for %s",
  182. l->filename, l->cline, sn, sa, source->name);
  183. return 0;
  184. } else {
  185. warning("%s:%d: stream %s is not accepted for %s",
  186. l->filename, l->cline, sn, source->name);
  187. return 0;
  188. }
  189. } else {
  190. int fd;
  191. /* try filename */
  192. if ((fd = open(l->token, O_RDWR | O_NONBLOCK, 0)) == -1) {
  193. warning("%s:%d: file '%s' cannot be opened",
  194. l->filename, l->cline, l->token);
  195. return 0;
  196. } else {
  197. close(fd);
  198. stream->file = xstrdup(l->token);
  199. }
  200. }
  201. break; /* LXT_CPU/LXT_IF/LXT_IO/LXT_MEM */
  202. default:
  203. parse_error(l, "{cpu|mem|if|io}");
  204. return 0;
  205. break;
  206. }
  207. break; /* LXT_WRITE */
  208. case LXT_END:
  209. return 1;
  210. default:
  211. parse_error(l, "accept|write");
  212. return 0;
  213. }
  214. }
  215. warning("%s:%d: missing close brace on source statement",
  216. l->filename, l->cline);
  217. return 0;
  218. }
  219. /* Read monmux.conf */
  220. int
  221. read_config_file(struct muxlist *mul,
  222. struct sourcelist *sol,
  223. const char *filename)
  224. {
  225. struct lex *l;
  226. struct source *source;
  227. struct stream *stream;
  228. SLIST_INIT(mul);
  229. SLIST_INIT(sol);
  230. if ((l = open_lex(filename)) == NULL)
  231. return 0;
  232. info("reading configfile '%s'", filename);
  233. while (lex_nexttoken(l)) {
  234. /* expecting keyword now */
  235. switch (l->op) {
  236. case LXT_MUX:
  237. if (!read_mux(mul, l))
  238. return 0;
  239. break;
  240. case LXT_SOURCE:
  241. if (!read_source(sol, l))
  242. return 0;
  243. break;
  244. default:
  245. parse_error(l, "mux|source" );
  246. return 0;
  247. break;
  248. }
  249. }
  250. /* sanity checks */
  251. if (SLIST_EMPTY(mul)) {
  252. warning("%s: no mux statement seen",
  253. l->filename);
  254. return 0;
  255. }
  256. if (SLIST_EMPTY(sol)) {
  257. warning("%s: no source section seen",
  258. l->filename);
  259. return 0;
  260. } else {
  261. SLIST_FOREACH(source, sol, sources) {
  262. if (SLIST_EMPTY(&source->sl)) {
  263. warning("%s: no streams accepted for source '%s'",
  264. l->filename, source->name);
  265. return 0;
  266. } else {
  267. SLIST_FOREACH(stream, &source->sl, streams) {
  268. if (stream->file == NULL) {
  269. warning("%s: no filename specified for stream '%s(%s)' in source '%s'",
  270. l->filename, type2str(stream->type), stream->args, source->name);
  271. return 0;
  272. }
  273. }
  274. }
  275. }
  276. }
  277. close_lex(l);
  278. return 1;
  279. }