mainconfig.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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_parse_t;
  57. char *strstr_rs (const char *haystack, const char *needle)
  58. {
  59. char *end_address;
  60. char *new_needle;
  61. new_needle = (char *)mempool_strdup (needle);
  62. new_needle[strlen(new_needle) - 1] = '\0';
  63. end_address = strstr (haystack, new_needle);
  64. if (end_address) {
  65. end_address += strlen (new_needle);
  66. end_address = strstr (end_address, needle + strlen (new_needle));
  67. }
  68. if (end_address) {
  69. end_address += 1; /* skip past { or = */
  70. do {
  71. if (*end_address == '\t' || *end_address == ' ') {
  72. end_address++;
  73. } else {
  74. break;
  75. }
  76. } while (*end_address != '\0');
  77. }
  78. mempool_free (new_needle);
  79. return (end_address);
  80. }
  81. extern int openais_main_config_read (char **error_string,
  82. struct openais_config *openais_config,
  83. int interface_max)
  84. {
  85. FILE *fp;
  86. int line_number = 0;
  87. main_parse_t parse = MAIN_HEAD;
  88. int logging_parsed = 0;
  89. int event_parsed = 0;
  90. int amf_parsed = 0;
  91. char *loc;
  92. int i;
  93. int parse_done = 0;
  94. char line[512];
  95. char *error_reason = error_string_response;
  96. memset (openais_config, 0, sizeof (struct openais_config));
  97. fp = fopen ("/etc/ais/openais.conf", "r");
  98. if (fp == 0) {
  99. parse_done = 1;
  100. sprintf (error_reason, "Can't read file reason = (%s)\n", strerror (errno));
  101. *error_string = error_reason;
  102. return -1;
  103. }
  104. while (fgets (line, 255, fp)) {
  105. line_number += 1;
  106. line[strlen(line) - 1] = '\0';
  107. /*
  108. * Clear out white space and tabs
  109. */
  110. for (i = strlen (line) - 1; i > -1; i--) {
  111. if (line[i] == '\t' || line[i] == ' ') {
  112. line[i] = '\0';
  113. } else {
  114. break;
  115. }
  116. }
  117. /*
  118. * Clear out comments and empty lines
  119. */
  120. if (line[0] == '#' || line[0] == '\0') {
  121. continue;
  122. }
  123. line_number += 1;
  124. switch (parse) {
  125. case MAIN_HEAD:
  126. if (logging_parsed == 0 && strstr_rs (line, "logging{")) {
  127. logging_parsed = 1;
  128. parse = MAIN_LOGGING;
  129. } else
  130. if (event_parsed == 0 && strstr_rs (line, "event{")) {
  131. event_parsed = 1;
  132. parse = MAIN_EVENT;
  133. } else
  134. if (amf_parsed == 0 && strstr_rs (line, "amf{")) {
  135. amf_parsed = 1;
  136. parse = MAIN_AMF;
  137. } else {
  138. continue;
  139. }
  140. break;
  141. case MAIN_LOGGING:
  142. if ((loc = strstr_rs (line, "logoutput:"))) {
  143. if (strcmp (loc, "file") == 0) {
  144. openais_config->logmode |= LOG_MODE_FILE;
  145. } else
  146. if (strcmp (loc, "syslog") == 0) {
  147. openais_config->logmode |= LOG_MODE_SYSLOG;
  148. } else
  149. if (strcmp (loc, "stderr") == 0) {
  150. openais_config->logmode |= LOG_MODE_STDERR;
  151. } else {
  152. goto parse_error;
  153. }
  154. } else
  155. if ((loc = strstr_rs (line, "debug:"))) {
  156. if (strcmp (loc, "on") == 0) {
  157. openais_config->logmode |= LOG_MODE_DEBUG;
  158. } else
  159. if (strcmp (loc, "off") == 0) {
  160. openais_config->logmode &= ~LOG_MODE_DEBUG;
  161. } else {
  162. goto parse_error;
  163. }
  164. } else
  165. if ((loc = strstr_rs (line, "timestamp:"))) {
  166. if (strcmp (loc, "on") == 0) {
  167. openais_config->logmode |= LOG_MODE_TIMESTAMP;
  168. } else
  169. if (strcmp (loc, "off") == 0) {
  170. openais_config->logmode &= ~LOG_MODE_TIMESTAMP;
  171. } else {
  172. goto parse_error;
  173. }
  174. } else
  175. if ((loc = strstr_rs (line, "logfile:"))) {
  176. openais_config->logfile = strdup (loc);
  177. } else
  178. if ((loc = strstr_rs (line, "}"))) {
  179. parse = MAIN_HEAD;
  180. } else {
  181. goto parse_error;
  182. }
  183. break;
  184. case MAIN_EVENT:
  185. if ((loc = strstr_rs (line, "delivery_queue_size:"))) {
  186. openais_config->evt_delivery_queue_size = atoi(loc);
  187. } else if ((loc = strstr_rs (line, "delivery_queue_resume:"))) {
  188. openais_config->evt_delivery_queue_resume = atoi(loc);
  189. } else if ((loc = strstr_rs (line, "}"))) {
  190. parse = MAIN_HEAD;
  191. } else {
  192. goto parse_error;
  193. }
  194. break;
  195. case MAIN_AMF:
  196. if ((loc = strstr_rs (line, "mode:"))) {
  197. if (strcmp (loc, "enabled") == 0) {
  198. openais_config->amf_enabled = 1;
  199. } else
  200. if (strcmp (loc, "disabled") == 0) {
  201. openais_config->amf_enabled = 0;
  202. } else {
  203. goto parse_error;
  204. }
  205. } else
  206. if ((loc = strstr_rs (line, "}"))) {
  207. parse = MAIN_HEAD;
  208. } else {
  209. goto parse_error;
  210. }
  211. break;
  212. default:
  213. assert (0 == 1); /* SHOULDN'T HAPPEN */
  214. break;
  215. }
  216. }
  217. if ((openais_config->logmode & LOG_MODE_FILE) && openais_config->logfile == 0) {
  218. error_reason = "logmode set to 'file' but no logfile specified";
  219. goto parse_error;
  220. }
  221. if (parse == MAIN_HEAD) {
  222. fclose (fp);
  223. return (0);
  224. } else {
  225. error_reason = "Missing closing brace";
  226. goto parse_error;
  227. }
  228. parse_error:
  229. if (parse_done) {
  230. sprintf (error_string_response,
  231. "parse error in /etc/ais/openais.conf: %s.\n", error_reason);
  232. } else {
  233. sprintf (error_string_response,
  234. "parse error in /etc/ais/openais.conf: %s (line %d).\n",
  235. error_reason, line_number);
  236. }
  237. *error_string = error_string_response;
  238. fclose (fp);
  239. return (-1);
  240. }