coroparse.c 11 KB

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