mainconfig.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include <errno.h>
  38. #include <assert.h>
  39. #include <sys/socket.h>
  40. #include <netinet/in.h>
  41. #include <arpa/inet.h>
  42. #include "../include/saAis.h"
  43. #include "../include/list.h"
  44. #include "util.h"
  45. #include "mainconfig.h"
  46. #include "mempool.h"
  47. #include "print.h"
  48. #include "totem.h"
  49. #include "service.h"
  50. DECLARE_LIST_INIT (saAmfGroupHead);
  51. static char error_string_response[512];
  52. typedef enum {
  53. MAIN_HEAD,
  54. MAIN_LOGGING,
  55. MAIN_EVENT,
  56. MAIN_AMF,
  57. MAIN_COMPONENTS
  58. } main_parse_t;
  59. char *strstr_rs (const char *haystack, const char *needle)
  60. {
  61. char *end_address;
  62. char *new_needle;
  63. new_needle = (char *)mempool_strdup (needle);
  64. new_needle[strlen(new_needle) - 1] = '\0';
  65. end_address = strstr (haystack, new_needle);
  66. if (end_address) {
  67. end_address += strlen (new_needle);
  68. end_address = strstr (end_address, needle + strlen (new_needle));
  69. }
  70. if (end_address) {
  71. end_address += 1; /* skip past { or = */
  72. do {
  73. if (*end_address == '\t' || *end_address == ' ') {
  74. end_address++;
  75. } else {
  76. break;
  77. }
  78. } while (*end_address != '\0');
  79. }
  80. mempool_free (new_needle);
  81. return (end_address);
  82. }
  83. /* Returns an allocated string */
  84. static char *get_component(const char *line, int *version)
  85. {
  86. char *start_address;
  87. char *end_address;
  88. char *newline;
  89. char *compname;
  90. newline = strdup(line);
  91. start_address = newline + strspn(newline, " \t");
  92. end_address = start_address + strcspn(start_address, " \t:");
  93. *end_address = '\0';
  94. compname = strdup(start_address);
  95. /* Now get version */
  96. start_address = end_address+1;
  97. start_address = start_address + strspn(start_address, " \t:");
  98. *version = atoi(start_address);
  99. free(newline);
  100. return compname;
  101. }
  102. extern int openais_main_config_read (
  103. struct objdb_iface_ver0 *objdb,
  104. char **error_string,
  105. struct openais_config *openais_config,
  106. int interface_max)
  107. {
  108. FILE *fp;
  109. int line_number = 0;
  110. main_parse_t parse = MAIN_HEAD;
  111. int logging_parsed = 0;
  112. int event_parsed = 0;
  113. int amf_parsed = 0;
  114. int components_parsed = 0;
  115. char *loc;
  116. int i;
  117. int parse_done = 0;
  118. char line[512];
  119. char *error_reason = error_string_response;
  120. memset (openais_config, 0, sizeof (struct openais_config));
  121. fp = fopen (OPENAIS_CONFDIR "/openais.conf", "r");
  122. if (fp == 0) {
  123. parse_done = 1;
  124. sprintf (error_reason, "Can't read file %s/openais.conf reason = (%s)\n",
  125. OPENAIS_CONFDIR, strerror (errno));
  126. *error_string = error_reason;
  127. return -1;
  128. }
  129. while (fgets (line, 255, fp)) {
  130. line_number += 1;
  131. line[strlen(line) - 1] = '\0';
  132. /*
  133. * Clear out white space and tabs
  134. */
  135. for (i = strlen (line) - 1; i > -1; i--) {
  136. if (line[i] == '\t' || line[i] == ' ') {
  137. line[i] = '\0';
  138. } else {
  139. break;
  140. }
  141. }
  142. /*
  143. * Clear out comments and empty lines
  144. */
  145. if (line[0] == '#' || line[0] == '\0') {
  146. continue;
  147. }
  148. line_number += 1;
  149. switch (parse) {
  150. case MAIN_HEAD:
  151. if (logging_parsed == 0 && strstr_rs (line, "logging{")) {
  152. logging_parsed = 1;
  153. parse = MAIN_LOGGING;
  154. } else
  155. if (event_parsed == 0 && strstr_rs (line, "event{")) {
  156. event_parsed = 1;
  157. parse = MAIN_EVENT;
  158. } else
  159. if (amf_parsed == 0 && strstr_rs (line, "amf{")) {
  160. amf_parsed = 1;
  161. parse = MAIN_AMF;
  162. } else
  163. if (components_parsed == 0 && strstr_rs (line, "components{")) {
  164. components_parsed = 1;
  165. parse = MAIN_COMPONENTS;
  166. } else {
  167. continue;
  168. }
  169. break;
  170. case MAIN_LOGGING:
  171. if ((loc = strstr_rs (line, "logoutput:"))) {
  172. if (strcmp (loc, "file") == 0) {
  173. openais_config->logmode |= LOG_MODE_FILE;
  174. } else
  175. if (strcmp (loc, "syslog") == 0) {
  176. openais_config->logmode |= LOG_MODE_SYSLOG;
  177. } else
  178. if (strcmp (loc, "stderr") == 0) {
  179. openais_config->logmode |= LOG_MODE_STDERR;
  180. } else {
  181. goto parse_error;
  182. }
  183. } else
  184. if ((loc = strstr_rs (line, "debug:"))) {
  185. if (strcmp (loc, "on") == 0) {
  186. openais_config->logmode |= LOG_MODE_DEBUG;
  187. } else
  188. if (strcmp (loc, "off") == 0) {
  189. openais_config->logmode &= ~LOG_MODE_DEBUG;
  190. } else {
  191. goto parse_error;
  192. }
  193. } else
  194. if ((loc = strstr_rs (line, "timestamp:"))) {
  195. if (strcmp (loc, "on") == 0) {
  196. openais_config->logmode |= LOG_MODE_TIMESTAMP;
  197. } else
  198. if (strcmp (loc, "off") == 0) {
  199. openais_config->logmode &= ~LOG_MODE_TIMESTAMP;
  200. } else {
  201. goto parse_error;
  202. }
  203. } else
  204. if ((loc = strstr_rs (line, "logfile:"))) {
  205. openais_config->logfile = strdup (loc);
  206. } else
  207. if ((loc = strstr_rs (line, "}"))) {
  208. parse = MAIN_HEAD;
  209. } else {
  210. goto parse_error;
  211. }
  212. break;
  213. case MAIN_EVENT:
  214. if ((loc = strstr_rs (line, "delivery_queue_size:"))) {
  215. openais_config->evt_delivery_queue_size = atoi(loc);
  216. } else if ((loc = strstr_rs (line, "delivery_queue_resume:"))) {
  217. openais_config->evt_delivery_queue_resume = atoi(loc);
  218. } else if ((loc = strstr_rs (line, "}"))) {
  219. parse = MAIN_HEAD;
  220. } else {
  221. goto parse_error;
  222. }
  223. break;
  224. case MAIN_AMF:
  225. if ((loc = strstr_rs (line, "mode:"))) {
  226. if (strcmp (loc, "enabled") == 0) {
  227. openais_config->amf_enabled = 1;
  228. } else
  229. if (strcmp (loc, "disabled") == 0) {
  230. openais_config->amf_enabled = 0;
  231. } else {
  232. goto parse_error;
  233. }
  234. } else
  235. if ((loc = strstr_rs (line, "}"))) {
  236. parse = MAIN_HEAD;
  237. } else {
  238. goto parse_error;
  239. }
  240. break;
  241. case MAIN_COMPONENTS:
  242. if ((loc = strstr_rs (line, "}"))) {
  243. parse = MAIN_HEAD;
  244. }
  245. else {
  246. int version;
  247. char *name = get_component(line, &version);
  248. if (name) {
  249. openais_service_objdb_add (objdb, name, version);
  250. }
  251. }
  252. break;
  253. default:
  254. assert (0 == 1); /* SHOULDN'T HAPPEN */
  255. break;
  256. }
  257. }
  258. if ((openais_config->logmode & LOG_MODE_FILE) && openais_config->logfile == 0) {
  259. error_reason = "logmode set to 'file' but no logfile specified";
  260. goto parse_error;
  261. }
  262. if (parse == MAIN_HEAD) {
  263. fclose (fp);
  264. return (0);
  265. } else {
  266. error_reason = "Missing closing brace";
  267. goto parse_error;
  268. }
  269. parse_error:
  270. if (parse_done) {
  271. sprintf (error_string_response,
  272. "parse error in %s/openais.conf: %s.\n",
  273. OPENAIS_CONFDIR, error_reason);
  274. } else {
  275. sprintf (error_string_response,
  276. "parse error in %s/openais.conf: %s (line %d).\n",
  277. OPENAIS_CONFDIR, error_reason, line_number);
  278. }
  279. *error_string = error_string_response;
  280. fclose (fp);
  281. return (-1);
  282. }