coroparse.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #include "util.h"
  52. static int read_config_file_into_objdb(
  53. struct objdb_iface_ver0 *objdb,
  54. char **error_string);
  55. static char error_string_response[512];
  56. static int aisparser_readconfig (struct objdb_iface_ver0 *objdb, char **error_string)
  57. {
  58. if (read_config_file_into_objdb(objdb, error_string)) {
  59. return -1;
  60. }
  61. return 0;
  62. }
  63. static char *remove_whitespace(char *string)
  64. {
  65. char *start = string+strspn(string, " \t");
  66. char *end = start+(strlen(start))-1;
  67. while ((*end == ' ' || *end == '\t' || *end == ':' || *end == '{') && end > start)
  68. end--;
  69. if (end != start)
  70. *(end+1) = '\0';
  71. return start;
  72. }
  73. static int parse_section(FILE *fp,
  74. struct objdb_iface_ver0 *objdb,
  75. unsigned int parent_handle,
  76. char **error_string)
  77. {
  78. char line[512];
  79. int i;
  80. char *loc;
  81. while (fgets (line, 255, fp)) {
  82. line[strlen(line) - 1] = '\0';
  83. /*
  84. * Clear out white space and tabs
  85. */
  86. for (i = strlen (line) - 1; i > -1; i--) {
  87. if (line[i] == '\t' || line[i] == ' ') {
  88. line[i] = '\0';
  89. } else {
  90. break;
  91. }
  92. }
  93. /*
  94. * Clear out comments and empty lines
  95. */
  96. if (line[0] == '#' || line[0] == '\0') {
  97. continue;
  98. }
  99. /* New section ? */
  100. if ((loc = strstr_rs (line, "{"))) {
  101. unsigned int new_parent;
  102. char *section = remove_whitespace(line);
  103. loc--;
  104. *loc = '\0';
  105. objdb->object_create (parent_handle, &new_parent,
  106. section, strlen (section));
  107. if (parse_section(fp, objdb, new_parent, error_string))
  108. return -1;
  109. }
  110. /* New key/value */
  111. if ((loc = strstr_rs (line, ":"))) {
  112. char *key;
  113. char *value;
  114. *(loc-1) = '\0';
  115. key = remove_whitespace(line);
  116. value = remove_whitespace(loc);
  117. objdb->object_key_create (parent_handle, key,
  118. strlen (key),
  119. value, strlen (value) + 1);
  120. }
  121. if ((loc = strstr_rs (line, "}"))) {
  122. return 0;
  123. }
  124. }
  125. if (parent_handle != OBJECT_PARENT_HANDLE) {
  126. *error_string = "Missing closing brace";
  127. return -1;
  128. }
  129. return 0;
  130. }
  131. /* Read config file and load into objdb */
  132. static int read_config_file_into_objdb(
  133. struct objdb_iface_ver0 *objdb,
  134. char **error_string)
  135. {
  136. FILE *fp;
  137. char *filename;
  138. char *error_reason = error_string_response;
  139. int res;
  140. filename = getenv ("COROSYNC_MAIN_CONFIG_FILE");
  141. if (!filename)
  142. filename = "/etc/corosync.conf";
  143. fp = fopen (filename, "r");
  144. if (fp == 0) {
  145. sprintf (error_reason, "Can't read file %s reason = (%s)\n",
  146. filename, strerror (errno));
  147. *error_string = error_reason;
  148. return -1;
  149. }
  150. res = parse_section(fp, objdb, OBJECT_PARENT_HANDLE, error_string);
  151. fclose(fp);
  152. sprintf (error_reason, "Successfully read main configuration file '%s'.\n", filename);
  153. *error_string = error_reason;
  154. return res;
  155. }
  156. /*
  157. * Dynamic Loader definition
  158. */
  159. struct config_iface_ver0 aisparser_iface_ver0 = {
  160. .config_readconfig = aisparser_readconfig
  161. };
  162. struct lcr_iface openais_aisparser_ver0[1] = {
  163. {
  164. .name = "corosync_parser",
  165. .version = 0,
  166. .versions_replace = 0,
  167. .versions_replace_count = 0,
  168. .dependencies = 0,
  169. .dependency_count = 0,
  170. .constructor = NULL,
  171. .destructor = NULL,
  172. .interfaces = NULL,
  173. }
  174. };
  175. struct openais_service_handler *aisparser_get_handler_ver0 (void);
  176. struct lcr_comp aisparser_comp_ver0 = {
  177. .iface_count = 1,
  178. .ifaces = openais_aisparser_ver0
  179. };
  180. __attribute__ ((constructor)) static void aisparser_comp_register (void) {
  181. lcr_interfaces_set (&openais_aisparser_ver0[0], &aisparser_iface_ver0);
  182. lcr_component_register (&aisparser_comp_ver0);
  183. }