4
0

mainconfig.c 14 KB

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