mainconfig.c 9.0 KB

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