4
0

logsys.c 22 KB

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