logconfig.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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 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. char timestamp_str_to_add[8];
  130. if (map_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. free(value);
  148. goto parse_error;
  149. }
  150. free(value);
  151. }
  152. if (err) {
  153. error_reason = "not enough memory to set logging format buffer";
  154. goto parse_error;
  155. }
  156. if (map_get_string("logging.function_name", &value) == CS_OK) {
  157. if (strcmp (value, "on") == 0) {
  158. if (!insert_into_buffer(new_format_buffer,
  159. sizeof(new_format_buffer),
  160. "%n:", "f:")) {
  161. err = logsys_format_set(new_format_buffer);
  162. } else
  163. if (!insert_into_buffer(new_format_buffer,
  164. sizeof(new_format_buffer),
  165. " %n", "g]")) {
  166. err = logsys_format_set(new_format_buffer);
  167. }
  168. } else
  169. if (strcmp (value, "off") == 0) {
  170. /* nothing to do here */
  171. } else {
  172. error_reason = "unknown value for function_name";
  173. free(value);
  174. goto parse_error;
  175. }
  176. free(value);
  177. }
  178. if (err) {
  179. error_reason = "not enough memory to set logging format buffer";
  180. goto parse_error;
  181. }
  182. memset(timestamp_str_to_add, 0, sizeof(timestamp_str_to_add));
  183. if (map_get_string("logging.timestamp", &value) == CS_OK) {
  184. if (strcmp (value, "on") == 0) {
  185. strcpy(timestamp_str_to_add, "%t");
  186. #ifdef QB_FEATURE_LOG_HIRES_TIMESTAMPS
  187. } else if (strcmp (value, "hires") == 0) {
  188. strcpy(timestamp_str_to_add, "%T");
  189. #endif
  190. } else if (strcmp (value, "off") == 0) {
  191. /* nothing to do here */
  192. } else {
  193. error_reason = "unknown value for timestamp";
  194. free(value);
  195. goto parse_error;
  196. }
  197. free(value);
  198. } else {
  199. /*
  200. * Display hires timestamp by default, otherwise standard timestamp
  201. */
  202. #ifdef QB_FEATURE_LOG_HIRES_TIMESTAMPS
  203. strcpy(timestamp_str_to_add, "%T");
  204. #else
  205. strcpy(timestamp_str_to_add, "%t");
  206. #endif
  207. }
  208. if(strcmp(timestamp_str_to_add, "") != 0) {
  209. strcat(timestamp_str_to_add, " ");
  210. if (insert_into_buffer(new_format_buffer, sizeof(new_format_buffer),
  211. timestamp_str_to_add, NULL) == 0) {
  212. err = logsys_format_set(new_format_buffer);
  213. }
  214. }
  215. if (err) {
  216. error_reason = "not enough memory to set logging format buffer";
  217. goto parse_error;
  218. }
  219. return (0);
  220. parse_error:
  221. *error_string = error_reason;
  222. return (-1);
  223. }
  224. /*
  225. * blackbox is another global specific option that
  226. * doesn't apply at system/subsystem level.
  227. */
  228. static int corosync_main_config_blackbox_set (
  229. const char **error_string)
  230. {
  231. const char *error_reason;
  232. char *value = NULL;
  233. if (map_get_string("logging.blackbox", &value) == CS_OK) {
  234. if (strcmp (value, "on") == 0) {
  235. (void)logsys_blackbox_set(QB_TRUE);
  236. } else if (strcmp (value, "off") == 0) {
  237. (void)logsys_blackbox_set(QB_FALSE);
  238. } else {
  239. error_reason = "unknown value for blackbox";
  240. free(value);
  241. goto parse_error;
  242. }
  243. free(value);
  244. } else {
  245. (void)logsys_blackbox_set(QB_TRUE);
  246. }
  247. return (0);
  248. parse_error:
  249. *error_string = error_reason;
  250. return (-1);
  251. }
  252. static int corosync_main_config_log_destination_set (
  253. const char *path,
  254. const char *key,
  255. const char *subsys,
  256. const char **error_string,
  257. unsigned int mode_mask,
  258. char deprecated,
  259. char default_value,
  260. const char *replacement)
  261. {
  262. static char formatted_error_reason[128];
  263. char *value = NULL;
  264. unsigned int mode;
  265. char key_name[MAP_KEYNAME_MAXLEN];
  266. snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, key);
  267. if (map_get_string(key_name, &value) == CS_OK) {
  268. if (deprecated) {
  269. log_printf(LOGSYS_LEVEL_WARNING,
  270. "Warning: the %s config parameter has been obsoleted."
  271. " See corosync.conf man page %s directive.",
  272. key, replacement);
  273. }
  274. mode = logsys_config_mode_get (subsys);
  275. if (strcmp (value, "yes") == 0 || strcmp (value, "on") == 0) {
  276. mode |= mode_mask;
  277. if (logsys_config_mode_set(subsys, mode) < 0) {
  278. sprintf (formatted_error_reason, "unable to set mode %s", key);
  279. goto parse_error;
  280. }
  281. } else
  282. if (strcmp (value, "no") == 0 || strcmp (value, "off") == 0) {
  283. mode &= ~mode_mask;
  284. if (logsys_config_mode_set(subsys, mode) < 0) {
  285. sprintf (formatted_error_reason, "unable to unset mode %s", key);
  286. goto parse_error;
  287. }
  288. } else {
  289. sprintf (formatted_error_reason, "unknown value for %s", key);
  290. goto parse_error;
  291. }
  292. }
  293. /* Set to default if we are the top-level logger */
  294. else if (!subsys && !deprecated) {
  295. mode = logsys_config_mode_get (subsys);
  296. if (default_value) {
  297. mode |= mode_mask;
  298. }
  299. else {
  300. mode &= ~mode_mask;
  301. }
  302. if (logsys_config_mode_set(subsys, mode) < 0) {
  303. sprintf (formatted_error_reason, "unable to change mode %s", key);
  304. goto parse_error;
  305. }
  306. }
  307. free(value);
  308. return (0);
  309. parse_error:
  310. *error_string = formatted_error_reason;
  311. free(value);
  312. return (-1);
  313. }
  314. static int corosync_main_config_set (
  315. const char *path,
  316. const char *subsys,
  317. const char **error_string)
  318. {
  319. const char *error_reason = error_string_response;
  320. char *value = NULL;
  321. int mode;
  322. char key_name[MAP_KEYNAME_MAXLEN];
  323. /*
  324. * this bit abuses the internal logsys exported API
  325. * to guarantee that all configured subsystems are
  326. * initialized too.
  327. *
  328. * using this approach avoids some headaches caused
  329. * by IPC and TOTEM that have a special logging
  330. * handling requirements
  331. */
  332. if (subsys != NULL) {
  333. if (_logsys_subsys_create(subsys, NULL) < 0) {
  334. error_reason = "unable to create new logging subsystem";
  335. goto parse_error;
  336. }
  337. }
  338. mode = logsys_config_mode_get(subsys);
  339. if (mode < 0) {
  340. error_reason = "unable to get mode";
  341. goto parse_error;
  342. }
  343. if (corosync_main_config_log_destination_set (path, "to_stderr", subsys, &error_reason,
  344. LOGSYS_MODE_OUTPUT_STDERR, 0, 1, NULL) != 0)
  345. goto parse_error;
  346. if (corosync_main_config_log_destination_set (path, "to_syslog", subsys, &error_reason,
  347. LOGSYS_MODE_OUTPUT_SYSLOG, 0, 1, NULL) != 0)
  348. goto parse_error;
  349. snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "syslog_facility");
  350. if (map_get_string(key_name, &value) == CS_OK) {
  351. int syslog_facility;
  352. syslog_facility = qb_log_facility2int(value);
  353. if (syslog_facility < 0) {
  354. error_reason = "unknown syslog facility specified";
  355. goto parse_error;
  356. }
  357. if (logsys_config_syslog_facility_set(subsys,
  358. syslog_facility) < 0) {
  359. error_reason = "unable to set syslog facility";
  360. goto parse_error;
  361. }
  362. free(value);
  363. }
  364. else {
  365. /* Set default here in case of a reload */
  366. if (logsys_config_syslog_facility_set(subsys,
  367. qb_log_facility2int("daemon")) < 0) {
  368. error_reason = "unable to set syslog facility";
  369. goto parse_error;
  370. }
  371. }
  372. snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "syslog_level");
  373. if (map_get_string(key_name, &value) == CS_OK) {
  374. int syslog_priority;
  375. log_printf(LOGSYS_LEVEL_WARNING,
  376. "Warning: the syslog_level config parameter has been obsoleted."
  377. " See corosync.conf man page syslog_priority directive.");
  378. syslog_priority = logsys_priority_id_get(value);
  379. free(value);
  380. if (syslog_priority < 0) {
  381. error_reason = "unknown syslog level specified";
  382. goto parse_error;
  383. }
  384. if (logsys_config_syslog_priority_set(subsys,
  385. syslog_priority) < 0) {
  386. error_reason = "unable to set syslog level";
  387. goto parse_error;
  388. }
  389. }
  390. snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "syslog_priority");
  391. if (map_get_string(key_name, &value) == CS_OK) {
  392. int syslog_priority;
  393. syslog_priority = logsys_priority_id_get(value);
  394. free(value);
  395. if (syslog_priority < 0) {
  396. error_reason = "unknown syslog priority specified";
  397. goto parse_error;
  398. }
  399. if (logsys_config_syslog_priority_set(subsys,
  400. syslog_priority) < 0) {
  401. error_reason = "unable to set syslog priority";
  402. goto parse_error;
  403. }
  404. }
  405. else if(strcmp(key_name, "logging.syslog_priority") == 0){
  406. if (logsys_config_syslog_priority_set(subsys,
  407. logsys_priority_id_get("info")) < 0) {
  408. error_reason = "unable to set syslog level";
  409. goto parse_error;
  410. }
  411. }
  412. #ifdef LOGCONFIG_USE_ICMAP
  413. snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "logfile");
  414. if (map_get_string(key_name, &value) == CS_OK) {
  415. if (logsys_config_file_set (subsys, &error_reason, value) < 0) {
  416. goto parse_error;
  417. }
  418. free(value);
  419. }
  420. #else
  421. if (!subsys) {
  422. if (logsys_config_file_set (subsys, &error_reason, main_logfile) < 0) {
  423. goto parse_error;
  424. }
  425. }
  426. #endif
  427. if (corosync_main_config_log_destination_set (path, "to_file", subsys, &error_reason,
  428. LOGSYS_MODE_OUTPUT_FILE, 1, 0, "to_logfile") != 0)
  429. goto parse_error;
  430. if (corosync_main_config_log_destination_set (path, "to_logfile", subsys, &error_reason,
  431. LOGSYS_MODE_OUTPUT_FILE, 0, 0, NULL) != 0)
  432. goto parse_error;
  433. snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "logfile_priority");
  434. if (map_get_string(key_name, &value) == CS_OK) {
  435. int logfile_priority;
  436. logfile_priority = logsys_priority_id_get(value);
  437. free(value);
  438. if (logfile_priority < 0) {
  439. error_reason = "unknown logfile priority specified";
  440. goto parse_error;
  441. }
  442. if (logsys_config_logfile_priority_set(subsys,
  443. logfile_priority) < 0) {
  444. error_reason = "unable to set logfile priority";
  445. goto parse_error;
  446. }
  447. }
  448. else if(strcmp(key_name,"logging.logfile_priority") == 0){
  449. if (logsys_config_logfile_priority_set(subsys,
  450. logsys_priority_id_get("info")) < 0) {
  451. error_reason = "unable to set syslog level";
  452. goto parse_error;
  453. }
  454. }
  455. snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "debug");
  456. if (map_get_string(key_name, &value) == CS_OK) {
  457. if (strcmp (value, "trace") == 0) {
  458. if (logsys_config_debug_set (subsys, LOGSYS_DEBUG_TRACE) < 0) {
  459. error_reason = "unable to set debug trace";
  460. free(value);
  461. goto parse_error;
  462. }
  463. } else
  464. if (strcmp (value, "on") == 0) {
  465. if (logsys_config_debug_set (subsys, LOGSYS_DEBUG_ON) < 0) {
  466. error_reason = "unable to set debug on";
  467. free(value);
  468. goto parse_error;
  469. }
  470. } else
  471. if (strcmp (value, "off") == 0) {
  472. if (logsys_config_debug_set (subsys, LOGSYS_DEBUG_OFF) < 0) {
  473. error_reason = "unable to set debug off";
  474. free(value);
  475. goto parse_error;
  476. }
  477. } else {
  478. error_reason = "unknown value for debug";
  479. free(value);
  480. goto parse_error;
  481. }
  482. free(value);
  483. }
  484. else {
  485. if (logsys_config_debug_set (subsys, LOGSYS_DEBUG_OFF) < 0) {
  486. error_reason = "unable to set debug off";
  487. goto parse_error;
  488. }
  489. }
  490. return (0);
  491. parse_error:
  492. *error_string = error_reason;
  493. return (-1);
  494. }
  495. static int corosync_main_config_read_logging (
  496. const char **error_string)
  497. {
  498. const char *error_reason;
  499. #ifdef LOGCONFIG_USE_ICMAP
  500. icmap_iter_t iter;
  501. const char *key_name;
  502. #else
  503. cmap_iter_handle_t iter;
  504. char key_name[CMAP_KEYNAME_MAXLEN];
  505. #endif
  506. char key_subsys[MAP_KEYNAME_MAXLEN];
  507. char key_item[MAP_KEYNAME_MAXLEN];
  508. int res;
  509. /* format set is supported only for toplevel */
  510. if (corosync_main_config_format_set(&error_reason) < 0) {
  511. goto parse_error;
  512. }
  513. if (corosync_main_config_blackbox_set(&error_reason) < 0) {
  514. goto parse_error;
  515. }
  516. if (corosync_main_config_set ("logging", NULL, &error_reason) < 0) {
  517. goto parse_error;
  518. }
  519. /*
  520. * we will need 2 of these to compensate for new logging
  521. * config format
  522. */
  523. #ifdef LOGCONFIG_USE_ICMAP
  524. iter = icmap_iter_init("logging.logger_subsys.");
  525. while ((key_name = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  526. #else
  527. cmap_iter_init(cmap_handle, "logging.logger_subsys.", &iter);
  528. while ((cmap_iter_next(cmap_handle, iter, key_name, NULL, NULL)) == CS_OK) {
  529. #endif
  530. res = sscanf(key_name, "logging.logger_subsys.%[^.].%s", key_subsys, key_item);
  531. if (res != 2) {
  532. continue ;
  533. }
  534. if (strcmp(key_item, "subsys") != 0) {
  535. continue ;
  536. }
  537. if (snprintf(key_item, MAP_KEYNAME_MAXLEN, "logging.logger_subsys.%s",
  538. key_subsys) >= MAP_KEYNAME_MAXLEN) {
  539. /*
  540. * This should never happen
  541. */
  542. error_reason = "Can't snprintf logger_subsys key_item";
  543. goto parse_error;
  544. }
  545. if (corosync_main_config_set(key_item, key_subsys, &error_reason) < 0) {
  546. goto parse_error;
  547. }
  548. }
  549. #ifdef LOGCONFIG_USE_ICMAP
  550. icmap_iter_finalize(iter);
  551. #else
  552. cmap_iter_finalize(cmap_handle, iter);
  553. #endif
  554. logsys_config_apply();
  555. return 0;
  556. parse_error:
  557. *error_string = error_reason;
  558. return (-1);
  559. }
  560. #ifdef LOGCONFIG_USE_ICMAP
  561. static void main_logging_notify(
  562. int32_t event,
  563. const char *key_name,
  564. struct icmap_notify_value new_val,
  565. struct icmap_notify_value old_val,
  566. void *user_data)
  567. #else
  568. static void main_logging_notify(
  569. cmap_handle_t cmap_handle_unused,
  570. cmap_handle_t cmap_track_handle_unused,
  571. int32_t event,
  572. const char *key_name,
  573. struct cmap_notify_value new_val,
  574. struct cmap_notify_value old_val,
  575. void *user_data)
  576. #endif
  577. {
  578. const char *error_string;
  579. static int reload_in_progress = 0;
  580. /* If a full reload happens then suspend updates for individual keys until
  581. * it's all completed
  582. */
  583. if (strcmp(key_name, "config.reload_in_progress") == 0) {
  584. if (*(uint8_t *)new_val.data == 1) {
  585. reload_in_progress = 1;
  586. } else {
  587. reload_in_progress = 0;
  588. }
  589. }
  590. if (reload_in_progress) {
  591. log_printf(LOGSYS_LEVEL_DEBUG, "Ignoring key change, reload in progress. %s\n", key_name);
  592. return;
  593. }
  594. /*
  595. * Reload the logsys configuration
  596. */
  597. if (logsys_format_set(NULL) == -1) {
  598. fprintf (stderr, "Unable to setup logging format.\n");
  599. }
  600. corosync_main_config_read_logging(&error_string);
  601. }
  602. #ifdef LOGCONFIG_USE_ICMAP
  603. static void add_logsys_config_notification(void)
  604. {
  605. icmap_track_t icmap_track = NULL;
  606. icmap_track_add("logging.",
  607. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  608. main_logging_notify,
  609. NULL,
  610. &icmap_track);
  611. icmap_track_add("config.reload_in_progress",
  612. ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY,
  613. main_logging_notify,
  614. NULL,
  615. &icmap_track);
  616. }
  617. #else
  618. static void add_logsys_config_notification(void)
  619. {
  620. cmap_track_handle_t cmap_track;
  621. cmap_track_add(cmap_handle, "logging.",
  622. CMAP_TRACK_ADD | CMAP_TRACK_DELETE | CMAP_TRACK_MODIFY | CMAP_TRACK_PREFIX,
  623. main_logging_notify,
  624. NULL,
  625. &cmap_track);
  626. cmap_track_add(cmap_handle, "config.reload_in_progress",
  627. CMAP_TRACK_ADD | CMAP_TRACK_MODIFY,
  628. main_logging_notify,
  629. NULL,
  630. &cmap_track);
  631. }
  632. #endif
  633. int corosync_log_config_read (
  634. #ifndef LOGCONFIG_USE_ICMAP
  635. cmap_handle_t cmap_h,
  636. const char *default_logfile,
  637. #endif
  638. const char **error_string)
  639. {
  640. const char *error_reason = error_string_response;
  641. #ifndef LOGCONFIG_USE_ICMAP
  642. if (!cmap_h) {
  643. error_reason = "No cmap handle";
  644. return (-1);
  645. }
  646. if (!default_logfile) {
  647. error_reason = "No default logfile";
  648. return (-1);
  649. }
  650. cmap_handle = cmap_h;
  651. main_logfile = default_logfile;
  652. #endif
  653. if (corosync_main_config_read_logging(error_string) < 0) {
  654. error_reason = *error_string;
  655. goto parse_error;
  656. }
  657. add_logsys_config_notification();
  658. return 0;
  659. parse_error:
  660. snprintf (error_string_response, sizeof(error_string_response),
  661. "parse error in config: %s.\n",
  662. error_reason);
  663. *error_string = error_string_response;
  664. return (-1);
  665. }