logconfig.c 16 KB

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