coroparse.c 9.9 KB

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