coroparse.c 10 KB

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