4
0

mainconfig.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@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 <stdio.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include <errno.h>
  40. #include <unistd.h>
  41. #include <sys/socket.h>
  42. #include <netinet/in.h>
  43. #include <arpa/inet.h>
  44. #include <pwd.h>
  45. #include <grp.h>
  46. #include <limits.h>
  47. #include <corosync/corotypes.h>
  48. #include <corosync/list.h>
  49. #include <corosync/totem/totem.h>
  50. #include <corosync/engine/logsys.h>
  51. #include "util.h"
  52. #include "mainconfig.h"
  53. static char error_string_response[512];
  54. static struct objdb_iface_ver0 *global_objdb;
  55. DECLARE_LIST_INIT(uidgid_list_head);
  56. /* This just makes the code below a little neater */
  57. static inline int objdb_get_string (
  58. const struct objdb_iface_ver0 *objdb,
  59. hdb_handle_t object_service_handle,
  60. const char *key, char **value)
  61. {
  62. int res;
  63. *value = NULL;
  64. if ( !(res = objdb->object_key_get (object_service_handle,
  65. key,
  66. strlen (key),
  67. (void *)value,
  68. NULL))) {
  69. if (*value) {
  70. return 0;
  71. }
  72. }
  73. return -1;
  74. }
  75. static inline void objdb_get_int (
  76. const struct objdb_iface_ver0 *objdb,
  77. hdb_handle_t object_service_handle,
  78. char *key, unsigned int *intvalue)
  79. {
  80. char *value = NULL;
  81. if (!objdb->object_key_get (object_service_handle,
  82. key,
  83. strlen (key),
  84. (void *)&value,
  85. NULL)) {
  86. if (value) {
  87. *intvalue = atoi(value);
  88. }
  89. }
  90. }
  91. /**
  92. * insert_into_buffer
  93. * @target_buffer: a buffer where to write results
  94. * @bufferlen: tell us the size of the buffer to avoid overflows
  95. * @entry: entry that needs to be added to the buffer
  96. * @after: can either be NULL or set to a string.
  97. * if NULL, @entry is prependend to logsys_format_get buffer.
  98. * if set, @entry is added immediately after @after.
  99. *
  100. * Since the function is specific to logsys_format_get handling, it is implicit
  101. * that source is logsys_format_get();
  102. *
  103. * In case of failure, target_buffer could be left dirty. So don't trust
  104. * any data leftover in it.
  105. *
  106. * Searching for "after" assumes that there is only entry of "after"
  107. * in the source. Afterall we control the string here and for logging format
  108. * it makes little to no sense to have duplicate format entries.
  109. *
  110. * Returns: 0 on success, -1 on failure
  111. **/
  112. static int insert_into_buffer(
  113. char *target_buffer,
  114. size_t bufferlen,
  115. const char *entry,
  116. const char *after)
  117. {
  118. const char *current_format = NULL;
  119. current_format = logsys_format_get();
  120. /* if the entry is already in the format we don't add it again */
  121. if (strstr(current_format, entry) != NULL) {
  122. return -1;
  123. }
  124. /* if there is no "after", simply prepend the requested entry
  125. * otherwise go for beautiful string manipulation.... </sarcasm> */
  126. if (!after) {
  127. if (snprintf(target_buffer, bufferlen - 1, "%s%s",
  128. entry,
  129. current_format) >= bufferlen - 1) {
  130. return -1;
  131. }
  132. } else {
  133. const char *afterpos;
  134. size_t afterlen;
  135. size_t templen;
  136. /* check if after is contained in the format
  137. * and afterlen has a meaning or return an error */
  138. afterpos = strstr(current_format, after);
  139. afterlen = strlen(after);
  140. if ((!afterpos) || (!afterlen)) {
  141. return -1;
  142. }
  143. templen = afterpos - current_format + afterlen;
  144. if (snprintf(target_buffer, templen + 1, "%s", current_format)
  145. >= bufferlen - 1) {
  146. return -1;
  147. }
  148. if (snprintf(target_buffer + templen, bufferlen - ( templen + 1 ),
  149. "%s%s", entry, current_format + templen)
  150. >= bufferlen - ( templen + 1 )) {
  151. return -1;
  152. }
  153. }
  154. return 0;
  155. }
  156. /*
  157. * format set is the only global specific option that
  158. * doesn't apply at system/subsystem level.
  159. */
  160. static int corosync_main_config_format_set (
  161. struct objdb_iface_ver0 *objdb,
  162. hdb_handle_t object_handle,
  163. const char **error_string)
  164. {
  165. const char *error_reason;
  166. char new_format_buffer[PATH_MAX];
  167. char *value;
  168. int err = 0;
  169. if (!objdb_get_string (objdb,object_handle, "fileline", &value)) {
  170. if (strcmp (value, "on") == 0) {
  171. if (!insert_into_buffer(new_format_buffer,
  172. sizeof(new_format_buffer),
  173. " %f:%l", "s]")) {
  174. err = logsys_format_set(new_format_buffer);
  175. } else
  176. if (!insert_into_buffer(new_format_buffer,
  177. sizeof(new_format_buffer),
  178. "%f:%l", NULL)) {
  179. err = logsys_format_set(new_format_buffer);
  180. }
  181. } else
  182. if (strcmp (value, "off") == 0) {
  183. /* nothing to do here */
  184. } else {
  185. error_reason = "unknown value for fileline";
  186. goto parse_error;
  187. }
  188. }
  189. if (!objdb_get_string (objdb,object_handle, "function_name", &value)) {
  190. if (strcmp (value, "on") == 0) {
  191. if (!insert_into_buffer(new_format_buffer,
  192. sizeof(new_format_buffer),
  193. "%n:", "f:")) {
  194. err = logsys_format_set(new_format_buffer);
  195. } else
  196. if (!insert_into_buffer(new_format_buffer,
  197. sizeof(new_format_buffer),
  198. " %n", "s]")) {
  199. err = logsys_format_set(new_format_buffer);
  200. }
  201. } else
  202. if (strcmp (value, "off") == 0) {
  203. /* nothing to do here */
  204. } else {
  205. error_reason = "unknown value for function_name";
  206. goto parse_error;
  207. }
  208. }
  209. if (!objdb_get_string (objdb,object_handle, "timestamp", &value)) {
  210. if (strcmp (value, "on") == 0) {
  211. if(!insert_into_buffer(new_format_buffer,
  212. sizeof(new_format_buffer),
  213. "%t ", NULL)) {
  214. err = logsys_format_set(new_format_buffer);
  215. }
  216. } else
  217. if (strcmp (value, "off") == 0) {
  218. /* nothing to do here */
  219. } else {
  220. error_reason = "unknown value for timestamp";
  221. goto parse_error;
  222. }
  223. }
  224. if (err) {
  225. error_reason = "exhausted virtual memory";
  226. goto parse_error;
  227. }
  228. return (0);
  229. parse_error:
  230. *error_string = error_reason;
  231. return (-1);
  232. }
  233. static int corosync_main_config_log_destination_set (
  234. struct objdb_iface_ver0 *objdb,
  235. hdb_handle_t object_handle,
  236. const char *subsys,
  237. const char **error_string,
  238. const char *objdb_key,
  239. unsigned int mode_mask,
  240. char deprecated,
  241. const char *replacement)
  242. {
  243. static char formatted_error_reason[128];
  244. char *value;
  245. unsigned int mode;
  246. if (!objdb_get_string (objdb, object_handle, objdb_key, &value)) {
  247. if (deprecated) {
  248. log_printf(LOGSYS_LEVEL_WARNING,
  249. "Warning: the %s config paramater has been obsoleted."
  250. " See corosync.conf man page %s directive.",
  251. objdb_key, replacement);
  252. }
  253. mode = logsys_config_mode_get (subsys);
  254. if (strcmp (value, "yes") == 0 || strcmp (value, "on") == 0) {
  255. mode |= mode_mask;
  256. if (logsys_config_mode_set(subsys, mode) < 0) {
  257. sprintf (formatted_error_reason, "unable to set mode %s", objdb_key);
  258. *error_string = formatted_error_reason;
  259. return -1;
  260. }
  261. } else
  262. if (strcmp (value, "no") == 0 || strcmp (value, "off") == 0) {
  263. mode &= ~mode_mask;
  264. if (logsys_config_mode_set(subsys, mode) < 0) {
  265. sprintf (formatted_error_reason, "unable to unset mode %s", objdb_key);
  266. *error_string = formatted_error_reason;
  267. return -1;
  268. }
  269. } else {
  270. sprintf (formatted_error_reason, "unknown value for %s", objdb_key);
  271. *error_string = formatted_error_reason;
  272. return -1;
  273. }
  274. }
  275. return 0;
  276. }
  277. static int corosync_main_config_set (
  278. struct objdb_iface_ver0 *objdb,
  279. hdb_handle_t object_handle,
  280. const char *subsys,
  281. const char **error_string)
  282. {
  283. const char *error_reason = error_string_response;
  284. char *value;
  285. int mode;
  286. /*
  287. * this bit abuses the internal logsys exported API
  288. * to guarantee that all configured subsystems are
  289. * initialized too.
  290. *
  291. * using this approach avoids some headaches caused
  292. * by IPC and TOTEM that have a special logging
  293. * handling requirements
  294. */
  295. if (subsys != NULL) {
  296. if (_logsys_subsys_create(subsys) < 0) {
  297. error_reason = "unable to create new logging subsystem";
  298. goto parse_error;
  299. }
  300. }
  301. mode = logsys_config_mode_get(subsys);
  302. if (mode < 0) {
  303. error_reason = "unable to get mode";
  304. goto parse_error;
  305. }
  306. if (corosync_main_config_log_destination_set (objdb, object_handle, subsys, &error_reason,
  307. "to_logfile", LOGSYS_MODE_OUTPUT_FILE, 0, NULL) != 0)
  308. goto parse_error;
  309. if (corosync_main_config_log_destination_set (objdb, object_handle, subsys, &error_reason,
  310. "to_stderr", LOGSYS_MODE_OUTPUT_STDERR, 0, NULL) != 0)
  311. goto parse_error;
  312. if (corosync_main_config_log_destination_set (objdb, object_handle, subsys, &error_reason,
  313. "to_syslog", LOGSYS_MODE_OUTPUT_SYSLOG, 0, NULL) != 0)
  314. goto parse_error;
  315. if (corosync_main_config_log_destination_set (objdb, object_handle, subsys, &error_reason,
  316. "to_file", LOGSYS_MODE_OUTPUT_FILE, 1, "to_logfile") != 0)
  317. goto parse_error;
  318. if (!objdb_get_string (objdb,object_handle, "syslog_facility", &value)) {
  319. int syslog_facility;
  320. syslog_facility = logsys_facility_id_get(value);
  321. if (syslog_facility < 0) {
  322. error_reason = "unknown syslog facility specified";
  323. goto parse_error;
  324. }
  325. if (logsys_config_syslog_facility_set(subsys,
  326. syslog_facility) < 0) {
  327. error_reason = "unable to set syslog facility";
  328. goto parse_error;
  329. }
  330. }
  331. if (!objdb_get_string (objdb,object_handle, "syslog_level", &value)) {
  332. int syslog_priority;
  333. log_printf(LOGSYS_LEVEL_WARNING,
  334. "Warning: the syslog_level config paramater has been obsoleted."
  335. " See corosync.conf man page syslog_priority directive.");
  336. syslog_priority = logsys_priority_id_get(value);
  337. if (syslog_priority < 0) {
  338. error_reason = "unknown syslog level specified";
  339. goto parse_error;
  340. }
  341. if (logsys_config_syslog_priority_set(subsys,
  342. syslog_priority) < 0) {
  343. error_reason = "unable to set syslog level";
  344. goto parse_error;
  345. }
  346. }
  347. if (!objdb_get_string (objdb,object_handle, "syslog_priority", &value)) {
  348. int syslog_priority;
  349. syslog_priority = logsys_priority_id_get(value);
  350. if (syslog_priority < 0) {
  351. error_reason = "unknown syslog priority specified";
  352. goto parse_error;
  353. }
  354. if (logsys_config_syslog_priority_set(subsys,
  355. syslog_priority) < 0) {
  356. error_reason = "unable to set syslog priority";
  357. goto parse_error;
  358. }
  359. }
  360. if (!objdb_get_string (objdb,object_handle, "logfile", &value)) {
  361. if (logsys_config_file_set (subsys, &error_reason, value) < 0) {
  362. goto parse_error;
  363. }
  364. }
  365. if (!objdb_get_string (objdb,object_handle, "logfile_priority", &value)) {
  366. int logfile_priority;
  367. logfile_priority = logsys_priority_id_get(value);
  368. if (logfile_priority < 0) {
  369. error_reason = "unknown logfile priority specified";
  370. goto parse_error;
  371. }
  372. if (logsys_config_logfile_priority_set(subsys,
  373. logfile_priority) < 0) {
  374. error_reason = "unable to set logfile priority";
  375. goto parse_error;
  376. }
  377. }
  378. if (!objdb_get_string (objdb, object_handle, "debug", &value)) {
  379. if (strcmp (value, "trace") == 0) {
  380. if (logsys_config_debug_set (subsys, LOGSYS_DEBUG_TRACE) < 0) {
  381. error_reason = "unable to set debug on";
  382. goto parse_error;
  383. }
  384. } else
  385. if (strcmp (value, "on") == 0) {
  386. if (logsys_config_debug_set (subsys, LOGSYS_DEBUG_ON) < 0) {
  387. error_reason = "unable to set debug on";
  388. goto parse_error;
  389. }
  390. } else
  391. if (strcmp (value, "off") == 0) {
  392. if (logsys_config_debug_set (subsys, LOGSYS_DEBUG_OFF) < 0) {
  393. error_reason = "unable to set debug off";
  394. goto parse_error;
  395. }
  396. } else {
  397. error_reason = "unknown value for debug";
  398. goto parse_error;
  399. }
  400. }
  401. return (0);
  402. parse_error:
  403. *error_string = error_reason;
  404. return (-1);
  405. }
  406. static int corosync_main_config_read_logging (
  407. struct objdb_iface_ver0 *objdb,
  408. const char **error_string)
  409. {
  410. hdb_handle_t object_service_handle;
  411. hdb_handle_t object_logger_subsys_handle;
  412. hdb_handle_t object_find_handle;
  413. hdb_handle_t object_find_logsys_handle;
  414. const char *error_reason;
  415. char *value;
  416. objdb->object_find_create (
  417. OBJECT_PARENT_HANDLE,
  418. "logging",
  419. strlen ("logging"),
  420. &object_find_handle);
  421. if (objdb->object_find_next (
  422. object_find_handle,
  423. &object_service_handle) == 0) {
  424. /* format set is supported only for toplevel */
  425. if (corosync_main_config_format_set (objdb,
  426. object_service_handle,
  427. &error_reason) < 0) {
  428. goto parse_error;
  429. }
  430. if (corosync_main_config_set (objdb,
  431. object_service_handle,
  432. NULL,
  433. &error_reason) < 0) {
  434. goto parse_error;
  435. }
  436. /* we will need 2 of these to compensate for new logging
  437. * config format */
  438. objdb->object_find_create (
  439. object_service_handle,
  440. "logger_subsys",
  441. strlen ("logger_subsys"),
  442. &object_find_logsys_handle);
  443. while (objdb->object_find_next (
  444. object_find_logsys_handle,
  445. &object_logger_subsys_handle) == 0) {
  446. if (!objdb_get_string (objdb,
  447. object_logger_subsys_handle,
  448. "subsys", &value)) {
  449. if (corosync_main_config_set (objdb,
  450. object_logger_subsys_handle,
  451. value,
  452. &error_reason) < 0) {
  453. goto parse_error;
  454. }
  455. }
  456. else {
  457. error_reason = "subsys required for logger directive";
  458. goto parse_error;
  459. }
  460. }
  461. objdb->object_find_destroy (object_find_logsys_handle);
  462. objdb->object_find_create (
  463. object_service_handle,
  464. "logging_daemon",
  465. strlen ("logging_daemon"),
  466. &object_find_logsys_handle);
  467. while (objdb->object_find_next (
  468. object_find_logsys_handle,
  469. &object_logger_subsys_handle) == 0) {
  470. if (!objdb_get_string (objdb,
  471. object_logger_subsys_handle,
  472. "name", &value)) {
  473. if (strcmp(value, "corosync") == 0) {
  474. if (!objdb_get_string (objdb,
  475. object_logger_subsys_handle,
  476. "subsys", &value)) {
  477. if (corosync_main_config_set (objdb,
  478. object_logger_subsys_handle,
  479. value,
  480. &error_reason) < 0) {
  481. goto parse_error;
  482. }
  483. }
  484. else {
  485. if (corosync_main_config_set (objdb,
  486. object_logger_subsys_handle,
  487. NULL,
  488. &error_reason) < 0) {
  489. goto parse_error;
  490. }
  491. }
  492. }
  493. }
  494. else {
  495. error_reason = "name required for logging_daemon directive";
  496. goto parse_error;
  497. }
  498. }
  499. objdb->object_find_destroy (object_find_logsys_handle);
  500. }
  501. objdb->object_find_destroy (object_find_handle);
  502. return 0;
  503. parse_error:
  504. *error_string = error_reason;
  505. return (-1);
  506. }
  507. static int uid_determine (const char *req_user)
  508. {
  509. int pw_uid = 0;
  510. struct passwd passwd;
  511. struct passwd* pwdptr = &passwd;
  512. struct passwd* temp_pwd_pt;
  513. char *pwdbuffer;
  514. int pwdlinelen, rc;
  515. long int id;
  516. char *ep;
  517. id = strtol(req_user, &ep, 10);
  518. if (*ep == '\0' && id >= 0 && id <= UINT_MAX) {
  519. return (id);
  520. }
  521. pwdlinelen = sysconf (_SC_GETPW_R_SIZE_MAX);
  522. if (pwdlinelen == -1) {
  523. pwdlinelen = 256;
  524. }
  525. pwdbuffer = malloc (pwdlinelen);
  526. while ((rc = getpwnam_r (req_user, pwdptr, pwdbuffer, pwdlinelen, &temp_pwd_pt)) == ERANGE) {
  527. char *n;
  528. pwdlinelen *= 2;
  529. if (pwdlinelen <= 32678) {
  530. n = realloc (pwdbuffer, pwdlinelen);
  531. if (n != NULL) {
  532. pwdbuffer = n;
  533. continue;
  534. }
  535. }
  536. }
  537. if (rc != 0) {
  538. free (pwdbuffer);
  539. log_printf (LOGSYS_LEVEL_ERROR, "getpwnam_r(): %s", strerror(rc));
  540. corosync_exit_error (AIS_DONE_UID_DETERMINE);
  541. }
  542. if (temp_pwd_pt == NULL) {
  543. free (pwdbuffer);
  544. log_printf (LOGSYS_LEVEL_ERROR,
  545. "The '%s' user is not found in /etc/passwd, please read the documentation.",
  546. req_user);
  547. corosync_exit_error (AIS_DONE_UID_DETERMINE);
  548. }
  549. pw_uid = passwd.pw_uid;
  550. free (pwdbuffer);
  551. return pw_uid;
  552. }
  553. static int gid_determine (const char *req_group)
  554. {
  555. int corosync_gid = 0;
  556. struct group group;
  557. struct group * grpptr = &group;
  558. struct group * temp_grp_pt;
  559. char *grpbuffer;
  560. int grplinelen, rc;
  561. long int id;
  562. char *ep;
  563. id = strtol(req_group, &ep, 10);
  564. if (*ep == '\0' && id >= 0 && id <= UINT_MAX) {
  565. return (id);
  566. }
  567. grplinelen = sysconf (_SC_GETGR_R_SIZE_MAX);
  568. if (grplinelen == -1) {
  569. grplinelen = 256;
  570. }
  571. grpbuffer = malloc (grplinelen);
  572. while ((rc = getgrnam_r (req_group, grpptr, grpbuffer, grplinelen, &temp_grp_pt)) == ERANGE) {
  573. char *n;
  574. grplinelen *= 2;
  575. if (grplinelen <= 32678) {
  576. n = realloc (grpbuffer, grplinelen);
  577. if (n != NULL) {
  578. grpbuffer = n;
  579. continue;
  580. }
  581. }
  582. }
  583. if (rc != 0) {
  584. free (grpbuffer);
  585. log_printf (LOGSYS_LEVEL_ERROR, "getgrnam_r(): %s", strerror(rc));
  586. corosync_exit_error (AIS_DONE_GID_DETERMINE);
  587. }
  588. if (temp_grp_pt == NULL) {
  589. free (grpbuffer);
  590. log_printf (LOGSYS_LEVEL_ERROR,
  591. "The '%s' group is not found in /etc/group, please read the documentation.",
  592. req_group);
  593. corosync_exit_error (AIS_DONE_GID_DETERMINE);
  594. }
  595. corosync_gid = group.gr_gid;
  596. free (grpbuffer);
  597. return corosync_gid;
  598. }
  599. static unsigned int logging_handle_find (
  600. struct objdb_iface_ver0 *objdb,
  601. hdb_handle_t *logging_find_handle)
  602. {
  603. hdb_handle_t object_find_handle;
  604. unsigned int res;
  605. objdb->object_find_create (
  606. OBJECT_PARENT_HANDLE,
  607. "logging",
  608. strlen ("logging"),
  609. &object_find_handle);
  610. res = objdb->object_find_next (
  611. object_find_handle,
  612. logging_find_handle);
  613. objdb->object_find_destroy (object_find_handle);
  614. if (res == -1) {
  615. return (-1);
  616. }
  617. return (0);
  618. }
  619. static void logsys_objdb_key_change_notify(object_change_type_t change_type,
  620. hdb_handle_t parent_object_handle,
  621. hdb_handle_t object_handle,
  622. const void *object_name_pt, size_t object_name_len,
  623. const void *key_name_pt, size_t key_len,
  624. const void *key_value_pt, size_t key_value_len,
  625. void *priv_data_pt)
  626. {
  627. const char *error_string;
  628. if (logsys_format_set(NULL) == -1) {
  629. fprintf (stderr, "Unable to setup logging format.\n");
  630. }
  631. corosync_main_config_read_logging(global_objdb,
  632. &error_string);
  633. }
  634. static void main_objdb_reload_notify(objdb_reload_notify_type_t type, int flush,
  635. void *priv_data_pt)
  636. {
  637. const char *error_string;
  638. hdb_handle_t logsys_object_handle;
  639. /*
  640. * A new logsys {} key might exist, cancel the
  641. * existing notification at the start of reload,
  642. * and start a new one on the new object when
  643. * it's all settled.
  644. */
  645. if (type == OBJDB_RELOAD_NOTIFY_START) {
  646. global_objdb->object_track_stop(
  647. logsys_objdb_key_change_notify,
  648. NULL,
  649. NULL,
  650. NULL,
  651. NULL);
  652. }
  653. if (type == OBJDB_RELOAD_NOTIFY_END || type == OBJDB_RELOAD_NOTIFY_FAILED) {
  654. if (!logging_handle_find(global_objdb, &logsys_object_handle)) {
  655. /*
  656. * Reload the logsys configuration
  657. */
  658. if (logsys_format_set(NULL) == -1) {
  659. fprintf (stderr, "Unable to setup logging format.\n");
  660. }
  661. corosync_main_config_read_logging(global_objdb,
  662. &error_string);
  663. global_objdb->object_track_start(logsys_object_handle,
  664. 1,
  665. logsys_objdb_key_change_notify,
  666. NULL, // object_create_notify,
  667. NULL, // object_destroy_notify,
  668. NULL, // object_reload_notify
  669. NULL); // priv_data
  670. } else {
  671. log_printf(LOGSYS_LEVEL_ERROR, "logsys objdb tracking stopped, cannot find logsys{} handle on objdb\n");
  672. }
  673. }
  674. }
  675. static void add_logsys_config_notification(
  676. struct objdb_iface_ver0 *objdb)
  677. {
  678. hdb_handle_t logsys_object_handle;
  679. global_objdb = objdb;
  680. if (!logging_handle_find(global_objdb, &logsys_object_handle)) {
  681. objdb->object_track_start(logsys_object_handle,
  682. 1,
  683. logsys_objdb_key_change_notify,
  684. NULL, // object_create_notify,
  685. NULL, // object_destroy_notify,
  686. NULL, // object_reload_notify
  687. NULL); // priv_data
  688. } else {
  689. log_printf(LOGSYS_LEVEL_ERROR, "logsys objdb tracking stopped, cannot find logsys{} handle on objdb\n");
  690. }
  691. objdb->object_track_start(OBJECT_PARENT_HANDLE,
  692. 1,
  693. NULL,
  694. NULL,
  695. NULL,
  696. main_objdb_reload_notify,
  697. NULL);
  698. }
  699. static int corosync_main_config_read_uidgid (
  700. struct objdb_iface_ver0 *objdb,
  701. const char **error_string)
  702. {
  703. hdb_handle_t object_find_handle;
  704. hdb_handle_t object_service_handle;
  705. char *value;
  706. int uid, gid;
  707. struct uidgid_item *ugi;
  708. objdb->object_find_create (
  709. OBJECT_PARENT_HANDLE,
  710. "uidgid",
  711. strlen ("uidgid"),
  712. &object_find_handle);
  713. while (objdb->object_find_next (
  714. object_find_handle,
  715. &object_service_handle) == 0) {
  716. uid = -1;
  717. gid = -1;
  718. if (!objdb_get_string (objdb,object_service_handle, "uid", &value)) {
  719. uid = uid_determine(value);
  720. }
  721. if (!objdb_get_string (objdb,object_service_handle, "gid", &value)) {
  722. gid = gid_determine(value);
  723. }
  724. if (uid > -1 || gid > -1) {
  725. ugi = malloc (sizeof (*ugi));
  726. if (ugi == NULL) {
  727. _corosync_out_of_memory_error();
  728. }
  729. ugi->uid = uid;
  730. ugi->gid = gid;
  731. list_init (&ugi->list);
  732. list_add (&ugi->list, &uidgid_list_head);
  733. }
  734. }
  735. objdb->object_find_destroy (object_find_handle);
  736. return 0;
  737. }
  738. int corosync_main_config_read (
  739. struct objdb_iface_ver0 *objdb,
  740. const char **error_string)
  741. {
  742. const char *error_reason = error_string_response;
  743. if (corosync_main_config_read_logging(objdb, error_string) < 0) {
  744. error_reason = *error_string;
  745. goto parse_error;
  746. }
  747. corosync_main_config_read_uidgid (objdb, error_string);
  748. add_logsys_config_notification(objdb);
  749. return 0;
  750. parse_error:
  751. snprintf (error_string_response, sizeof(error_string_response),
  752. "parse error in config: %s.\n",
  753. error_reason);
  754. *error_string = error_string_response;
  755. return (-1);
  756. }
  757. int corosync_main_config_compatibility_read (
  758. struct objdb_iface_ver0 *objdb,
  759. enum cs_sync_mode *minimum_sync_mode,
  760. const char **error_string)
  761. {
  762. const char *error_reason = error_string_response;
  763. char *value;
  764. *minimum_sync_mode = CS_SYNC_V1;
  765. if (!objdb_get_string (objdb, OBJECT_PARENT_HANDLE, "compatibility", &value)) {
  766. if (strcmp (value, "whitetank") == 0) {
  767. *minimum_sync_mode = CS_SYNC_V1;
  768. } else
  769. if (strcmp (value, "none") == 0) {
  770. *minimum_sync_mode = CS_SYNC_V2;
  771. } else {
  772. snprintf (error_string_response, sizeof (error_string_response),
  773. "Invalid compatibility option '%s' specified, must be none or whitetank.\n", value);
  774. goto parse_error;
  775. }
  776. }
  777. return 0;
  778. parse_error:
  779. *error_string = error_reason;
  780. return (-1);
  781. }