mainconfig.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 ("/etc/ais/openais.conf", "r");
  139. if (fp == 0) {
  140. parse_done = 1;
  141. sprintf (error_reason, "Can't read file reason = (%s)\n", strerror (errno));
  142. *error_string = error_reason;
  143. return -1;
  144. }
  145. while (fgets (line, 255, fp)) {
  146. line_number += 1;
  147. line[strlen(line) - 1] = '\0';
  148. /*
  149. * Clear out white space and tabs
  150. */
  151. for (i = strlen (line) - 1; i > -1; i--) {
  152. if (line[i] == '\t' || line[i] == ' ') {
  153. line[i] = '\0';
  154. } else {
  155. break;
  156. }
  157. }
  158. /*
  159. * Clear out comments and empty lines
  160. */
  161. if (line[0] == '#' || line[0] == '\0') {
  162. continue;
  163. }
  164. line_number += 1;
  165. switch (parse) {
  166. case MAIN_HEAD:
  167. if (logging_parsed == 0 && strstr_rs (line, "logging{")) {
  168. logging_parsed = 1;
  169. parse = MAIN_LOGGING;
  170. } else
  171. if (event_parsed == 0 && strstr_rs (line, "event{")) {
  172. event_parsed = 1;
  173. parse = MAIN_EVENT;
  174. } else
  175. if (amf_parsed == 0 && strstr_rs (line, "amf{")) {
  176. amf_parsed = 1;
  177. parse = MAIN_AMF;
  178. } else
  179. if (components_parsed == 0 && strstr_rs (line, "components{")) {
  180. components_parsed = 1;
  181. parse = MAIN_COMPONENTS;
  182. } else {
  183. continue;
  184. }
  185. break;
  186. case MAIN_LOGGING:
  187. if ((loc = strstr_rs (line, "logoutput:"))) {
  188. if (strcmp (loc, "file") == 0) {
  189. openais_config->logmode |= LOG_MODE_FILE;
  190. } else
  191. if (strcmp (loc, "syslog") == 0) {
  192. openais_config->logmode |= LOG_MODE_SYSLOG;
  193. } else
  194. if (strcmp (loc, "stderr") == 0) {
  195. openais_config->logmode |= LOG_MODE_STDERR;
  196. } else {
  197. goto parse_error;
  198. }
  199. } else
  200. if ((loc = strstr_rs (line, "debug:"))) {
  201. if (strcmp (loc, "on") == 0) {
  202. openais_config->logmode |= LOG_MODE_DEBUG;
  203. } else
  204. if (strcmp (loc, "off") == 0) {
  205. openais_config->logmode &= ~LOG_MODE_DEBUG;
  206. } else {
  207. goto parse_error;
  208. }
  209. } else
  210. if ((loc = strstr_rs (line, "timestamp:"))) {
  211. if (strcmp (loc, "on") == 0) {
  212. openais_config->logmode |= LOG_MODE_TIMESTAMP;
  213. } else
  214. if (strcmp (loc, "off") == 0) {
  215. openais_config->logmode &= ~LOG_MODE_TIMESTAMP;
  216. } else {
  217. goto parse_error;
  218. }
  219. } else
  220. if ((loc = strstr_rs (line, "logfile:"))) {
  221. openais_config->logfile = strdup (loc);
  222. } else
  223. if ((loc = strstr_rs (line, "}"))) {
  224. parse = MAIN_HEAD;
  225. } else {
  226. goto parse_error;
  227. }
  228. break;
  229. case MAIN_EVENT:
  230. if ((loc = strstr_rs (line, "delivery_queue_size:"))) {
  231. openais_config->evt_delivery_queue_size = atoi(loc);
  232. } else if ((loc = strstr_rs (line, "delivery_queue_resume:"))) {
  233. openais_config->evt_delivery_queue_resume = atoi(loc);
  234. } else if ((loc = strstr_rs (line, "}"))) {
  235. parse = MAIN_HEAD;
  236. } else {
  237. goto parse_error;
  238. }
  239. break;
  240. case MAIN_AMF:
  241. if ((loc = strstr_rs (line, "mode:"))) {
  242. if (strcmp (loc, "enabled") == 0) {
  243. openais_config->amf_enabled = 1;
  244. } else
  245. if (strcmp (loc, "disabled") == 0) {
  246. openais_config->amf_enabled = 0;
  247. } else {
  248. goto parse_error;
  249. }
  250. } else
  251. if ((loc = strstr_rs (line, "}"))) {
  252. parse = MAIN_HEAD;
  253. } else {
  254. goto parse_error;
  255. }
  256. break;
  257. case MAIN_COMPONENTS:
  258. if ((loc = strstr_rs (line, "}"))) {
  259. parse = MAIN_HEAD;
  260. }
  261. else {
  262. int version;
  263. char *name = get_component(line, &version);
  264. if (name) {
  265. openais_config->dynamic_services[openais_config->num_dynamic_services].name = name;
  266. openais_config->dynamic_services[openais_config->num_dynamic_services].ver = version;
  267. openais_config->num_dynamic_services++;
  268. }
  269. }
  270. break;
  271. default:
  272. assert (0 == 1); /* SHOULDN'T HAPPEN */
  273. break;
  274. }
  275. }
  276. if ((openais_config->logmode & LOG_MODE_FILE) && openais_config->logfile == 0) {
  277. error_reason = "logmode set to 'file' but no logfile specified";
  278. goto parse_error;
  279. }
  280. /* Load default services if the config file doesn't specify */
  281. if (!openais_config->num_dynamic_services) {
  282. set_default_services(openais_config);
  283. }
  284. if (parse == MAIN_HEAD) {
  285. fclose (fp);
  286. return (0);
  287. } else {
  288. error_reason = "Missing closing brace";
  289. goto parse_error;
  290. }
  291. parse_error:
  292. if (parse_done) {
  293. sprintf (error_string_response,
  294. "parse error in /etc/ais/openais.conf: %s.\n", error_reason);
  295. } else {
  296. sprintf (error_string_response,
  297. "parse error in /etc/ais/openais.conf: %s (line %d).\n",
  298. error_reason, line_number);
  299. }
  300. *error_string = error_string_response;
  301. fclose (fp);
  302. return (-1);
  303. }