logsys.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2007 Red Hat, Inc.
  4. *
  5. * Author: Steven Dake (sdake@redhat.com)
  6. * Author: Lon Hohberger (lhh@redhat.com)
  7. *
  8. * All rights reserved.
  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 <assert.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <stdarg.h>
  40. #include <sys/time.h>
  41. #include <time.h>
  42. #include <errno.h>
  43. #include <sys/types.h>
  44. #include <sys/socket.h>
  45. #if defined(COROSYNC_LINUX)
  46. #include <linux/un.h>
  47. #endif
  48. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  49. #include <sys/un.h>
  50. #endif
  51. #define SYSLOG_NAMES
  52. #include <syslog.h>
  53. #include <stdlib.h>
  54. #include <pthread.h>
  55. #include <corosync/engine/logsys.h>
  56. #include "wthread.h"
  57. /*
  58. * Configuration parameters for logging system
  59. */
  60. static char *logsys_name = NULL;
  61. static unsigned int logsys_mode = 0;
  62. static char *logsys_file = NULL;
  63. static FILE *logsys_file_fp = NULL;
  64. static int logsys_facility = LOG_DAEMON;
  65. static int logsys_wthread_active = 0;
  66. static pthread_mutex_t logsys_config_mutex = PTHREAD_MUTEX_INITIALIZER;
  67. static pthread_mutex_t logsys_new_log_mutex = PTHREAD_MUTEX_INITIALIZER;
  68. static struct worker_thread_group log_thread_group;
  69. static unsigned int dropped_log_entries = 0;
  70. #ifndef MAX_LOGGERS
  71. #define MAX_LOGGERS 32
  72. #endif
  73. struct logsys_logger logsys_loggers[MAX_LOGGERS];
  74. int logsys_single_id = 0;
  75. struct log_entry {
  76. char *file;
  77. int line;
  78. int priority;
  79. char str[128];
  80. struct log_entry *next;
  81. };
  82. static struct log_entry *head;
  83. static struct log_entry *tail;
  84. struct log_data {
  85. unsigned int syslog_pos;
  86. unsigned int priority;
  87. char *log_string;
  88. };
  89. enum logsys_config_mutex_state {
  90. LOGSYS_CONFIG_MUTEX_LOCKED,
  91. LOGSYS_CONFIG_MUTEX_UNLOCKED
  92. };
  93. static void logsys_atexit (void);
  94. #define LEVELMASK 0x07 /* 3 bits */
  95. #define LOG_LEVEL(p) ((p) & LEVELMASK)
  96. #define LOGSYS_IDMASK (0x3f << 3) /* 6 bits */
  97. #define LOG_ID(p) (((p) & LOGSYS_IDMASK) >> 3)
  98. static void logsys_buffer_flush (void);
  99. void _logsys_nosubsys_set (void)
  100. {
  101. logsys_mode |= LOG_MODE_NOSUBSYS;
  102. }
  103. int logsys_facility_id_get (const char *name)
  104. {
  105. unsigned int i;
  106. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  107. if (strcasecmp(name, facilitynames[i].c_name) == 0) {
  108. return (facilitynames[i].c_val);
  109. }
  110. }
  111. return (-1);
  112. }
  113. const char *logsys_facility_name_get (unsigned int facility)
  114. {
  115. unsigned int i;
  116. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  117. if (facility == facilitynames[i].c_val) {
  118. return (facilitynames[i].c_name);
  119. }
  120. }
  121. return (NULL);
  122. }
  123. int logsys_priority_id_get (const char *name)
  124. {
  125. unsigned int i;
  126. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  127. if (strcasecmp(name, prioritynames[i].c_name) == 0) {
  128. return (prioritynames[i].c_val);
  129. }
  130. }
  131. return (-1);
  132. }
  133. const char *logsys_priority_name_get (unsigned int priority)
  134. {
  135. unsigned int i;
  136. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  137. if (priority == prioritynames[i].c_val) {
  138. return (prioritynames[i].c_name);
  139. }
  140. }
  141. return (NULL);
  142. }
  143. unsigned int logsys_config_subsys_set (
  144. const char *subsys,
  145. unsigned int tags,
  146. unsigned int priority)
  147. {
  148. int i;
  149. pthread_mutex_lock (&logsys_config_mutex);
  150. for (i = 0; i < MAX_LOGGERS; i++) {
  151. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  152. logsys_loggers[i].tags = tags;
  153. logsys_loggers[i].priority = priority;
  154. break;
  155. }
  156. }
  157. if (i == MAX_LOGGERS) {
  158. for (i = 0; i < MAX_LOGGERS; i++) {
  159. if (strcmp (logsys_loggers[i].subsys, "") == 0) {
  160. strncpy (logsys_loggers[i].subsys, subsys,
  161. sizeof(logsys_loggers[i].subsys));
  162. logsys_loggers[i].tags = tags;
  163. logsys_loggers[i].priority = priority;
  164. break;
  165. }
  166. }
  167. }
  168. assert(i < MAX_LOGGERS);
  169. pthread_mutex_unlock (&logsys_config_mutex);
  170. return i;
  171. }
  172. inline int logsys_mkpri (int priority, int id)
  173. {
  174. return (((id) << 3) | (priority));
  175. }
  176. int logsys_config_subsys_get (
  177. const char *subsys,
  178. unsigned int *tags,
  179. unsigned int *priority)
  180. {
  181. unsigned int i;
  182. pthread_mutex_lock (&logsys_config_mutex);
  183. for (i = 0; i < MAX_LOGGERS; i++) {
  184. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  185. *tags = logsys_loggers[i].tags;
  186. *priority = logsys_loggers[i].priority;
  187. pthread_mutex_unlock (&logsys_config_mutex);
  188. return i;
  189. }
  190. }
  191. pthread_mutex_unlock (&logsys_config_mutex);
  192. return (-1);
  193. }
  194. static void buffered_log_printf (
  195. char *file,
  196. int line,
  197. int priority,
  198. char *format,
  199. va_list ap)
  200. {
  201. struct log_entry *entry = malloc(sizeof(struct log_entry));
  202. entry->file = file;
  203. entry->line = line;
  204. entry->priority = priority;
  205. entry->next = NULL;
  206. if (head == NULL) {
  207. head = tail = entry;
  208. } else {
  209. tail->next = entry;
  210. tail = entry;
  211. }
  212. vsnprintf(entry->str, sizeof(entry->str), format, ap);
  213. }
  214. static void log_printf_worker_fn (void *thread_data, void *work_item)
  215. {
  216. struct log_data *log_data = (struct log_data *)work_item;
  217. if (logsys_wthread_active)
  218. pthread_mutex_lock (&logsys_config_mutex);
  219. /*
  220. * Output the log data
  221. */
  222. if (logsys_mode & LOG_MODE_OUTPUT_FILE && logsys_file_fp != 0) {
  223. fprintf (logsys_file_fp, "%s", log_data->log_string);
  224. fflush (logsys_file_fp);
  225. }
  226. if (logsys_mode & LOG_MODE_OUTPUT_STDERR) {
  227. fprintf (stderr, "%s", log_data->log_string);
  228. fflush (stdout);
  229. }
  230. /* release mutex here in case syslog blocks */
  231. if (logsys_wthread_active)
  232. pthread_mutex_unlock (&logsys_config_mutex);
  233. if ((logsys_mode & LOG_MODE_OUTPUT_SYSLOG_THREADED) &&
  234. (!((logsys_mode & LOG_MODE_FILTER_DEBUG_FROM_SYSLOG) &&
  235. (log_data->priority == LOG_LEVEL_DEBUG)))) {
  236. syslog (log_data->priority,
  237. &log_data->log_string[log_data->syslog_pos]);
  238. }
  239. free (log_data->log_string);
  240. }
  241. static void _log_printf (
  242. enum logsys_config_mutex_state config_mutex_state,
  243. char *file,
  244. int line,
  245. int priority,
  246. int id,
  247. char *format,
  248. va_list ap)
  249. {
  250. char newstring[4096];
  251. char log_string[4096];
  252. char char_time[512];
  253. char *p = NULL;
  254. struct timeval tv;
  255. int i = 0;
  256. int len;
  257. struct log_data log_data;
  258. unsigned int res = 0;
  259. assert (id < MAX_LOGGERS);
  260. if (config_mutex_state == LOGSYS_CONFIG_MUTEX_UNLOCKED) {
  261. pthread_mutex_lock (&logsys_config_mutex);
  262. }
  263. pthread_mutex_lock (&logsys_new_log_mutex);
  264. /*
  265. ** Buffer before log has been configured has been called.
  266. */
  267. if (logsys_mode & LOG_MODE_BUFFER_BEFORE_CONFIG) {
  268. buffered_log_printf(file, line, logsys_mkpri(priority, id), format, ap);
  269. pthread_mutex_unlock (&logsys_new_log_mutex);
  270. if (config_mutex_state == LOGSYS_CONFIG_MUTEX_UNLOCKED) {
  271. pthread_mutex_unlock (&logsys_config_mutex);
  272. }
  273. return;
  274. }
  275. if (((logsys_mode & LOG_MODE_OUTPUT_FILE) || (logsys_mode & LOG_MODE_OUTPUT_STDERR)) &&
  276. (logsys_mode & LOG_MODE_DISPLAY_TIMESTAMP)) {
  277. gettimeofday (&tv, NULL);
  278. strftime (char_time, sizeof (char_time), "%b %e %k:%M:%S",
  279. localtime ((time_t *)&tv.tv_sec));
  280. i = sprintf (newstring, "%s.%06ld ", char_time, (long)tv.tv_usec);
  281. }
  282. if ((priority == LOG_LEVEL_DEBUG) || (logsys_mode & LOG_MODE_DISPLAY_FILELINE)) {
  283. if (logsys_mode & LOG_MODE_SHORT_FILELINE) {
  284. p = strrchr(file, '/');
  285. if (p)
  286. file = ++p;
  287. }
  288. sprintf (&newstring[i], "[%s:%04u] %s", file, line, format);
  289. } else {
  290. if (logsys_mode & LOG_MODE_NOSUBSYS) {
  291. sprintf (&newstring[i], "%s", format);
  292. } else {
  293. sprintf (&newstring[i], "[%-5s] %s", logsys_loggers[id].subsys, format);
  294. }
  295. }
  296. if (dropped_log_entries) {
  297. /*
  298. * Get rid of \n if there is one
  299. */
  300. if (newstring[strlen (newstring) - 1] == '\n') {
  301. newstring[strlen (newstring) - 1] = '\0';
  302. }
  303. len = sprintf (log_string,
  304. "%s - prior to this log entry, corosync logger dropped '%d' messages because of overflow.", newstring, dropped_log_entries + 1);
  305. } else {
  306. len = vsprintf (log_string, newstring, ap);
  307. }
  308. /*
  309. ** add line feed if not done yet
  310. */
  311. if (log_string[len - 1] != '\n') {
  312. log_string[len] = '\n';
  313. log_string[len + 1] = '\0';
  314. }
  315. /*
  316. * Create work thread data
  317. */
  318. log_data.syslog_pos = i;
  319. log_data.priority = priority;
  320. log_data.log_string = strdup (log_string);
  321. if (log_data.log_string == NULL) {
  322. goto drop_log_msg;
  323. }
  324. if (logsys_wthread_active) {
  325. res = worker_thread_group_work_add (&log_thread_group, &log_data);
  326. if (res == 0) {
  327. dropped_log_entries = 0;
  328. } else {
  329. dropped_log_entries += 1;
  330. }
  331. } else {
  332. log_printf_worker_fn (NULL, &log_data);
  333. }
  334. pthread_mutex_unlock (&logsys_new_log_mutex);
  335. if (config_mutex_state == LOGSYS_CONFIG_MUTEX_UNLOCKED) {
  336. pthread_mutex_unlock (&logsys_config_mutex);
  337. }
  338. return;
  339. drop_log_msg:
  340. dropped_log_entries++;
  341. pthread_mutex_unlock (&logsys_new_log_mutex);
  342. if (config_mutex_state == LOGSYS_CONFIG_MUTEX_UNLOCKED) {
  343. pthread_mutex_unlock (&logsys_config_mutex);
  344. }
  345. }
  346. unsigned int _logsys_subsys_create (
  347. const char *subsys,
  348. unsigned int priority)
  349. {
  350. assert (subsys != NULL);
  351. return logsys_config_subsys_set (
  352. subsys,
  353. LOGSYS_TAG_LOG,
  354. priority);
  355. }
  356. void logsys_config_mode_set (unsigned int mode)
  357. {
  358. pthread_mutex_lock (&logsys_config_mutex);
  359. logsys_mode = mode;
  360. if (mode & LOG_MODE_FLUSH_AFTER_CONFIG) {
  361. _logsys_wthread_create ();
  362. logsys_buffer_flush ();
  363. }
  364. pthread_mutex_unlock (&logsys_config_mutex);
  365. }
  366. unsigned int logsys_config_mode_get (void)
  367. {
  368. return logsys_mode;
  369. }
  370. int logsys_config_file_set (char **error_string, char *file)
  371. {
  372. static char error_string_response[512];
  373. if (file == NULL) {
  374. return (0);
  375. }
  376. pthread_mutex_lock (&logsys_new_log_mutex);
  377. pthread_mutex_lock (&logsys_config_mutex);
  378. if (logsys_mode & LOG_MODE_OUTPUT_FILE) {
  379. logsys_file = file;
  380. if (logsys_file_fp != NULL) {
  381. fclose (logsys_file_fp);
  382. }
  383. logsys_file_fp = fopen (file, "a+");
  384. if (logsys_file_fp == 0) {
  385. sprintf (error_string_response,
  386. "Can't open logfile '%s' for reason (%s).\n",
  387. file, strerror (errno));
  388. *error_string = error_string_response;
  389. pthread_mutex_unlock (&logsys_config_mutex);
  390. pthread_mutex_unlock (&logsys_new_log_mutex);
  391. return (-1);
  392. }
  393. }
  394. pthread_mutex_unlock (&logsys_config_mutex);
  395. pthread_mutex_unlock (&logsys_new_log_mutex);
  396. return (0);
  397. }
  398. void logsys_config_facility_set (char *name, unsigned int facility)
  399. {
  400. pthread_mutex_lock (&logsys_new_log_mutex);
  401. pthread_mutex_lock (&logsys_config_mutex);
  402. logsys_name = name;
  403. logsys_facility = facility;
  404. pthread_mutex_unlock (&logsys_config_mutex);
  405. pthread_mutex_unlock (&logsys_new_log_mutex);
  406. }
  407. void _logsys_config_priority_set (unsigned int id, unsigned int priority)
  408. {
  409. pthread_mutex_lock (&logsys_new_log_mutex);
  410. logsys_loggers[id].priority = priority;
  411. pthread_mutex_unlock (&logsys_new_log_mutex);
  412. }
  413. static void child_cleanup (void)
  414. {
  415. memset(&log_thread_group, 0, sizeof(log_thread_group));
  416. logsys_wthread_active = 0;
  417. pthread_mutex_init(&logsys_config_mutex, NULL);
  418. pthread_mutex_init(&logsys_new_log_mutex, NULL);
  419. }
  420. int _logsys_wthread_create (void)
  421. {
  422. worker_thread_group_init (
  423. &log_thread_group,
  424. 1,
  425. 1024,
  426. sizeof (struct log_data),
  427. 0,
  428. NULL,
  429. log_printf_worker_fn);
  430. logsys_flush();
  431. atexit (logsys_atexit);
  432. pthread_atfork(NULL, NULL, child_cleanup);
  433. if (logsys_mode & LOG_MODE_OUTPUT_SYSLOG_THREADED && logsys_name != NULL) {
  434. openlog (logsys_name, LOG_CONS|LOG_PID, logsys_facility);
  435. }
  436. logsys_wthread_active = 1;
  437. return (0);
  438. }
  439. void logsys_log_printf (
  440. char *file,
  441. int line,
  442. int priority,
  443. char *format,
  444. ...)
  445. {
  446. int id = LOG_ID(priority);
  447. int level = LOG_LEVEL(priority);
  448. va_list ap;
  449. assert (id < MAX_LOGGERS);
  450. if (LOG_LEVEL(priority) > logsys_loggers[id].priority) {
  451. return;
  452. }
  453. va_start (ap, format);
  454. _log_printf (LOGSYS_CONFIG_MUTEX_UNLOCKED, file, line, level, id,
  455. format, ap);
  456. va_end(ap);
  457. }
  458. static void logsys_log_printf_locked (
  459. char *file,
  460. int line,
  461. int priority,
  462. char *format,
  463. ...)
  464. {
  465. int id = LOG_ID(priority);
  466. int level = LOG_LEVEL(priority);
  467. va_list ap;
  468. assert (id < MAX_LOGGERS);
  469. if (LOG_LEVEL(priority) > logsys_loggers[id].priority) {
  470. return;
  471. }
  472. va_start (ap, format);
  473. _log_printf (LOGSYS_CONFIG_MUTEX_LOCKED, file, line, level, id,
  474. format, ap);
  475. va_end(ap);
  476. }
  477. void _logsys_log_printf2 (
  478. char *file,
  479. int line,
  480. int priority,
  481. int id,
  482. char *format, ...)
  483. {
  484. va_list ap;
  485. assert (id < MAX_LOGGERS);
  486. va_start (ap, format);
  487. _log_printf (LOGSYS_CONFIG_MUTEX_UNLOCKED, file, line, priority, id,
  488. format, ap);
  489. va_end(ap);
  490. }
  491. void _logsys_trace (char *file, int line, int tag, int id, char *format, ...)
  492. {
  493. assert (id < MAX_LOGGERS);
  494. pthread_mutex_lock (&logsys_config_mutex);
  495. if (tag & logsys_loggers[id].tags) {
  496. va_list ap;
  497. va_start (ap, format);
  498. _log_printf (LOGSYS_CONFIG_MUTEX_LOCKED, file, line,
  499. LOG_LEVEL_DEBUG, id, format, ap);
  500. va_end(ap);
  501. }
  502. pthread_mutex_unlock (&logsys_config_mutex);
  503. }
  504. static void logsys_atexit (void)
  505. {
  506. if (logsys_wthread_active) {
  507. worker_thread_group_wait (&log_thread_group);
  508. }
  509. if (logsys_mode & LOG_MODE_OUTPUT_SYSLOG_THREADED) {
  510. closelog ();
  511. }
  512. }
  513. static void logsys_buffer_flush (void)
  514. {
  515. struct log_entry *entry = head;
  516. struct log_entry *tmp;
  517. if (logsys_mode & LOG_MODE_FLUSH_AFTER_CONFIG) {
  518. logsys_mode &= ~LOG_MODE_FLUSH_AFTER_CONFIG;
  519. while (entry) {
  520. logsys_log_printf_locked (
  521. entry->file,
  522. entry->line,
  523. entry->priority,
  524. entry->str);
  525. tmp = entry;
  526. entry = entry->next;
  527. free (tmp);
  528. }
  529. }
  530. head = tail = NULL;
  531. }
  532. void logsys_flush (void)
  533. {
  534. worker_thread_group_wait (&log_thread_group);
  535. }
  536. int logsys_init (char *name, int mode, int facility, int priority, char *file)
  537. {
  538. char *errstr;
  539. /* logsys_subsys_id will be 0 */
  540. logsys_single_id = 1;
  541. strncpy (logsys_loggers[0].subsys, name,
  542. sizeof (logsys_loggers[0].subsys));
  543. logsys_config_mode_set (mode);
  544. logsys_config_facility_set (name, facility);
  545. logsys_config_file_set (&errstr, file);
  546. _logsys_config_priority_set (0, priority);
  547. if ((mode & LOG_MODE_BUFFER_BEFORE_CONFIG) == 0) {
  548. _logsys_wthread_create ();
  549. }
  550. return (0);
  551. }
  552. int logsys_conf (char *name, int mode, int facility, int priority, char *file)
  553. {
  554. char *errstr;
  555. strncpy (logsys_loggers[0].subsys, name,
  556. sizeof (logsys_loggers[0].subsys));
  557. logsys_config_mode_set (mode);
  558. logsys_config_facility_set (name, facility);
  559. logsys_config_file_set (&errstr, file);
  560. _logsys_config_priority_set (0, priority);
  561. return (0);
  562. }
  563. void logsys_exit (void)
  564. {
  565. logsys_flush ();
  566. }