4
0

logsys.c 22 KB

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