openaisparser.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (c) 2006 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Patrick Caulfield (pcaulfie@redhat.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 <sys/types.h>
  35. #include <sys/uio.h>
  36. #include <sys/socket.h>
  37. #include <sys/un.h>
  38. #include <netinet/in.h>
  39. #include <arpa/inet.h>
  40. #include <unistd.h>
  41. #include <fcntl.h>
  42. #include <stdlib.h>
  43. #include <stdio.h>
  44. #include <errno.h>
  45. #include <signal.h>
  46. #include <string.h>
  47. #include <corosync/engine/config.h>
  48. #include <corosync/lcr/lcr_comp.h>
  49. #include <corosync/engine/objdb.h>
  50. static int read_config_file_into_objdb(
  51. struct objdb_iface_ver0 *objdb,
  52. char **error_string);
  53. static char error_string_response[512];
  54. static char *strstr_rs (const char *haystack, const char *needle)
  55. {
  56. char *end_address;
  57. char *new_needle;
  58. new_needle = (char *)strdup (needle);
  59. new_needle[strlen (new_needle) - 1] = '\0';
  60. end_address = strstr (haystack, new_needle);
  61. if (end_address) {
  62. end_address += strlen (new_needle);
  63. end_address = strstr (end_address, needle + strlen (new_needle));
  64. }
  65. if (end_address) {
  66. end_address += 1; /* skip past { or = */
  67. do {
  68. if (*end_address == '\t' || *end_address == ' ') {
  69. end_address++;
  70. } else {
  71. break;
  72. }
  73. } while (*end_address != '\0');
  74. }
  75. free (new_needle);
  76. return (end_address);
  77. }
  78. static int aisparser_readconfig (struct objdb_iface_ver0 *objdb, char **error_string)
  79. {
  80. if (read_config_file_into_objdb(objdb, error_string)) {
  81. return -1;
  82. }
  83. return 0;
  84. }
  85. static char *remove_whitespace(char *string)
  86. {
  87. char *start = string+strspn(string, " \t");
  88. char *end = start+(strlen(start))-1;
  89. while ((*end == ' ' || *end == '\t' || *end == ':' || *end == '{') && end > start)
  90. end--;
  91. if (end != start)
  92. *(end+1) = '\0';
  93. return start;
  94. }
  95. static int parse_section(FILE *fp,
  96. struct objdb_iface_ver0 *objdb,
  97. unsigned int parent_handle,
  98. char **error_string)
  99. {
  100. char line[512];
  101. int i;
  102. char *loc;
  103. while (fgets (line, 255, fp)) {
  104. line[strlen(line) - 1] = '\0';
  105. /*
  106. * Clear out white space and tabs
  107. */
  108. for (i = strlen (line) - 1; i > -1; i--) {
  109. if (line[i] == '\t' || line[i] == ' ') {
  110. line[i] = '\0';
  111. } else {
  112. break;
  113. }
  114. }
  115. /*
  116. * Clear out comments and empty lines
  117. */
  118. if (line[0] == '#' || line[0] == '\0') {
  119. continue;
  120. }
  121. /* New section ? */
  122. if ((loc = strstr_rs (line, "{"))) {
  123. unsigned int new_parent;
  124. char *section = remove_whitespace(line);
  125. loc--;
  126. *loc = '\0';
  127. objdb->object_create (parent_handle, &new_parent,
  128. section, strlen (section));
  129. if (parse_section(fp, objdb, new_parent, error_string))
  130. return -1;
  131. }
  132. /* New key/value */
  133. if ((loc = strstr_rs (line, ":"))) {
  134. char *key;
  135. char *value;
  136. *(loc-1) = '\0';
  137. key = remove_whitespace(line);
  138. value = remove_whitespace(loc);
  139. objdb->object_key_create (parent_handle, key,
  140. strlen (key),
  141. value, strlen (value) + 1);
  142. }
  143. if ((loc = strstr_rs (line, "}"))) {
  144. return 0;
  145. }
  146. }
  147. if (parent_handle != OBJECT_PARENT_HANDLE) {
  148. *error_string = "Missing closing brace";
  149. return -1;
  150. }
  151. return 0;
  152. }
  153. /* Read config file and load into objdb */
  154. static int read_config_file_into_objdb(
  155. struct objdb_iface_ver0 *objdb,
  156. char **error_string)
  157. {
  158. FILE *fp;
  159. char *filename = "/etc/ais/openais.conf";
  160. char *error_reason = error_string_response;
  161. int res;
  162. unsigned int object_handle;
  163. fp = fopen (filename, "r");
  164. if (fp == 0) {
  165. sprintf (error_reason, "Can't read file %s reason = (%s)\n",
  166. filename, strerror (errno));
  167. *error_string = error_reason;
  168. return -1;
  169. }
  170. res = parse_section(fp, objdb, OBJECT_PARENT_HANDLE, error_string);
  171. fclose(fp);
  172. /*
  173. * Load clm module
  174. */
  175. objdb->object_create(OBJECT_PARENT_HANDLE, &object_handle,
  176. "service", strlen("service"));
  177. objdb->object_key_create(object_handle, "name", strlen("name"),
  178. "openais_clm", strlen("openais_clm") + 1);
  179. objdb->object_key_create(object_handle, "ver", strlen("ver"),
  180. "0", 2);
  181. /*
  182. * Load ckpt module
  183. */
  184. objdb->object_create(OBJECT_PARENT_HANDLE, &object_handle,
  185. "service", strlen("service"));
  186. objdb->object_key_create(object_handle, "name", strlen("name"),
  187. "openais_ckpt", strlen("openais_ckpt") + 1);
  188. objdb->object_key_create(object_handle, "ver", strlen("ver"),
  189. "0", 2);
  190. /*
  191. * Load evt module
  192. */
  193. objdb->object_create(OBJECT_PARENT_HANDLE, &object_handle,
  194. "service", strlen("service"));
  195. objdb->object_key_create(object_handle, "name", strlen("name"),
  196. "openais_evt", strlen("openais_evt") + 1);
  197. objdb->object_key_create(object_handle, "ver", strlen("ver"),
  198. "0", 2);
  199. /*
  200. * Load msg module
  201. */
  202. objdb->object_create(OBJECT_PARENT_HANDLE, &object_handle,
  203. "service", strlen("service"));
  204. objdb->object_key_create(object_handle, "name", strlen("name"),
  205. "openais_msg", strlen("openais_msg") + 1);
  206. objdb->object_key_create(object_handle, "ver", strlen("ver"),
  207. "0", 2);
  208. /*
  209. * Load lck module
  210. */
  211. objdb->object_create(OBJECT_PARENT_HANDLE, &object_handle,
  212. "service", strlen("service"));
  213. objdb->object_key_create(object_handle, "name", strlen("name"),
  214. "openais_lck", strlen("openais_lck") + 1);
  215. objdb->object_key_create(object_handle, "ver", strlen("ver"),
  216. "0", 2);
  217. sprintf (error_reason, "Successfully read main configuration file '%s'.\n", filename);
  218. *error_string = error_reason;
  219. return res;
  220. }
  221. /*
  222. * Dynamic Loader definition
  223. */
  224. struct config_iface_ver0 aisparser_iface_ver0 = {
  225. .config_readconfig = aisparser_readconfig,
  226. .config_writeconfig = NULL
  227. };
  228. struct lcr_iface openais_aisparser_ver0[1] = {
  229. {
  230. .name = "openaisparser",
  231. .version = 0,
  232. .versions_replace = 0,
  233. .versions_replace_count = 0,
  234. .dependencies = 0,
  235. .dependency_count = 0,
  236. .constructor = NULL,
  237. .destructor = NULL,
  238. .interfaces = NULL,
  239. }
  240. };
  241. struct openais_service_handler *aisparser_get_handler_ver0 (void);
  242. struct lcr_comp aisparser_comp_ver0 = {
  243. .iface_count = 1,
  244. .ifaces = openais_aisparser_ver0
  245. };
  246. __attribute__ ((constructor)) static void aisparser_comp_register (void) {
  247. lcr_interfaces_set (&openais_aisparser_ver0[0], &aisparser_iface_ver0);
  248. lcr_component_register (&aisparser_comp_ver0);
  249. }