coroparse.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  1. /*
  2. * Copyright (c) 2006-2022 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Patrick Caulfield (pcaulfie@redhat.com)
  7. * Jan Friesse (jfriesse@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <config.h>
  36. #include <sys/types.h>
  37. #include <sys/uio.h>
  38. #include <sys/socket.h>
  39. #include <sys/stat.h>
  40. #include <sys/un.h>
  41. #include <netinet/in.h>
  42. #include <arpa/inet.h>
  43. #include <unistd.h>
  44. #include <fcntl.h>
  45. #include <stdlib.h>
  46. #include <stdio.h>
  47. #include <errno.h>
  48. #include <string.h>
  49. #include <dirent.h>
  50. #include <libgen.h>
  51. #include <limits.h>
  52. #include <stddef.h>
  53. #include <grp.h>
  54. #include <pwd.h>
  55. #include <qb/qblist.h>
  56. #include <qb/qbutil.h>
  57. #define LOGSYS_UTILS_ONLY 1
  58. #include <corosync/logsys.h>
  59. #include <corosync/icmap.h>
  60. #include "main.h"
  61. #include "util.h"
  62. enum parser_cb_type {
  63. PARSER_CB_START,
  64. PARSER_CB_END,
  65. PARSER_CB_SECTION_START,
  66. PARSER_CB_SECTION_END,
  67. PARSER_CB_ITEM,
  68. };
  69. enum main_cp_cb_data_state {
  70. MAIN_CP_CB_DATA_STATE_NORMAL,
  71. MAIN_CP_CB_DATA_STATE_TOTEM,
  72. MAIN_CP_CB_DATA_STATE_INTERFACE,
  73. MAIN_CP_CB_DATA_STATE_LOGGER_SUBSYS,
  74. MAIN_CP_CB_DATA_STATE_UIDGID,
  75. MAIN_CP_CB_DATA_STATE_LOGGING_DAEMON,
  76. MAIN_CP_CB_DATA_STATE_MEMBER,
  77. MAIN_CP_CB_DATA_STATE_QUORUM,
  78. MAIN_CP_CB_DATA_STATE_QDEVICE,
  79. MAIN_CP_CB_DATA_STATE_NODELIST,
  80. MAIN_CP_CB_DATA_STATE_NODELIST_NODE,
  81. MAIN_CP_CB_DATA_STATE_PLOAD,
  82. MAIN_CP_CB_DATA_STATE_SYSTEM,
  83. MAIN_CP_CB_DATA_STATE_RESOURCES,
  84. MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM,
  85. MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS,
  86. MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM_MEMUSED,
  87. MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS_MEMUSED
  88. };
  89. typedef int (*parser_cb_f)(const char *path,
  90. char *key,
  91. char *value,
  92. enum main_cp_cb_data_state *state,
  93. enum parser_cb_type type,
  94. const char **error_string,
  95. icmap_map_t config_map,
  96. void *user_data);
  97. struct key_value_list_item {
  98. char *key;
  99. char *value;
  100. struct qb_list_head list;
  101. };
  102. struct main_cp_cb_data {
  103. int linknumber;
  104. char *bindnetaddr;
  105. char *mcastaddr;
  106. char *broadcast;
  107. int mcastport;
  108. int ttl;
  109. int knet_link_priority;
  110. int knet_ping_interval;
  111. int knet_ping_timeout;
  112. int knet_ping_precision;
  113. int knet_pong_count;
  114. int knet_pmtud_interval;
  115. unsigned int knet_mtu;
  116. char *knet_transport;
  117. struct qb_list_head logger_subsys_items_head;
  118. char *subsys;
  119. char *logging_daemon_name;
  120. struct qb_list_head member_items_head;
  121. int node_number;
  122. };
  123. static int read_config_file_into_icmap(
  124. const char **error_string, icmap_map_t config_map);
  125. static char error_string_response[512];
  126. static int uid_determine (const char *req_user)
  127. {
  128. int pw_uid = 0;
  129. struct passwd passwd;
  130. struct passwd* pwdptr = &passwd;
  131. struct passwd* temp_pwd_pt;
  132. char *pwdbuffer;
  133. int pwdlinelen, rc;
  134. long int id;
  135. char *ep;
  136. id = strtol(req_user, &ep, 10);
  137. if (*req_user != '\0' && *ep == '\0' && id >= 0 && id <= UINT_MAX) {
  138. return (id);
  139. }
  140. pwdlinelen = sysconf (_SC_GETPW_R_SIZE_MAX);
  141. if (pwdlinelen == -1) {
  142. pwdlinelen = 256;
  143. }
  144. pwdbuffer = malloc (pwdlinelen);
  145. while ((rc = getpwnam_r (req_user, pwdptr, pwdbuffer, pwdlinelen, &temp_pwd_pt)) == ERANGE) {
  146. char *n;
  147. pwdlinelen *= 2;
  148. if (pwdlinelen <= 32678) {
  149. n = realloc (pwdbuffer, pwdlinelen);
  150. if (n != NULL) {
  151. pwdbuffer = n;
  152. continue;
  153. }
  154. }
  155. }
  156. if (rc != 0) {
  157. free (pwdbuffer);
  158. sprintf (error_string_response, "getpwnam_r(): %s", strerror(rc));
  159. return (-1);
  160. }
  161. if (temp_pwd_pt == NULL) {
  162. free (pwdbuffer);
  163. sprintf (error_string_response,
  164. "The '%s' user is not found in /etc/passwd, please read the documentation.",
  165. req_user);
  166. return (-1);
  167. }
  168. pw_uid = passwd.pw_uid;
  169. free (pwdbuffer);
  170. return pw_uid;
  171. }
  172. static int gid_determine (const char *req_group)
  173. {
  174. int corosync_gid = 0;
  175. struct group group;
  176. struct group * grpptr = &group;
  177. struct group * temp_grp_pt;
  178. char *grpbuffer;
  179. int grplinelen, rc;
  180. long int id;
  181. char *ep;
  182. id = strtol(req_group, &ep, 10);
  183. if (*req_group != '\0' && *ep == '\0' && id >= 0 && id <= UINT_MAX) {
  184. return (id);
  185. }
  186. grplinelen = sysconf (_SC_GETGR_R_SIZE_MAX);
  187. if (grplinelen == -1) {
  188. grplinelen = 256;
  189. }
  190. grpbuffer = malloc (grplinelen);
  191. while ((rc = getgrnam_r (req_group, grpptr, grpbuffer, grplinelen, &temp_grp_pt)) == ERANGE) {
  192. char *n;
  193. grplinelen *= 2;
  194. if (grplinelen <= 32678) {
  195. n = realloc (grpbuffer, grplinelen);
  196. if (n != NULL) {
  197. grpbuffer = n;
  198. continue;
  199. }
  200. }
  201. }
  202. if (rc != 0) {
  203. free (grpbuffer);
  204. sprintf (error_string_response, "getgrnam_r(): %s", strerror(rc));
  205. return (-1);
  206. }
  207. if (temp_grp_pt == NULL) {
  208. free (grpbuffer);
  209. sprintf (error_string_response,
  210. "The '%s' group is not found in /etc/group, please read the documentation.",
  211. req_group);
  212. return (-1);
  213. }
  214. corosync_gid = group.gr_gid;
  215. free (grpbuffer);
  216. return corosync_gid;
  217. }
  218. static char *strchr_rs (const char *haystack, int byte)
  219. {
  220. const char *end_address = strchr (haystack, byte);
  221. if (end_address) {
  222. end_address += 1; /* skip past { or = */
  223. while (*end_address == ' ' || *end_address == '\t' || (unsigned char)*end_address == 0xA0)
  224. end_address++;
  225. }
  226. return ((char *) end_address);
  227. }
  228. int coroparse_configparse (icmap_map_t config_map, const char **error_string)
  229. {
  230. if (read_config_file_into_icmap(error_string, config_map)) {
  231. return -1;
  232. }
  233. return 0;
  234. }
  235. static char *remove_whitespace(char *string, int remove_colon_and_brace)
  236. {
  237. char *start;
  238. char *end;
  239. start = string;
  240. if (*start == '\0')
  241. return start;
  242. while (*start == ' ' || *start == '\t' || (unsigned char)*start == 0xA0)
  243. start++;
  244. end = start+(strlen(start))-1;
  245. while ((*end == ' ' || *end == '\t' || (unsigned char)*end == 0xA0 || (remove_colon_and_brace && (*end == ':' || *end == '{'))) && end > start)
  246. end--;
  247. if (*end != '\0')
  248. *(end + 1) = '\0';
  249. return start;
  250. }
  251. static int parse_section(FILE *fp,
  252. const char *fname,
  253. int *line_no,
  254. const char *path,
  255. const char **error_string,
  256. int depth,
  257. enum main_cp_cb_data_state state,
  258. parser_cb_f parser_cb,
  259. icmap_map_t config_map,
  260. void *user_data)
  261. {
  262. char line[512];
  263. int i;
  264. char *loc;
  265. int ignore_line;
  266. char new_keyname[ICMAP_KEYNAME_MAXLEN];
  267. static char formated_err[384];
  268. const char *tmp_error_string;
  269. if (strcmp(path, "") == 0) {
  270. parser_cb("", NULL, NULL, &state, PARSER_CB_START, error_string, config_map, user_data);
  271. }
  272. tmp_error_string = NULL;
  273. while (fgets (line, sizeof (line), fp)) {
  274. (*line_no)++;
  275. if (strlen(line) > 0) {
  276. /*
  277. * Check if complete line was read. Use feof to handle files
  278. * without ending \n at the end of the file
  279. */
  280. if ((line[strlen(line) - 1] != '\n') && !feof(fp)) {
  281. tmp_error_string = "Line too long";
  282. goto parse_error;
  283. }
  284. if (line[strlen(line) - 1] == '\n')
  285. line[strlen(line) - 1] = '\0';
  286. if (strlen (line) > 0 && line[strlen(line) - 1] == '\r')
  287. line[strlen(line) - 1] = '\0';
  288. }
  289. /*
  290. * Clear out white space and tabs
  291. */
  292. for (i = strlen (line) - 1; i > -1; i--) {
  293. if (line[i] == '\t' || line[i] == ' ' || (unsigned char)line[i] == 0xA0) {
  294. line[i] = '\0';
  295. } else {
  296. break;
  297. }
  298. }
  299. ignore_line = 1;
  300. for (i = 0; i < strlen (line); i++) {
  301. if (line[i] != '\t' && line[i] != ' ' && (unsigned char)line[i] != 0xA0) {
  302. if (line[i] != '#')
  303. ignore_line = 0;
  304. break;
  305. }
  306. }
  307. /*
  308. * Clear out comments and empty lines
  309. */
  310. if (ignore_line) {
  311. continue;
  312. }
  313. /* New section ? */
  314. if ((loc = strchr_rs (line, '{'))) {
  315. char *section;
  316. char *after_section;
  317. enum main_cp_cb_data_state newstate;
  318. *(loc-1) = '\0';
  319. section = remove_whitespace(line, 1);
  320. after_section = remove_whitespace(loc, 0);
  321. if (strcmp(section, "") == 0) {
  322. tmp_error_string = "Missing section name before opening bracket '{'";
  323. goto parse_error;
  324. }
  325. if (strcmp(after_section, "") != 0) {
  326. tmp_error_string = "Extra characters after opening bracket '{'";
  327. goto parse_error;
  328. }
  329. if (strlen(path) + strlen(section) + 1 >= ICMAP_KEYNAME_MAXLEN) {
  330. tmp_error_string = "Start of section makes total cmap path too long";
  331. goto parse_error;
  332. }
  333. strcpy(new_keyname, path);
  334. if (strcmp(path, "") != 0) {
  335. strcat(new_keyname, ".");
  336. }
  337. strcat(new_keyname, section);
  338. /* Only use the new state for items further down the stack */
  339. newstate = state;
  340. if (!parser_cb(new_keyname, NULL, NULL, &newstate, PARSER_CB_SECTION_START,
  341. &tmp_error_string, config_map, user_data)) {
  342. goto parse_error;
  343. }
  344. if (parse_section(fp, fname, line_no, new_keyname, error_string, depth + 1, newstate,
  345. parser_cb, config_map, user_data))
  346. return -1;
  347. continue ;
  348. }
  349. /* New key/value */
  350. if ((loc = strchr_rs (line, ':'))) {
  351. char *key;
  352. char *value;
  353. *(loc-1) = '\0';
  354. key = remove_whitespace(line, 1);
  355. value = remove_whitespace(loc, 0);
  356. if (strlen(key) == 0) {
  357. tmp_error_string = "Key name can't be empty";
  358. goto parse_error;
  359. }
  360. if (strlen(path) + strlen(key) + 1 >= ICMAP_KEYNAME_MAXLEN) {
  361. tmp_error_string = "New key makes total cmap path too long";
  362. goto parse_error;
  363. }
  364. strcpy(new_keyname, path);
  365. if (strcmp(path, "") != 0) {
  366. strcat(new_keyname, ".");
  367. }
  368. strcat(new_keyname, key);
  369. if (!parser_cb(new_keyname, key, value, &state, PARSER_CB_ITEM, &tmp_error_string,
  370. config_map, user_data)) {
  371. goto parse_error;
  372. }
  373. continue ;
  374. }
  375. if (strchr_rs (line, '}')) {
  376. char *trimmed_line;
  377. trimmed_line = remove_whitespace(line, 0);
  378. if (strcmp(trimmed_line, "}") != 0) {
  379. tmp_error_string = "Extra characters before or after closing bracket '}'";
  380. goto parse_error;
  381. }
  382. if (depth == 0) {
  383. tmp_error_string = "Unexpected closing brace";
  384. goto parse_error;
  385. }
  386. if (!parser_cb(path, NULL, NULL, &state, PARSER_CB_SECTION_END, &tmp_error_string,
  387. config_map, user_data)) {
  388. goto parse_error;
  389. }
  390. return 0;
  391. }
  392. /*
  393. * Line is not opening section, ending section or value -> error
  394. */
  395. tmp_error_string = "Line is not opening or closing section or key value";
  396. goto parse_error;
  397. }
  398. if (strcmp(path, "") != 0) {
  399. tmp_error_string = "Missing closing brace";
  400. goto parse_error;
  401. }
  402. if (strcmp(path, "") == 0) {
  403. parser_cb("", NULL, NULL, &state, PARSER_CB_END, error_string, config_map, user_data);
  404. }
  405. return 0;
  406. parse_error:
  407. if (snprintf(formated_err, sizeof(formated_err), "parser error: %s:%u: %s", fname, *line_no,
  408. tmp_error_string) >= sizeof(formated_err)) {
  409. *error_string = "Can't format parser error message";
  410. } else {
  411. *error_string = formated_err;
  412. }
  413. return -1;
  414. }
  415. static int safe_atoq_range(icmap_value_types_t value_type, long long int *min_val, long long int *max_val)
  416. {
  417. switch (value_type) {
  418. case ICMAP_VALUETYPE_INT8: *min_val = INT8_MIN; *max_val = INT8_MAX; break;
  419. case ICMAP_VALUETYPE_UINT8: *min_val = 0; *max_val = UINT8_MAX; break;
  420. case ICMAP_VALUETYPE_INT16: *min_val = INT16_MIN; *max_val = INT16_MAX; break;
  421. case ICMAP_VALUETYPE_UINT16: *min_val = 0; *max_val = UINT16_MAX; break;
  422. case ICMAP_VALUETYPE_INT32: *min_val = INT32_MIN; *max_val = INT32_MAX; break;
  423. case ICMAP_VALUETYPE_UINT32: *min_val = 0; *max_val = UINT32_MAX; break;
  424. default:
  425. return (-1);
  426. }
  427. return (0);
  428. }
  429. /*
  430. * Convert string str to long long int res. Type of result is target_type and currently only
  431. * ICMAP_VALUETYPE_[U]INT[8|16|32] is supported.
  432. * Return 0 on success, -1 on failure.
  433. */
  434. static int safe_atoq(const char *str, long long int *res, icmap_value_types_t target_type)
  435. {
  436. long long int val;
  437. long long int min_val, max_val;
  438. char *endptr;
  439. errno = 0;
  440. val = strtoll(str, &endptr, 10);
  441. if (errno == ERANGE) {
  442. return (-1);
  443. }
  444. if (endptr == str) {
  445. return (-1);
  446. }
  447. if (*endptr != '\0') {
  448. return (-1);
  449. }
  450. if (safe_atoq_range(target_type, &min_val, &max_val) != 0) {
  451. return (-1);
  452. }
  453. if (val < min_val || val > max_val) {
  454. return (-1);
  455. }
  456. *res = val;
  457. return (0);
  458. }
  459. static int str_to_ull(const char *str, unsigned long long int *res)
  460. {
  461. unsigned long long int val;
  462. char *endptr;
  463. errno = 0;
  464. val = strtoull(str, &endptr, 10);
  465. if (errno == ERANGE) {
  466. return (-1);
  467. }
  468. if (endptr == str) {
  469. return (-1);
  470. }
  471. if (*endptr != '\0') {
  472. return (-1);
  473. }
  474. *res = val;
  475. return (0);
  476. }
  477. static int handle_crypto_model(const char *val, const char **error_string)
  478. {
  479. if (util_is_valid_knet_crypto_model(val, NULL, 0,
  480. "Invalid crypto model. Should be ", error_string) == 1) {
  481. return (0);
  482. } else {
  483. return (-1);
  484. }
  485. }
  486. static int handle_compress_model(const char *val, const char **error_string)
  487. {
  488. if (util_is_valid_knet_compress_model(val, NULL, 0,
  489. "Invalid compression model. Should be ", error_string) == 1) {
  490. return (0);
  491. } else {
  492. return (-1);
  493. }
  494. }
  495. static int main_config_parser_cb(const char *path,
  496. char *key,
  497. char *value,
  498. enum main_cp_cb_data_state *state,
  499. enum parser_cb_type type,
  500. const char **error_string,
  501. icmap_map_t config_map,
  502. void *user_data)
  503. {
  504. int ii;
  505. long long int val;
  506. long long int min_val, max_val;
  507. icmap_value_types_t val_type = ICMAP_VALUETYPE_BINARY;
  508. unsigned long long int ull;
  509. int add_as_string;
  510. char key_name[ICMAP_KEYNAME_MAXLEN + 1];
  511. static char formated_err[256];
  512. struct main_cp_cb_data *data = (struct main_cp_cb_data *)user_data;
  513. struct key_value_list_item *kv_item;
  514. struct qb_list_head *iter, *tmp_iter;
  515. int uid, gid;
  516. cs_error_t cs_err;
  517. cs_err = CS_OK;
  518. /*
  519. * Formally this check is not needed because length is checked by parse_section
  520. */
  521. if (strlen(path) >= sizeof(key_name)) {
  522. if (snprintf(formated_err, sizeof(formated_err),
  523. "Can't store path \"%s\" into key_name", path) >= sizeof(formated_err)) {
  524. *error_string = "Can't format path into key_name error message";
  525. } else {
  526. *error_string = formated_err;
  527. }
  528. return (0);
  529. }
  530. /*
  531. * Key_name is used in atoi_error/icmap_set_error, but many of icmap_set*
  532. * are using path, so initialize key_name to valid value
  533. */
  534. strncpy(key_name, path, sizeof(key_name) - 1);
  535. switch (type) {
  536. case PARSER_CB_START:
  537. memset(data, 0, sizeof(struct main_cp_cb_data));
  538. *state = MAIN_CP_CB_DATA_STATE_NORMAL;
  539. break;
  540. case PARSER_CB_END:
  541. break;
  542. case PARSER_CB_ITEM:
  543. add_as_string = 1;
  544. switch (*state) {
  545. case MAIN_CP_CB_DATA_STATE_NORMAL:
  546. break;
  547. case MAIN_CP_CB_DATA_STATE_PLOAD:
  548. if ((strcmp(path, "pload.count") == 0) ||
  549. (strcmp(path, "pload.size") == 0)) {
  550. val_type = ICMAP_VALUETYPE_UINT32;
  551. if (safe_atoq(value, &val, val_type) != 0) {
  552. goto atoi_error;
  553. }
  554. if ((cs_err = icmap_set_uint32_r(config_map, path, val)) != CS_OK) {
  555. goto icmap_set_error;
  556. }
  557. add_as_string = 0;
  558. }
  559. break;
  560. case MAIN_CP_CB_DATA_STATE_QUORUM:
  561. if ((strcmp(path, "quorum.expected_votes") == 0) ||
  562. (strcmp(path, "quorum.votes") == 0) ||
  563. (strcmp(path, "quorum.last_man_standing_window") == 0) ||
  564. (strcmp(path, "quorum.leaving_timeout") == 0)) {
  565. val_type = ICMAP_VALUETYPE_UINT32;
  566. if (safe_atoq(value, &val, val_type) != 0) {
  567. goto atoi_error;
  568. }
  569. if ((cs_err = icmap_set_uint32_r(config_map, path, val)) != CS_OK) {
  570. goto icmap_set_error;
  571. }
  572. add_as_string = 0;
  573. }
  574. if ((strcmp(path, "quorum.two_node") == 0) ||
  575. (strcmp(path, "quorum.expected_votes_tracking") == 0) ||
  576. (strcmp(path, "quorum.allow_downscale") == 0) ||
  577. (strcmp(path, "quorum.wait_for_all") == 0) ||
  578. (strcmp(path, "quorum.auto_tie_breaker") == 0) ||
  579. (strcmp(path, "quorum.last_man_standing") == 0)) {
  580. val_type = ICMAP_VALUETYPE_UINT8;
  581. if (safe_atoq(value, &val, val_type) != 0) {
  582. goto atoi_error;
  583. }
  584. if ((cs_err = icmap_set_uint8_r(config_map, path, val)) != CS_OK) {
  585. goto icmap_set_error;
  586. }
  587. add_as_string = 0;
  588. }
  589. break;
  590. case MAIN_CP_CB_DATA_STATE_QDEVICE:
  591. if ((strcmp(path, "quorum.device.timeout") == 0) ||
  592. (strcmp(path, "quorum.device.sync_timeout") == 0) ||
  593. (strcmp(path, "quorum.device.votes") == 0)) {
  594. val_type = ICMAP_VALUETYPE_UINT32;
  595. if (safe_atoq(value, &val, val_type) != 0) {
  596. goto atoi_error;
  597. }
  598. if ((cs_err = icmap_set_uint32_r(config_map, path, val)) != CS_OK) {
  599. goto icmap_set_error;
  600. }
  601. add_as_string = 0;
  602. }
  603. if ((strcmp(path, "quorum.device.master_wins") == 0)) {
  604. val_type = ICMAP_VALUETYPE_UINT8;
  605. if (safe_atoq(value, &val, val_type) != 0) {
  606. goto atoi_error;
  607. }
  608. if ((cs_err = icmap_set_uint8_r(config_map, path, val)) != CS_OK) {
  609. goto icmap_set_error;
  610. }
  611. add_as_string = 0;
  612. }
  613. break;
  614. case MAIN_CP_CB_DATA_STATE_TOTEM:
  615. if ((strcmp(path, "totem.version") == 0) ||
  616. (strcmp(path, "totem.nodeid") == 0) ||
  617. (strcmp(path, "totem.threads") == 0) ||
  618. (strcmp(path, "totem.token") == 0) ||
  619. (strcmp(path, "totem.token_coefficient") == 0) ||
  620. (strcmp(path, "totem.token_retransmit") == 0) ||
  621. (strcmp(path, "totem.token_warning") == 0) ||
  622. (strcmp(path, "totem.hold") == 0) ||
  623. (strcmp(path, "totem.token_retransmits_before_loss_const") == 0) ||
  624. (strcmp(path, "totem.join") == 0) ||
  625. (strcmp(path, "totem.send_join") == 0) ||
  626. (strcmp(path, "totem.consensus") == 0) ||
  627. (strcmp(path, "totem.merge") == 0) ||
  628. (strcmp(path, "totem.downcheck") == 0) ||
  629. (strcmp(path, "totem.fail_recv_const") == 0) ||
  630. (strcmp(path, "totem.seqno_unchanged_const") == 0) ||
  631. (strcmp(path, "totem.rrp_token_expired_timeout") == 0) ||
  632. (strcmp(path, "totem.rrp_problem_count_timeout") == 0) ||
  633. (strcmp(path, "totem.rrp_problem_count_threshold") == 0) ||
  634. (strcmp(path, "totem.rrp_problem_count_mcast_threshold") == 0) ||
  635. (strcmp(path, "totem.rrp_autorecovery_check_timeout") == 0) ||
  636. (strcmp(path, "totem.heartbeat_failures_allowed") == 0) ||
  637. (strcmp(path, "totem.max_network_delay") == 0) ||
  638. (strcmp(path, "totem.window_size") == 0) ||
  639. (strcmp(path, "totem.max_messages") == 0) ||
  640. (strcmp(path, "totem.miss_count_const") == 0) ||
  641. (strcmp(path, "totem.knet_pmtud_interval") == 0) ||
  642. (strcmp(path, "totem.knet_mtu") == 0) ||
  643. (strcmp(path, "totem.knet_compression_threshold") == 0) ||
  644. (strcmp(path, "totem.netmtu") == 0)) {
  645. val_type = ICMAP_VALUETYPE_UINT32;
  646. if (safe_atoq(value, &val, val_type) != 0) {
  647. goto atoi_error;
  648. }
  649. if ((cs_err = icmap_set_uint32_r(config_map,path, val)) != CS_OK) {
  650. goto icmap_set_error;
  651. }
  652. add_as_string = 0;
  653. }
  654. if (strcmp(path, "totem.knet_compression_level") == 0) {
  655. val_type = ICMAP_VALUETYPE_INT32;
  656. if (safe_atoq(value, &val, val_type) != 0) {
  657. goto atoi_error;
  658. }
  659. if ((cs_err = icmap_set_int32_r(config_map, path, val)) != CS_OK) {
  660. goto icmap_set_error;
  661. }
  662. add_as_string = 0;
  663. }
  664. if (strcmp(path, "totem.config_version") == 0) {
  665. if (str_to_ull(value, &ull) != 0) {
  666. goto atoi_error;
  667. }
  668. if ((cs_err = icmap_set_uint64_r(config_map, path, ull)) != CS_OK) {
  669. goto icmap_set_error;
  670. }
  671. add_as_string = 0;
  672. }
  673. if (strcmp(path, "totem.ip_version") == 0) {
  674. if ((strcmp(value, "ipv4") != 0) &&
  675. (strcmp(value, "ipv6") != 0) &&
  676. (strcmp(value, "ipv6-4") != 0) &&
  677. (strcmp(value, "ipv4-6") != 0)) {
  678. *error_string = "Invalid ip_version type";
  679. return (0);
  680. }
  681. }
  682. if (strcmp(path, "totem.crypto_model") == 0) {
  683. if (handle_crypto_model(value, error_string) != 0) {
  684. return (0);
  685. }
  686. }
  687. if (strcmp(path, "totem.crypto_cipher") == 0) {
  688. if ((strcmp(value, "none") != 0) &&
  689. (strcmp(value, "aes256") != 0) &&
  690. (strcmp(value, "aes192") != 0) &&
  691. (strcmp(value, "aes128") != 0)) {
  692. *error_string = "Invalid cipher type. "
  693. "Should be none, aes256, aes192 or aes128";
  694. return (0);
  695. }
  696. }
  697. if (strcmp(path, "totem.crypto_hash") == 0) {
  698. if ((strcmp(value, "none") != 0) &&
  699. (strcmp(value, "md5") != 0) &&
  700. (strcmp(value, "sha1") != 0) &&
  701. (strcmp(value, "sha256") != 0) &&
  702. (strcmp(value, "sha384") != 0) &&
  703. (strcmp(value, "sha512") != 0)) {
  704. *error_string = "Invalid hash type. "
  705. "Should be none, md5, sha1, sha256, sha384 or sha512";
  706. return (0);
  707. }
  708. }
  709. if (strcmp(path, "totem.knet_compression_model") == 0) {
  710. if (handle_compress_model(value, error_string) != 0) {
  711. return (0);
  712. }
  713. }
  714. break;
  715. case MAIN_CP_CB_DATA_STATE_SYSTEM:
  716. if (strcmp(path, "system.qb_ipc_type") == 0) {
  717. if ((strcmp(value, "native") != 0) &&
  718. (strcmp(value, "shm") != 0) &&
  719. (strcmp(value, "socket") != 0)) {
  720. *error_string = "Invalid system.qb_ipc_type";
  721. return (0);
  722. }
  723. }
  724. if (strcmp(path, "system.sched_rr") == 0) {
  725. if ((strcmp(value, "yes") != 0) &&
  726. (strcmp(value, "no") != 0)) {
  727. *error_string = "Invalid system.sched_rr value";
  728. return (0);
  729. }
  730. }
  731. if (strcmp(path, "system.move_to_root_cgroup") == 0) {
  732. if ((strcmp(value, "yes") != 0) &&
  733. (strcmp(value, "no") != 0) &&
  734. (strcmp(value, "auto") != 0)) {
  735. *error_string = "Invalid system.move_to_root_cgroup";
  736. return (0);
  737. }
  738. }
  739. if (strcmp(path, "system.allow_knet_handle_fallback") == 0) {
  740. if ((strcmp(value, "yes") != 0) &&
  741. (strcmp(value, "no") != 0)) {
  742. *error_string = "Invalid system.allow_knet_handle_fallback";
  743. return (0);
  744. }
  745. }
  746. break;
  747. case MAIN_CP_CB_DATA_STATE_INTERFACE:
  748. if (strcmp(path, "totem.interface.linknumber") == 0) {
  749. val_type = ICMAP_VALUETYPE_UINT8;
  750. if (safe_atoq(value, &val, val_type) != 0) {
  751. goto atoi_error;
  752. }
  753. data->linknumber = val;
  754. add_as_string = 0;
  755. }
  756. if (strcmp(path, "totem.interface.bindnetaddr") == 0) {
  757. data->bindnetaddr = strdup(value);
  758. add_as_string = 0;
  759. }
  760. if (strcmp(path, "totem.interface.mcastaddr") == 0) {
  761. data->mcastaddr = strdup(value);
  762. add_as_string = 0;
  763. }
  764. if (strcmp(path, "totem.interface.broadcast") == 0) {
  765. data->broadcast = strdup(value);
  766. add_as_string = 0;
  767. }
  768. if (strcmp(path, "totem.interface.mcastport") == 0) {
  769. val_type = ICMAP_VALUETYPE_UINT16;
  770. if (safe_atoq(value, &val, val_type) != 0) {
  771. goto atoi_error;
  772. }
  773. data->mcastport = val;
  774. add_as_string = 0;
  775. }
  776. if (strcmp(path, "totem.interface.ttl") == 0) {
  777. val_type = ICMAP_VALUETYPE_UINT8;
  778. if (safe_atoq(value, &val, val_type) != 0) {
  779. goto atoi_error;
  780. }
  781. data->ttl = val;
  782. add_as_string = 0;
  783. }
  784. if (strcmp(path, "totem.interface.knet_link_priority") == 0) {
  785. val_type = ICMAP_VALUETYPE_UINT8;
  786. if (safe_atoq(value, &val, val_type) != 0) {
  787. goto atoi_error;
  788. }
  789. data->knet_link_priority = val;
  790. add_as_string = 0;
  791. }
  792. if (strcmp(path, "totem.interface.knet_ping_interval") == 0) {
  793. val_type = ICMAP_VALUETYPE_UINT32;
  794. if (safe_atoq(value, &val, val_type) != 0) {
  795. goto atoi_error;
  796. }
  797. data->knet_ping_interval = val;
  798. add_as_string = 0;
  799. }
  800. if (strcmp(path, "totem.interface.knet_ping_timeout") == 0) {
  801. val_type = ICMAP_VALUETYPE_UINT32;
  802. if (safe_atoq(value, &val, val_type) != 0) {
  803. goto atoi_error;
  804. }
  805. data->knet_ping_timeout = val;
  806. add_as_string = 0;
  807. }
  808. if (strcmp(path, "totem.interface.knet_ping_precision") == 0) {
  809. val_type = ICMAP_VALUETYPE_UINT32;
  810. if (safe_atoq(value, &val, val_type) != 0) {
  811. goto atoi_error;
  812. }
  813. data->knet_ping_precision = val;
  814. add_as_string = 0;
  815. }
  816. if (strcmp(path, "totem.interface.knet_pong_count") == 0) {
  817. val_type = ICMAP_VALUETYPE_UINT32;
  818. if (safe_atoq(value, &val, val_type) != 0) {
  819. goto atoi_error;
  820. }
  821. data->knet_pong_count = val;
  822. add_as_string = 0;
  823. }
  824. if (strcmp(path, "totem.interface.knet_transport") == 0) {
  825. val_type = ICMAP_VALUETYPE_STRING;
  826. data->knet_transport = strdup(value);
  827. add_as_string = 0;
  828. }
  829. break;
  830. case MAIN_CP_CB_DATA_STATE_LOGGER_SUBSYS:
  831. if (strcmp(key, "subsys") == 0) {
  832. data->subsys = strdup(value);
  833. if (data->subsys == NULL) {
  834. *error_string = "Can't alloc memory";
  835. return (0);
  836. }
  837. } else {
  838. kv_item = malloc(sizeof(*kv_item));
  839. if (kv_item == NULL) {
  840. *error_string = "Can't alloc memory";
  841. return (0);
  842. }
  843. memset(kv_item, 0, sizeof(*kv_item));
  844. kv_item->key = strdup(key);
  845. kv_item->value = strdup(value);
  846. if (kv_item->key == NULL || kv_item->value == NULL) {
  847. free(kv_item->key);
  848. free(kv_item->value);
  849. free(kv_item);
  850. *error_string = "Can't alloc memory";
  851. return (0);
  852. }
  853. qb_list_init(&kv_item->list);
  854. qb_list_add(&kv_item->list, &data->logger_subsys_items_head);
  855. }
  856. add_as_string = 0;
  857. break;
  858. case MAIN_CP_CB_DATA_STATE_LOGGING_DAEMON:
  859. if (strcmp(key, "subsys") == 0) {
  860. data->subsys = strdup(value);
  861. if (data->subsys == NULL) {
  862. *error_string = "Can't alloc memory";
  863. return (0);
  864. }
  865. } else if (strcmp(key, "name") == 0) {
  866. data->logging_daemon_name = strdup(value);
  867. if (data->logging_daemon_name == NULL) {
  868. *error_string = "Can't alloc memory";
  869. return (0);
  870. }
  871. } else {
  872. kv_item = malloc(sizeof(*kv_item));
  873. if (kv_item == NULL) {
  874. *error_string = "Can't alloc memory";
  875. return (0);
  876. }
  877. memset(kv_item, 0, sizeof(*kv_item));
  878. kv_item->key = strdup(key);
  879. kv_item->value = strdup(value);
  880. if (kv_item->key == NULL || kv_item->value == NULL) {
  881. free(kv_item->key);
  882. free(kv_item->value);
  883. free(kv_item);
  884. *error_string = "Can't alloc memory";
  885. return (0);
  886. }
  887. qb_list_init(&kv_item->list);
  888. qb_list_add(&kv_item->list, &data->logger_subsys_items_head);
  889. }
  890. add_as_string = 0;
  891. break;
  892. case MAIN_CP_CB_DATA_STATE_UIDGID:
  893. if (strcmp(key, "uid") == 0) {
  894. uid = uid_determine(value);
  895. if (uid == -1) {
  896. *error_string = error_string_response;
  897. return (0);
  898. }
  899. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "uidgid.config.uid.%u",
  900. uid);
  901. if ((cs_err = icmap_set_uint8_r(config_map, key_name, 1)) != CS_OK) {
  902. goto icmap_set_error;
  903. }
  904. add_as_string = 0;
  905. } else if (strcmp(key, "gid") == 0) {
  906. gid = gid_determine(value);
  907. if (gid == -1) {
  908. *error_string = error_string_response;
  909. return (0);
  910. }
  911. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "uidgid.config.gid.%u",
  912. gid);
  913. if ((cs_err = icmap_set_uint8_r(config_map, key_name, 1)) != CS_OK) {
  914. goto icmap_set_error;
  915. }
  916. add_as_string = 0;
  917. } else {
  918. *error_string = "uidgid: Only uid and gid are allowed items";
  919. return (0);
  920. }
  921. break;
  922. case MAIN_CP_CB_DATA_STATE_MEMBER:
  923. if (strcmp(key, "memberaddr") != 0) {
  924. *error_string = "Only memberaddr is allowed in member section";
  925. return (0);
  926. }
  927. kv_item = malloc(sizeof(*kv_item));
  928. if (kv_item == NULL) {
  929. *error_string = "Can't alloc memory";
  930. return (0);
  931. }
  932. memset(kv_item, 0, sizeof(*kv_item));
  933. kv_item->key = strdup(key);
  934. kv_item->value = strdup(value);
  935. if (kv_item->key == NULL || kv_item->value == NULL) {
  936. free(kv_item->key);
  937. free(kv_item->value);
  938. free(kv_item);
  939. *error_string = "Can't alloc memory";
  940. return (0);
  941. }
  942. qb_list_init(&kv_item->list);
  943. qb_list_add(&kv_item->list, &data->member_items_head);
  944. add_as_string = 0;
  945. break;
  946. case MAIN_CP_CB_DATA_STATE_NODELIST:
  947. break;
  948. case MAIN_CP_CB_DATA_STATE_NODELIST_NODE:
  949. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.%s", data->node_number, key);
  950. if ((strcmp(key, "nodeid") == 0) ||
  951. (strcmp(key, "quorum_votes") == 0)) {
  952. val_type = ICMAP_VALUETYPE_UINT32;
  953. if (safe_atoq(value, &val, val_type) != 0) {
  954. goto atoi_error;
  955. }
  956. if ((cs_err = icmap_set_uint32_r(config_map, key_name, val)) != CS_OK) {
  957. goto icmap_set_error;
  958. }
  959. add_as_string = 0;
  960. }
  961. if (add_as_string) {
  962. if ((cs_err = icmap_set_string_r(config_map, key_name, value)) != CS_OK) {
  963. goto icmap_set_error;
  964. };
  965. add_as_string = 0;
  966. }
  967. break;
  968. case MAIN_CP_CB_DATA_STATE_RESOURCES:
  969. if (strcmp(key, "watchdog_timeout") == 0) {
  970. val_type = ICMAP_VALUETYPE_UINT32;
  971. if (safe_atoq(value, &val, val_type) != 0) {
  972. goto atoi_error;
  973. }
  974. if ((cs_err = icmap_set_uint32_r(config_map,path, val)) != CS_OK) {
  975. goto icmap_set_error;
  976. }
  977. add_as_string = 0;
  978. }
  979. break;
  980. case MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM:
  981. case MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM_MEMUSED:
  982. if (strcmp(key, "poll_period") == 0) {
  983. if (str_to_ull(value, &ull) != 0) {
  984. goto atoi_error;
  985. }
  986. if ((cs_err = icmap_set_uint64_r(config_map,path, ull)) != CS_OK) {
  987. goto icmap_set_error;
  988. }
  989. add_as_string = 0;
  990. }
  991. break;
  992. case MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS:
  993. case MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS_MEMUSED:
  994. if (strcmp(key, "poll_period") == 0) {
  995. if (str_to_ull(value, &ull) != 0) {
  996. goto atoi_error;
  997. }
  998. if ((cs_err = icmap_set_uint64_r(config_map,path, ull)) != CS_OK) {
  999. goto icmap_set_error;
  1000. }
  1001. add_as_string = 0;
  1002. }
  1003. break;
  1004. }
  1005. if (add_as_string) {
  1006. if ((cs_err = icmap_set_string_r(config_map, path, value)) != CS_OK) {
  1007. goto icmap_set_error;
  1008. }
  1009. }
  1010. break;
  1011. case PARSER_CB_SECTION_START:
  1012. if (strcmp(path, "totem.interface") == 0) {
  1013. *state = MAIN_CP_CB_DATA_STATE_INTERFACE;
  1014. data->linknumber = 0;
  1015. data->mcastport = -1;
  1016. data->ttl = -1;
  1017. data->knet_link_priority = -1;
  1018. data->knet_ping_interval = -1;
  1019. data->knet_ping_timeout = -1;
  1020. data->knet_ping_precision = -1;
  1021. data->knet_pong_count = -1;
  1022. data->knet_transport = NULL;
  1023. qb_list_init(&data->member_items_head);
  1024. };
  1025. if (strcmp(path, "totem") == 0) {
  1026. *state = MAIN_CP_CB_DATA_STATE_TOTEM;
  1027. };
  1028. if (strcmp(path, "system") == 0) {
  1029. *state = MAIN_CP_CB_DATA_STATE_SYSTEM;
  1030. }
  1031. if (strcmp(path, "logging.logger_subsys") == 0) {
  1032. *state = MAIN_CP_CB_DATA_STATE_LOGGER_SUBSYS;
  1033. qb_list_init(&data->logger_subsys_items_head);
  1034. data->subsys = NULL;
  1035. }
  1036. if (strcmp(path, "logging.logging_daemon") == 0) {
  1037. *state = MAIN_CP_CB_DATA_STATE_LOGGING_DAEMON;
  1038. qb_list_init(&data->logger_subsys_items_head);
  1039. data->subsys = NULL;
  1040. data->logging_daemon_name = NULL;
  1041. }
  1042. if (strcmp(path, "uidgid") == 0) {
  1043. *state = MAIN_CP_CB_DATA_STATE_UIDGID;
  1044. }
  1045. if (strcmp(path, "totem.interface.member") == 0) {
  1046. *state = MAIN_CP_CB_DATA_STATE_MEMBER;
  1047. }
  1048. if (strcmp(path, "quorum") == 0) {
  1049. *state = MAIN_CP_CB_DATA_STATE_QUORUM;
  1050. }
  1051. if (strcmp(path, "quorum.device") == 0) {
  1052. *state = MAIN_CP_CB_DATA_STATE_QDEVICE;
  1053. }
  1054. if (strcmp(path, "nodelist") == 0) {
  1055. *state = MAIN_CP_CB_DATA_STATE_NODELIST;
  1056. data->node_number = 0;
  1057. }
  1058. if (strcmp(path, "nodelist.node") == 0) {
  1059. *state = MAIN_CP_CB_DATA_STATE_NODELIST_NODE;
  1060. }
  1061. if (strcmp(path, "resources") == 0) {
  1062. *state = MAIN_CP_CB_DATA_STATE_RESOURCES;
  1063. }
  1064. if (strcmp(path, "resources.system") == 0) {
  1065. *state = MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM;
  1066. }
  1067. if (strcmp(path, "resources.system.memory_used") == 0) {
  1068. *state = MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM_MEMUSED;
  1069. }
  1070. if (strcmp(path, "resources.process") == 0) {
  1071. *state = MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS;
  1072. }
  1073. if (strcmp(path, "resources.process.memory_used") == 0) {
  1074. *state = MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS_MEMUSED;
  1075. }
  1076. break;
  1077. case PARSER_CB_SECTION_END:
  1078. switch (*state) {
  1079. case MAIN_CP_CB_DATA_STATE_INTERFACE:
  1080. /*
  1081. * Create new interface section
  1082. */
  1083. if (data->bindnetaddr != NULL) {
  1084. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.bindnetaddr",
  1085. data->linknumber);
  1086. cs_err = icmap_set_string_r(config_map, key_name, data->bindnetaddr);
  1087. free(data->bindnetaddr);
  1088. data->bindnetaddr = NULL;
  1089. if (cs_err != CS_OK) {
  1090. goto icmap_set_error;
  1091. }
  1092. }
  1093. if (data->mcastaddr != NULL) {
  1094. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr",
  1095. data->linknumber);
  1096. cs_err = icmap_set_string_r(config_map, key_name, data->mcastaddr);
  1097. free(data->mcastaddr);
  1098. data->mcastaddr = NULL;
  1099. if (cs_err != CS_OK) {
  1100. goto icmap_set_error;
  1101. }
  1102. }
  1103. if (data->broadcast != NULL) {
  1104. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.broadcast",
  1105. data->linknumber);
  1106. cs_err = icmap_set_string_r(config_map, key_name, data->broadcast);
  1107. free(data->broadcast);
  1108. data->broadcast = NULL;
  1109. if (cs_err != CS_OK) {
  1110. goto icmap_set_error;
  1111. }
  1112. }
  1113. if (data->mcastport > -1) {
  1114. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport",
  1115. data->linknumber);
  1116. if ((cs_err = icmap_set_uint16_r(config_map, key_name,
  1117. data->mcastport)) != CS_OK) {
  1118. goto icmap_set_error;
  1119. }
  1120. }
  1121. if (data->ttl > -1) {
  1122. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.ttl",
  1123. data->linknumber);
  1124. if ((cs_err = icmap_set_uint8_r(config_map, key_name, data->ttl)) != CS_OK) {
  1125. goto icmap_set_error;
  1126. }
  1127. }
  1128. if (data->knet_link_priority > -1) {
  1129. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_link_priority",
  1130. data->linknumber);
  1131. if ((cs_err = icmap_set_uint8_r(config_map, key_name,
  1132. data->knet_link_priority)) != CS_OK) {
  1133. goto icmap_set_error;
  1134. }
  1135. }
  1136. if (data->knet_ping_interval > -1) {
  1137. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_interval",
  1138. data->linknumber);
  1139. if ((cs_err = icmap_set_uint32_r(config_map, key_name,
  1140. data->knet_ping_interval)) != CS_OK) {
  1141. goto icmap_set_error;
  1142. }
  1143. }
  1144. if (data->knet_ping_timeout > -1) {
  1145. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_timeout",
  1146. data->linknumber);
  1147. if ((cs_err = icmap_set_uint32_r(config_map, key_name,
  1148. data->knet_ping_timeout)) != CS_OK) {
  1149. goto icmap_set_error;
  1150. }
  1151. }
  1152. if (data->knet_ping_precision > -1) {
  1153. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_precision",
  1154. data->linknumber);
  1155. if ((cs_err = icmap_set_uint32_r(config_map, key_name,
  1156. data->knet_ping_precision)) != CS_OK) {
  1157. goto icmap_set_error;
  1158. }
  1159. }
  1160. if (data->knet_pong_count > -1) {
  1161. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_pong_count",
  1162. data->linknumber);
  1163. if ((cs_err = icmap_set_uint32_r(config_map, key_name,
  1164. data->knet_pong_count)) != CS_OK) {
  1165. goto icmap_set_error;
  1166. }
  1167. }
  1168. if (data->knet_transport) {
  1169. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_transport",
  1170. data->linknumber);
  1171. cs_err = icmap_set_string_r(config_map, key_name, data->knet_transport);
  1172. free(data->knet_transport);
  1173. if (cs_err != CS_OK) {
  1174. goto icmap_set_error;
  1175. }
  1176. }
  1177. ii = 0;
  1178. qb_list_for_each_safe(iter, tmp_iter, &(data->member_items_head)) {
  1179. kv_item = qb_list_entry(iter, struct key_value_list_item, list);
  1180. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.member.%u",
  1181. data->linknumber, ii);
  1182. cs_err = icmap_set_string_r(config_map, key_name, kv_item->value);
  1183. free(kv_item->value);
  1184. free(kv_item->key);
  1185. free(kv_item);
  1186. ii++;
  1187. if (cs_err != CS_OK) {
  1188. goto icmap_set_error;
  1189. }
  1190. }
  1191. break;
  1192. case MAIN_CP_CB_DATA_STATE_LOGGER_SUBSYS:
  1193. if (data->subsys == NULL) {
  1194. *error_string = "No subsys key in logger_subsys directive";
  1195. return (0);
  1196. }
  1197. qb_list_for_each_safe(iter, tmp_iter, &(data->logger_subsys_items_head)) {
  1198. kv_item = qb_list_entry(iter, struct key_value_list_item, list);
  1199. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logger_subsys.%s.%s",
  1200. data->subsys, kv_item->key);
  1201. cs_err = icmap_set_string_r(config_map, key_name, kv_item->value);
  1202. free(kv_item->value);
  1203. free(kv_item->key);
  1204. free(kv_item);
  1205. if (cs_err != CS_OK) {
  1206. goto icmap_set_error;
  1207. }
  1208. }
  1209. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logger_subsys.%s.subsys",
  1210. data->subsys);
  1211. cs_err = icmap_set_string_r(config_map, key_name, data->subsys);
  1212. free(data->subsys);
  1213. if (cs_err != CS_OK) {
  1214. goto icmap_set_error;
  1215. }
  1216. break;
  1217. case MAIN_CP_CB_DATA_STATE_LOGGING_DAEMON:
  1218. if (data->logging_daemon_name == NULL) {
  1219. *error_string = "No name key in logging_daemon directive";
  1220. return (0);
  1221. }
  1222. qb_list_for_each_safe(iter, tmp_iter, &(data->logger_subsys_items_head)) {
  1223. kv_item = qb_list_entry(iter, struct key_value_list_item, list);
  1224. if (data->subsys == NULL) {
  1225. if (strcmp(data->logging_daemon_name, "corosync") == 0) {
  1226. snprintf(key_name, ICMAP_KEYNAME_MAXLEN,
  1227. "logging.%s",
  1228. kv_item->key);
  1229. } else {
  1230. snprintf(key_name, ICMAP_KEYNAME_MAXLEN,
  1231. "logging.logging_daemon.%s.%s",
  1232. data->logging_daemon_name, kv_item->key);
  1233. }
  1234. } else {
  1235. if (strcmp(data->logging_daemon_name, "corosync") == 0) {
  1236. snprintf(key_name, ICMAP_KEYNAME_MAXLEN,
  1237. "logging.logger_subsys.%s.%s",
  1238. data->subsys,
  1239. kv_item->key);
  1240. } else {
  1241. snprintf(key_name, ICMAP_KEYNAME_MAXLEN,
  1242. "logging.logging_daemon.%s.%s.%s",
  1243. data->logging_daemon_name, data->subsys,
  1244. kv_item->key);
  1245. }
  1246. }
  1247. cs_err = icmap_set_string_r(config_map, key_name, kv_item->value);
  1248. free(kv_item->value);
  1249. free(kv_item->key);
  1250. free(kv_item);
  1251. if (cs_err != CS_OK) {
  1252. goto icmap_set_error;
  1253. }
  1254. }
  1255. if (data->subsys == NULL) {
  1256. if (strcmp(data->logging_daemon_name, "corosync") != 0) {
  1257. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logging_daemon.%s.name",
  1258. data->logging_daemon_name);
  1259. cs_err = icmap_set_string_r(config_map, key_name, data->logging_daemon_name);
  1260. }
  1261. } else {
  1262. if (strcmp(data->logging_daemon_name, "corosync") == 0) {
  1263. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logger_subsys.%s.subsys",
  1264. data->subsys);
  1265. cs_err = icmap_set_string_r(config_map, key_name, data->subsys);
  1266. } else {
  1267. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logging_daemon.%s.%s.subsys",
  1268. data->logging_daemon_name, data->subsys);
  1269. cs_err = icmap_set_string_r(config_map, key_name, data->subsys);
  1270. if (cs_err != CS_OK) {
  1271. free(data->subsys);
  1272. free(data->logging_daemon_name);
  1273. goto icmap_set_error;
  1274. }
  1275. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logging_daemon.%s.%s.name",
  1276. data->logging_daemon_name, data->subsys);
  1277. cs_err = icmap_set_string_r(config_map, key_name, data->logging_daemon_name);
  1278. }
  1279. }
  1280. free(data->subsys);
  1281. free(data->logging_daemon_name);
  1282. if (cs_err != CS_OK) {
  1283. goto icmap_set_error;
  1284. }
  1285. break;
  1286. case MAIN_CP_CB_DATA_STATE_NODELIST_NODE:
  1287. data->node_number++;
  1288. break;
  1289. case MAIN_CP_CB_DATA_STATE_NORMAL:
  1290. case MAIN_CP_CB_DATA_STATE_PLOAD:
  1291. case MAIN_CP_CB_DATA_STATE_UIDGID:
  1292. case MAIN_CP_CB_DATA_STATE_MEMBER:
  1293. case MAIN_CP_CB_DATA_STATE_QUORUM:
  1294. case MAIN_CP_CB_DATA_STATE_QDEVICE:
  1295. case MAIN_CP_CB_DATA_STATE_NODELIST:
  1296. case MAIN_CP_CB_DATA_STATE_TOTEM:
  1297. case MAIN_CP_CB_DATA_STATE_SYSTEM:
  1298. case MAIN_CP_CB_DATA_STATE_RESOURCES:
  1299. case MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM:
  1300. case MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM_MEMUSED:
  1301. case MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS:
  1302. case MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS_MEMUSED:
  1303. break;
  1304. }
  1305. break;
  1306. }
  1307. return (1);
  1308. atoi_error:
  1309. min_val = max_val = 0;
  1310. /*
  1311. * This is really assert, because developer ether doesn't set val_type correctly or
  1312. * we've got here after some nasty memory overwrite
  1313. */
  1314. assert(safe_atoq_range(val_type, &min_val, &max_val) == 0);
  1315. if (snprintf(formated_err, sizeof(formated_err),
  1316. "Value of key \"%s\" is expected to be integer in range (%lld..%lld), but \"%s\" was given",
  1317. key_name, min_val, max_val, value) >= sizeof(formated_err)) {
  1318. *error_string = "Can't format parser error message";
  1319. } else {
  1320. *error_string = formated_err;
  1321. }
  1322. return (0);
  1323. icmap_set_error:
  1324. if (snprintf(formated_err, sizeof(formated_err),
  1325. "Can't store key \"%s\" into icmap, returned error is %s",
  1326. key_name, cs_strerror(cs_err)) >= sizeof(formated_err)) {
  1327. *error_string = "Can't format parser error message";
  1328. } else {
  1329. *error_string = formated_err;
  1330. }
  1331. return (0);
  1332. }
  1333. static int uidgid_config_parser_cb(const char *path,
  1334. char *key,
  1335. char *value,
  1336. enum main_cp_cb_data_state *state,
  1337. enum parser_cb_type type,
  1338. const char **error_string,
  1339. icmap_map_t config_map,
  1340. void *user_data)
  1341. {
  1342. char key_name[ICMAP_KEYNAME_MAXLEN];
  1343. int uid, gid;
  1344. static char formated_err[256];
  1345. cs_error_t cs_err;
  1346. switch (type) {
  1347. case PARSER_CB_START:
  1348. break;
  1349. case PARSER_CB_END:
  1350. break;
  1351. case PARSER_CB_ITEM:
  1352. if (strcmp(path, "uidgid.uid") == 0) {
  1353. uid = uid_determine(value);
  1354. if (uid == -1) {
  1355. *error_string = error_string_response;
  1356. return (0);
  1357. }
  1358. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "uidgid.config.uid.%u",
  1359. uid);
  1360. if ((cs_err = icmap_set_uint8_r(config_map, key_name, 1)) != CS_OK) {
  1361. goto icmap_set_error;
  1362. }
  1363. } else if (strcmp(path, "uidgid.gid") == 0) {
  1364. gid = gid_determine(value);
  1365. if (gid == -1) {
  1366. *error_string = error_string_response;
  1367. return (0);
  1368. }
  1369. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "uidgid.config.gid.%u",
  1370. gid);
  1371. if ((cs_err = icmap_set_uint8_r(config_map, key_name, 1)) != CS_OK) {
  1372. goto icmap_set_error;
  1373. }
  1374. } else {
  1375. *error_string = "uidgid: Only uid and gid are allowed items";
  1376. return (0);
  1377. }
  1378. break;
  1379. case PARSER_CB_SECTION_START:
  1380. if (strcmp(path, "uidgid") != 0) {
  1381. *error_string = "uidgid: Can't add subsection different than uidgid";
  1382. return (0);
  1383. };
  1384. break;
  1385. case PARSER_CB_SECTION_END:
  1386. break;
  1387. }
  1388. return (1);
  1389. icmap_set_error:
  1390. if (snprintf(formated_err, sizeof(formated_err),
  1391. "Can't store key \"%s\" into icmap, returned error is %s",
  1392. key_name, cs_strerror(cs_err)) >= sizeof(formated_err)) {
  1393. *error_string = "Can't format parser error message";
  1394. } else {
  1395. *error_string = formated_err;
  1396. }
  1397. return (0);
  1398. }
  1399. static int read_uidgid_files_into_icmap(
  1400. const char **error_string,
  1401. icmap_map_t config_map)
  1402. {
  1403. FILE *fp;
  1404. char *dirname_res;
  1405. DIR *dp;
  1406. struct dirent *dirent;
  1407. char filename[PATH_MAX + FILENAME_MAX + 1];
  1408. char uidgid_dirname[PATH_MAX + FILENAME_MAX + 1];
  1409. int res = 0;
  1410. struct stat stat_buf;
  1411. enum main_cp_cb_data_state state = MAIN_CP_CB_DATA_STATE_NORMAL;
  1412. char key_name[ICMAP_KEYNAME_MAXLEN];
  1413. int line_no;
  1414. /*
  1415. * Build uidgid directory based on corosync.conf file location
  1416. */
  1417. res = snprintf(filename, sizeof(filename), "%s",
  1418. corosync_get_config_file());
  1419. if (res >= sizeof(filename)) {
  1420. *error_string = "uidgid.d path too long";
  1421. return (-1);
  1422. }
  1423. dirname_res = dirname(filename);
  1424. res = snprintf(uidgid_dirname, sizeof(uidgid_dirname), "%s/%s",
  1425. dirname_res, "uidgid.d");
  1426. if (res >= sizeof(uidgid_dirname)) {
  1427. *error_string = "uidgid.d path too long";
  1428. return (-1);
  1429. }
  1430. dp = opendir (uidgid_dirname);
  1431. if (dp == NULL)
  1432. return 0;
  1433. for (dirent = readdir(dp);
  1434. dirent != NULL;
  1435. dirent = readdir(dp)) {
  1436. res = snprintf(filename, sizeof (filename), "%s/%s", uidgid_dirname, dirent->d_name);
  1437. if (res >= sizeof(filename)) {
  1438. res = -1;
  1439. *error_string = "uidgid.d dirname path too long";
  1440. goto error_exit;
  1441. }
  1442. res = stat (filename, &stat_buf);
  1443. if (res == 0 && S_ISREG(stat_buf.st_mode)) {
  1444. fp = fopen (filename, "r");
  1445. if (fp == NULL) continue;
  1446. key_name[0] = 0;
  1447. line_no = 0;
  1448. res = parse_section(fp, filename, &line_no, key_name, error_string, 0, state,
  1449. uidgid_config_parser_cb, config_map, NULL);
  1450. fclose (fp);
  1451. if (res != 0) {
  1452. goto error_exit;
  1453. }
  1454. }
  1455. }
  1456. error_exit:
  1457. closedir(dp);
  1458. return res;
  1459. }
  1460. /* Read config file and load into icmap */
  1461. static int read_config_file_into_icmap(
  1462. const char **error_string,
  1463. icmap_map_t config_map)
  1464. {
  1465. FILE *fp;
  1466. const char *filename;
  1467. char *error_reason = error_string_response;
  1468. int res;
  1469. char key_name[ICMAP_KEYNAME_MAXLEN];
  1470. struct main_cp_cb_data data;
  1471. enum main_cp_cb_data_state state = MAIN_CP_CB_DATA_STATE_NORMAL;
  1472. int line_no;
  1473. filename = corosync_get_config_file();
  1474. fp = fopen (filename, "r");
  1475. if (fp == NULL) {
  1476. char error_str[100];
  1477. const char *error_ptr = qb_strerror_r(errno, error_str, sizeof(error_str));
  1478. snprintf (error_reason, sizeof(error_string_response),
  1479. "Can't read file %s: %s",
  1480. filename, error_ptr);
  1481. *error_string = error_reason;
  1482. return -1;
  1483. }
  1484. key_name[0] = 0;
  1485. line_no = 0;
  1486. res = parse_section(fp, filename, &line_no, key_name, error_string, 0, state,
  1487. main_config_parser_cb, config_map, &data);
  1488. fclose(fp);
  1489. if (res == 0) {
  1490. res = read_uidgid_files_into_icmap(error_string, config_map);
  1491. }
  1492. if (res == 0) {
  1493. snprintf (error_reason, sizeof(error_string_response),
  1494. "Successfully read main configuration file '%s'.", filename);
  1495. *error_string = error_reason;
  1496. }
  1497. return res;
  1498. }