coroparse.c 10 KB

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