mainconfig.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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/engine/logsys.h>
  50. #include <corosync/engine/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. cs_error_t err;
  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 (icmap_get_string("logging.function_name", &value) == CS_OK) {
  152. if (strcmp (value, "on") == 0) {
  153. if (!insert_into_buffer(new_format_buffer,
  154. sizeof(new_format_buffer),
  155. "%n:", "f:")) {
  156. err = logsys_format_set(new_format_buffer);
  157. } else
  158. if (!insert_into_buffer(new_format_buffer,
  159. sizeof(new_format_buffer),
  160. " %n", "g]")) {
  161. err = logsys_format_set(new_format_buffer);
  162. }
  163. } else
  164. if (strcmp (value, "off") == 0) {
  165. /* nothing to do here */
  166. } else {
  167. error_reason = "unknown value for function_name";
  168. goto parse_error;
  169. }
  170. free(value);
  171. }
  172. if (icmap_get_string("logging.timestamp", &value) == CS_OK) {
  173. if (strcmp (value, "on") == 0) {
  174. if(!insert_into_buffer(new_format_buffer,
  175. sizeof(new_format_buffer),
  176. "%t ", NULL)) {
  177. err = logsys_format_set(new_format_buffer);
  178. }
  179. } else
  180. if (strcmp (value, "off") == 0) {
  181. /* nothing to do here */
  182. } else {
  183. error_reason = "unknown value for timestamp";
  184. goto parse_error;
  185. }
  186. free(value);
  187. }
  188. return (0);
  189. parse_error:
  190. free(value);
  191. *error_string = error_reason;
  192. return (-1);
  193. }
  194. static int corosync_main_config_log_destination_set (
  195. const char *path,
  196. const char *key,
  197. const char *subsys,
  198. const char **error_string,
  199. unsigned int mode_mask,
  200. char deprecated,
  201. const char *replacement)
  202. {
  203. static char formatted_error_reason[128];
  204. char *value = NULL;
  205. unsigned int mode;
  206. char key_name[ICMAP_KEYNAME_MAXLEN];
  207. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.%s", path, key);
  208. if (icmap_get_string(key_name, &value) == CS_OK) {
  209. if (deprecated) {
  210. log_printf(LOGSYS_LEVEL_WARNING,
  211. "Warning: the %s config paramater has been obsoleted."
  212. " See corosync.conf man page %s directive.",
  213. key, replacement);
  214. }
  215. mode = logsys_config_mode_get (subsys);
  216. if (strcmp (value, "yes") == 0 || strcmp (value, "on") == 0) {
  217. mode |= mode_mask;
  218. if (logsys_config_mode_set(subsys, mode) < 0) {
  219. sprintf (formatted_error_reason, "unable to set mode %s", key);
  220. goto parse_error;
  221. }
  222. } else
  223. if (strcmp (value, "no") == 0 || strcmp (value, "off") == 0) {
  224. mode &= ~mode_mask;
  225. if (logsys_config_mode_set(subsys, mode) < 0) {
  226. sprintf (formatted_error_reason, "unable to unset mode %s", key);
  227. goto parse_error;
  228. }
  229. } else {
  230. sprintf (formatted_error_reason, "unknown value for %s", key);
  231. goto parse_error;
  232. }
  233. }
  234. free(value);
  235. return (0);
  236. parse_error:
  237. *error_string = formatted_error_reason;
  238. free(value);
  239. return (-1);
  240. }
  241. static int corosync_main_config_set (
  242. const char *path,
  243. const char *subsys,
  244. const char **error_string)
  245. {
  246. const char *error_reason = error_string_response;
  247. char *value;
  248. int mode;
  249. char key_name[ICMAP_KEYNAME_MAXLEN];
  250. /*
  251. * this bit abuses the internal logsys exported API
  252. * to guarantee that all configured subsystems are
  253. * initialized too.
  254. *
  255. * using this approach avoids some headaches caused
  256. * by IPC and TOTEM that have a special logging
  257. * handling requirements
  258. */
  259. if (subsys != NULL) {
  260. if (_logsys_subsys_create(subsys, NULL) < 0) {
  261. error_reason = "unable to create new logging subsystem";
  262. goto parse_error;
  263. }
  264. }
  265. mode = logsys_config_mode_get(subsys);
  266. if (mode < 0) {
  267. error_reason = "unable to get mode";
  268. goto parse_error;
  269. }
  270. if (corosync_main_config_log_destination_set (path, "to_stderr", subsys, &error_reason,
  271. LOGSYS_MODE_OUTPUT_STDERR, 0, NULL) != 0)
  272. goto parse_error;
  273. if (corosync_main_config_log_destination_set (path, "to_syslog", subsys, &error_reason,
  274. LOGSYS_MODE_OUTPUT_SYSLOG, 0, NULL) != 0)
  275. goto parse_error;
  276. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.%s", path, "syslog_facility");
  277. if (icmap_get_string(key_name, &value) == CS_OK) {
  278. int syslog_facility;
  279. syslog_facility = qb_log_facility2int(value);
  280. if (syslog_facility < 0) {
  281. error_reason = "unknown syslog facility specified";
  282. goto parse_error;
  283. }
  284. if (logsys_config_syslog_facility_set(subsys,
  285. syslog_facility) < 0) {
  286. error_reason = "unable to set syslog facility";
  287. goto parse_error;
  288. }
  289. free(value);
  290. }
  291. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.%s", path, "syslog_level");
  292. if (icmap_get_string(key_name, &value) == CS_OK) {
  293. int syslog_priority;
  294. log_printf(LOGSYS_LEVEL_WARNING,
  295. "Warning: the syslog_level config paramater has been obsoleted."
  296. " See corosync.conf man page syslog_priority directive.");
  297. syslog_priority = logsys_priority_id_get(value);
  298. if (syslog_priority < 0) {
  299. error_reason = "unknown syslog level specified";
  300. goto parse_error;
  301. }
  302. if (logsys_config_syslog_priority_set(subsys,
  303. syslog_priority) < 0) {
  304. error_reason = "unable to set syslog level";
  305. goto parse_error;
  306. }
  307. free(value);
  308. }
  309. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.%s", path, "syslog_priority");
  310. if (icmap_get_string(key_name, &value) == CS_OK) {
  311. int syslog_priority;
  312. syslog_priority = logsys_priority_id_get(value);
  313. if (syslog_priority < 0) {
  314. error_reason = "unknown syslog priority specified";
  315. goto parse_error;
  316. }
  317. if (logsys_config_syslog_priority_set(subsys,
  318. syslog_priority) < 0) {
  319. error_reason = "unable to set syslog priority";
  320. goto parse_error;
  321. }
  322. free(value);
  323. }
  324. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.%s", path, "logfile");
  325. if (icmap_get_string(key_name, &value) == CS_OK) {
  326. if (logsys_config_file_set (subsys, error_string, value) < 0) {
  327. goto parse_error;
  328. }
  329. free(value);
  330. }
  331. if (corosync_main_config_log_destination_set (path, "to_file", subsys, &error_reason,
  332. LOGSYS_MODE_OUTPUT_FILE, 1, "to_logfile") != 0)
  333. goto parse_error;
  334. if (corosync_main_config_log_destination_set (path, "to_logfile", subsys, &error_reason,
  335. LOGSYS_MODE_OUTPUT_FILE, 0, NULL) != 0)
  336. goto parse_error;
  337. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.%s", path, "logfile_priority");
  338. if (icmap_get_string(key_name, &value) == CS_OK) {
  339. int logfile_priority;
  340. logfile_priority = logsys_priority_id_get(value);
  341. if (logfile_priority < 0) {
  342. error_reason = "unknown logfile priority specified";
  343. goto parse_error;
  344. }
  345. if (logsys_config_logfile_priority_set(subsys,
  346. logfile_priority) < 0) {
  347. error_reason = "unable to set logfile priority";
  348. goto parse_error;
  349. }
  350. free(value);
  351. }
  352. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "%s.%s", path, "debug");
  353. if (icmap_get_string(key_name, &value) == CS_OK) {
  354. if (strcmp (value, "on") == 0) {
  355. if (logsys_config_debug_set (subsys, 1) < 0) {
  356. error_reason = "unable to set debug on";
  357. goto parse_error;
  358. }
  359. } else
  360. if (strcmp (value, "off") == 0) {
  361. if (logsys_config_debug_set (subsys, 0) < 0) {
  362. error_reason = "unable to set debug off";
  363. goto parse_error;
  364. }
  365. } else {
  366. error_reason = "unknown value for debug";
  367. goto parse_error;
  368. }
  369. free(value);
  370. }
  371. return (0);
  372. parse_error:
  373. *error_string = error_reason;
  374. free(value);
  375. return (-1);
  376. }
  377. static int corosync_main_config_read_logging (
  378. const char **error_string)
  379. {
  380. const char *error_reason;
  381. icmap_iter_t iter;
  382. const char *key_name;
  383. char key_subsys[ICMAP_KEYNAME_MAXLEN];
  384. char key_item[ICMAP_KEYNAME_MAXLEN];
  385. int res;
  386. /* format set is supported only for toplevel */
  387. if (corosync_main_config_format_set(&error_reason) < 0) {
  388. goto parse_error;
  389. }
  390. if (corosync_main_config_set ("logging", NULL, &error_reason) < 0) {
  391. goto parse_error;
  392. }
  393. /*
  394. * we will need 2 of these to compensate for new logging
  395. * config format
  396. */
  397. iter = icmap_iter_init("logging.logger_subsys.");
  398. while ((key_name = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  399. res = sscanf(key_name, "logging.logger_subsys.%[^.].%s", key_subsys, key_item);
  400. if (res != 2) {
  401. continue ;
  402. }
  403. if (strcmp(key_item, "subsys") != 0) {
  404. continue ;
  405. }
  406. snprintf(key_item, ICMAP_KEYNAME_MAXLEN, "logging.logger_subsys.%s", key_subsys);
  407. if (corosync_main_config_set(key_item, key_subsys, &error_reason) < 0) {
  408. goto parse_error;
  409. }
  410. }
  411. icmap_iter_finalize(iter);
  412. logsys_config_apply();
  413. return 0;
  414. parse_error:
  415. *error_string = error_reason;
  416. return (-1);
  417. }
  418. static void main_logging_notify(
  419. int32_t event,
  420. const char *key_name,
  421. struct icmap_notify_value new_val,
  422. struct icmap_notify_value old_val,
  423. void *user_data)
  424. {
  425. const char *error_string;
  426. /*
  427. * Reload the logsys configuration
  428. */
  429. if (logsys_format_set(NULL) == -1) {
  430. fprintf (stderr, "Unable to setup logging format.\n");
  431. }
  432. corosync_main_config_read_logging(&error_string);
  433. }
  434. static void add_logsys_config_notification(void)
  435. {
  436. icmap_track_t icmap_track;
  437. icmap_track_add("logging.",
  438. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  439. main_logging_notify,
  440. NULL,
  441. &icmap_track);
  442. }
  443. int corosync_main_config_read (
  444. const char **error_string)
  445. {
  446. const char *error_reason = error_string_response;
  447. if (corosync_main_config_read_logging(error_string) < 0) {
  448. error_reason = *error_string;
  449. goto parse_error;
  450. }
  451. add_logsys_config_notification();
  452. return 0;
  453. parse_error:
  454. snprintf (error_string_response, sizeof(error_string_response),
  455. "parse error in config: %s.\n",
  456. error_reason);
  457. *error_string = error_string_response;
  458. return (-1);
  459. }
  460. int corosync_main_config_compatibility_read (
  461. enum cs_sync_mode *minimum_sync_mode,
  462. const char **error_string)
  463. {
  464. const char *error_reason = error_string_response;
  465. char *value;
  466. *minimum_sync_mode = CS_SYNC_V1;
  467. if (icmap_get_string("compatibility", &value) == CS_OK) {
  468. if (strcmp (value, "whitetank") == 0) {
  469. *minimum_sync_mode = CS_SYNC_V1;
  470. } else
  471. if (strcmp (value, "none") == 0) {
  472. *minimum_sync_mode = CS_SYNC_V2;
  473. } else {
  474. snprintf (error_string_response, sizeof (error_string_response),
  475. "Invalid compatibility option '%s' specified, must be none or whitetank.\n", value);
  476. goto parse_error;
  477. }
  478. free(value);
  479. }
  480. return 0;
  481. parse_error:
  482. *error_string = error_reason;
  483. free(value);
  484. return (-1);
  485. }