mainconfig.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2011 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. * Jan Friesse (jfriesse@redhat.com)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <config.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <stdlib.h>
  40. #include <errno.h>
  41. #include <unistd.h>
  42. #include <sys/socket.h>
  43. #include <netinet/in.h>
  44. #include <arpa/inet.h>
  45. #include <limits.h>
  46. #include <corosync/corotypes.h>
  47. #include <corosync/list.h>
  48. #include <corosync/totem/totem.h>
  49. #include <corosync/logsys.h>
  50. #include <corosync/icmap.h>
  51. #include "util.h"
  52. #include "mainconfig.h"
  53. static char error_string_response[512];
  54. /**
  55. * insert_into_buffer
  56. * @target_buffer: a buffer where to write results
  57. * @bufferlen: tell us the size of the buffer to avoid overflows
  58. * @entry: entry that needs to be added to the buffer
  59. * @after: can either be NULL or set to a string.
  60. * if NULL, @entry is prependend to logsys_format_get buffer.
  61. * if set, @entry is added immediately after @after.
  62. *
  63. * Since the function is specific to logsys_format_get handling, it is implicit
  64. * that source is logsys_format_get();
  65. *
  66. * In case of failure, target_buffer could be left dirty. So don't trust
  67. * any data leftover in it.
  68. *
  69. * Searching for "after" assumes that there is only entry of "after"
  70. * in the source. Afterall we control the string here and for logging format
  71. * it makes little to no sense to have duplicate format entries.
  72. *
  73. * Returns: 0 on success, -1 on failure
  74. **/
  75. static int insert_into_buffer(
  76. char *target_buffer,
  77. size_t bufferlen,
  78. const char *entry,
  79. const char *after)
  80. {
  81. const char *current_format = NULL;
  82. current_format = logsys_format_get();
  83. /* if the entry is already in the format we don't add it again */
  84. if (strstr(current_format, entry) != NULL) {
  85. return -1;
  86. }
  87. /* if there is no "after", simply prepend the requested entry
  88. * otherwise go for beautiful string manipulation.... </sarcasm> */
  89. if (!after) {
  90. if (snprintf(target_buffer, bufferlen - 1, "%s%s",
  91. entry,
  92. current_format) >= bufferlen - 1) {
  93. return -1;
  94. }
  95. } else {
  96. const char *afterpos;
  97. size_t afterlen;
  98. size_t templen;
  99. /* check if after is contained in the format
  100. * and afterlen has a meaning or return an error */
  101. afterpos = strstr(current_format, after);
  102. afterlen = strlen(after);
  103. if ((!afterpos) || (!afterlen)) {
  104. return -1;
  105. }
  106. templen = afterpos - current_format + afterlen;
  107. if (snprintf(target_buffer, templen + 1, "%s", current_format)
  108. >= bufferlen - 1) {
  109. return -1;
  110. }
  111. if (snprintf(target_buffer + templen, bufferlen - ( templen + 1 ),
  112. "%s%s", entry, current_format + templen)
  113. >= bufferlen - ( templen + 1 )) {
  114. return -1;
  115. }
  116. }
  117. return 0;
  118. }
  119. /*
  120. * format set is the only global specific option that
  121. * doesn't apply at system/subsystem level.
  122. */
  123. static int corosync_main_config_format_set (
  124. const char **error_string)
  125. {
  126. const char *error_reason;
  127. char new_format_buffer[PATH_MAX];
  128. char *value = NULL;
  129. int err = 0;
  130. if (icmap_get_string("logging.fileline", &value) == CS_OK) {
  131. if (strcmp (value, "on") == 0) {
  132. if (!insert_into_buffer(new_format_buffer,
  133. sizeof(new_format_buffer),
  134. " %f:%l", "g]")) {
  135. err = logsys_format_set(new_format_buffer);
  136. } else
  137. if (!insert_into_buffer(new_format_buffer,
  138. sizeof(new_format_buffer),
  139. "%f:%l", NULL)) {
  140. err = logsys_format_set(new_format_buffer);
  141. }
  142. } else
  143. if (strcmp (value, "off") == 0) {
  144. /* nothing to do here */
  145. } else {
  146. error_reason = "unknown value for fileline";
  147. goto parse_error;
  148. }
  149. free(value);
  150. }
  151. if (err) {
  152. error_reason = "not enough memory to set logging format buffer";
  153. goto parse_error;
  154. }
  155. if (icmap_get_string("logging.function_name", &value) == CS_OK) {
  156. if (strcmp (value, "on") == 0) {
  157. if (!insert_into_buffer(new_format_buffer,
  158. sizeof(new_format_buffer),
  159. "%n:", "f:")) {
  160. err = logsys_format_set(new_format_buffer);
  161. } else
  162. if (!insert_into_buffer(new_format_buffer,
  163. sizeof(new_format_buffer),
  164. " %n", "g]")) {
  165. err = logsys_format_set(new_format_buffer);
  166. }
  167. } else
  168. if (strcmp (value, "off") == 0) {
  169. /* nothing to do here */
  170. } else {
  171. error_reason = "unknown value for function_name";
  172. goto parse_error;
  173. }
  174. free(value);
  175. }
  176. if (err) {
  177. error_reason = "not enough memory to set logging format buffer";
  178. goto parse_error;
  179. }
  180. if (icmap_get_string("logging.timestamp", &value) == CS_OK) {
  181. if (strcmp (value, "on") == 0) {
  182. if(!insert_into_buffer(new_format_buffer,
  183. sizeof(new_format_buffer),
  184. "%t ", NULL)) {
  185. err = logsys_format_set(new_format_buffer);
  186. }
  187. } else
  188. if (strcmp (value, "off") == 0) {
  189. /* nothing to do here */
  190. } else {
  191. error_reason = "unknown value for timestamp";
  192. goto parse_error;
  193. }
  194. free(value);
  195. }
  196. if (err) {
  197. error_reason = "not enough memory to set logging format buffer";
  198. goto parse_error;
  199. }
  200. return (0);
  201. parse_error:
  202. free(value);
  203. *error_string = error_reason;
  204. return (-1);
  205. }
  206. static int corosync_main_config_log_destination_set (
  207. const char *path,
  208. const char *key,
  209. const char *subsys,
  210. const char **error_string,
  211. unsigned int mode_mask,
  212. char deprecated,
  213. const char *replacement)
  214. {
  215. static char formatted_error_reason[128];
  216. char *value = NULL;
  217. unsigned int mode;
  218. char key_name[ICMAP_KEYNAME_MAXLEN];
  219. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.%s", path, key);
  220. if (icmap_get_string(key_name, &value) == CS_OK) {
  221. if (deprecated) {
  222. log_printf(LOGSYS_LEVEL_WARNING,
  223. "Warning: the %s config paramater has been obsoleted."
  224. " See corosync.conf man page %s directive.",
  225. key, replacement);
  226. }
  227. mode = logsys_config_mode_get (subsys);
  228. if (strcmp (value, "yes") == 0 || strcmp (value, "on") == 0) {
  229. mode |= mode_mask;
  230. if (logsys_config_mode_set(subsys, mode) < 0) {
  231. sprintf (formatted_error_reason, "unable to set mode %s", key);
  232. goto parse_error;
  233. }
  234. } else
  235. if (strcmp (value, "no") == 0 || strcmp (value, "off") == 0) {
  236. mode &= ~mode_mask;
  237. if (logsys_config_mode_set(subsys, mode) < 0) {
  238. sprintf (formatted_error_reason, "unable to unset mode %s", key);
  239. goto parse_error;
  240. }
  241. } else {
  242. sprintf (formatted_error_reason, "unknown value for %s", key);
  243. goto parse_error;
  244. }
  245. }
  246. free(value);
  247. return (0);
  248. parse_error:
  249. *error_string = formatted_error_reason;
  250. free(value);
  251. return (-1);
  252. }
  253. static int corosync_main_config_set (
  254. const char *path,
  255. const char *subsys,
  256. const char **error_string)
  257. {
  258. const char *error_reason = error_string_response;
  259. char *value;
  260. int mode;
  261. char key_name[ICMAP_KEYNAME_MAXLEN];
  262. /*
  263. * this bit abuses the internal logsys exported API
  264. * to guarantee that all configured subsystems are
  265. * initialized too.
  266. *
  267. * using this approach avoids some headaches caused
  268. * by IPC and TOTEM that have a special logging
  269. * handling requirements
  270. */
  271. if (subsys != NULL) {
  272. if (_logsys_subsys_create(subsys, NULL) < 0) {
  273. error_reason = "unable to create new logging subsystem";
  274. goto parse_error;
  275. }
  276. }
  277. mode = logsys_config_mode_get(subsys);
  278. if (mode < 0) {
  279. error_reason = "unable to get mode";
  280. goto parse_error;
  281. }
  282. if (corosync_main_config_log_destination_set (path, "to_stderr", subsys, &error_reason,
  283. LOGSYS_MODE_OUTPUT_STDERR, 0, NULL) != 0)
  284. goto parse_error;
  285. if (corosync_main_config_log_destination_set (path, "to_syslog", subsys, &error_reason,
  286. LOGSYS_MODE_OUTPUT_SYSLOG, 0, NULL) != 0)
  287. goto parse_error;
  288. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.%s", path, "syslog_facility");
  289. if (icmap_get_string(key_name, &value) == CS_OK) {
  290. int syslog_facility;
  291. syslog_facility = qb_log_facility2int(value);
  292. if (syslog_facility < 0) {
  293. error_reason = "unknown syslog facility specified";
  294. goto parse_error;
  295. }
  296. if (logsys_config_syslog_facility_set(subsys,
  297. syslog_facility) < 0) {
  298. error_reason = "unable to set syslog facility";
  299. goto parse_error;
  300. }
  301. free(value);
  302. }
  303. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.%s", path, "syslog_level");
  304. if (icmap_get_string(key_name, &value) == CS_OK) {
  305. int syslog_priority;
  306. log_printf(LOGSYS_LEVEL_WARNING,
  307. "Warning: the syslog_level config paramater has been obsoleted."
  308. " See corosync.conf man page syslog_priority directive.");
  309. syslog_priority = logsys_priority_id_get(value);
  310. if (syslog_priority < 0) {
  311. error_reason = "unknown syslog level specified";
  312. goto parse_error;
  313. }
  314. if (logsys_config_syslog_priority_set(subsys,
  315. syslog_priority) < 0) {
  316. error_reason = "unable to set syslog level";
  317. goto parse_error;
  318. }
  319. free(value);
  320. }
  321. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.%s", path, "syslog_priority");
  322. if (icmap_get_string(key_name, &value) == CS_OK) {
  323. int syslog_priority;
  324. syslog_priority = logsys_priority_id_get(value);
  325. if (syslog_priority < 0) {
  326. error_reason = "unknown syslog priority specified";
  327. goto parse_error;
  328. }
  329. if (logsys_config_syslog_priority_set(subsys,
  330. syslog_priority) < 0) {
  331. error_reason = "unable to set syslog priority";
  332. goto parse_error;
  333. }
  334. free(value);
  335. }
  336. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.%s", path, "logfile");
  337. if (icmap_get_string(key_name, &value) == CS_OK) {
  338. if (logsys_config_file_set (subsys, error_string, value) < 0) {
  339. goto parse_error;
  340. }
  341. free(value);
  342. }
  343. if (corosync_main_config_log_destination_set (path, "to_file", subsys, &error_reason,
  344. LOGSYS_MODE_OUTPUT_FILE, 1, "to_logfile") != 0)
  345. goto parse_error;
  346. if (corosync_main_config_log_destination_set (path, "to_logfile", subsys, &error_reason,
  347. LOGSYS_MODE_OUTPUT_FILE, 0, NULL) != 0)
  348. goto parse_error;
  349. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.%s", path, "logfile_priority");
  350. if (icmap_get_string(key_name, &value) == CS_OK) {
  351. int logfile_priority;
  352. logfile_priority = logsys_priority_id_get(value);
  353. if (logfile_priority < 0) {
  354. error_reason = "unknown logfile priority specified";
  355. goto parse_error;
  356. }
  357. if (logsys_config_logfile_priority_set(subsys,
  358. logfile_priority) < 0) {
  359. error_reason = "unable to set logfile priority";
  360. goto parse_error;
  361. }
  362. free(value);
  363. }
  364. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.%s", path, "debug");
  365. if (icmap_get_string(key_name, &value) == CS_OK) {
  366. if (strcmp (value, "on") == 0) {
  367. if (logsys_config_debug_set (subsys, 1) < 0) {
  368. error_reason = "unable to set debug on";
  369. goto parse_error;
  370. }
  371. } else
  372. if (strcmp (value, "off") == 0) {
  373. if (logsys_config_debug_set (subsys, 0) < 0) {
  374. error_reason = "unable to set debug off";
  375. goto parse_error;
  376. }
  377. } else {
  378. error_reason = "unknown value for debug";
  379. goto parse_error;
  380. }
  381. free(value);
  382. }
  383. return (0);
  384. parse_error:
  385. *error_string = error_reason;
  386. free(value);
  387. return (-1);
  388. }
  389. static int corosync_main_config_read_logging (
  390. const char **error_string)
  391. {
  392. const char *error_reason;
  393. icmap_iter_t iter;
  394. const char *key_name;
  395. char key_subsys[ICMAP_KEYNAME_MAXLEN];
  396. char key_item[ICMAP_KEYNAME_MAXLEN];
  397. int res;
  398. /* format set is supported only for toplevel */
  399. if (corosync_main_config_format_set(&error_reason) < 0) {
  400. goto parse_error;
  401. }
  402. if (corosync_main_config_set ("logging", NULL, &error_reason) < 0) {
  403. goto parse_error;
  404. }
  405. /*
  406. * we will need 2 of these to compensate for new logging
  407. * config format
  408. */
  409. iter = icmap_iter_init("logging.logger_subsys.");
  410. while ((key_name = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  411. res = sscanf(key_name, "logging.logger_subsys.%[^.].%s", key_subsys, key_item);
  412. if (res != 2) {
  413. continue ;
  414. }
  415. if (strcmp(key_item, "subsys") != 0) {
  416. continue ;
  417. }
  418. snprintf(key_item, ICMAP_KEYNAME_MAXLEN, "logging.logger_subsys.%s", key_subsys);
  419. if (corosync_main_config_set(key_item, key_subsys, &error_reason) < 0) {
  420. goto parse_error;
  421. }
  422. }
  423. icmap_iter_finalize(iter);
  424. logsys_config_apply();
  425. return 0;
  426. parse_error:
  427. *error_string = error_reason;
  428. return (-1);
  429. }
  430. static void main_logging_notify(
  431. int32_t event,
  432. const char *key_name,
  433. struct icmap_notify_value new_val,
  434. struct icmap_notify_value old_val,
  435. void *user_data)
  436. {
  437. const char *error_string;
  438. /*
  439. * Reload the logsys configuration
  440. */
  441. if (logsys_format_set(NULL) == -1) {
  442. fprintf (stderr, "Unable to setup logging format.\n");
  443. }
  444. corosync_main_config_read_logging(&error_string);
  445. }
  446. static void add_logsys_config_notification(void)
  447. {
  448. icmap_track_t icmap_track = NULL;
  449. icmap_track_add("logging.",
  450. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  451. main_logging_notify,
  452. NULL,
  453. &icmap_track);
  454. }
  455. int corosync_main_config_read (
  456. const char **error_string)
  457. {
  458. const char *error_reason = error_string_response;
  459. if (corosync_main_config_read_logging(error_string) < 0) {
  460. error_reason = *error_string;
  461. goto parse_error;
  462. }
  463. add_logsys_config_notification();
  464. return 0;
  465. parse_error:
  466. snprintf (error_string_response, sizeof(error_string_response),
  467. "parse error in config: %s.\n",
  468. error_reason);
  469. *error_string = error_string_response;
  470. return (-1);
  471. }
  472. int corosync_main_config_compatibility_read (
  473. enum cs_sync_mode *minimum_sync_mode,
  474. const char **error_string)
  475. {
  476. const char *error_reason = error_string_response;
  477. char *value;
  478. *minimum_sync_mode = CS_SYNC_V1;
  479. if (icmap_get_string("compatibility", &value) == CS_OK) {
  480. if (strcmp (value, "whitetank") == 0) {
  481. *minimum_sync_mode = CS_SYNC_V1;
  482. } else
  483. if (strcmp (value, "none") == 0) {
  484. *minimum_sync_mode = CS_SYNC_V2;
  485. } else {
  486. snprintf (error_string_response, sizeof (error_string_response),
  487. "Invalid compatibility option '%s' specified, must be none or whitetank.\n", value);
  488. goto parse_error;
  489. }
  490. free(value);
  491. }
  492. return 0;
  493. parse_error:
  494. *error_string = error_reason;
  495. free(value);
  496. return (-1);
  497. }