mainconfig.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. DECLARE_LIST_INIT (saAmfGroupHead);
  50. static char error_string_response[512];
  51. typedef enum {
  52. MAIN_HEAD,
  53. MAIN_LOGGING,
  54. MAIN_EVENT,
  55. MAIN_AMF,
  56. MAIN_COMPONENTS
  57. } main_parse_t;
  58. char *strstr_rs (const char *haystack, const char *needle)
  59. {
  60. char *end_address;
  61. char *new_needle;
  62. new_needle = (char *)mempool_strdup (needle);
  63. new_needle[strlen(new_needle) - 1] = '\0';
  64. end_address = strstr (haystack, new_needle);
  65. if (end_address) {
  66. end_address += strlen (new_needle);
  67. end_address = strstr (end_address, needle + strlen (new_needle));
  68. }
  69. if (end_address) {
  70. end_address += 1; /* skip past { or = */
  71. do {
  72. if (*end_address == '\t' || *end_address == ' ') {
  73. end_address++;
  74. } else {
  75. break;
  76. }
  77. } while (*end_address != '\0');
  78. }
  79. mempool_free (new_needle);
  80. return (end_address);
  81. }
  82. #ifdef BUILD_DYNAMIC
  83. static void set_default_services(struct openais_config *config)
  84. {
  85. config->dynamic_services[0].name = "openais_evs";
  86. config->dynamic_services[0].ver = 0;
  87. config->dynamic_services[1].name = "openais_clm";
  88. config->dynamic_services[1].ver = 0;
  89. config->dynamic_services[2].name = "openais_amf";
  90. config->dynamic_services[2].ver = 0;
  91. config->dynamic_services[3].name = "openais_ckpt";
  92. config->dynamic_services[3].ver = 0;
  93. config->dynamic_services[4].name = "openais_evt";
  94. config->dynamic_services[4].ver = 0;
  95. config->dynamic_services[5].name = "openais_lck";
  96. config->dynamic_services[5].ver = 0;
  97. config->dynamic_services[6].name = "openais_msg";
  98. config->dynamic_services[6].ver = 0;
  99. config->dynamic_services[7].name = "openais_cfg";
  100. config->dynamic_services[7].ver = 0;
  101. config->num_dynamic_services = 8;
  102. }
  103. /* Returns an allocated string */
  104. static char *get_component(const char *line, int *version)
  105. {
  106. char *start_address;
  107. char *end_address;
  108. char *newline;
  109. char *compname;
  110. newline = strdup(line);
  111. start_address = newline + strspn(newline, " \t");
  112. end_address = start_address + strcspn(start_address, " \t:");
  113. *end_address = '\0';
  114. compname = strdup(start_address);
  115. /* Now get version */
  116. start_address = end_address+1;
  117. start_address = start_address + strspn(start_address, " \t:");
  118. *version = atoi(start_address);
  119. free(newline);
  120. return compname;
  121. }
  122. #endif
  123. extern int openais_main_config_read (char **error_string,
  124. struct openais_config *openais_config,
  125. int interface_max)
  126. {
  127. FILE *fp;
  128. int line_number = 0;
  129. main_parse_t parse = MAIN_HEAD;
  130. int logging_parsed = 0;
  131. int event_parsed = 0;
  132. int amf_parsed = 0;
  133. int components_parsed = 0;
  134. char *loc;
  135. int i;
  136. int parse_done = 0;
  137. char line[512];
  138. char *error_reason = error_string_response;
  139. memset (openais_config, 0, sizeof (struct openais_config));
  140. fp = fopen ("/etc/ais/openais.conf", "r");
  141. if (fp == 0) {
  142. parse_done = 1;
  143. sprintf (error_reason, "Can't read file reason = (%s)\n", strerror (errno));
  144. *error_string = error_reason;
  145. return -1;
  146. }
  147. while (fgets (line, 255, fp)) {
  148. line_number += 1;
  149. line[strlen(line) - 1] = '\0';
  150. /*
  151. * Clear out white space and tabs
  152. */
  153. for (i = strlen (line) - 1; i > -1; i--) {
  154. if (line[i] == '\t' || line[i] == ' ') {
  155. line[i] = '\0';
  156. } else {
  157. break;
  158. }
  159. }
  160. /*
  161. * Clear out comments and empty lines
  162. */
  163. if (line[0] == '#' || line[0] == '\0') {
  164. continue;
  165. }
  166. line_number += 1;
  167. switch (parse) {
  168. case MAIN_HEAD:
  169. if (logging_parsed == 0 && strstr_rs (line, "logging{")) {
  170. logging_parsed = 1;
  171. parse = MAIN_LOGGING;
  172. } else
  173. if (event_parsed == 0 && strstr_rs (line, "event{")) {
  174. event_parsed = 1;
  175. parse = MAIN_EVENT;
  176. } else
  177. if (amf_parsed == 0 && strstr_rs (line, "amf{")) {
  178. amf_parsed = 1;
  179. parse = MAIN_AMF;
  180. } else
  181. if (components_parsed == 0 && strstr_rs (line, "components{")) {
  182. components_parsed = 1;
  183. parse = MAIN_COMPONENTS;
  184. } else {
  185. continue;
  186. }
  187. break;
  188. case MAIN_LOGGING:
  189. if ((loc = strstr_rs (line, "logoutput:"))) {
  190. if (strcmp (loc, "file") == 0) {
  191. openais_config->logmode |= LOG_MODE_FILE;
  192. } else
  193. if (strcmp (loc, "syslog") == 0) {
  194. openais_config->logmode |= LOG_MODE_SYSLOG;
  195. } else
  196. if (strcmp (loc, "stderr") == 0) {
  197. openais_config->logmode |= LOG_MODE_STDERR;
  198. } else {
  199. goto parse_error;
  200. }
  201. } else
  202. if ((loc = strstr_rs (line, "debug:"))) {
  203. if (strcmp (loc, "on") == 0) {
  204. openais_config->logmode |= LOG_MODE_DEBUG;
  205. } else
  206. if (strcmp (loc, "off") == 0) {
  207. openais_config->logmode &= ~LOG_MODE_DEBUG;
  208. } else {
  209. goto parse_error;
  210. }
  211. } else
  212. if ((loc = strstr_rs (line, "timestamp:"))) {
  213. if (strcmp (loc, "on") == 0) {
  214. openais_config->logmode |= LOG_MODE_TIMESTAMP;
  215. } else
  216. if (strcmp (loc, "off") == 0) {
  217. openais_config->logmode &= ~LOG_MODE_TIMESTAMP;
  218. } else {
  219. goto parse_error;
  220. }
  221. } else
  222. if ((loc = strstr_rs (line, "logfile:"))) {
  223. openais_config->logfile = strdup (loc);
  224. } else
  225. if ((loc = strstr_rs (line, "}"))) {
  226. parse = MAIN_HEAD;
  227. } else {
  228. goto parse_error;
  229. }
  230. break;
  231. case MAIN_EVENT:
  232. if ((loc = strstr_rs (line, "delivery_queue_size:"))) {
  233. openais_config->evt_delivery_queue_size = atoi(loc);
  234. } else if ((loc = strstr_rs (line, "delivery_queue_resume:"))) {
  235. openais_config->evt_delivery_queue_resume = atoi(loc);
  236. } else if ((loc = strstr_rs (line, "}"))) {
  237. parse = MAIN_HEAD;
  238. } else {
  239. goto parse_error;
  240. }
  241. break;
  242. case MAIN_AMF:
  243. if ((loc = strstr_rs (line, "mode:"))) {
  244. if (strcmp (loc, "enabled") == 0) {
  245. openais_config->amf_enabled = 1;
  246. } else
  247. if (strcmp (loc, "disabled") == 0) {
  248. openais_config->amf_enabled = 0;
  249. } else {
  250. goto parse_error;
  251. }
  252. } else
  253. if ((loc = strstr_rs (line, "}"))) {
  254. parse = MAIN_HEAD;
  255. } else {
  256. goto parse_error;
  257. }
  258. break;
  259. case MAIN_COMPONENTS:
  260. if ((loc = strstr_rs (line, "}"))) {
  261. parse = MAIN_HEAD;
  262. }
  263. #ifdef BUILD_DYNAMIC
  264. else {
  265. int version;
  266. char *name = get_component(line, &version);
  267. if (name) {
  268. openais_config->dynamic_services[openais_config->num_dynamic_services].name = name;
  269. openais_config->dynamic_services[openais_config->num_dynamic_services].ver = version;
  270. openais_config->num_dynamic_services++;
  271. }
  272. }
  273. #endif
  274. break;
  275. default:
  276. assert (0 == 1); /* SHOULDN'T HAPPEN */
  277. break;
  278. }
  279. }
  280. if ((openais_config->logmode & LOG_MODE_FILE) && openais_config->logfile == 0) {
  281. error_reason = "logmode set to 'file' but no logfile specified";
  282. goto parse_error;
  283. }
  284. #ifdef BUILD_DYNAMIC
  285. /* Load default services if the config file doesn't specify */
  286. if (!openais_config->num_dynamic_services) {
  287. set_default_services(openais_config);
  288. }
  289. #endif
  290. if (parse == MAIN_HEAD) {
  291. fclose (fp);
  292. return (0);
  293. } else {
  294. error_reason = "Missing closing brace";
  295. goto parse_error;
  296. }
  297. parse_error:
  298. if (parse_done) {
  299. sprintf (error_string_response,
  300. "parse error in /etc/ais/openais.conf: %s.\n", error_reason);
  301. } else {
  302. sprintf (error_string_response,
  303. "parse error in /etc/ais/openais.conf: %s (line %d).\n",
  304. error_reason, line_number);
  305. }
  306. *error_string = error_string_response;
  307. fclose (fp);
  308. return (-1);
  309. }