aisparser.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "../lcr/lcr_comp.h"
  48. #include "objdb.h"
  49. #include "config.h"
  50. #include "mempool.h"
  51. static int read_config_file_into_objdb(
  52. struct objdb_iface_ver0 *objdb,
  53. char **error_string);
  54. static char error_string_response[512];
  55. static int aisparser_readconfig (struct objdb_iface_ver0 *objdb, char **error_string)
  56. {
  57. if (read_config_file_into_objdb(objdb, error_string)) {
  58. return -1;
  59. }
  60. return 0;
  61. }
  62. char *remove_whitespace(char *string)
  63. {
  64. char *start = string+strspn(string, " \t");
  65. char *end = start+(strlen(start))-1;
  66. while ((*end == ' ' || *end == '\t' || *end == ':' || *end == '{') && end > start)
  67. end--;
  68. if (end != start)
  69. *(end+1) = '\0';
  70. return start;
  71. }
  72. char *strstr_rs (const char *haystack, const char *needle)
  73. {
  74. char *end_address;
  75. char *new_needle;
  76. new_needle = (char *)mempool_strdup (needle);
  77. new_needle[strlen(new_needle) - 1] = '\0';
  78. end_address = strstr (haystack, new_needle);
  79. if (end_address) {
  80. end_address += strlen (new_needle);
  81. end_address = strstr (end_address, needle + strlen (new_needle));
  82. }
  83. if (end_address) {
  84. end_address += 1; /* skip past { or = */
  85. do {
  86. if (*end_address == '\t' || *end_address == ' ') {
  87. end_address++;
  88. } else {
  89. break;
  90. }
  91. } while (*end_address != '\0');
  92. }
  93. mempool_free (new_needle);
  94. return (end_address);
  95. }
  96. static int parse_section(FILE *fp,
  97. struct objdb_iface_ver0 *objdb,
  98. unsigned int parent_key,
  99. char **error_string)
  100. {
  101. char line[512];
  102. int i;
  103. char *loc;
  104. while (fgets (line, 255, fp)) {
  105. line[strlen(line) - 1] = '\0';
  106. /*
  107. * Clear out white space and tabs
  108. */
  109. for (i = strlen (line) - 1; i > -1; i--) {
  110. if (line[i] == '\t' || line[i] == ' ') {
  111. line[i] = '\0';
  112. } else {
  113. break;
  114. }
  115. }
  116. /*
  117. * Clear out comments and empty lines
  118. */
  119. if (line[0] == '#' || line[0] == '\0') {
  120. continue;
  121. }
  122. /* New section ? */
  123. if ((loc = strstr_rs (line, "{"))) {
  124. unsigned int new_parent;
  125. char *section = remove_whitespace(line);
  126. loc--;
  127. *loc = '\0';
  128. objdb->object_create (OBJECT_PARENT_HANDLE, &new_parent,
  129. section, strlen (section));
  130. if (parse_section(fp, objdb, new_parent, error_string))
  131. return -1;
  132. }
  133. /* New key/value */
  134. if ((loc = strstr_rs (line, ":"))) {
  135. char *key;
  136. char *value;
  137. *(loc-1) = '\0';
  138. key = remove_whitespace(line);
  139. value = remove_whitespace(loc);
  140. objdb->object_key_create (parent_key, key, strlen (key),
  141. value, strlen (value) + 1);
  142. }
  143. if ((loc = strstr_rs (line, "}"))) {
  144. return 0;
  145. }
  146. }
  147. if (parent_key != 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 *error_reason = error_string_response;
  160. int res;
  161. fp = fopen (OPENAIS_CONFDIR "/openais.conf", "r");
  162. if (fp == 0) {
  163. sprintf (error_reason, "Can't read file %s/openais.conf reason = (%s)\n",
  164. OPENAIS_CONFDIR, strerror (errno));
  165. *error_string = error_reason;
  166. return -1;
  167. }
  168. res = parse_section(fp, objdb, OBJECT_PARENT_HANDLE, error_string);
  169. fclose(fp);
  170. return res;
  171. }
  172. /*
  173. * Dynamic Loader definition
  174. */
  175. struct config_iface_ver0 aisparser_iface_ver0 = {
  176. .config_readconfig = aisparser_readconfig
  177. };
  178. struct lcr_iface openais_aisparser_ver0[1] = {
  179. {
  180. .name = "aisparser",
  181. .version = 0,
  182. .versions_replace = 0,
  183. .versions_replace_count = 0,
  184. .dependencies = 0,
  185. .dependency_count = 0,
  186. .constructor = NULL,
  187. .destructor = NULL,
  188. .interfaces = (void **)(void *)&aisparser_iface_ver0,
  189. }
  190. };
  191. struct openais_service_handler *aisparser_get_handler_ver0 (void);
  192. struct lcr_comp aisparser_comp_ver0 = {
  193. .iface_count = 1,
  194. .ifaces = openais_aisparser_ver0
  195. };
  196. __attribute__ ((constructor)) static void aisparser_comp_register (void) {
  197. lcr_component_register (&aisparser_comp_ver0);
  198. }