coroparse.c 44 KB

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