logconfig.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 <corosync/totem/totem.h>
  38. #include <corosync/logsys.h>
  39. #ifdef LOGCONFIG_USE_ICMAP
  40. #include <corosync/icmap.h>
  41. #define MAP_KEYNAME_MAXLEN ICMAP_KEYNAME_MAXLEN
  42. #define map_get_string(key_name, value) icmap_get_string(key_name, value)
  43. #else
  44. #include <corosync/cmap.h>
  45. static cmap_handle_t cmap_handle;
  46. static const char *main_logfile;
  47. #define MAP_KEYNAME_MAXLEN CMAP_KEYNAME_MAXLEN
  48. #define map_get_string(key_name, value) cmap_get_string(cmap_handle, key_name, value)
  49. #endif
  50. #include "util.h"
  51. #include "logconfig.h"
  52. static char error_string_response[512];
  53. /**
  54. * insert_into_buffer
  55. * @target_buffer: a buffer where to write results
  56. * @bufferlen: tell us the size of the buffer to avoid overflows
  57. * @entry: entry that needs to be added to the buffer
  58. * @after: can either be NULL or set to a string.
  59. * if NULL, @entry is prependend to logsys_format_get buffer.
  60. * if set, @entry is added immediately after @after.
  61. *
  62. * Since the function is specific to logsys_format_get handling, it is implicit
  63. * that source is logsys_format_get();
  64. *
  65. * In case of failure, target_buffer could be left dirty. So don't trust
  66. * any data leftover in it.
  67. *
  68. * Searching for "after" assumes that there is only entry of "after"
  69. * in the source. Afterall we control the string here and for logging format
  70. * it makes little to no sense to have duplicate format entries.
  71. *
  72. * Returns: 0 on success, -1 on failure
  73. **/
  74. static int insert_into_buffer(
  75. char *target_buffer,
  76. size_t bufferlen,
  77. const char *entry,
  78. const char *after)
  79. {
  80. const char *current_format = NULL;
  81. current_format = logsys_format_get();
  82. /* if the entry is already in the format we don't add it again */
  83. if (strstr(current_format, entry) != NULL) {
  84. return -1;
  85. }
  86. /* if there is no "after", simply prepend the requested entry
  87. * otherwise go for beautiful string manipulation.... </sarcasm> */
  88. if (!after) {
  89. if (snprintf(target_buffer, bufferlen - 1, "%s%s",
  90. entry,
  91. current_format) >= bufferlen - 1) {
  92. return -1;
  93. }
  94. } else {
  95. const char *afterpos;
  96. size_t afterlen;
  97. size_t templen;
  98. /* check if after is contained in the format
  99. * and afterlen has a meaning or return an error */
  100. afterpos = strstr(current_format, after);
  101. afterlen = strlen(after);
  102. if ((!afterpos) || (!afterlen)) {
  103. return -1;
  104. }
  105. templen = afterpos - current_format + afterlen;
  106. if (snprintf(target_buffer, templen + 1, "%s", current_format)
  107. >= bufferlen - 1) {
  108. return -1;
  109. }
  110. if (snprintf(target_buffer + templen, bufferlen - ( templen + 1 ),
  111. "%s%s", entry, current_format + templen)
  112. >= bufferlen - ( templen + 1 )) {
  113. return -1;
  114. }
  115. }
  116. return 0;
  117. }
  118. /*
  119. * format set is the only global specific option that
  120. * doesn't apply at system/subsystem level.
  121. */
  122. static int corosync_main_config_format_set (
  123. const char **error_string)
  124. {
  125. const char *error_reason;
  126. char new_format_buffer[PATH_MAX];
  127. char *value = NULL;
  128. int err = 0;
  129. if (map_get_string("logging.fileline", &value) == CS_OK) {
  130. if (strcmp (value, "on") == 0) {
  131. if (!insert_into_buffer(new_format_buffer,
  132. sizeof(new_format_buffer),
  133. " %f:%l", "g]")) {
  134. err = logsys_format_set(new_format_buffer);
  135. } else
  136. if (!insert_into_buffer(new_format_buffer,
  137. sizeof(new_format_buffer),
  138. "%f:%l", NULL)) {
  139. err = logsys_format_set(new_format_buffer);
  140. }
  141. } else
  142. if (strcmp (value, "off") == 0) {
  143. /* nothing to do here */
  144. } else {
  145. error_reason = "unknown value for fileline";
  146. free(value);
  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 (map_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. free(value);
  173. goto parse_error;
  174. }
  175. free(value);
  176. }
  177. if (err) {
  178. error_reason = "not enough memory to set logging format buffer";
  179. goto parse_error;
  180. }
  181. if (map_get_string("logging.timestamp", &value) == CS_OK) {
  182. if (strcmp (value, "on") == 0) {
  183. if(!insert_into_buffer(new_format_buffer,
  184. sizeof(new_format_buffer),
  185. "%t ", NULL)) {
  186. err = logsys_format_set(new_format_buffer);
  187. }
  188. } else
  189. if (strcmp (value, "off") == 0) {
  190. /* nothing to do here */
  191. } else {
  192. error_reason = "unknown value for timestamp";
  193. free(value);
  194. goto parse_error;
  195. }
  196. free(value);
  197. }
  198. if (err) {
  199. error_reason = "not enough memory to set logging format buffer";
  200. goto parse_error;
  201. }
  202. return (0);
  203. parse_error:
  204. *error_string = error_reason;
  205. return (-1);
  206. }
  207. static int corosync_main_config_log_destination_set (
  208. const char *path,
  209. const char *key,
  210. const char *subsys,
  211. const char **error_string,
  212. unsigned int mode_mask,
  213. char deprecated,
  214. const char *replacement)
  215. {
  216. static char formatted_error_reason[128];
  217. char *value = NULL;
  218. unsigned int mode;
  219. char key_name[MAP_KEYNAME_MAXLEN];
  220. snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, key);
  221. if (map_get_string(key_name, &value) == CS_OK) {
  222. if (deprecated) {
  223. log_printf(LOGSYS_LEVEL_WARNING,
  224. "Warning: the %s config paramater has been obsoleted."
  225. " See corosync.conf man page %s directive.",
  226. key, replacement);
  227. }
  228. mode = logsys_config_mode_get (subsys);
  229. if (strcmp (value, "yes") == 0 || strcmp (value, "on") == 0) {
  230. mode |= mode_mask;
  231. if (logsys_config_mode_set(subsys, mode) < 0) {
  232. sprintf (formatted_error_reason, "unable to set mode %s", key);
  233. goto parse_error;
  234. }
  235. } else
  236. if (strcmp (value, "no") == 0 || strcmp (value, "off") == 0) {
  237. mode &= ~mode_mask;
  238. if (logsys_config_mode_set(subsys, mode) < 0) {
  239. sprintf (formatted_error_reason, "unable to unset mode %s", key);
  240. goto parse_error;
  241. }
  242. } else {
  243. sprintf (formatted_error_reason, "unknown value for %s", key);
  244. goto parse_error;
  245. }
  246. }
  247. free(value);
  248. return (0);
  249. parse_error:
  250. *error_string = formatted_error_reason;
  251. free(value);
  252. return (-1);
  253. }
  254. static int corosync_main_config_set (
  255. const char *path,
  256. const char *subsys,
  257. const char **error_string)
  258. {
  259. const char *error_reason = error_string_response;
  260. char *value = NULL;
  261. int mode;
  262. char key_name[MAP_KEYNAME_MAXLEN];
  263. /*
  264. * this bit abuses the internal logsys exported API
  265. * to guarantee that all configured subsystems are
  266. * initialized too.
  267. *
  268. * using this approach avoids some headaches caused
  269. * by IPC and TOTEM that have a special logging
  270. * handling requirements
  271. */
  272. if (subsys != NULL) {
  273. if (_logsys_subsys_create(subsys, NULL) < 0) {
  274. error_reason = "unable to create new logging subsystem";
  275. goto parse_error;
  276. }
  277. }
  278. mode = logsys_config_mode_get(subsys);
  279. if (mode < 0) {
  280. error_reason = "unable to get mode";
  281. goto parse_error;
  282. }
  283. if (corosync_main_config_log_destination_set (path, "to_stderr", subsys, &error_reason,
  284. LOGSYS_MODE_OUTPUT_STDERR, 0, NULL) != 0)
  285. goto parse_error;
  286. if (corosync_main_config_log_destination_set (path, "to_syslog", subsys, &error_reason,
  287. LOGSYS_MODE_OUTPUT_SYSLOG, 0, NULL) != 0)
  288. goto parse_error;
  289. snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "syslog_facility");
  290. if (map_get_string(key_name, &value) == CS_OK) {
  291. int syslog_facility;
  292. syslog_facility = qb_log_facility2int(value);
  293. if (syslog_facility < 0) {
  294. error_reason = "unknown syslog facility specified";
  295. goto parse_error;
  296. }
  297. if (logsys_config_syslog_facility_set(subsys,
  298. syslog_facility) < 0) {
  299. error_reason = "unable to set syslog facility";
  300. goto parse_error;
  301. }
  302. free(value);
  303. }
  304. snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "syslog_level");
  305. if (map_get_string(key_name, &value) == CS_OK) {
  306. int syslog_priority;
  307. log_printf(LOGSYS_LEVEL_WARNING,
  308. "Warning: the syslog_level config paramater has been obsoleted."
  309. " See corosync.conf man page syslog_priority directive.");
  310. syslog_priority = logsys_priority_id_get(value);
  311. free(value);
  312. if (syslog_priority < 0) {
  313. error_reason = "unknown syslog level specified";
  314. goto parse_error;
  315. }
  316. if (logsys_config_syslog_priority_set(subsys,
  317. syslog_priority) < 0) {
  318. error_reason = "unable to set syslog level";
  319. goto parse_error;
  320. }
  321. }
  322. snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "syslog_priority");
  323. if (map_get_string(key_name, &value) == CS_OK) {
  324. int syslog_priority;
  325. syslog_priority = logsys_priority_id_get(value);
  326. free(value);
  327. if (syslog_priority < 0) {
  328. error_reason = "unknown syslog priority specified";
  329. goto parse_error;
  330. }
  331. if (logsys_config_syslog_priority_set(subsys,
  332. syslog_priority) < 0) {
  333. error_reason = "unable to set syslog priority";
  334. goto parse_error;
  335. }
  336. }
  337. #ifdef LOGCONFIG_USE_ICMAP
  338. snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "logfile");
  339. if (map_get_string(key_name, &value) == CS_OK) {
  340. if (logsys_config_file_set (subsys, &error_reason, value) < 0) {
  341. goto parse_error;
  342. }
  343. free(value);
  344. }
  345. #else
  346. if (!subsys) {
  347. if (logsys_config_file_set (subsys, &error_reason, main_logfile) < 0) {
  348. goto parse_error;
  349. }
  350. }
  351. #endif
  352. if (corosync_main_config_log_destination_set (path, "to_file", subsys, &error_reason,
  353. LOGSYS_MODE_OUTPUT_FILE, 1, "to_logfile") != 0)
  354. goto parse_error;
  355. if (corosync_main_config_log_destination_set (path, "to_logfile", subsys, &error_reason,
  356. LOGSYS_MODE_OUTPUT_FILE, 0, NULL) != 0)
  357. goto parse_error;
  358. snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "logfile_priority");
  359. if (map_get_string(key_name, &value) == CS_OK) {
  360. int logfile_priority;
  361. logfile_priority = logsys_priority_id_get(value);
  362. free(value);
  363. if (logfile_priority < 0) {
  364. error_reason = "unknown logfile priority specified";
  365. goto parse_error;
  366. }
  367. if (logsys_config_logfile_priority_set(subsys,
  368. logfile_priority) < 0) {
  369. error_reason = "unable to set logfile priority";
  370. goto parse_error;
  371. }
  372. }
  373. snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "debug");
  374. if (map_get_string(key_name, &value) == CS_OK) {
  375. if (strcmp (value, "trace") == 0) {
  376. if (logsys_config_debug_set (subsys, LOGSYS_DEBUG_TRACE) < 0) {
  377. error_reason = "unable to set debug trace";
  378. free(value);
  379. goto parse_error;
  380. }
  381. } else
  382. if (strcmp (value, "on") == 0) {
  383. if (logsys_config_debug_set (subsys, LOGSYS_DEBUG_ON) < 0) {
  384. error_reason = "unable to set debug on";
  385. free(value);
  386. goto parse_error;
  387. }
  388. } else
  389. if (strcmp (value, "off") == 0) {
  390. if (logsys_config_debug_set (subsys, LOGSYS_DEBUG_OFF) < 0) {
  391. error_reason = "unable to set debug off";
  392. free(value);
  393. goto parse_error;
  394. }
  395. } else {
  396. error_reason = "unknown value for debug";
  397. free(value);
  398. goto parse_error;
  399. }
  400. free(value);
  401. }
  402. return (0);
  403. parse_error:
  404. *error_string = error_reason;
  405. return (-1);
  406. }
  407. static int corosync_main_config_read_logging (
  408. const char **error_string)
  409. {
  410. const char *error_reason;
  411. #ifdef LOGCONFIG_USE_ICMAP
  412. icmap_iter_t iter;
  413. const char *key_name;
  414. #else
  415. cmap_iter_handle_t iter;
  416. char key_name[CMAP_KEYNAME_MAXLEN];
  417. #endif
  418. char key_subsys[MAP_KEYNAME_MAXLEN];
  419. char key_item[MAP_KEYNAME_MAXLEN];
  420. int res;
  421. /* format set is supported only for toplevel */
  422. if (corosync_main_config_format_set(&error_reason) < 0) {
  423. goto parse_error;
  424. }
  425. if (corosync_main_config_set ("logging", NULL, &error_reason) < 0) {
  426. goto parse_error;
  427. }
  428. /*
  429. * we will need 2 of these to compensate for new logging
  430. * config format
  431. */
  432. #ifdef LOGCONFIG_USE_ICMAP
  433. iter = icmap_iter_init("logging.logger_subsys.");
  434. while ((key_name = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  435. #else
  436. cmap_iter_init(cmap_handle, "logging.logger_subsys.", &iter);
  437. while ((cmap_iter_next(cmap_handle, iter, key_name, NULL, NULL)) == CS_OK) {
  438. #endif
  439. res = sscanf(key_name, "logging.logger_subsys.%[^.].%s", key_subsys, key_item);
  440. if (res != 2) {
  441. continue ;
  442. }
  443. if (strcmp(key_item, "subsys") != 0) {
  444. continue ;
  445. }
  446. snprintf(key_item, MAP_KEYNAME_MAXLEN, "logging.logger_subsys.%s", key_subsys);
  447. if (corosync_main_config_set(key_item, key_subsys, &error_reason) < 0) {
  448. goto parse_error;
  449. }
  450. }
  451. #ifdef LOGCONFIG_USE_ICMAP
  452. icmap_iter_finalize(iter);
  453. #else
  454. cmap_iter_finalize(cmap_handle, iter);
  455. #endif
  456. logsys_config_apply();
  457. return 0;
  458. parse_error:
  459. *error_string = error_reason;
  460. return (-1);
  461. }
  462. #ifdef LOGCONFIG_USE_ICMAP
  463. static void main_logging_notify(
  464. int32_t event,
  465. const char *key_name,
  466. struct icmap_notify_value new_val,
  467. struct icmap_notify_value old_val,
  468. void *user_data)
  469. #else
  470. static void main_logging_notify(
  471. cmap_handle_t cmap_handle_unused,
  472. cmap_handle_t cmap_track_handle_unused,
  473. int32_t event,
  474. const char *key_name,
  475. struct cmap_notify_value new_val,
  476. struct cmap_notify_value old_val,
  477. void *user_data)
  478. #endif
  479. {
  480. const char *error_string;
  481. static int reload_in_progress = 0;
  482. /* If a full reload happens then suspend updates for individual keys until
  483. * it's all completed
  484. */
  485. if (strcmp(key_name, "config.reload_in_progress") == 0) {
  486. if (*(uint8_t *)new_val.data == 1) {
  487. reload_in_progress = 1;
  488. } else {
  489. reload_in_progress = 0;
  490. }
  491. }
  492. if (reload_in_progress) {
  493. log_printf(LOGSYS_LEVEL_DEBUG, "Ignoring key change, reload in progress. %s\n", key_name);
  494. return;
  495. }
  496. /*
  497. * Reload the logsys configuration
  498. */
  499. if (logsys_format_set(NULL) == -1) {
  500. fprintf (stderr, "Unable to setup logging format.\n");
  501. }
  502. corosync_main_config_read_logging(&error_string);
  503. }
  504. #ifdef LOGCONFIG_USE_ICMAP
  505. static void add_logsys_config_notification(void)
  506. {
  507. icmap_track_t icmap_track = NULL;
  508. icmap_track_add("logging.",
  509. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  510. main_logging_notify,
  511. NULL,
  512. &icmap_track);
  513. icmap_track_add("config.reload_in_progress",
  514. ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY,
  515. main_logging_notify,
  516. NULL,
  517. &icmap_track);
  518. }
  519. #else
  520. static void add_logsys_config_notification(void)
  521. {
  522. cmap_track_handle_t cmap_track;
  523. cmap_track_add(cmap_handle, "logging.",
  524. CMAP_TRACK_ADD | CMAP_TRACK_DELETE | CMAP_TRACK_MODIFY | CMAP_TRACK_PREFIX,
  525. main_logging_notify,
  526. NULL,
  527. &cmap_track);
  528. cmap_track_add(cmap_handle, "config.reload_in_progress",
  529. CMAP_TRACK_ADD | CMAP_TRACK_MODIFY,
  530. main_logging_notify,
  531. NULL,
  532. &cmap_track);
  533. }
  534. #endif
  535. int corosync_log_config_read (
  536. #ifndef LOGCONFIG_USE_ICMAP
  537. cmap_handle_t cmap_h,
  538. const char *default_logfile,
  539. #endif
  540. const char **error_string)
  541. {
  542. const char *error_reason = error_string_response;
  543. #ifndef LOGCONFIG_USE_ICMAP
  544. if (!cmap_h) {
  545. error_reason = "No cmap handle";
  546. return (-1);
  547. }
  548. if (!default_logfile) {
  549. error_reason = "No default logfile";
  550. return (-1);
  551. }
  552. cmap_handle = cmap_h;
  553. main_logfile = default_logfile;
  554. #endif
  555. if (corosync_main_config_read_logging(error_string) < 0) {
  556. error_reason = *error_string;
  557. goto parse_error;
  558. }
  559. add_logsys_config_notification();
  560. return 0;
  561. parse_error:
  562. snprintf (error_string_response, sizeof(error_string_response),
  563. "parse error in config: %s.\n",
  564. error_reason);
  565. *error_string = error_string_response;
  566. return (-1);
  567. }