4
0

coroparse.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  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. (strcmp(path, "totem.netmtu") == 0)) {
  619. val_type = ICMAP_VALUETYPE_UINT32;
  620. if (safe_atoq(value, &val, val_type) != 0) {
  621. goto atoi_error;
  622. }
  623. if ((cs_err = icmap_set_uint32_r(config_map,path, val)) != CS_OK) {
  624. goto icmap_set_error;
  625. }
  626. add_as_string = 0;
  627. }
  628. if (strcmp(path, "totem.knet_compression_level") == 0) {
  629. val_type = ICMAP_VALUETYPE_INT32;
  630. if (safe_atoq(value, &val, val_type) != 0) {
  631. goto atoi_error;
  632. }
  633. if ((cs_err = icmap_set_int32_r(config_map, path, val)) != CS_OK) {
  634. goto icmap_set_error;
  635. }
  636. add_as_string = 0;
  637. }
  638. if (strcmp(path, "totem.config_version") == 0) {
  639. if (str_to_ull(value, &ull) != 0) {
  640. goto atoi_error;
  641. }
  642. if ((cs_err = icmap_set_uint64_r(config_map, path, ull)) != CS_OK) {
  643. goto icmap_set_error;
  644. }
  645. add_as_string = 0;
  646. }
  647. if (strcmp(path, "totem.ip_version") == 0) {
  648. if ((strcmp(value, "ipv4") != 0) &&
  649. (strcmp(value, "ipv6") != 0) &&
  650. (strcmp(value, "ipv6-4") != 0) &&
  651. (strcmp(value, "ipv4-6") != 0)) {
  652. *error_string = "Invalid ip_version type";
  653. return (0);
  654. }
  655. }
  656. if (strcmp(path, "totem.crypto_model") == 0) {
  657. if ((strcmp(value, "nss") != 0) &&
  658. (strcmp(value, "openssl") != 0)) {
  659. *error_string = "Invalid crypto model. "
  660. "Should be nss or openssl";
  661. return (0);
  662. }
  663. }
  664. if (strcmp(path, "totem.crypto_cipher") == 0) {
  665. if ((strcmp(value, "none") != 0) &&
  666. (strcmp(value, "aes256") != 0) &&
  667. (strcmp(value, "aes192") != 0) &&
  668. (strcmp(value, "aes128") != 0)) {
  669. *error_string = "Invalid cipher type. "
  670. "Should be none, aes256, aes192 or aes128";
  671. return (0);
  672. }
  673. }
  674. if (strcmp(path, "totem.crypto_hash") == 0) {
  675. if ((strcmp(value, "none") != 0) &&
  676. (strcmp(value, "md5") != 0) &&
  677. (strcmp(value, "sha1") != 0) &&
  678. (strcmp(value, "sha256") != 0) &&
  679. (strcmp(value, "sha384") != 0) &&
  680. (strcmp(value, "sha512") != 0)) {
  681. *error_string = "Invalid hash type. "
  682. "Should be none, md5, sha1, sha256, sha384 or sha512";
  683. return (0);
  684. }
  685. }
  686. break;
  687. case MAIN_CP_CB_DATA_STATE_SYSTEM:
  688. if (strcmp(path, "system.qb_ipc_type") == 0) {
  689. if ((strcmp(value, "native") != 0) &&
  690. (strcmp(value, "shm") != 0) &&
  691. (strcmp(value, "socket") != 0)) {
  692. *error_string = "Invalid system.qb_ipc_type";
  693. return (0);
  694. }
  695. }
  696. if (strcmp(path, "system.sched_rr") == 0) {
  697. if ((strcmp(value, "yes") != 0) &&
  698. (strcmp(value, "no") != 0)) {
  699. *error_string = "Invalid system.sched_rr value";
  700. return (0);
  701. }
  702. }
  703. if (strcmp(path, "system.move_to_root_cgroup") == 0) {
  704. if ((strcmp(value, "yes") != 0) &&
  705. (strcmp(value, "no") != 0)) {
  706. *error_string = "Invalid system.move_to_root_cgroup";
  707. return (0);
  708. }
  709. }
  710. break;
  711. case MAIN_CP_CB_DATA_STATE_INTERFACE:
  712. if (strcmp(path, "totem.interface.linknumber") == 0) {
  713. val_type = ICMAP_VALUETYPE_UINT8;
  714. if (safe_atoq(value, &val, val_type) != 0) {
  715. goto atoi_error;
  716. }
  717. data->linknumber = val;
  718. add_as_string = 0;
  719. }
  720. if (strcmp(path, "totem.interface.bindnetaddr") == 0) {
  721. data->bindnetaddr = strdup(value);
  722. add_as_string = 0;
  723. }
  724. if (strcmp(path, "totem.interface.mcastaddr") == 0) {
  725. data->mcastaddr = strdup(value);
  726. add_as_string = 0;
  727. }
  728. if (strcmp(path, "totem.interface.broadcast") == 0) {
  729. data->broadcast = strdup(value);
  730. add_as_string = 0;
  731. }
  732. if (strcmp(path, "totem.interface.mcastport") == 0) {
  733. val_type = ICMAP_VALUETYPE_UINT16;
  734. if (safe_atoq(value, &val, val_type) != 0) {
  735. goto atoi_error;
  736. }
  737. data->mcastport = val;
  738. add_as_string = 0;
  739. }
  740. if (strcmp(path, "totem.interface.ttl") == 0) {
  741. val_type = ICMAP_VALUETYPE_UINT8;
  742. if (safe_atoq(value, &val, val_type) != 0) {
  743. goto atoi_error;
  744. }
  745. data->ttl = val;
  746. add_as_string = 0;
  747. }
  748. if (strcmp(path, "totem.interface.knet_link_priority") == 0) {
  749. val_type = ICMAP_VALUETYPE_UINT8;
  750. if (safe_atoq(value, &val, val_type) != 0) {
  751. goto atoi_error;
  752. }
  753. data->knet_link_priority = val;
  754. add_as_string = 0;
  755. }
  756. if (strcmp(path, "totem.interface.knet_ping_interval") == 0) {
  757. val_type = ICMAP_VALUETYPE_UINT32;
  758. if (safe_atoq(value, &val, val_type) != 0) {
  759. goto atoi_error;
  760. }
  761. data->knet_ping_interval = val;
  762. add_as_string = 0;
  763. }
  764. if (strcmp(path, "totem.interface.knet_ping_timeout") == 0) {
  765. val_type = ICMAP_VALUETYPE_UINT32;
  766. if (safe_atoq(value, &val, val_type) != 0) {
  767. goto atoi_error;
  768. }
  769. data->knet_ping_timeout = val;
  770. add_as_string = 0;
  771. }
  772. if (strcmp(path, "totem.interface.knet_ping_precision") == 0) {
  773. val_type = ICMAP_VALUETYPE_UINT32;
  774. if (safe_atoq(value, &val, val_type) != 0) {
  775. goto atoi_error;
  776. }
  777. data->knet_ping_precision = val;
  778. add_as_string = 0;
  779. }
  780. if (strcmp(path, "totem.interface.knet_pong_count") == 0) {
  781. val_type = ICMAP_VALUETYPE_UINT32;
  782. if (safe_atoq(value, &val, val_type) != 0) {
  783. goto atoi_error;
  784. }
  785. data->knet_pong_count = val;
  786. add_as_string = 0;
  787. }
  788. if (strcmp(path, "totem.interface.knet_transport") == 0) {
  789. val_type = ICMAP_VALUETYPE_STRING;
  790. data->knet_transport = strdup(value);
  791. add_as_string = 0;
  792. }
  793. break;
  794. case MAIN_CP_CB_DATA_STATE_LOGGER_SUBSYS:
  795. if (strcmp(key, "subsys") == 0) {
  796. data->subsys = strdup(value);
  797. if (data->subsys == NULL) {
  798. *error_string = "Can't alloc memory";
  799. return (0);
  800. }
  801. } else {
  802. kv_item = malloc(sizeof(*kv_item));
  803. if (kv_item == NULL) {
  804. *error_string = "Can't alloc memory";
  805. return (0);
  806. }
  807. memset(kv_item, 0, sizeof(*kv_item));
  808. kv_item->key = strdup(key);
  809. kv_item->value = strdup(value);
  810. if (kv_item->key == NULL || kv_item->value == NULL) {
  811. free(kv_item);
  812. *error_string = "Can't alloc memory";
  813. return (0);
  814. }
  815. qb_list_init(&kv_item->list);
  816. qb_list_add(&kv_item->list, &data->logger_subsys_items_head);
  817. }
  818. add_as_string = 0;
  819. break;
  820. case MAIN_CP_CB_DATA_STATE_LOGGING_DAEMON:
  821. if (strcmp(key, "subsys") == 0) {
  822. data->subsys = strdup(value);
  823. if (data->subsys == NULL) {
  824. *error_string = "Can't alloc memory";
  825. return (0);
  826. }
  827. } else if (strcmp(key, "name") == 0) {
  828. data->logging_daemon_name = strdup(value);
  829. if (data->logging_daemon_name == NULL) {
  830. *error_string = "Can't alloc memory";
  831. return (0);
  832. }
  833. } else {
  834. kv_item = malloc(sizeof(*kv_item));
  835. if (kv_item == NULL) {
  836. *error_string = "Can't alloc memory";
  837. return (0);
  838. }
  839. memset(kv_item, 0, sizeof(*kv_item));
  840. kv_item->key = strdup(key);
  841. kv_item->value = strdup(value);
  842. if (kv_item->key == NULL || kv_item->value == NULL) {
  843. free(kv_item);
  844. *error_string = "Can't alloc memory";
  845. return (0);
  846. }
  847. qb_list_init(&kv_item->list);
  848. qb_list_add(&kv_item->list, &data->logger_subsys_items_head);
  849. }
  850. add_as_string = 0;
  851. break;
  852. case MAIN_CP_CB_DATA_STATE_UIDGID:
  853. if (strcmp(key, "uid") == 0) {
  854. uid = uid_determine(value);
  855. if (uid == -1) {
  856. *error_string = error_string_response;
  857. return (0);
  858. }
  859. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "uidgid.config.uid.%u",
  860. uid);
  861. if ((cs_err = icmap_set_uint8_r(config_map, key_name, 1)) != CS_OK) {
  862. goto icmap_set_error;
  863. }
  864. add_as_string = 0;
  865. } else if (strcmp(key, "gid") == 0) {
  866. gid = gid_determine(value);
  867. if (gid == -1) {
  868. *error_string = error_string_response;
  869. return (0);
  870. }
  871. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "uidgid.config.gid.%u",
  872. gid);
  873. if ((cs_err = icmap_set_uint8_r(config_map, key_name, 1)) != CS_OK) {
  874. goto icmap_set_error;
  875. }
  876. add_as_string = 0;
  877. } else {
  878. *error_string = "uidgid: Only uid and gid are allowed items";
  879. return (0);
  880. }
  881. break;
  882. case MAIN_CP_CB_DATA_STATE_MEMBER:
  883. if (strcmp(key, "memberaddr") != 0) {
  884. *error_string = "Only memberaddr is allowed in member section";
  885. return (0);
  886. }
  887. kv_item = malloc(sizeof(*kv_item));
  888. if (kv_item == NULL) {
  889. *error_string = "Can't alloc memory";
  890. return (0);
  891. }
  892. memset(kv_item, 0, sizeof(*kv_item));
  893. kv_item->key = strdup(key);
  894. kv_item->value = strdup(value);
  895. if (kv_item->key == NULL || kv_item->value == NULL) {
  896. free(kv_item);
  897. *error_string = "Can't alloc memory";
  898. return (0);
  899. }
  900. qb_list_init(&kv_item->list);
  901. qb_list_add(&kv_item->list, &data->member_items_head);
  902. add_as_string = 0;
  903. break;
  904. case MAIN_CP_CB_DATA_STATE_NODELIST:
  905. break;
  906. case MAIN_CP_CB_DATA_STATE_NODELIST_NODE:
  907. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.%s", data->node_number, key);
  908. if ((strcmp(key, "nodeid") == 0) ||
  909. (strcmp(key, "quorum_votes") == 0)) {
  910. val_type = ICMAP_VALUETYPE_UINT32;
  911. if (safe_atoq(value, &val, val_type) != 0) {
  912. goto atoi_error;
  913. }
  914. if ((cs_err = icmap_set_uint32_r(config_map, key_name, val)) != CS_OK) {
  915. goto icmap_set_error;
  916. }
  917. add_as_string = 0;
  918. }
  919. if (add_as_string) {
  920. if ((cs_err = icmap_set_string_r(config_map, key_name, value)) != CS_OK) {
  921. goto icmap_set_error;
  922. };
  923. add_as_string = 0;
  924. }
  925. break;
  926. case MAIN_CP_CB_DATA_STATE_RESOURCES:
  927. if (strcmp(key, "watchdog_timeout") == 0) {
  928. val_type = ICMAP_VALUETYPE_UINT32;
  929. if (safe_atoq(value, &val, val_type) != 0) {
  930. goto atoi_error;
  931. }
  932. if ((cs_err = icmap_set_uint32_r(config_map,path, val)) != CS_OK) {
  933. goto icmap_set_error;
  934. }
  935. add_as_string = 0;
  936. }
  937. break;
  938. case MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM:
  939. case MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM_MEMUSED:
  940. if (strcmp(key, "poll_period") == 0) {
  941. if (str_to_ull(value, &ull) != 0) {
  942. goto atoi_error;
  943. }
  944. if ((cs_err = icmap_set_uint64_r(config_map,path, ull)) != CS_OK) {
  945. goto icmap_set_error;
  946. }
  947. add_as_string = 0;
  948. }
  949. break;
  950. case MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS:
  951. case MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS_MEMUSED:
  952. if (strcmp(key, "poll_period") == 0) {
  953. if (str_to_ull(value, &ull) != 0) {
  954. goto atoi_error;
  955. }
  956. if ((cs_err = icmap_set_uint64_r(config_map,path, ull)) != CS_OK) {
  957. goto icmap_set_error;
  958. }
  959. add_as_string = 0;
  960. }
  961. break;
  962. }
  963. if (add_as_string) {
  964. if ((cs_err = icmap_set_string_r(config_map, path, value)) != CS_OK) {
  965. goto icmap_set_error;
  966. }
  967. }
  968. break;
  969. case PARSER_CB_SECTION_START:
  970. if (strcmp(path, "totem.interface") == 0) {
  971. *state = MAIN_CP_CB_DATA_STATE_INTERFACE;
  972. data->linknumber = 0;
  973. data->mcastport = -1;
  974. data->ttl = -1;
  975. data->knet_link_priority = -1;
  976. data->knet_ping_interval = -1;
  977. data->knet_ping_timeout = -1;
  978. data->knet_ping_precision = -1;
  979. data->knet_pong_count = -1;
  980. data->knet_transport = NULL;
  981. qb_list_init(&data->member_items_head);
  982. };
  983. if (strcmp(path, "totem") == 0) {
  984. *state = MAIN_CP_CB_DATA_STATE_TOTEM;
  985. };
  986. if (strcmp(path, "system") == 0) {
  987. *state = MAIN_CP_CB_DATA_STATE_SYSTEM;
  988. }
  989. if (strcmp(path, "logging.logger_subsys") == 0) {
  990. *state = MAIN_CP_CB_DATA_STATE_LOGGER_SUBSYS;
  991. qb_list_init(&data->logger_subsys_items_head);
  992. data->subsys = NULL;
  993. }
  994. if (strcmp(path, "logging.logging_daemon") == 0) {
  995. *state = MAIN_CP_CB_DATA_STATE_LOGGING_DAEMON;
  996. qb_list_init(&data->logger_subsys_items_head);
  997. data->subsys = NULL;
  998. data->logging_daemon_name = NULL;
  999. }
  1000. if (strcmp(path, "uidgid") == 0) {
  1001. *state = MAIN_CP_CB_DATA_STATE_UIDGID;
  1002. }
  1003. if (strcmp(path, "totem.interface.member") == 0) {
  1004. *state = MAIN_CP_CB_DATA_STATE_MEMBER;
  1005. }
  1006. if (strcmp(path, "quorum") == 0) {
  1007. *state = MAIN_CP_CB_DATA_STATE_QUORUM;
  1008. }
  1009. if (strcmp(path, "quorum.device") == 0) {
  1010. *state = MAIN_CP_CB_DATA_STATE_QDEVICE;
  1011. }
  1012. if (strcmp(path, "nodelist") == 0) {
  1013. *state = MAIN_CP_CB_DATA_STATE_NODELIST;
  1014. data->node_number = 0;
  1015. }
  1016. if (strcmp(path, "nodelist.node") == 0) {
  1017. *state = MAIN_CP_CB_DATA_STATE_NODELIST_NODE;
  1018. }
  1019. if (strcmp(path, "resources") == 0) {
  1020. *state = MAIN_CP_CB_DATA_STATE_RESOURCES;
  1021. }
  1022. if (strcmp(path, "resources.system") == 0) {
  1023. *state = MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM;
  1024. }
  1025. if (strcmp(path, "resources.system.memory_used") == 0) {
  1026. *state = MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM_MEMUSED;
  1027. }
  1028. if (strcmp(path, "resources.process") == 0) {
  1029. *state = MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS;
  1030. }
  1031. if (strcmp(path, "resources.process.memory_used") == 0) {
  1032. *state = MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS_MEMUSED;
  1033. }
  1034. break;
  1035. case PARSER_CB_SECTION_END:
  1036. switch (*state) {
  1037. case MAIN_CP_CB_DATA_STATE_INTERFACE:
  1038. /*
  1039. * Create new interface section
  1040. */
  1041. if (data->bindnetaddr != NULL) {
  1042. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.bindnetaddr",
  1043. data->linknumber);
  1044. cs_err = icmap_set_string_r(config_map, key_name, data->bindnetaddr);
  1045. free(data->bindnetaddr);
  1046. data->bindnetaddr = NULL;
  1047. if (cs_err != CS_OK) {
  1048. goto icmap_set_error;
  1049. }
  1050. }
  1051. if (data->mcastaddr != NULL) {
  1052. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr",
  1053. data->linknumber);
  1054. cs_err = icmap_set_string_r(config_map, key_name, data->mcastaddr);
  1055. free(data->mcastaddr);
  1056. data->mcastaddr = NULL;
  1057. if (cs_err != CS_OK) {
  1058. goto icmap_set_error;
  1059. }
  1060. }
  1061. if (data->broadcast != NULL) {
  1062. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.broadcast",
  1063. data->linknumber);
  1064. cs_err = icmap_set_string_r(config_map, key_name, data->broadcast);
  1065. free(data->broadcast);
  1066. data->broadcast = NULL;
  1067. if (cs_err != CS_OK) {
  1068. goto icmap_set_error;
  1069. }
  1070. }
  1071. if (data->mcastport > -1) {
  1072. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport",
  1073. data->linknumber);
  1074. if ((cs_err = icmap_set_uint16_r(config_map, key_name,
  1075. data->mcastport)) != CS_OK) {
  1076. goto icmap_set_error;
  1077. }
  1078. }
  1079. if (data->ttl > -1) {
  1080. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.ttl",
  1081. data->linknumber);
  1082. if ((cs_err = icmap_set_uint8_r(config_map, key_name, data->ttl)) != CS_OK) {
  1083. goto icmap_set_error;
  1084. }
  1085. }
  1086. if (data->knet_link_priority > -1) {
  1087. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_link_priority",
  1088. data->linknumber);
  1089. if ((cs_err = icmap_set_uint8_r(config_map, key_name,
  1090. data->knet_link_priority)) != CS_OK) {
  1091. goto icmap_set_error;
  1092. }
  1093. }
  1094. if (data->knet_ping_interval > -1) {
  1095. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_interval",
  1096. data->linknumber);
  1097. if ((cs_err = icmap_set_uint32_r(config_map, key_name,
  1098. data->knet_ping_interval)) != CS_OK) {
  1099. goto icmap_set_error;
  1100. }
  1101. }
  1102. if (data->knet_ping_timeout > -1) {
  1103. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_timeout",
  1104. data->linknumber);
  1105. if ((cs_err = icmap_set_uint32_r(config_map, key_name,
  1106. data->knet_ping_timeout)) != CS_OK) {
  1107. goto icmap_set_error;
  1108. }
  1109. }
  1110. if (data->knet_ping_precision > -1) {
  1111. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_precision",
  1112. data->linknumber);
  1113. if ((cs_err = icmap_set_uint32_r(config_map, key_name,
  1114. data->knet_ping_precision)) != CS_OK) {
  1115. goto icmap_set_error;
  1116. }
  1117. }
  1118. if (data->knet_pong_count > -1) {
  1119. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_pong_count",
  1120. data->linknumber);
  1121. if ((cs_err = icmap_set_uint32_r(config_map, key_name,
  1122. data->knet_pong_count)) != CS_OK) {
  1123. goto icmap_set_error;
  1124. }
  1125. }
  1126. if (data->knet_transport) {
  1127. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_transport",
  1128. data->linknumber);
  1129. cs_err = icmap_set_string_r(config_map, key_name, data->knet_transport);
  1130. free(data->knet_transport);
  1131. if (cs_err != CS_OK) {
  1132. goto icmap_set_error;
  1133. }
  1134. }
  1135. ii = 0;
  1136. qb_list_for_each_safe(iter, tmp_iter, &(data->member_items_head)) {
  1137. kv_item = qb_list_entry(iter, struct key_value_list_item, list);
  1138. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.member.%u",
  1139. data->linknumber, ii);
  1140. cs_err = icmap_set_string_r(config_map, key_name, kv_item->value);
  1141. free(kv_item->value);
  1142. free(kv_item->key);
  1143. free(kv_item);
  1144. ii++;
  1145. if (cs_err != CS_OK) {
  1146. goto icmap_set_error;
  1147. }
  1148. }
  1149. break;
  1150. case MAIN_CP_CB_DATA_STATE_LOGGER_SUBSYS:
  1151. if (data->subsys == NULL) {
  1152. *error_string = "No subsys key in logger_subsys directive";
  1153. return (0);
  1154. }
  1155. qb_list_for_each_safe(iter, tmp_iter, &(data->logger_subsys_items_head)) {
  1156. kv_item = qb_list_entry(iter, struct key_value_list_item, list);
  1157. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logger_subsys.%s.%s",
  1158. data->subsys, kv_item->key);
  1159. cs_err = icmap_set_string_r(config_map, key_name, kv_item->value);
  1160. free(kv_item->value);
  1161. free(kv_item->key);
  1162. free(kv_item);
  1163. if (cs_err != CS_OK) {
  1164. goto icmap_set_error;
  1165. }
  1166. }
  1167. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logger_subsys.%s.subsys",
  1168. data->subsys);
  1169. cs_err = icmap_set_string_r(config_map, key_name, data->subsys);
  1170. free(data->subsys);
  1171. if (cs_err != CS_OK) {
  1172. goto icmap_set_error;
  1173. }
  1174. break;
  1175. case MAIN_CP_CB_DATA_STATE_LOGGING_DAEMON:
  1176. if (data->logging_daemon_name == NULL) {
  1177. *error_string = "No name key in logging_daemon directive";
  1178. return (0);
  1179. }
  1180. qb_list_for_each_safe(iter, tmp_iter, &(data->logger_subsys_items_head)) {
  1181. kv_item = qb_list_entry(iter, struct key_value_list_item, list);
  1182. if (data->subsys == NULL) {
  1183. if (strcmp(data->logging_daemon_name, "corosync") == 0) {
  1184. snprintf(key_name, ICMAP_KEYNAME_MAXLEN,
  1185. "logging.%s",
  1186. kv_item->key);
  1187. } else {
  1188. snprintf(key_name, ICMAP_KEYNAME_MAXLEN,
  1189. "logging.logging_daemon.%s.%s",
  1190. data->logging_daemon_name, kv_item->key);
  1191. }
  1192. } else {
  1193. if (strcmp(data->logging_daemon_name, "corosync") == 0) {
  1194. snprintf(key_name, ICMAP_KEYNAME_MAXLEN,
  1195. "logging.logger_subsys.%s.%s",
  1196. data->subsys,
  1197. kv_item->key);
  1198. } else {
  1199. snprintf(key_name, ICMAP_KEYNAME_MAXLEN,
  1200. "logging.logging_daemon.%s.%s.%s",
  1201. data->logging_daemon_name, data->subsys,
  1202. kv_item->key);
  1203. }
  1204. }
  1205. cs_err = icmap_set_string_r(config_map, key_name, kv_item->value);
  1206. free(kv_item->value);
  1207. free(kv_item->key);
  1208. free(kv_item);
  1209. if (cs_err != CS_OK) {
  1210. goto icmap_set_error;
  1211. }
  1212. }
  1213. if (data->subsys == NULL) {
  1214. if (strcmp(data->logging_daemon_name, "corosync") != 0) {
  1215. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logging_daemon.%s.name",
  1216. data->logging_daemon_name);
  1217. cs_err = icmap_set_string_r(config_map, key_name, data->logging_daemon_name);
  1218. }
  1219. } else {
  1220. if (strcmp(data->logging_daemon_name, "corosync") == 0) {
  1221. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logger_subsys.%s.subsys",
  1222. data->subsys);
  1223. cs_err = icmap_set_string_r(config_map, key_name, data->subsys);
  1224. } else {
  1225. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logging_daemon.%s.%s.subsys",
  1226. data->logging_daemon_name, data->subsys);
  1227. cs_err = icmap_set_string_r(config_map, key_name, data->subsys);
  1228. if (cs_err != CS_OK) {
  1229. free(data->subsys);
  1230. free(data->logging_daemon_name);
  1231. goto icmap_set_error;
  1232. }
  1233. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "logging.logging_daemon.%s.%s.name",
  1234. data->logging_daemon_name, data->subsys);
  1235. cs_err = icmap_set_string_r(config_map, key_name, data->logging_daemon_name);
  1236. }
  1237. }
  1238. free(data->subsys);
  1239. free(data->logging_daemon_name);
  1240. if (cs_err != CS_OK) {
  1241. goto icmap_set_error;
  1242. }
  1243. break;
  1244. case MAIN_CP_CB_DATA_STATE_NODELIST_NODE:
  1245. data->node_number++;
  1246. break;
  1247. case MAIN_CP_CB_DATA_STATE_NORMAL:
  1248. case MAIN_CP_CB_DATA_STATE_PLOAD:
  1249. case MAIN_CP_CB_DATA_STATE_UIDGID:
  1250. case MAIN_CP_CB_DATA_STATE_MEMBER:
  1251. case MAIN_CP_CB_DATA_STATE_QUORUM:
  1252. case MAIN_CP_CB_DATA_STATE_QDEVICE:
  1253. case MAIN_CP_CB_DATA_STATE_NODELIST:
  1254. case MAIN_CP_CB_DATA_STATE_TOTEM:
  1255. case MAIN_CP_CB_DATA_STATE_SYSTEM:
  1256. break;
  1257. case MAIN_CP_CB_DATA_STATE_RESOURCES:
  1258. *state = MAIN_CP_CB_DATA_STATE_NORMAL;
  1259. break;
  1260. case MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM:
  1261. *state = MAIN_CP_CB_DATA_STATE_RESOURCES;
  1262. break;
  1263. case MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM_MEMUSED:
  1264. *state = MAIN_CP_CB_DATA_STATE_RESOURCES_SYSTEM;
  1265. break;
  1266. case MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS:
  1267. *state = MAIN_CP_CB_DATA_STATE_RESOURCES;
  1268. break;
  1269. case MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS_MEMUSED:
  1270. *state = MAIN_CP_CB_DATA_STATE_RESOURCES_PROCESS;
  1271. break;
  1272. }
  1273. break;
  1274. }
  1275. return (1);
  1276. atoi_error:
  1277. min_val = max_val = 0;
  1278. /*
  1279. * This is really assert, because developer ether doesn't set val_type correctly or
  1280. * we've got here after some nasty memory overwrite
  1281. */
  1282. assert(safe_atoq_range(val_type, &min_val, &max_val) == 0);
  1283. if (snprintf(formated_err, sizeof(formated_err),
  1284. "Value of key \"%s\" is expected to be integer in range (%lld..%lld), but \"%s\" was given",
  1285. key_name, min_val, max_val, value) >= sizeof(formated_err)) {
  1286. *error_string = "Can't format parser error message";
  1287. } else {
  1288. *error_string = formated_err;
  1289. }
  1290. return (0);
  1291. icmap_set_error:
  1292. if (snprintf(formated_err, sizeof(formated_err),
  1293. "Can't store key \"%s\" into icmap, returned error is %s",
  1294. key_name, cs_strerror(cs_err)) >= sizeof(formated_err)) {
  1295. *error_string = "Can't format parser error message";
  1296. } else {
  1297. *error_string = formated_err;
  1298. }
  1299. return (0);
  1300. }
  1301. static int uidgid_config_parser_cb(const char *path,
  1302. char *key,
  1303. char *value,
  1304. enum main_cp_cb_data_state *state,
  1305. enum parser_cb_type type,
  1306. const char **error_string,
  1307. icmap_map_t config_map,
  1308. void *user_data)
  1309. {
  1310. char key_name[ICMAP_KEYNAME_MAXLEN];
  1311. int uid, gid;
  1312. static char formated_err[256];
  1313. cs_error_t cs_err;
  1314. switch (type) {
  1315. case PARSER_CB_START:
  1316. break;
  1317. case PARSER_CB_END:
  1318. break;
  1319. case PARSER_CB_ITEM:
  1320. if (strcmp(path, "uidgid.uid") == 0) {
  1321. uid = uid_determine(value);
  1322. if (uid == -1) {
  1323. *error_string = error_string_response;
  1324. return (0);
  1325. }
  1326. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "uidgid.config.uid.%u",
  1327. uid);
  1328. if ((cs_err = icmap_set_uint8_r(config_map, key_name, 1)) != CS_OK) {
  1329. goto icmap_set_error;
  1330. }
  1331. } else if (strcmp(path, "uidgid.gid") == 0) {
  1332. gid = gid_determine(value);
  1333. if (gid == -1) {
  1334. *error_string = error_string_response;
  1335. return (0);
  1336. }
  1337. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "uidgid.config.gid.%u",
  1338. gid);
  1339. if ((cs_err = icmap_set_uint8_r(config_map, key_name, 1)) != CS_OK) {
  1340. goto icmap_set_error;
  1341. }
  1342. } else {
  1343. *error_string = "uidgid: Only uid and gid are allowed items";
  1344. return (0);
  1345. }
  1346. break;
  1347. case PARSER_CB_SECTION_START:
  1348. if (strcmp(path, "uidgid") != 0) {
  1349. *error_string = "uidgid: Can't add subsection different than uidgid";
  1350. return (0);
  1351. };
  1352. break;
  1353. case PARSER_CB_SECTION_END:
  1354. break;
  1355. }
  1356. return (1);
  1357. icmap_set_error:
  1358. if (snprintf(formated_err, sizeof(formated_err),
  1359. "Can't store key \"%s\" into icmap, returned error is %s",
  1360. key_name, cs_strerror(cs_err)) >= sizeof(formated_err)) {
  1361. *error_string = "Can't format parser error message";
  1362. } else {
  1363. *error_string = formated_err;
  1364. }
  1365. return (0);
  1366. }
  1367. static int read_uidgid_files_into_icmap(
  1368. const char **error_string,
  1369. icmap_map_t config_map)
  1370. {
  1371. FILE *fp;
  1372. char *dirname_res;
  1373. DIR *dp;
  1374. struct dirent *dirent;
  1375. char filename[PATH_MAX + FILENAME_MAX + 1];
  1376. char uidgid_dirname[PATH_MAX + FILENAME_MAX + 1];
  1377. int res = 0;
  1378. struct stat stat_buf;
  1379. enum main_cp_cb_data_state state = MAIN_CP_CB_DATA_STATE_NORMAL;
  1380. char key_name[ICMAP_KEYNAME_MAXLEN];
  1381. int line_no;
  1382. /*
  1383. * Build uidgid directory based on corosync.conf file location
  1384. */
  1385. res = snprintf(filename, sizeof(filename), "%s",
  1386. corosync_get_config_file());
  1387. if (res >= sizeof(filename)) {
  1388. *error_string = "uidgid.d path too long";
  1389. return (-1);
  1390. }
  1391. dirname_res = dirname(filename);
  1392. res = snprintf(uidgid_dirname, sizeof(uidgid_dirname), "%s/%s",
  1393. dirname_res, "uidgid.d");
  1394. if (res >= sizeof(uidgid_dirname)) {
  1395. *error_string = "uidgid.d path too long";
  1396. return (-1);
  1397. }
  1398. dp = opendir (uidgid_dirname);
  1399. if (dp == NULL)
  1400. return 0;
  1401. for (dirent = readdir(dp);
  1402. dirent != NULL;
  1403. dirent = readdir(dp)) {
  1404. res = snprintf(filename, sizeof (filename), "%s/%s", uidgid_dirname, dirent->d_name);
  1405. if (res >= sizeof(filename)) {
  1406. res = -1;
  1407. *error_string = "uidgid.d dirname path too long";
  1408. goto error_exit;
  1409. }
  1410. res = stat (filename, &stat_buf);
  1411. if (res == 0 && S_ISREG(stat_buf.st_mode)) {
  1412. fp = fopen (filename, "r");
  1413. if (fp == NULL) continue;
  1414. key_name[0] = 0;
  1415. line_no = 0;
  1416. res = parse_section(fp, filename, &line_no, key_name, error_string, 0, state,
  1417. uidgid_config_parser_cb, config_map, NULL);
  1418. fclose (fp);
  1419. if (res != 0) {
  1420. goto error_exit;
  1421. }
  1422. }
  1423. }
  1424. error_exit:
  1425. closedir(dp);
  1426. return res;
  1427. }
  1428. /* Read config file and load into icmap */
  1429. static int read_config_file_into_icmap(
  1430. const char **error_string,
  1431. icmap_map_t config_map)
  1432. {
  1433. FILE *fp;
  1434. const char *filename;
  1435. char *error_reason = error_string_response;
  1436. int res;
  1437. char key_name[ICMAP_KEYNAME_MAXLEN];
  1438. struct main_cp_cb_data data;
  1439. enum main_cp_cb_data_state state = MAIN_CP_CB_DATA_STATE_NORMAL;
  1440. int line_no;
  1441. filename = corosync_get_config_file();
  1442. fp = fopen (filename, "r");
  1443. if (fp == NULL) {
  1444. char error_str[100];
  1445. const char *error_ptr = qb_strerror_r(errno, error_str, sizeof(error_str));
  1446. snprintf (error_reason, sizeof(error_string_response),
  1447. "Can't read file %s: %s",
  1448. filename, error_ptr);
  1449. *error_string = error_reason;
  1450. return -1;
  1451. }
  1452. key_name[0] = 0;
  1453. line_no = 0;
  1454. res = parse_section(fp, filename, &line_no, key_name, error_string, 0, state,
  1455. main_config_parser_cb, config_map, &data);
  1456. fclose(fp);
  1457. if (res == 0) {
  1458. res = read_uidgid_files_into_icmap(error_string, config_map);
  1459. }
  1460. if (res == 0) {
  1461. snprintf (error_reason, sizeof(error_string_response),
  1462. "Successfully read main configuration file '%s'.", filename);
  1463. *error_string = error_reason;
  1464. }
  1465. return res;
  1466. }