coroparse.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Copyright (c) 2006, 2009 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 <config.h>
  35. #include <sys/types.h>
  36. #include <sys/uio.h>
  37. #include <sys/socket.h>
  38. #include <sys/stat.h>
  39. #include <sys/un.h>
  40. #include <netinet/in.h>
  41. #include <arpa/inet.h>
  42. #include <unistd.h>
  43. #include <fcntl.h>
  44. #include <stdlib.h>
  45. #include <stdio.h>
  46. #include <errno.h>
  47. #include <string.h>
  48. #include <dirent.h>
  49. #include <limits.h>
  50. #include <corosync/lcr/lcr_comp.h>
  51. #include <corosync/engine/objdb.h>
  52. #include <corosync/engine/config.h>
  53. #include "util.h"
  54. static int read_config_file_into_objdb(
  55. struct objdb_iface_ver0 *objdb,
  56. const char **error_string);
  57. static char error_string_response[512];
  58. static int aisparser_readconfig (struct objdb_iface_ver0 *objdb,
  59. const char **error_string)
  60. {
  61. if (read_config_file_into_objdb(objdb, error_string)) {
  62. return -1;
  63. }
  64. return 0;
  65. }
  66. static char *remove_whitespace(char *string)
  67. {
  68. char *start = string+strspn(string, " \t");
  69. char *end = start+(strlen(start))-1;
  70. while ((*end == ' ' || *end == '\t' || *end == ':' || *end == '{') && end > start)
  71. end--;
  72. if (end != start)
  73. *(end+1) = '\0';
  74. return start;
  75. }
  76. #define PCHECK_ADD_SUBSECTION 1
  77. #define PCHECK_ADD_ITEM 2
  78. typedef int (*parser_check_item_f)(struct objdb_iface_ver0 *objdb,
  79. hdb_handle_t parent_handle,
  80. int type,
  81. const char *name,
  82. const char **error_string);
  83. static int parse_section(FILE *fp,
  84. struct objdb_iface_ver0 *objdb,
  85. hdb_handle_t parent_handle,
  86. const char **error_string,
  87. parser_check_item_f parser_check_item_call)
  88. {
  89. char line[512];
  90. int i;
  91. char *loc;
  92. while (fgets (line, sizeof (line), fp)) {
  93. if (strlen(line) > 0) {
  94. if (line[strlen(line) - 1] == '\n')
  95. line[strlen(line) - 1] = '\0';
  96. if (strlen (line) > 0 && line[strlen(line) - 1] == '\r')
  97. line[strlen(line) - 1] = '\0';
  98. }
  99. /*
  100. * Clear out white space and tabs
  101. */
  102. for (i = strlen (line) - 1; i > -1; i--) {
  103. if (line[i] == '\t' || line[i] == ' ') {
  104. line[i] = '\0';
  105. } else {
  106. break;
  107. }
  108. }
  109. /*
  110. * Clear out comments and empty lines
  111. */
  112. if (line[0] == '#' || line[0] == '\0') {
  113. continue;
  114. }
  115. /* New section ? */
  116. if ((loc = strchr_rs (line, '{'))) {
  117. hdb_handle_t new_parent;
  118. char *section = remove_whitespace(line);
  119. loc--;
  120. *loc = '\0';
  121. if (parser_check_item_call) {
  122. if (!parser_check_item_call(objdb, parent_handle, PCHECK_ADD_SUBSECTION,
  123. section, error_string))
  124. return -1;
  125. }
  126. objdb->object_create (parent_handle, &new_parent,
  127. section, strlen (section));
  128. if (parse_section(fp, objdb, new_parent, error_string, parser_check_item_call))
  129. return -1;
  130. }
  131. /* New key/value */
  132. if ((loc = strchr_rs (line, ':'))) {
  133. char *key;
  134. char *value;
  135. *(loc-1) = '\0';
  136. key = remove_whitespace(line);
  137. value = remove_whitespace(loc);
  138. if (parser_check_item_call) {
  139. if (!parser_check_item_call(objdb, parent_handle, PCHECK_ADD_ITEM,
  140. key, error_string))
  141. return -1;
  142. }
  143. objdb->object_key_create (parent_handle, key,
  144. strlen (key),
  145. value, strlen (value) + 1);
  146. }
  147. if ((loc = strchr_rs (line, '}'))) {
  148. return 0;
  149. }
  150. }
  151. if (parent_handle != OBJECT_PARENT_HANDLE) {
  152. *error_string = "Missing closing brace";
  153. return -1;
  154. }
  155. return 0;
  156. }
  157. static int parser_check_item_uidgid(struct objdb_iface_ver0 *objdb,
  158. hdb_handle_t parent_handle,
  159. int type,
  160. const char *name,
  161. const char **error_string)
  162. {
  163. if (type == PCHECK_ADD_SUBSECTION) {
  164. if (parent_handle != OBJECT_PARENT_HANDLE) {
  165. *error_string = "uidgid: Can't add second level subsection";
  166. return 0;
  167. }
  168. if (strcmp (name, "uidgid") != 0) {
  169. *error_string = "uidgid: Can't add subsection different then uidgid";
  170. return 0;
  171. }
  172. }
  173. if (type == PCHECK_ADD_ITEM) {
  174. if (!(strcmp (name, "uid") == 0 || strcmp (name, "gid") == 0)) {
  175. *error_string = "uidgid: Only uid and gid are allowed items";
  176. return 0;
  177. }
  178. }
  179. return 1;
  180. }
  181. static int parser_check_item_service(struct objdb_iface_ver0 *objdb,
  182. hdb_handle_t parent_handle,
  183. int type,
  184. const char *name,
  185. const char **error_string)
  186. {
  187. if (type == PCHECK_ADD_SUBSECTION) {
  188. if (parent_handle != OBJECT_PARENT_HANDLE) {
  189. *error_string = "service: Can't add second level subsection";
  190. return 0;
  191. }
  192. if (strcmp (name, "service") != 0) {
  193. *error_string = "service: Can't add subsection different then service";
  194. return 0;
  195. }
  196. }
  197. if (type == PCHECK_ADD_ITEM) {
  198. if (!(strcmp (name, "name") == 0 || strcmp (name, "ver") == 0)) {
  199. *error_string = "service: Only name and ver are allowed items";
  200. return 0;
  201. }
  202. }
  203. return 1;
  204. }
  205. static int read_uidgid_files_into_objdb(
  206. struct objdb_iface_ver0 *objdb,
  207. const char **error_string)
  208. {
  209. FILE *fp;
  210. const char *dirname;
  211. DIR *dp;
  212. struct dirent *dirent;
  213. char filename[PATH_MAX + FILENAME_MAX + 1];
  214. int res = 0;
  215. struct stat stat_buf;
  216. dirname = COROSYSCONFDIR "/uidgid.d";
  217. dp = opendir (dirname);
  218. if (dp == NULL)
  219. return 0;
  220. while ((dirent = readdir (dp))) {
  221. snprintf(filename, sizeof (filename), "%s/%s", dirname, dirent->d_name);
  222. stat (filename, &stat_buf);
  223. if (S_ISREG(stat_buf.st_mode)) {
  224. fp = fopen (filename, "r");
  225. if (fp == NULL) continue;
  226. res = parse_section(fp, objdb, OBJECT_PARENT_HANDLE, error_string, parser_check_item_uidgid);
  227. fclose (fp);
  228. if (res != 0) {
  229. goto error_exit;
  230. }
  231. }
  232. }
  233. error_exit:
  234. closedir(dp);
  235. return res;
  236. }
  237. static int read_service_files_into_objdb(
  238. struct objdb_iface_ver0 *objdb,
  239. const char **error_string)
  240. {
  241. FILE *fp;
  242. const char *dirname;
  243. DIR *dp;
  244. struct dirent *dirent;
  245. char filename[PATH_MAX + FILENAME_MAX + 1];
  246. int res = 0;
  247. struct stat stat_buf;
  248. dirname = COROSYSCONFDIR "/service.d";
  249. dp = opendir (dirname);
  250. if (dp == NULL)
  251. return 0;
  252. while ((dirent = readdir (dp))) {
  253. snprintf(filename, sizeof (filename), "%s/%s", dirname, dirent->d_name);
  254. stat (filename, &stat_buf);
  255. if (S_ISREG(stat_buf.st_mode)) {
  256. fp = fopen (filename, "r");
  257. if (fp == NULL) continue;
  258. res = parse_section(fp, objdb, OBJECT_PARENT_HANDLE, error_string, parser_check_item_service);
  259. fclose (fp);
  260. if (res != 0) {
  261. goto error_exit;
  262. }
  263. }
  264. }
  265. error_exit:
  266. closedir(dp);
  267. return res;
  268. }
  269. /* Read config file and load into objdb */
  270. static int read_config_file_into_objdb(
  271. struct objdb_iface_ver0 *objdb,
  272. const char **error_string)
  273. {
  274. FILE *fp;
  275. const char *filename;
  276. char *error_reason = error_string_response;
  277. int res;
  278. filename = getenv ("COROSYNC_MAIN_CONFIG_FILE");
  279. if (!filename)
  280. filename = COROSYSCONFDIR "/corosync.conf";
  281. fp = fopen (filename, "r");
  282. if (fp == NULL) {
  283. snprintf (error_reason, sizeof(error_string_response),
  284. "Can't read file %s reason = (%s)\n",
  285. filename, strerror (errno));
  286. *error_string = error_reason;
  287. return -1;
  288. }
  289. res = parse_section(fp, objdb, OBJECT_PARENT_HANDLE, error_string, NULL);
  290. fclose(fp);
  291. if (res == 0) {
  292. res = read_uidgid_files_into_objdb(objdb, error_string);
  293. }
  294. if (res == 0) {
  295. res = read_service_files_into_objdb(objdb, error_string);
  296. }
  297. if (res == 0) {
  298. snprintf (error_reason, sizeof(error_string_response),
  299. "Successfully read main configuration file '%s'.\n", filename);
  300. *error_string = error_reason;
  301. }
  302. return res;
  303. }
  304. /*
  305. * Dynamic Loader definition
  306. */
  307. struct config_iface_ver0 aisparser_iface_ver0 = {
  308. .config_readconfig = aisparser_readconfig
  309. };
  310. struct lcr_iface corosync_aisparser_ver0[1] = {
  311. {
  312. .name = "corosync_parser",
  313. .version = 0,
  314. .versions_replace = 0,
  315. .versions_replace_count = 0,
  316. .dependencies = 0,
  317. .dependency_count = 0,
  318. .constructor = NULL,
  319. .destructor = NULL,
  320. .interfaces = NULL,
  321. }
  322. };
  323. struct corosync_service_handler *aisparser_get_handler_ver0 (void);
  324. struct lcr_comp aisparser_comp_ver0 = {
  325. .iface_count = 1,
  326. .ifaces = corosync_aisparser_ver0
  327. };
  328. #ifdef COROSYNC_SOLARIS
  329. void corosync_lcr_component_register (void);
  330. void corosync_lcr_component_register (void) {
  331. #else
  332. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  333. #endif
  334. lcr_interfaces_set (&corosync_aisparser_ver0[0], &aisparser_iface_ver0);
  335. lcr_component_register (&aisparser_comp_ver0);
  336. }