mainconfig.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. switch (parse) {
  149. case MAIN_HEAD:
  150. if (logging_parsed == 0 && strstr_rs (line, "logging{")) {
  151. logging_parsed = 1;
  152. parse = MAIN_LOGGING;
  153. } else
  154. if (event_parsed == 0 && strstr_rs (line, "event{")) {
  155. event_parsed = 1;
  156. parse = MAIN_EVENT;
  157. } else
  158. if (amf_parsed == 0 && strstr_rs (line, "amf{")) {
  159. amf_parsed = 1;
  160. parse = MAIN_AMF;
  161. } else
  162. if (components_parsed == 0 && strstr_rs (line, "components{")) {
  163. components_parsed = 1;
  164. parse = MAIN_COMPONENTS;
  165. } else {
  166. continue;
  167. }
  168. break;
  169. case MAIN_LOGGING:
  170. if ((loc = strstr_rs (line, "logoutput:"))) {
  171. if (strcmp (loc, "file") == 0) {
  172. openais_config->logmode |= LOG_MODE_FILE;
  173. } else
  174. if (strcmp (loc, "syslog") == 0) {
  175. openais_config->logmode |= LOG_MODE_SYSLOG;
  176. } else
  177. if (strcmp (loc, "stderr") == 0) {
  178. openais_config->logmode |= LOG_MODE_STDERR;
  179. } else {
  180. goto parse_error;
  181. }
  182. } else
  183. if ((loc = strstr_rs (line, "debug:"))) {
  184. if (strcmp (loc, "on") == 0) {
  185. openais_config->logmode |= LOG_MODE_DEBUG;
  186. } else
  187. if (strcmp (loc, "off") == 0) {
  188. openais_config->logmode &= ~LOG_MODE_DEBUG;
  189. } else {
  190. goto parse_error;
  191. }
  192. } else
  193. if ((loc = strstr_rs (line, "timestamp:"))) {
  194. if (strcmp (loc, "on") == 0) {
  195. openais_config->logmode |= LOG_MODE_TIMESTAMP;
  196. } else
  197. if (strcmp (loc, "off") == 0) {
  198. openais_config->logmode &= ~LOG_MODE_TIMESTAMP;
  199. } else {
  200. goto parse_error;
  201. }
  202. } else
  203. if ((loc = strstr_rs (line, "logfile:"))) {
  204. openais_config->logfile = strdup (loc);
  205. } else
  206. if ((loc = strstr_rs (line, "}"))) {
  207. parse = MAIN_HEAD;
  208. } else {
  209. goto parse_error;
  210. }
  211. break;
  212. case MAIN_EVENT:
  213. if ((loc = strstr_rs (line, "delivery_queue_size:"))) {
  214. openais_config->evt_delivery_queue_size = atoi(loc);
  215. } else if ((loc = strstr_rs (line, "delivery_queue_resume:"))) {
  216. openais_config->evt_delivery_queue_resume = atoi(loc);
  217. } else if ((loc = strstr_rs (line, "}"))) {
  218. parse = MAIN_HEAD;
  219. } else {
  220. goto parse_error;
  221. }
  222. break;
  223. case MAIN_AMF:
  224. if ((loc = strstr_rs (line, "mode:"))) {
  225. if (strcmp (loc, "enabled") == 0) {
  226. openais_config->amf_enabled = 1;
  227. } else
  228. if (strcmp (loc, "disabled") == 0) {
  229. openais_config->amf_enabled = 0;
  230. } else {
  231. goto parse_error;
  232. }
  233. } else
  234. if ((loc = strstr_rs (line, "}"))) {
  235. parse = MAIN_HEAD;
  236. } else {
  237. goto parse_error;
  238. }
  239. break;
  240. case MAIN_COMPONENTS:
  241. if ((loc = strstr_rs (line, "}"))) {
  242. parse = MAIN_HEAD;
  243. }
  244. else {
  245. int version;
  246. char *name = get_component(line, &version);
  247. if (name) {
  248. openais_service_objdb_add (objdb, name, version);
  249. }
  250. }
  251. break;
  252. default:
  253. assert (0 == 1); /* SHOULDN'T HAPPEN */
  254. break;
  255. }
  256. }
  257. if ((openais_config->logmode & LOG_MODE_FILE) && openais_config->logfile == 0) {
  258. error_reason = "logmode set to 'file' but no logfile specified";
  259. goto parse_error;
  260. }
  261. if (parse == MAIN_HEAD) {
  262. fclose (fp);
  263. return (0);
  264. } else {
  265. error_reason = "Missing closing brace";
  266. goto parse_error;
  267. }
  268. parse_error:
  269. if (parse_done) {
  270. sprintf (error_string_response,
  271. "parse error in %s/openais.conf: %s.\n",
  272. OPENAIS_CONFDIR, error_reason);
  273. } else {
  274. sprintf (error_string_response,
  275. "parse error in %s/openais.conf: %s (line %d).\n",
  276. OPENAIS_CONFDIR, error_reason, line_number);
  277. }
  278. *error_string = error_string_response;
  279. fclose (fp);
  280. return (-1);
  281. }