logsys.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2012 Red Hat, Inc.
  4. *
  5. * Author: Steven Dake (sdake@redhat.com)
  6. * Author: Lon Hohberger (lhh@redhat.com)
  7. * Author: Fabio M. Di Nitto (fdinitto@redhat.com)
  8. *
  9. * All rights reserved.
  10. *
  11. * This software licensed under BSD license, the text of which follows:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * - Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  22. * contributors may be used to endorse or promote products derived from this
  23. * software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  35. * THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <config.h>
  38. #include <stdint.h>
  39. #include <ctype.h>
  40. #include <assert.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <qb/qbdefs.h>
  44. #include <qb/qbutil.h>
  45. #include <qb/qblog.h>
  46. #include <corosync/logsys.h>
  47. /*
  48. * syslog prioritynames, facility names to value mapping
  49. * Some C libraries build this in to their headers, but it is non-portable
  50. * so logsys supplies its own version.
  51. */
  52. struct syslog_names {
  53. const char *c_name;
  54. int c_val;
  55. };
  56. static struct syslog_names prioritynames[] =
  57. {
  58. { "alert", LOG_ALERT },
  59. { "crit", LOG_CRIT },
  60. { "debug", LOG_DEBUG },
  61. { "emerg", LOG_EMERG },
  62. { "err", LOG_ERR },
  63. { "error", LOG_ERR },
  64. { "info", LOG_INFO },
  65. { "notice", LOG_NOTICE },
  66. { "warning", LOG_WARNING },
  67. { NULL, -1 }
  68. };
  69. #define MAX_FILES_PER_SUBSYS 32
  70. #ifdef HAVE_SMALL_MEMORY_FOOTPRINT
  71. #define IPC_LOGSYS_SIZE 8192*64
  72. #else
  73. #define IPC_LOGSYS_SIZE 8192*1024
  74. #endif
  75. /*
  76. * need unlogical order to preserve 64bit alignment
  77. */
  78. struct logsys_logger {
  79. char subsys[LOGSYS_MAX_SUBSYS_NAMELEN]; /* subsystem name */
  80. char *logfile; /* log to file */
  81. unsigned int mode; /* subsystem mode */
  82. unsigned int debug; /* debug on|off|trace */
  83. int syslog_priority; /* priority */
  84. int logfile_priority; /* priority to file */
  85. int init_status; /* internal field to handle init queues
  86. for subsystems */
  87. int32_t target_id;
  88. char *files[MAX_FILES_PER_SUBSYS];
  89. int32_t file_idx;
  90. int32_t dirty;
  91. };
  92. /* values for logsys_logger init_status */
  93. #define LOGSYS_LOGGER_INIT_DONE 0
  94. #define LOGSYS_LOGGER_NEEDS_INIT 1
  95. static int logsys_system_needs_init = LOGSYS_LOGGER_NEEDS_INIT;
  96. static struct logsys_logger logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT + 1];
  97. static int32_t _logsys_config_mode_set_unlocked(int32_t subsysid, uint32_t new_mode);
  98. static void _logsys_config_apply_per_file(int32_t s, const char *filename);
  99. static void _logsys_config_apply_per_subsys(int32_t s);
  100. static void _logsys_subsys_filename_add (int32_t s, const char *filename);
  101. static void logsys_file_format_get(char* file_format, int buf_len);
  102. static char *format_buffer=NULL;
  103. static int logsys_thread_started = 0;
  104. static int logsys_blackbox_enabled = 1;
  105. static int _logsys_config_subsys_get_unlocked (const char *subsys)
  106. {
  107. int i;
  108. if (!subsys) {
  109. return LOGSYS_MAX_SUBSYS_COUNT;
  110. }
  111. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  112. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  113. return i;
  114. }
  115. }
  116. return (-1);
  117. }
  118. /*
  119. * we need a version that can work when somebody else is already
  120. * holding a config mutex lock or we will never get out of here
  121. */
  122. static int logsys_config_file_set_unlocked (
  123. int subsysid,
  124. const char **error_string,
  125. const char *file)
  126. {
  127. static char error_string_response[512];
  128. int i;
  129. char file_format[128];
  130. if (logsys_loggers[subsysid].target_id > 0) {
  131. int32_t f;
  132. for (f = 0; f < logsys_loggers[subsysid].file_idx; f++) {
  133. qb_log_filter_ctl(logsys_loggers[subsysid].target_id,
  134. QB_LOG_FILTER_REMOVE,
  135. QB_LOG_FILTER_FILE,
  136. logsys_loggers[subsysid].files[f],
  137. LOG_TRACE);
  138. }
  139. }
  140. logsys_loggers[subsysid].dirty = QB_TRUE;
  141. if (file == NULL) {
  142. return (0);
  143. }
  144. if (logsys_loggers[subsysid].target_id > 0 &&
  145. logsys_loggers[subsysid].logfile != NULL &&
  146. strcmp(file, logsys_loggers[subsysid].logfile) == 0) {
  147. return (0);
  148. }
  149. if (strlen(file) >= PATH_MAX) {
  150. snprintf (error_string_response,
  151. sizeof(error_string_response),
  152. "%s: logfile name exceed maximum system filename length",
  153. logsys_loggers[subsysid].subsys);
  154. *error_string = error_string_response;
  155. return (-1);
  156. }
  157. if (logsys_loggers[subsysid].logfile != NULL) {
  158. free(logsys_loggers[subsysid].logfile);
  159. logsys_loggers[subsysid].logfile = NULL;
  160. }
  161. logsys_loggers[subsysid].logfile = strdup(file);
  162. if (logsys_loggers[subsysid].logfile == NULL) {
  163. snprintf (error_string_response,
  164. sizeof(error_string_response),
  165. "Unable to allocate memory for logfile '%s'",
  166. file);
  167. *error_string = error_string_response;
  168. return (-1);
  169. }
  170. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  171. if ((logsys_loggers[i].logfile != NULL) &&
  172. (strcmp (logsys_loggers[i].logfile, file) == 0) &&
  173. (i != subsysid)) {
  174. /* we have found another subsys with this config file
  175. * so add a filter
  176. */
  177. logsys_loggers[subsysid].target_id = logsys_loggers[i].target_id;
  178. return (0);
  179. }
  180. }
  181. if (logsys_loggers[subsysid].target_id > 0) {
  182. int num_using_current = 0;
  183. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  184. if (logsys_loggers[subsysid].target_id ==
  185. logsys_loggers[i].target_id) {
  186. num_using_current++;
  187. }
  188. }
  189. if (num_using_current == 1) {
  190. /* no one else is using this close it */
  191. qb_log_file_close(logsys_loggers[subsysid].target_id);
  192. }
  193. }
  194. logsys_loggers[subsysid].target_id = qb_log_file_open(file);
  195. if (logsys_loggers[subsysid].target_id < 0) {
  196. int err = -logsys_loggers[subsysid].target_id;
  197. char error_str[LOGSYS_MAX_PERROR_MSG_LEN];
  198. const char *error_ptr;
  199. error_ptr = qb_strerror_r(err, error_str, sizeof(error_str));
  200. free(logsys_loggers[subsysid].logfile);
  201. logsys_loggers[subsysid].logfile = NULL;
  202. snprintf (error_string_response,
  203. sizeof(error_string_response),
  204. "Can't open logfile '%s' for reason: %s (%d)",
  205. file, error_ptr, err);
  206. *error_string = error_string_response;
  207. return (-1);
  208. }
  209. logsys_file_format_get(file_format, 128);
  210. qb_log_format_set(logsys_loggers[subsysid].target_id, file_format);
  211. qb_log_ctl(logsys_loggers[subsysid].target_id,
  212. QB_LOG_CONF_ENABLED,
  213. (logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_FILE));
  214. if (logsys_thread_started) {
  215. qb_log_ctl(logsys_loggers[subsysid].target_id, QB_LOG_CONF_THREADED, QB_TRUE);
  216. }
  217. return (0);
  218. }
  219. static void logsys_subsys_init (
  220. const char *subsys,
  221. int subsysid)
  222. {
  223. if (logsys_system_needs_init == LOGSYS_LOGGER_NEEDS_INIT) {
  224. logsys_loggers[subsysid].init_status =
  225. LOGSYS_LOGGER_NEEDS_INIT;
  226. } else {
  227. logsys_loggers[subsysid].mode = logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode;
  228. logsys_loggers[subsysid].debug = logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].debug;
  229. logsys_loggers[subsysid].syslog_priority = logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].syslog_priority;
  230. logsys_loggers[subsysid].logfile_priority = logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].logfile_priority;
  231. logsys_loggers[subsysid].init_status = LOGSYS_LOGGER_INIT_DONE;
  232. }
  233. strncpy (logsys_loggers[subsysid].subsys, subsys,
  234. sizeof (logsys_loggers[subsysid].subsys));
  235. logsys_loggers[subsysid].subsys[
  236. sizeof (logsys_loggers[subsysid].subsys) - 1] = '\0';
  237. logsys_loggers[subsysid].file_idx = 0;
  238. }
  239. static const char *_logsys_tags_stringify(uint32_t tags)
  240. {
  241. if (tags == QB_LOG_TAG_LIBQB_MSG) {
  242. return "QB";
  243. } else {
  244. return logsys_loggers[tags].subsys;
  245. }
  246. }
  247. void logsys_system_fini (void)
  248. {
  249. int i;
  250. int f;
  251. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  252. free(logsys_loggers[i].logfile);
  253. for (f = 0; f < logsys_loggers[i].file_idx; f++) {
  254. free(logsys_loggers[i].files[f]);
  255. }
  256. }
  257. qb_log_fini ();
  258. }
  259. /*
  260. * Internal API - exported
  261. */
  262. int _logsys_system_setup(
  263. const char *mainsystem,
  264. unsigned int mode,
  265. int syslog_facility,
  266. int syslog_priority)
  267. {
  268. int i;
  269. int32_t fidx;
  270. char tempsubsys[LOGSYS_MAX_SUBSYS_NAMELEN];
  271. if ((mainsystem == NULL) ||
  272. (strlen(mainsystem) >= LOGSYS_MAX_SUBSYS_NAMELEN)) {
  273. return -1;
  274. }
  275. /*
  276. * Setup libqb as a subsys
  277. */
  278. i = _logsys_subsys_create ("QB", "array.c,log.c,log_syslog.c,log_blackbox.c,log_format.c,"
  279. "log_file.c,log_dcs.c,log_thread.c,ipc_shm.c,ipcs.c,ipc_us.c,loop.c,"
  280. "loop_poll_epoll.c,loop_job.c,loop_poll_poll.c,loop_poll_kqueue.c,"
  281. "loop_timerlist.c,loop_poll.c,ringbuffer.c,ringbuffer_helper.c,trie.c,"
  282. "map.c,skiplist.c,rpl_sem.c,hdb.c,unix.c,hashtable.c,strlcpy.c,ipc_socket.c,"
  283. "strchrnul.c,ipc_setup.c,strlcat.c");
  284. if (i < 0) {
  285. return -1;
  286. }
  287. /*
  288. * name clash
  289. * _logsys_subsys_filename_add (i, "util.c");
  290. */
  291. /*
  292. * This file (logsys.c) is not exactly QB. We need tag for logsys.c if flightrecorder init
  293. * fails, and QB seems to be closest.
  294. */
  295. _logsys_subsys_filename_add (i, "logsys.c");
  296. i = LOGSYS_MAX_SUBSYS_COUNT;
  297. snprintf(logsys_loggers[i].subsys,
  298. LOGSYS_MAX_SUBSYS_NAMELEN,
  299. "%s", mainsystem);
  300. logsys_loggers[i].mode = mode;
  301. logsys_loggers[i].debug = LOGSYS_DEBUG_OFF;
  302. logsys_loggers[i].file_idx = 0;
  303. logsys_loggers[i].logfile_priority = syslog_priority;
  304. logsys_loggers[i].syslog_priority = syslog_priority;
  305. qb_log_init(mainsystem, syslog_facility, syslog_priority);
  306. if (logsys_loggers[i].mode & LOGSYS_MODE_OUTPUT_STDERR) {
  307. qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
  308. } else {
  309. qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_FALSE);
  310. }
  311. if (logsys_loggers[i].mode & LOGSYS_MODE_OUTPUT_SYSLOG) {
  312. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_TRUE);
  313. } else {
  314. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
  315. }
  316. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_PRIORITY_BUMP, LOG_INFO - LOG_DEBUG);
  317. qb_log_filter_ctl(QB_LOG_BLACKBOX, QB_LOG_FILTER_ADD,
  318. QB_LOG_FILTER_FILE, "*", LOG_TRACE);
  319. qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_SIZE, IPC_LOGSYS_SIZE);
  320. qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_THREADED, QB_FALSE);
  321. /*
  322. * Blackbox is disabled at the init and enabled later based
  323. * on config (logging.blackbox) value.
  324. */
  325. qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_FALSE);
  326. if (logsys_format_set(NULL) == -1) {
  327. return -1;
  328. }
  329. qb_log_tags_stringify_fn_set(_logsys_tags_stringify);
  330. logsys_loggers[i].init_status = LOGSYS_LOGGER_INIT_DONE;
  331. logsys_system_needs_init = LOGSYS_LOGGER_INIT_DONE;
  332. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  333. if ((strcmp (logsys_loggers[i].subsys, "") != 0) &&
  334. (logsys_loggers[i].init_status ==
  335. LOGSYS_LOGGER_NEEDS_INIT)) {
  336. fidx = logsys_loggers[i].file_idx;
  337. strncpy (tempsubsys, logsys_loggers[i].subsys,
  338. sizeof (tempsubsys));
  339. tempsubsys[sizeof (tempsubsys) - 1] = '\0';
  340. logsys_subsys_init(tempsubsys, i);
  341. logsys_loggers[i].file_idx = fidx;
  342. _logsys_config_mode_set_unlocked(i, logsys_loggers[i].mode);
  343. _logsys_config_apply_per_subsys(i);
  344. }
  345. }
  346. return (0);
  347. }
  348. static void _logsys_subsys_filename_add (int32_t s, const char *filename)
  349. {
  350. int i;
  351. if (filename == NULL) {
  352. return;
  353. }
  354. assert(logsys_loggers[s].file_idx < MAX_FILES_PER_SUBSYS);
  355. assert(logsys_loggers[s].file_idx >= 0);
  356. for (i = 0; i < logsys_loggers[s].file_idx; i++) {
  357. if (strcmp(logsys_loggers[s].files[i], filename) == 0) {
  358. return;
  359. }
  360. }
  361. logsys_loggers[s].files[logsys_loggers[s].file_idx++] = strdup(filename);
  362. if (logsys_system_needs_init == LOGSYS_LOGGER_INIT_DONE) {
  363. _logsys_config_apply_per_file(s, filename);
  364. }
  365. }
  366. int _logsys_subsys_create (const char *subsys, const char *filename)
  367. {
  368. int i;
  369. if ((subsys == NULL) ||
  370. (strlen(subsys) >= LOGSYS_MAX_SUBSYS_NAMELEN)) {
  371. return -1;
  372. }
  373. i = _logsys_config_subsys_get_unlocked (subsys);
  374. if ((i > -1) && (i < LOGSYS_MAX_SUBSYS_COUNT)) {
  375. _logsys_subsys_filename_add(i, filename);
  376. return i;
  377. }
  378. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  379. if (strcmp (logsys_loggers[i].subsys, "") == 0) {
  380. logsys_subsys_init(subsys, i);
  381. _logsys_subsys_filename_add(i, filename);
  382. break;
  383. }
  384. }
  385. if (i >= LOGSYS_MAX_SUBSYS_COUNT) {
  386. i = -1;
  387. }
  388. return i;
  389. }
  390. int _logsys_config_subsys_get (const char *subsys)
  391. {
  392. int i;
  393. i = _logsys_config_subsys_get_unlocked (subsys);
  394. return i;
  395. }
  396. static int32_t _logsys_config_mode_set_unlocked(int32_t subsysid, uint32_t new_mode)
  397. {
  398. if ( logsys_loggers[subsysid].mode == new_mode) {
  399. return 0;
  400. }
  401. if (logsys_loggers[subsysid].target_id > 0) {
  402. qb_log_ctl(logsys_loggers[subsysid].target_id,
  403. QB_LOG_CONF_ENABLED,
  404. (new_mode & LOGSYS_MODE_OUTPUT_FILE));
  405. }
  406. if (subsysid == LOGSYS_MAX_SUBSYS_COUNT) {
  407. qb_log_ctl(QB_LOG_STDERR,
  408. QB_LOG_CONF_ENABLED,
  409. (new_mode & LOGSYS_MODE_OUTPUT_STDERR));
  410. qb_log_ctl(QB_LOG_SYSLOG,
  411. QB_LOG_CONF_ENABLED,
  412. (new_mode & LOGSYS_MODE_OUTPUT_SYSLOG));
  413. }
  414. logsys_loggers[subsysid].mode = new_mode;
  415. return 0;
  416. }
  417. int logsys_config_mode_set (const char *subsys, unsigned int mode)
  418. {
  419. int i;
  420. if (subsys != NULL) {
  421. i = _logsys_config_subsys_get_unlocked (subsys);
  422. if (i >= 0) {
  423. i = _logsys_config_mode_set_unlocked(i, mode);
  424. }
  425. } else {
  426. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  427. _logsys_config_mode_set_unlocked(i, mode);
  428. }
  429. i = 0;
  430. }
  431. return i;
  432. }
  433. unsigned int logsys_config_mode_get (const char *subsys)
  434. {
  435. int i;
  436. i = _logsys_config_subsys_get (subsys);
  437. if (i < 0) {
  438. return i;
  439. }
  440. return logsys_loggers[i].mode;
  441. }
  442. int logsys_config_file_set (
  443. const char *subsys,
  444. const char **error_string,
  445. const char *file)
  446. {
  447. int i;
  448. int res;
  449. if (subsys != NULL) {
  450. i = _logsys_config_subsys_get_unlocked (subsys);
  451. if (i < 0) {
  452. res = i;
  453. } else {
  454. res = logsys_config_file_set_unlocked(i, error_string, file);
  455. }
  456. } else {
  457. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  458. res = logsys_config_file_set_unlocked(i, error_string, file);
  459. if (res < 0) {
  460. break;
  461. }
  462. }
  463. }
  464. return res;
  465. }
  466. static void
  467. logsys_file_format_get(char* file_format, int buf_len)
  468. {
  469. char *format_buffer_start;
  470. char *str_pos;
  471. file_format[0] = '\0';
  472. format_buffer_start = format_buffer;
  473. if ((str_pos = strstr(format_buffer, "%t"))) {
  474. strcpy(file_format, "%t ");
  475. format_buffer_start = str_pos + 2;
  476. }
  477. if ((str_pos = strstr(format_buffer, "%T"))) {
  478. strcpy(file_format, "%T ");
  479. format_buffer_start = str_pos + 2;
  480. }
  481. strcat(file_format, "[%P] %H %N");
  482. strncat(file_format, format_buffer_start, buf_len - strlen(file_format));
  483. }
  484. int logsys_format_set (const char *format)
  485. {
  486. int i;
  487. int c;
  488. int w;
  489. int reminder;
  490. char syslog_format[128];
  491. char file_format[128];
  492. if (format_buffer) {
  493. free(format_buffer);
  494. format_buffer = NULL;
  495. }
  496. format_buffer = strdup(format ? format : "%7p [%6g] %b");
  497. if (format_buffer == NULL) {
  498. return -1;
  499. }
  500. qb_log_format_set(QB_LOG_STDERR, format_buffer);
  501. logsys_file_format_get(file_format, 128);
  502. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  503. if (logsys_loggers[i].target_id > 0) {
  504. qb_log_format_set(logsys_loggers[i].target_id, file_format);
  505. }
  506. }
  507. /*
  508. * This just goes through and remove %t, %T and %p from
  509. * the format string for syslog.
  510. */
  511. w = 0;
  512. memset(syslog_format, '\0', sizeof(syslog_format));
  513. for (c = 0; c < strlen(format_buffer); c++) {
  514. if (format_buffer[c] == '%') {
  515. reminder = c;
  516. for (c++; c < strlen(format_buffer); c++) {
  517. if (isdigit(format_buffer[c])) {
  518. continue;
  519. }
  520. if (format_buffer[c] == 't' ||
  521. format_buffer[c] == 'p' ||
  522. format_buffer[c] == 'T') {
  523. c++;
  524. } else {
  525. c = reminder;
  526. }
  527. break;
  528. }
  529. }
  530. syslog_format[w] = format_buffer[c];
  531. w++;
  532. }
  533. qb_log_format_set(QB_LOG_SYSLOG, syslog_format);
  534. return 0;
  535. }
  536. char *logsys_format_get (void)
  537. {
  538. return format_buffer;
  539. }
  540. int logsys_config_syslog_facility_set (
  541. const char *subsys,
  542. unsigned int facility)
  543. {
  544. return qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_FACILITY, facility);
  545. }
  546. int logsys_config_syslog_priority_set (
  547. const char *subsys,
  548. unsigned int priority)
  549. {
  550. int i;
  551. if (subsys != NULL) {
  552. i = _logsys_config_subsys_get_unlocked (subsys);
  553. if (i >= 0) {
  554. logsys_loggers[i].syslog_priority = priority;
  555. logsys_loggers[i].dirty = QB_TRUE;
  556. i = 0;
  557. }
  558. } else {
  559. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  560. logsys_loggers[i].syslog_priority = priority;
  561. logsys_loggers[i].dirty = QB_TRUE;
  562. }
  563. i = 0;
  564. }
  565. return i;
  566. }
  567. int logsys_config_logfile_priority_set (
  568. const char *subsys,
  569. unsigned int priority)
  570. {
  571. int i;
  572. if (subsys != NULL) {
  573. i = _logsys_config_subsys_get_unlocked (subsys);
  574. if (i >= 0) {
  575. logsys_loggers[i].logfile_priority = priority;
  576. logsys_loggers[i].dirty = QB_TRUE;
  577. i = 0;
  578. }
  579. } else {
  580. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  581. logsys_loggers[i].logfile_priority = priority;
  582. logsys_loggers[i].dirty = QB_TRUE;
  583. }
  584. i = 0;
  585. }
  586. return i;
  587. }
  588. static void _logsys_config_apply_per_file(int32_t s, const char *filename)
  589. {
  590. uint32_t syslog_priority = logsys_loggers[s].syslog_priority;
  591. uint32_t logfile_priority = logsys_loggers[s].logfile_priority;
  592. qb_log_filter_ctl(s, QB_LOG_TAG_SET, QB_LOG_FILTER_FILE,
  593. filename, LOG_TRACE);
  594. qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_REMOVE,
  595. QB_LOG_FILTER_FILE, filename, LOG_TRACE);
  596. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_REMOVE,
  597. QB_LOG_FILTER_FILE, filename, LOG_TRACE);
  598. if (logsys_loggers[s].target_id > 0) {
  599. qb_log_filter_ctl(logsys_loggers[s].target_id,
  600. QB_LOG_FILTER_REMOVE,
  601. QB_LOG_FILTER_FILE, filename, LOG_TRACE);
  602. }
  603. if (logsys_loggers[s].debug != LOGSYS_DEBUG_OFF) {
  604. switch (logsys_loggers[s].debug) {
  605. case LOGSYS_DEBUG_ON:
  606. syslog_priority = LOG_DEBUG;
  607. logfile_priority = LOG_DEBUG;
  608. break;
  609. case LOGSYS_DEBUG_TRACE:
  610. syslog_priority = LOG_TRACE;
  611. logfile_priority = LOG_TRACE;
  612. break;
  613. default:
  614. assert(0);
  615. }
  616. }
  617. qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_ADD,
  618. QB_LOG_FILTER_FILE, filename,
  619. syslog_priority);
  620. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
  621. QB_LOG_FILTER_FILE, filename,
  622. logfile_priority);
  623. if (logsys_loggers[s].target_id > 0) {
  624. qb_log_filter_ctl(logsys_loggers[s].target_id,
  625. QB_LOG_FILTER_ADD,
  626. QB_LOG_FILTER_FILE, filename,
  627. logfile_priority);
  628. }
  629. }
  630. static void _logsys_config_apply_per_subsys(int32_t s)
  631. {
  632. int32_t f;
  633. for (f = 0; f < logsys_loggers[s].file_idx; f++) {
  634. _logsys_config_apply_per_file(s, logsys_loggers[s].files[f]);
  635. }
  636. if (logsys_loggers[s].target_id > 0) {
  637. qb_log_ctl(logsys_loggers[s].target_id,
  638. QB_LOG_CONF_ENABLED,
  639. (logsys_loggers[s].mode & LOGSYS_MODE_OUTPUT_FILE));
  640. }
  641. logsys_loggers[s].dirty = QB_FALSE;
  642. }
  643. static void _logsys_config_apply_blackbox(void) {
  644. int blackbox_enable_res;
  645. blackbox_enable_res = qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, logsys_blackbox_enabled);
  646. if (blackbox_enable_res < 0) {
  647. LOGSYS_PERROR (-blackbox_enable_res, LOGSYS_LEVEL_WARNING,
  648. "Unable to initialize log flight recorder. "\
  649. "The most common cause of this error is " \
  650. "not enough space on /dev/shm. Corosync will continue work, " \
  651. "but blackbox will not be available");
  652. }
  653. }
  654. void logsys_config_apply(void)
  655. {
  656. int32_t s;
  657. _logsys_config_apply_blackbox();
  658. for (s = 0; s <= LOGSYS_MAX_SUBSYS_COUNT; s++) {
  659. if (strcmp(logsys_loggers[s].subsys, "") == 0) {
  660. continue;
  661. }
  662. _logsys_config_apply_per_subsys(s);
  663. }
  664. }
  665. extern int logsys_config_debug_get (
  666. const char *subsys)
  667. {
  668. int debug_level = logsys_loggers[0].debug;
  669. int i;
  670. if (subsys != NULL) {
  671. i = _logsys_config_subsys_get_unlocked (subsys);
  672. if (i >= 0) {
  673. debug_level = logsys_loggers[i].debug;
  674. }
  675. }
  676. return debug_level;
  677. }
  678. int logsys_config_debug_set (
  679. const char *subsys,
  680. unsigned int debug)
  681. {
  682. int i;
  683. if (subsys != NULL) {
  684. i = _logsys_config_subsys_get_unlocked (subsys);
  685. if (i >= 0) {
  686. logsys_loggers[i].dirty = QB_TRUE;
  687. logsys_loggers[i].debug = debug;
  688. i = 0;
  689. }
  690. } else {
  691. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  692. logsys_loggers[i].debug = debug;
  693. logsys_loggers[i].dirty = QB_TRUE;
  694. }
  695. i = 0;
  696. }
  697. return i;
  698. }
  699. int logsys_priority_id_get (const char *name)
  700. {
  701. unsigned int i;
  702. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  703. if (strcasecmp(name, prioritynames[i].c_name) == 0) {
  704. return (prioritynames[i].c_val);
  705. }
  706. }
  707. return (-1);
  708. }
  709. int logsys_thread_start (void)
  710. {
  711. int i;
  712. int err;
  713. err = qb_log_thread_start();
  714. if (err != 0) {
  715. return (err);
  716. }
  717. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_THREADED, QB_TRUE);
  718. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  719. if (logsys_loggers[i].target_id > 0) {
  720. qb_log_ctl(logsys_loggers[i].target_id, QB_LOG_CONF_THREADED, QB_TRUE);
  721. }
  722. }
  723. logsys_thread_started = 1;
  724. return (0);
  725. }
  726. void logsys_blackbox_set(int enable)
  727. {
  728. logsys_blackbox_enabled = enable;
  729. }
  730. /*
  731. * To set correct pid to qb blackbox filename after tty dettach (fork) we have to
  732. * close (this function) and (if needed) reopen blackbox (logsys_blackbox_postfork function).
  733. */
  734. void logsys_blackbox_prefork(void)
  735. {
  736. (void)qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_FALSE);
  737. }
  738. void logsys_blackbox_postfork(void)
  739. {
  740. _logsys_config_apply_blackbox();
  741. }
  742. cs_error_t logsys_reopen_log_files(void)
  743. {
  744. cs_error_t res;
  745. #ifdef HAVE_QB_LOG_FILE_REOPEN
  746. int i, j;
  747. int num_using_current;
  748. int32_t rc;
  749. res = CS_OK;
  750. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  751. if (logsys_loggers[i].target_id <= 0 || logsys_loggers[i].logfile == NULL) {
  752. continue ;
  753. }
  754. num_using_current = 0;
  755. for (j = 0; j <= i; j++) {
  756. if (logsys_loggers[i].target_id == logsys_loggers[j].target_id) {
  757. num_using_current++;
  758. }
  759. }
  760. if (num_using_current == 1) {
  761. /*
  762. * First instance of target file. Reopen it.
  763. */
  764. rc = qb_log_file_reopen(logsys_loggers[i].target_id, NULL);
  765. if (rc != 0) {
  766. LOGSYS_PERROR (-rc, LOGSYS_LEVEL_WARNING,
  767. "Unable to reopen log file %s", logsys_loggers[i].logfile);
  768. res = qb_to_cs_error(rc);
  769. }
  770. }
  771. }
  772. #else
  773. res = CS_ERR_NOT_SUPPORTED;
  774. #endif
  775. return (res);
  776. }