logsys.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2010 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 <stdio.h>
  40. #include <ctype.h>
  41. #include <string.h>
  42. #include <stdarg.h>
  43. #include <sys/time.h>
  44. #include <sys/stat.h>
  45. #include <fcntl.h>
  46. #include <time.h>
  47. #include <errno.h>
  48. #include <sys/types.h>
  49. #include <sys/socket.h>
  50. #include <unistd.h>
  51. #if defined(COROSYNC_LINUX)
  52. #include <linux/un.h>
  53. #endif
  54. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  55. #include <sys/un.h>
  56. #endif
  57. #include <syslog.h>
  58. #include <stdlib.h>
  59. #include <pthread.h>
  60. #include <limits.h>
  61. #include <sys/mman.h>
  62. #include <semaphore.h>
  63. #include <corosync/list.h>
  64. #include <corosync/engine/logsys.h>
  65. #include "util.h"
  66. #define YIELD_AFTER_LOG_OPS 10
  67. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  68. #define ROUNDUP(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
  69. /*
  70. * syslog prioritynames, facility names to value mapping
  71. * Some C libraries build this in to their headers, but it is non-portable
  72. * so logsys supplies its own version.
  73. */
  74. struct syslog_names {
  75. const char *c_name;
  76. int c_val;
  77. };
  78. struct syslog_names prioritynames[] =
  79. {
  80. { "alert", LOG_ALERT },
  81. { "crit", LOG_CRIT },
  82. { "debug", LOG_DEBUG },
  83. { "emerg", LOG_EMERG },
  84. { "err", LOG_ERR },
  85. { "error", LOG_ERR },
  86. { "info", LOG_INFO },
  87. { "notice", LOG_NOTICE },
  88. { "warning", LOG_WARNING },
  89. { NULL, -1 }
  90. };
  91. struct syslog_names facilitynames[] =
  92. {
  93. { "auth", LOG_AUTH },
  94. { "cron", LOG_CRON },
  95. { "daemon", LOG_DAEMON },
  96. { "kern", LOG_KERN },
  97. { "lpr", LOG_LPR },
  98. { "mail", LOG_MAIL },
  99. { "news", LOG_NEWS },
  100. { "syslog", LOG_SYSLOG },
  101. { "user", LOG_USER },
  102. { "uucp", LOG_UUCP },
  103. { "local0", LOG_LOCAL0 },
  104. { "local1", LOG_LOCAL1 },
  105. { "local2", LOG_LOCAL2 },
  106. { "local3", LOG_LOCAL3 },
  107. { "local4", LOG_LOCAL4 },
  108. { "local5", LOG_LOCAL5 },
  109. { "local6", LOG_LOCAL6 },
  110. { "local7", LOG_LOCAL7 },
  111. { NULL, -1 }
  112. };
  113. struct record {
  114. unsigned int rec_ident;
  115. const char *file_name;
  116. const char *function_name;
  117. int file_line;
  118. char *buffer;
  119. struct list_head list;
  120. };
  121. /*
  122. * need unlogical order to preserve 64bit alignment
  123. */
  124. struct logsys_logger {
  125. char subsys[LOGSYS_MAX_SUBSYS_NAMELEN]; /* subsystem name */
  126. char *logfile; /* log to file */
  127. FILE *logfile_fp; /* track file descriptor */
  128. unsigned int mode; /* subsystem mode */
  129. unsigned int debug; /* debug on|off */
  130. int syslog_facility; /* facility */
  131. int syslog_priority; /* priority */
  132. int logfile_priority; /* priority to file */
  133. int init_status; /* internal field to handle init queues
  134. for subsystems */
  135. unsigned int trace1_allowed;
  136. };
  137. /*
  138. * These are not static so they can be read from the core file
  139. */
  140. int *flt_data;
  141. uint32_t flt_head;
  142. uint32_t flt_tail;
  143. unsigned int flt_data_size;
  144. #define COMBINE_BUFFER_SIZE 2048
  145. /* values for logsys_logger init_status */
  146. #define LOGSYS_LOGGER_INIT_DONE 0
  147. #define LOGSYS_LOGGER_NEEDS_INIT 1
  148. static int logsys_system_needs_init = LOGSYS_LOGGER_NEEDS_INIT;
  149. static int logsys_memory_used = 0;
  150. static int logsys_sched_param_queued = 0;
  151. static int logsys_sched_policy;
  152. static struct sched_param logsys_sched_param;
  153. static int logsys_after_log_ops_yield = 10;
  154. static struct logsys_logger logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT + 1];
  155. static int wthread_active = 0;
  156. static int wthread_should_exit = 0;
  157. static pthread_mutex_t logsys_config_mutex = PTHREAD_MUTEX_INITIALIZER;
  158. static unsigned int records_written = 1;
  159. static pthread_t logsys_thread_id;
  160. static sem_t logsys_thread_start;
  161. static sem_t logsys_print_finished;
  162. static pthread_mutex_t logsys_flt_mutex = PTHREAD_MUTEX_INITIALIZER;
  163. static pthread_mutex_t logsys_wthread_mutex = PTHREAD_MUTEX_INITIALIZER;
  164. static int logsys_buffer_full = 0;
  165. static char *format_buffer=NULL;
  166. static int logsys_dropped_messages = 0;
  167. void *logsys_rec_end;
  168. static pid_t startup_pid = 0;
  169. static DECLARE_LIST_INIT(logsys_print_finished_records);
  170. #define FDMAX_ARGS 64
  171. #define CIRCULAR_BUFFER_WRITE_SIZE 64
  172. /* forward declarations */
  173. static void logsys_close_logfile(int subsysid);
  174. static uint32_t circular_memory_map (void **buf, size_t bytes)
  175. {
  176. void *addr_orig;
  177. void *addr;
  178. int fd;
  179. int res;
  180. const char *file = "fdata-XXXXXX";
  181. char path[PATH_MAX];
  182. char buffer[CIRCULAR_BUFFER_WRITE_SIZE];
  183. int i;
  184. int written;
  185. int error_return = 0;
  186. snprintf (path, PATH_MAX, "/dev/shm/%s", file);
  187. fd = mkstemp (path);
  188. if (fd == -1) {
  189. snprintf (path, PATH_MAX, LOCALSTATEDIR "/run/%s", file);
  190. fd = mkstemp (path);
  191. if (fd == -1) {
  192. error_return = -1;
  193. goto error_exit;
  194. }
  195. }
  196. /*
  197. * ftruncate doesn't return ENOSPC
  198. * have to use write to determine if shared memory is actually available
  199. */
  200. res = ftruncate (fd, 0);
  201. if (res == -1) {
  202. error_return = -1;
  203. goto unlink_exit;
  204. }
  205. memset (buffer, 0, sizeof (buffer));
  206. for (i = 0; i < (bytes / CIRCULAR_BUFFER_WRITE_SIZE); i++) {
  207. retry_write:
  208. written = write (fd, buffer, CIRCULAR_BUFFER_WRITE_SIZE);
  209. if (written == -1 && errno == EINTR) {
  210. goto retry_write;
  211. }
  212. if (written != 64) {
  213. error_return = -1;
  214. goto unlink_exit;
  215. }
  216. }
  217. addr_orig = mmap (NULL, bytes << 1, PROT_NONE,
  218. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  219. if (addr_orig == MAP_FAILED) {
  220. error_return = -1;
  221. goto unlink_exit;
  222. }
  223. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  224. MAP_SHARED | MAP_FIXED, fd, 0);
  225. if (addr != addr_orig) {
  226. error_return = -1;
  227. goto mmap_exit;
  228. }
  229. #if (defined COROSYNC_BSD && defined MADV_NOSYNC)
  230. madvise(addr_orig, bytes, MADV_NOSYNC);
  231. #endif
  232. addr = mmap (((char *)addr_orig) + bytes,
  233. bytes, PROT_READ | PROT_WRITE,
  234. MAP_SHARED | MAP_FIXED, fd, 0);
  235. if ((char *)addr != (char *)((char *)addr_orig + bytes)) {
  236. error_return = -1;
  237. goto mmap_exit;
  238. }
  239. #if (defined COROSYNC_BSD && defined MADV_NOSYNC)
  240. madvise(((char *)addr_orig) + bytes, bytes, MADV_NOSYNC);
  241. #endif
  242. *buf = addr_orig;
  243. error_return = 0;
  244. goto unlink_exit;
  245. mmap_exit:
  246. munmap (addr_orig, bytes << 1);
  247. unlink_exit:
  248. unlink (path);
  249. close (fd);
  250. error_exit:
  251. return (error_return);
  252. }
  253. static void logsys_flt_lock (void)
  254. {
  255. pthread_mutex_lock (&logsys_flt_mutex);
  256. }
  257. static void logsys_flt_unlock (void)
  258. {
  259. pthread_mutex_unlock (&logsys_flt_mutex);
  260. }
  261. static void logsys_wthread_lock (void)
  262. {
  263. pthread_mutex_lock (&logsys_wthread_mutex);
  264. }
  265. static void logsys_wthread_unlock (void)
  266. {
  267. pthread_mutex_unlock (&logsys_wthread_mutex);
  268. }
  269. /*
  270. * Before any write operation, a reclaim on the buffer area must be executed
  271. */
  272. static inline void records_reclaim (unsigned int idx, unsigned int words)
  273. {
  274. unsigned int should_reclaim;
  275. should_reclaim = 0;
  276. if ((idx + words) >= flt_data_size) {
  277. logsys_buffer_full = 1;
  278. }
  279. if (logsys_buffer_full == 0) {
  280. return;
  281. }
  282. if (flt_tail > flt_head) {
  283. if (idx + words >= flt_tail) {
  284. should_reclaim = 1;
  285. }
  286. } else {
  287. if ((idx + words) >= (flt_tail + flt_data_size)) {
  288. should_reclaim = 1;
  289. }
  290. }
  291. if (should_reclaim) {
  292. int words_needed = 0;
  293. words_needed = words + 1;
  294. do {
  295. unsigned int old_tail;
  296. words_needed -= flt_data[flt_tail];
  297. old_tail = flt_tail;
  298. flt_tail =
  299. (flt_tail +
  300. flt_data[flt_tail]) % (flt_data_size);
  301. } while (words_needed > 0);
  302. }
  303. }
  304. #define idx_word_step(idx) \
  305. do { \
  306. if (idx > (flt_data_size - 1)) { \
  307. idx = 0; \
  308. } \
  309. } while (0);
  310. #define idx_buffer_step(idx) \
  311. do { \
  312. if (idx > (flt_data_size - 1)) { \
  313. idx = ((idx) % (flt_data_size)); \
  314. } \
  315. } while (0);
  316. /*
  317. * Internal threaded logging implementation
  318. */
  319. static inline int strcpy_cutoff (char *dest, const char *src, size_t cutoff,
  320. size_t buf_len)
  321. {
  322. size_t len = strlen (src);
  323. if (buf_len <= 1) {
  324. if (buf_len == 0)
  325. dest[0] = 0;
  326. return 0;
  327. }
  328. if (cutoff == 0) {
  329. cutoff = len;
  330. }
  331. cutoff = MIN (cutoff, buf_len - 1);
  332. len = MIN (len, cutoff);
  333. memcpy (dest, src, len);
  334. memset (dest + len, ' ', cutoff - len);
  335. dest[cutoff] = '\0';
  336. return (cutoff);
  337. }
  338. static const char log_month_name[][4] = {
  339. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  340. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  341. };
  342. /*
  343. * %s SUBSYSTEM
  344. * %n FUNCTION NAME
  345. * %f FILENAME
  346. * %l FILELINE
  347. * %p PRIORITY
  348. * %t TIMESTAMP
  349. * %b BUFFER
  350. *
  351. * any number between % and character specify field length to pad or chop
  352. */
  353. static void log_printf_to_logs (
  354. unsigned int rec_ident,
  355. const char *file_name,
  356. const char *function_name,
  357. int file_line,
  358. const char *buffer)
  359. {
  360. char normal_output_buffer[COMBINE_BUFFER_SIZE];
  361. char syslog_output_buffer[COMBINE_BUFFER_SIZE];
  362. char char_time[128];
  363. char line_no[30];
  364. unsigned int format_buffer_idx = 0;
  365. unsigned int normal_output_buffer_idx = 0;
  366. unsigned int syslog_output_buffer_idx = 0;
  367. struct timeval tv;
  368. size_t cutoff;
  369. unsigned int normal_len, syslog_len;
  370. int subsysid;
  371. unsigned int level;
  372. int c;
  373. struct tm tm_res;
  374. subsysid = LOGSYS_DECODE_SUBSYSID(rec_ident);
  375. level = LOGSYS_DECODE_LEVEL(rec_ident);
  376. if (!((LOGSYS_DECODE_RECID(rec_ident) == LOGSYS_RECID_LOG) ||
  377. (logsys_loggers[subsysid].trace1_allowed && LOGSYS_DECODE_RECID(rec_ident) == LOGSYS_RECID_TRACE1))) {
  378. return;
  379. }
  380. pthread_mutex_lock (&logsys_config_mutex);
  381. while ((c = format_buffer[format_buffer_idx])) {
  382. cutoff = 0;
  383. if (c != '%') {
  384. normal_output_buffer[normal_output_buffer_idx++] = c;
  385. syslog_output_buffer[syslog_output_buffer_idx++] = c;
  386. format_buffer_idx++;
  387. } else {
  388. const char *normal_p, *syslog_p;
  389. format_buffer_idx += 1;
  390. if (isdigit (format_buffer[format_buffer_idx])) {
  391. cutoff = atoi (&format_buffer[format_buffer_idx]);
  392. }
  393. while (isdigit (format_buffer[format_buffer_idx])) {
  394. format_buffer_idx += 1;
  395. }
  396. switch (format_buffer[format_buffer_idx]) {
  397. case 's':
  398. normal_p = logsys_loggers[subsysid].subsys;
  399. syslog_p = logsys_loggers[subsysid].subsys;
  400. break;
  401. case 'n':
  402. normal_p = function_name;
  403. syslog_p = function_name;
  404. break;
  405. case 'f':
  406. normal_p = file_name;
  407. syslog_p = file_name;
  408. break;
  409. case 'l':
  410. snprintf (line_no, sizeof (line_no), "%d", file_line);
  411. normal_p = line_no;
  412. syslog_p = line_no;
  413. break;
  414. case 't':
  415. gettimeofday (&tv, NULL);
  416. (void)localtime_r ((time_t *)&tv.tv_sec, &tm_res);
  417. snprintf (char_time, sizeof (char_time), "%s %02d %02d:%02d:%02d",
  418. log_month_name[tm_res.tm_mon], tm_res.tm_mday, tm_res.tm_hour,
  419. tm_res.tm_min, tm_res.tm_sec);
  420. normal_p = char_time;
  421. /*
  422. * syslog does timestamping on its own.
  423. * also strip extra space in case.
  424. */
  425. syslog_p = "";
  426. break;
  427. case 'b':
  428. normal_p = buffer;
  429. syslog_p = buffer;
  430. break;
  431. case 'p':
  432. normal_p = logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].subsys;
  433. syslog_p = "";
  434. break;
  435. default:
  436. normal_p = "";
  437. syslog_p = "";
  438. break;
  439. }
  440. normal_len = strcpy_cutoff (normal_output_buffer + normal_output_buffer_idx,
  441. normal_p, cutoff,
  442. (sizeof (normal_output_buffer)
  443. - normal_output_buffer_idx));
  444. normal_output_buffer_idx += normal_len;
  445. syslog_len = strcpy_cutoff (syslog_output_buffer + syslog_output_buffer_idx,
  446. syslog_p, cutoff,
  447. (sizeof (syslog_output_buffer)
  448. - syslog_output_buffer_idx));
  449. syslog_output_buffer_idx += syslog_len;
  450. format_buffer_idx += 1;
  451. }
  452. if ((normal_output_buffer_idx >= sizeof (normal_output_buffer) - 2) ||
  453. (syslog_output_buffer_idx >= sizeof (syslog_output_buffer) - 1)) {
  454. /* Note: we make allowance for '\0' at the end of
  455. * both of these arrays and normal_output_buffer also
  456. * needs a '\n'.
  457. */
  458. break;
  459. }
  460. }
  461. pthread_mutex_unlock (&logsys_config_mutex);
  462. normal_output_buffer[normal_output_buffer_idx] = '\0';
  463. syslog_output_buffer[syslog_output_buffer_idx] = '\0';
  464. /*
  465. * Output to syslog
  466. */
  467. if ((logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_SYSLOG) &&
  468. ((level <= logsys_loggers[subsysid].syslog_priority) ||
  469. (logsys_loggers[subsysid].debug != 0))) {
  470. syslog (level | logsys_loggers[subsysid].syslog_facility, "%s", syslog_output_buffer);
  471. }
  472. /*
  473. * Terminate string with \n \0
  474. */
  475. normal_output_buffer[normal_output_buffer_idx++] = '\n';
  476. normal_output_buffer[normal_output_buffer_idx] = '\0';
  477. /*
  478. * Output to configured file
  479. */
  480. if (((logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_FILE) &&
  481. (logsys_loggers[subsysid].logfile_fp != NULL)) &&
  482. ((level <= logsys_loggers[subsysid].logfile_priority) ||
  483. (logsys_loggers[subsysid].debug != 0))) {
  484. /*
  485. * Output to a file
  486. */
  487. if ((fwrite (normal_output_buffer, strlen (normal_output_buffer), 1,
  488. logsys_loggers[subsysid].logfile_fp) < 1) ||
  489. (fflush (logsys_loggers[subsysid].logfile_fp) == EOF)) {
  490. char tmpbuffer[1024];
  491. /*
  492. * if we are here, it's bad.. it's really really bad.
  493. * Best thing would be to light a candle in a church
  494. * and pray.
  495. */
  496. snprintf(tmpbuffer, sizeof(tmpbuffer),
  497. "LOGSYS EMERGENCY: %s Unable to write to %s.",
  498. logsys_loggers[subsysid].subsys,
  499. logsys_loggers[subsysid].logfile);
  500. pthread_mutex_lock (&logsys_config_mutex);
  501. logsys_close_logfile(subsysid);
  502. logsys_loggers[subsysid].mode &= ~LOGSYS_MODE_OUTPUT_FILE;
  503. pthread_mutex_unlock (&logsys_config_mutex);
  504. log_printf_to_logs(
  505. LOGSYS_ENCODE_RECID(
  506. LOGSYS_LEVEL_EMERG,
  507. subsysid,
  508. LOGSYS_RECID_LOG),
  509. __FILE__, __FUNCTION__, __LINE__,
  510. tmpbuffer);
  511. }
  512. }
  513. /*
  514. * Output to stderr
  515. */
  516. if ((logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_STDERR) &&
  517. ((level <= logsys_loggers[subsysid].logfile_priority) ||
  518. (logsys_loggers[subsysid].debug != 0))) {
  519. if (write (STDERR_FILENO, normal_output_buffer, strlen (normal_output_buffer)) < 0) {
  520. char tmpbuffer[1024];
  521. /*
  522. * if we are here, it's bad.. it's really really bad.
  523. * Best thing would be to light 20 candles for each saint
  524. * in the calendar and pray a lot...
  525. */
  526. pthread_mutex_lock (&logsys_config_mutex);
  527. logsys_loggers[subsysid].mode &= ~LOGSYS_MODE_OUTPUT_STDERR;
  528. pthread_mutex_unlock (&logsys_config_mutex);
  529. snprintf(tmpbuffer, sizeof(tmpbuffer),
  530. "LOGSYS EMERGENCY: %s Unable to write to STDERR.",
  531. logsys_loggers[subsysid].subsys);
  532. log_printf_to_logs(
  533. LOGSYS_ENCODE_RECID(
  534. LOGSYS_LEVEL_EMERG,
  535. subsysid,
  536. LOGSYS_RECID_LOG),
  537. __FILE__, __FUNCTION__, __LINE__,
  538. tmpbuffer);
  539. }
  540. }
  541. }
  542. static void log_printf_to_logs_wthread (
  543. unsigned int rec_ident,
  544. const char *file_name,
  545. const char *function_name,
  546. int file_line,
  547. const char *buffer)
  548. {
  549. struct record *rec;
  550. uint32_t length;
  551. rec = malloc (sizeof (struct record));
  552. if (rec == NULL) {
  553. return;
  554. }
  555. length = strlen (buffer);
  556. rec->rec_ident = rec_ident;
  557. rec->file_name = file_name;
  558. rec->function_name = function_name;
  559. rec->file_line = file_line;
  560. rec->buffer = malloc (length + 1);
  561. if (rec->buffer == NULL) {
  562. free (rec);
  563. return;
  564. }
  565. memcpy (rec->buffer, buffer, length + 1);
  566. list_init (&rec->list);
  567. logsys_wthread_lock();
  568. logsys_memory_used += length + 1 + sizeof (struct record);
  569. if (logsys_memory_used > 512000) {
  570. free (rec->buffer);
  571. free (rec);
  572. logsys_memory_used = logsys_memory_used - length - 1 - sizeof (struct record);
  573. logsys_dropped_messages += 1;
  574. logsys_wthread_unlock();
  575. return;
  576. } else {
  577. list_add_tail (&rec->list, &logsys_print_finished_records);
  578. }
  579. logsys_wthread_unlock();
  580. sem_post (&logsys_print_finished);
  581. }
  582. static void *logsys_worker_thread (void *data) __attribute__((noreturn));
  583. static void *logsys_worker_thread (void *data)
  584. {
  585. struct record *rec;
  586. int dropped = 0;
  587. int res;
  588. /*
  589. * Signal wthread_create that the initialization process may continue
  590. */
  591. sem_post (&logsys_thread_start);
  592. for (;;) {
  593. dropped = 0;
  594. retry_sem_wait:
  595. res = sem_wait (&logsys_print_finished);
  596. if (res == -1 && errno == EINTR) {
  597. goto retry_sem_wait;
  598. } else
  599. if (res == -1) {
  600. /*
  601. * This case shouldn't happen
  602. */
  603. pthread_exit (NULL);
  604. }
  605. logsys_wthread_lock();
  606. if (wthread_should_exit) {
  607. int value;
  608. res = sem_getvalue (&logsys_print_finished, &value);
  609. if (value == 0) {
  610. logsys_wthread_unlock();
  611. pthread_exit (NULL);
  612. }
  613. }
  614. rec = list_entry (logsys_print_finished_records.next, struct record, list);
  615. list_del (&rec->list);
  616. logsys_memory_used = logsys_memory_used - strlen (rec->buffer) -
  617. sizeof (struct record) - 1;
  618. dropped = logsys_dropped_messages;
  619. logsys_dropped_messages = 0;
  620. logsys_wthread_unlock();
  621. if (dropped) {
  622. printf ("%d messages lost\n", dropped);
  623. }
  624. log_printf_to_logs (
  625. rec->rec_ident,
  626. rec->file_name,
  627. rec->function_name,
  628. rec->file_line,
  629. rec->buffer);
  630. free (rec->buffer);
  631. free (rec);
  632. }
  633. }
  634. static void wthread_create (void)
  635. {
  636. int res;
  637. if (wthread_active) {
  638. return;
  639. }
  640. wthread_active = 1;
  641. /*
  642. * TODO: propagate pthread_create errors back to the caller
  643. */
  644. res = pthread_create (&logsys_thread_id, NULL,
  645. logsys_worker_thread, NULL);
  646. sem_wait (&logsys_thread_start);
  647. if (res == 0) {
  648. if (logsys_sched_param_queued == 1) {
  649. /*
  650. * TODO: propagate logsys_thread_priority_set errors back to
  651. * the caller
  652. */
  653. res = logsys_thread_priority_set (
  654. logsys_sched_policy,
  655. &logsys_sched_param,
  656. logsys_after_log_ops_yield);
  657. logsys_sched_param_queued = 0;
  658. }
  659. } else {
  660. wthread_active = 0;
  661. }
  662. }
  663. static int _logsys_config_subsys_get_unlocked (const char *subsys)
  664. {
  665. unsigned int i;
  666. if (!subsys) {
  667. return LOGSYS_MAX_SUBSYS_COUNT;
  668. }
  669. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  670. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  671. return i;
  672. }
  673. }
  674. return (-1);
  675. }
  676. static void syslog_facility_reconf (void)
  677. {
  678. closelog();
  679. openlog(logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].subsys,
  680. LOG_CONS|LOG_PID,
  681. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].syslog_facility);
  682. }
  683. /*
  684. * this is always invoked within the mutex, so it's safe to parse the
  685. * whole thing as we need.
  686. */
  687. static void logsys_close_logfile (
  688. int subsysid)
  689. {
  690. int i;
  691. if ((logsys_loggers[subsysid].logfile_fp == NULL) &&
  692. (logsys_loggers[subsysid].logfile == NULL)) {
  693. return;
  694. }
  695. /*
  696. * if there is another subsystem or system using the same fp,
  697. * then we clean our own structs, but we can't close the file
  698. * as it is in use by somebody else.
  699. * Only the last users will be allowed to perform the fclose.
  700. */
  701. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  702. if ((logsys_loggers[i].logfile_fp == logsys_loggers[subsysid].logfile_fp) &&
  703. (i != subsysid)) {
  704. logsys_loggers[subsysid].logfile = NULL;
  705. logsys_loggers[subsysid].logfile_fp = NULL;
  706. return;
  707. }
  708. }
  709. /*
  710. * if we are here, we are the last users of that fp, so we can safely
  711. * close it.
  712. */
  713. fclose (logsys_loggers[subsysid].logfile_fp);
  714. logsys_loggers[subsysid].logfile_fp = NULL;
  715. free (logsys_loggers[subsysid].logfile);
  716. logsys_loggers[subsysid].logfile = NULL;
  717. }
  718. /*
  719. * we need a version that can work when somebody else is already
  720. * holding a config mutex lock or we will never get out of here
  721. */
  722. static int logsys_config_file_set_unlocked (
  723. int subsysid,
  724. const char **error_string,
  725. const char *file)
  726. {
  727. static char error_string_response[512];
  728. int i;
  729. logsys_close_logfile(subsysid);
  730. if ((file == NULL) ||
  731. (strcmp(logsys_loggers[subsysid].subsys, "") == 0)) {
  732. return (0);
  733. }
  734. if (strlen(file) >= PATH_MAX) {
  735. snprintf (error_string_response,
  736. sizeof(error_string_response),
  737. "%s: logfile name exceed maximum system filename lenght\n",
  738. logsys_loggers[subsysid].subsys);
  739. *error_string = error_string_response;
  740. return (-1);
  741. }
  742. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  743. if ((logsys_loggers[i].logfile != NULL) &&
  744. (strcmp (logsys_loggers[i].logfile, file) == 0) &&
  745. (i != subsysid)) {
  746. logsys_loggers[subsysid].logfile =
  747. logsys_loggers[i].logfile;
  748. logsys_loggers[subsysid].logfile_fp =
  749. logsys_loggers[i].logfile_fp;
  750. return (0);
  751. }
  752. }
  753. logsys_loggers[subsysid].logfile = strdup(file);
  754. if (logsys_loggers[subsysid].logfile == NULL) {
  755. snprintf (error_string_response,
  756. sizeof(error_string_response),
  757. "Unable to allocate memory for logfile '%s'\n",
  758. file);
  759. *error_string = error_string_response;
  760. return (-1);
  761. }
  762. logsys_loggers[subsysid].logfile_fp = fopen (file, "a+");
  763. if (logsys_loggers[subsysid].logfile_fp == NULL) {
  764. int err;
  765. char error_str[LOGSYS_MAX_PERROR_MSG_LEN];
  766. const char *error_ptr;
  767. err = errno;
  768. #ifdef COROSYNC_LINUX
  769. /* The GNU version of strerror_r returns a (char*) that *must* be used */
  770. error_ptr = strerror_r(err, error_str, sizeof(error_str));
  771. #else
  772. /* The XSI-compliant strerror_r() return 0 or -1 (in case the buffer is full) */
  773. if ( strerror_r(err, error_str, sizeof(error_str)) < 0 )
  774. error_ptr = "";
  775. else
  776. error_ptr = error_str;
  777. #endif
  778. free(logsys_loggers[subsysid].logfile);
  779. logsys_loggers[subsysid].logfile = NULL;
  780. snprintf (error_string_response,
  781. sizeof(error_string_response),
  782. "Can't open logfile '%s' for reason: %s (%d).\n",
  783. file, error_ptr, err);
  784. *error_string = error_string_response;
  785. return (-1);
  786. }
  787. return (0);
  788. }
  789. static void logsys_subsys_init (
  790. const char *subsys,
  791. int subsysid)
  792. {
  793. if (logsys_system_needs_init == LOGSYS_LOGGER_NEEDS_INIT) {
  794. logsys_loggers[subsysid].init_status =
  795. LOGSYS_LOGGER_NEEDS_INIT;
  796. } else {
  797. memcpy(&logsys_loggers[subsysid],
  798. &logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT],
  799. sizeof(logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT]));
  800. logsys_loggers[subsysid].init_status =
  801. LOGSYS_LOGGER_INIT_DONE;
  802. }
  803. strncpy (logsys_loggers[subsysid].subsys, subsys,
  804. sizeof (logsys_loggers[subsysid].subsys));
  805. logsys_loggers[subsysid].subsys[
  806. sizeof (logsys_loggers[subsysid].subsys) - 1] = '\0';
  807. }
  808. /*
  809. * Internal API - exported
  810. */
  811. int _logsys_system_setup(
  812. const char *mainsystem,
  813. unsigned int mode,
  814. unsigned int debug,
  815. const char *logfile,
  816. int logfile_priority,
  817. int syslog_facility,
  818. int syslog_priority)
  819. {
  820. int i;
  821. const char *errstr;
  822. char tempsubsys[LOGSYS_MAX_SUBSYS_NAMELEN];
  823. if ((mainsystem == NULL) ||
  824. (strlen(mainsystem) >= LOGSYS_MAX_SUBSYS_NAMELEN)) {
  825. return -1;
  826. }
  827. i = LOGSYS_MAX_SUBSYS_COUNT;
  828. pthread_mutex_lock (&logsys_config_mutex);
  829. snprintf(logsys_loggers[i].subsys,
  830. LOGSYS_MAX_SUBSYS_NAMELEN,
  831. "%s", mainsystem);
  832. logsys_loggers[i].mode = mode;
  833. logsys_loggers[i].debug = debug;
  834. logsys_loggers[i].trace1_allowed = 0;
  835. if (logsys_config_file_set_unlocked (i, &errstr, logfile) < 0) {
  836. pthread_mutex_unlock (&logsys_config_mutex);
  837. return (-1);
  838. }
  839. logsys_loggers[i].logfile_priority = logfile_priority;
  840. logsys_loggers[i].syslog_facility = syslog_facility;
  841. logsys_loggers[i].syslog_priority = syslog_priority;
  842. syslog_facility_reconf();
  843. logsys_loggers[i].init_status = LOGSYS_LOGGER_INIT_DONE;
  844. logsys_system_needs_init = LOGSYS_LOGGER_INIT_DONE;
  845. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  846. if ((strcmp (logsys_loggers[i].subsys, "") != 0) &&
  847. (logsys_loggers[i].init_status ==
  848. LOGSYS_LOGGER_NEEDS_INIT)) {
  849. strncpy (tempsubsys, logsys_loggers[i].subsys,
  850. sizeof (tempsubsys));
  851. tempsubsys[sizeof (tempsubsys) - 1] = '\0';
  852. logsys_subsys_init(tempsubsys, i);
  853. }
  854. }
  855. pthread_mutex_unlock (&logsys_config_mutex);
  856. return (0);
  857. }
  858. int _logsys_subsys_create (const char *subsys)
  859. {
  860. int i;
  861. if ((subsys == NULL) ||
  862. (strlen(subsys) >= LOGSYS_MAX_SUBSYS_NAMELEN)) {
  863. return -1;
  864. }
  865. pthread_mutex_lock (&logsys_config_mutex);
  866. i = _logsys_config_subsys_get_unlocked (subsys);
  867. if ((i > -1) && (i < LOGSYS_MAX_SUBSYS_COUNT)) {
  868. pthread_mutex_unlock (&logsys_config_mutex);
  869. return i;
  870. }
  871. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  872. if (strcmp (logsys_loggers[i].subsys, "") == 0) {
  873. logsys_subsys_init(subsys, i);
  874. break;
  875. }
  876. }
  877. if (i >= LOGSYS_MAX_SUBSYS_COUNT) {
  878. i = -1;
  879. }
  880. pthread_mutex_unlock (&logsys_config_mutex);
  881. return i;
  882. }
  883. int _logsys_wthread_create (void)
  884. {
  885. if (((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_FORK) == 0) &&
  886. ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) != 0)) {
  887. wthread_create();
  888. }
  889. return (0);
  890. }
  891. int _logsys_rec_init (unsigned int fltsize)
  892. {
  893. size_t flt_real_size;
  894. int res;
  895. sem_init (&logsys_thread_start, 0, 0);
  896. sem_init (&logsys_print_finished, 0, 0);
  897. /*
  898. * XXX: kill me for 1.1 because I am a dirty hack
  899. * temporary workaround that will be replaced by supporting
  900. * 0 byte size flight recorder buffer.
  901. * 0 byte size buffer will enable direct printing to logs
  902. * without flight recoder.
  903. */
  904. if (fltsize < 64000) {
  905. fltsize = 64000;
  906. }
  907. flt_real_size = ROUNDUP(fltsize, sysconf(_SC_PAGESIZE)) * 4;
  908. res = circular_memory_map ((void **)&flt_data, flt_real_size);
  909. if (res == -1) {
  910. sem_destroy (&logsys_thread_start);
  911. sem_destroy (&logsys_print_finished);
  912. }
  913. memset (flt_data, 0, flt_real_size * 2);
  914. /*
  915. * flt_data_size tracks data by ints and not bytes/chars.
  916. */
  917. flt_data_size = flt_real_size / sizeof (uint32_t);
  918. /*
  919. * First record starts at zero
  920. * Last record ends at zero
  921. */
  922. flt_head = 0;
  923. flt_tail = 0;
  924. return (0);
  925. }
  926. /*
  927. * u32 RECORD SIZE
  928. * u32 record ident
  929. * u64 time_t timestamp
  930. * u32 arg count
  931. * u32 file line
  932. * u32 subsys length
  933. * buffer null terminated subsys
  934. * u32 filename length
  935. * buffer null terminated filename
  936. * u32 filename length
  937. * buffer null terminated function
  938. * u32 arg1 length
  939. * buffer arg1
  940. * ... repeats length & arg
  941. */
  942. void _logsys_log_rec (
  943. unsigned int rec_ident,
  944. const char *function_name,
  945. const char *file_name,
  946. int file_line,
  947. ...)
  948. {
  949. va_list ap;
  950. const void *buf_args[FDMAX_ARGS];
  951. unsigned int buf_len[FDMAX_ARGS];
  952. unsigned int i;
  953. unsigned int idx;
  954. unsigned int arguments = 0;
  955. unsigned int record_reclaim_size = 0;
  956. unsigned int index_start;
  957. struct timeval the_timeval;
  958. int words_written;
  959. int subsysid;
  960. subsysid = LOGSYS_DECODE_SUBSYSID(rec_ident);
  961. /*
  962. * Decode VA Args
  963. */
  964. va_start (ap, file_line);
  965. arguments = 3;
  966. for (;;) {
  967. buf_args[arguments] = va_arg (ap, void *);
  968. if (buf_args[arguments] == LOGSYS_REC_END) {
  969. break;
  970. }
  971. buf_len[arguments] = va_arg (ap, int);
  972. record_reclaim_size += ((buf_len[arguments] + 3) >> 2) + 1;
  973. arguments++;
  974. if (arguments >= FDMAX_ARGS) {
  975. break;
  976. }
  977. }
  978. va_end (ap);
  979. /*
  980. * Encode logsys subsystem identity, filename, and function
  981. */
  982. buf_args[0] = logsys_loggers[subsysid].subsys;
  983. buf_len[0] = strlen (logsys_loggers[subsysid].subsys) + 1;
  984. buf_args[1] = file_name;
  985. buf_len[1] = strlen (file_name) + 1;
  986. buf_args[2] = function_name;
  987. buf_len[2] = strlen (function_name) + 1;
  988. for (i = 0; i < 3; i++) {
  989. record_reclaim_size += ((buf_len[i] + 3) >> 2) + 1;
  990. }
  991. logsys_flt_lock();
  992. idx = flt_head;
  993. index_start = idx;
  994. /*
  995. * Reclaim data needed for record including 6 words for the header
  996. */
  997. records_reclaim (idx, record_reclaim_size + 6);
  998. /*
  999. * Write record size of zero and rest of header information
  1000. */
  1001. flt_data[idx++] = 0;
  1002. idx_word_step(idx);
  1003. flt_data[idx++] = rec_ident;
  1004. idx_word_step(idx);
  1005. flt_data[idx++] = file_line;
  1006. idx_word_step(idx);
  1007. flt_data[idx++] = records_written;
  1008. idx_word_step(idx);
  1009. gettimeofday(&the_timeval, NULL);
  1010. flt_data[idx++] = the_timeval.tv_sec & 0xFFFFFFFF;
  1011. idx_word_step(idx);
  1012. flt_data[idx++] = the_timeval.tv_sec >> 32;
  1013. idx_word_step(idx);
  1014. /*
  1015. * Encode all of the arguments into the log message
  1016. */
  1017. for (i = 0; i < arguments; i++) {
  1018. unsigned int bytes;
  1019. unsigned int total_words;
  1020. bytes = buf_len[i];
  1021. total_words = (bytes + 3) >> 2;
  1022. flt_data[idx++] = total_words;
  1023. idx_word_step(idx);
  1024. memcpy (&flt_data[idx], buf_args[i], buf_len[i]);
  1025. idx += total_words;
  1026. idx_buffer_step (idx);
  1027. }
  1028. words_written = idx - index_start;
  1029. if (words_written < 0) {
  1030. words_written += flt_data_size;
  1031. }
  1032. /*
  1033. * Commit the write of the record size now that the full record
  1034. * is in the memory buffer
  1035. */
  1036. flt_data[index_start] = words_written;
  1037. flt_head = idx;
  1038. logsys_flt_unlock();
  1039. records_written++;
  1040. }
  1041. void _logsys_log_vprintf (
  1042. unsigned int rec_ident,
  1043. const char *function_name,
  1044. const char *file_name,
  1045. int file_line,
  1046. const char *format,
  1047. va_list ap)
  1048. {
  1049. char logsys_print_buffer[COMBINE_BUFFER_SIZE];
  1050. unsigned int len;
  1051. unsigned int level;
  1052. int subsysid;
  1053. const char *short_file_name;
  1054. subsysid = LOGSYS_DECODE_SUBSYSID(rec_ident);
  1055. level = LOGSYS_DECODE_LEVEL(rec_ident);
  1056. len = vsnprintf (logsys_print_buffer, sizeof (logsys_print_buffer), format, ap);
  1057. if (logsys_print_buffer[len - 1] == '\n') {
  1058. logsys_print_buffer[len - 1] = '\0';
  1059. len -= 1;
  1060. }
  1061. #ifdef BUILDING_IN_PLACE
  1062. short_file_name = file_name;
  1063. #else
  1064. short_file_name = strrchr (file_name, '/');
  1065. if (short_file_name == NULL)
  1066. short_file_name = file_name;
  1067. else
  1068. short_file_name++; /* move past the "/" */
  1069. #endif /* BUILDING_IN_PLACE */
  1070. if (startup_pid == 0 || startup_pid == getpid()) {
  1071. /*
  1072. * Create a log record if we are really true corosync
  1073. * process (not fork of some service) or if we didn't finished
  1074. * initialization yet.
  1075. */
  1076. _logsys_log_rec (
  1077. rec_ident,
  1078. function_name,
  1079. short_file_name,
  1080. file_line,
  1081. logsys_print_buffer, len + 1,
  1082. LOGSYS_REC_END);
  1083. }
  1084. /*
  1085. * If logsys is not going to print a message to a log target don't
  1086. * queue one
  1087. */
  1088. if ((level > logsys_loggers[subsysid].syslog_priority &&
  1089. level > logsys_loggers[subsysid].logfile_priority &&
  1090. logsys_loggers[subsysid].debug == 0) ||
  1091. (level == LOGSYS_LEVEL_DEBUG &&
  1092. logsys_loggers[subsysid].debug == 0)) {
  1093. return;
  1094. }
  1095. if ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) == 0) {
  1096. /*
  1097. * Output (and block) if the log mode is not threaded otherwise
  1098. * expect the worker thread to output the log data once signaled
  1099. */
  1100. log_printf_to_logs (rec_ident,
  1101. short_file_name,
  1102. function_name,
  1103. file_line,
  1104. logsys_print_buffer);
  1105. } else {
  1106. /*
  1107. * Signal worker thread to display logging output
  1108. */
  1109. log_printf_to_logs_wthread (rec_ident,
  1110. short_file_name,
  1111. function_name,
  1112. file_line,
  1113. logsys_print_buffer);
  1114. }
  1115. }
  1116. void _logsys_log_printf (
  1117. unsigned int rec_ident,
  1118. const char *function_name,
  1119. const char *file_name,
  1120. int file_line,
  1121. const char *format,
  1122. ...)
  1123. {
  1124. va_list ap;
  1125. va_start (ap, format);
  1126. _logsys_log_vprintf (rec_ident, function_name, file_name, file_line,
  1127. format, ap);
  1128. va_end (ap);
  1129. }
  1130. int _logsys_config_subsys_get (const char *subsys)
  1131. {
  1132. unsigned int i;
  1133. pthread_mutex_lock (&logsys_config_mutex);
  1134. i = _logsys_config_subsys_get_unlocked (subsys);
  1135. pthread_mutex_unlock (&logsys_config_mutex);
  1136. return i;
  1137. }
  1138. /*
  1139. * External Configuration and Initialization API
  1140. */
  1141. void logsys_fork_completed (void)
  1142. {
  1143. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode &= ~LOGSYS_MODE_FORK;
  1144. startup_pid = getpid();
  1145. (void)_logsys_wthread_create ();
  1146. }
  1147. int logsys_config_mode_set (const char *subsys, unsigned int mode)
  1148. {
  1149. int i;
  1150. pthread_mutex_lock (&logsys_config_mutex);
  1151. if (subsys != NULL) {
  1152. i = _logsys_config_subsys_get_unlocked (subsys);
  1153. if (i >= 0) {
  1154. logsys_loggers[i].mode = mode;
  1155. i = 0;
  1156. }
  1157. } else {
  1158. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1159. logsys_loggers[i].mode = mode;
  1160. }
  1161. i = 0;
  1162. }
  1163. pthread_mutex_unlock (&logsys_config_mutex);
  1164. return i;
  1165. }
  1166. unsigned int logsys_config_mode_get (const char *subsys)
  1167. {
  1168. int i;
  1169. i = _logsys_config_subsys_get (subsys);
  1170. if (i < 0) {
  1171. return i;
  1172. }
  1173. return logsys_loggers[i].mode;
  1174. }
  1175. int logsys_config_file_set (
  1176. const char *subsys,
  1177. const char **error_string,
  1178. const char *file)
  1179. {
  1180. int i;
  1181. int res;
  1182. pthread_mutex_lock (&logsys_config_mutex);
  1183. if (subsys != NULL) {
  1184. i = _logsys_config_subsys_get_unlocked (subsys);
  1185. if (i < 0) {
  1186. res = i;
  1187. } else {
  1188. res = logsys_config_file_set_unlocked(i, error_string, file);
  1189. }
  1190. } else {
  1191. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1192. res = logsys_config_file_set_unlocked(i, error_string, file);
  1193. if (res < 0) {
  1194. break;
  1195. }
  1196. }
  1197. }
  1198. pthread_mutex_unlock (&logsys_config_mutex);
  1199. return res;
  1200. }
  1201. int logsys_format_set (const char *format)
  1202. {
  1203. int ret = 0;
  1204. pthread_mutex_lock (&logsys_config_mutex);
  1205. if (format_buffer) {
  1206. free(format_buffer);
  1207. format_buffer = NULL;
  1208. }
  1209. format_buffer = strdup(format ? format : "%p [%6s] %b");
  1210. if (format_buffer == NULL) {
  1211. ret = -1;
  1212. }
  1213. pthread_mutex_unlock (&logsys_config_mutex);
  1214. return ret;
  1215. }
  1216. char *logsys_format_get (void)
  1217. {
  1218. return format_buffer;
  1219. }
  1220. int logsys_config_syslog_facility_set (
  1221. const char *subsys,
  1222. unsigned int facility)
  1223. {
  1224. int i;
  1225. pthread_mutex_lock (&logsys_config_mutex);
  1226. if (subsys != NULL) {
  1227. i = _logsys_config_subsys_get_unlocked (subsys);
  1228. if (i >= 0) {
  1229. logsys_loggers[i].syslog_facility = facility;
  1230. if (i == LOGSYS_MAX_SUBSYS_COUNT) {
  1231. syslog_facility_reconf();
  1232. }
  1233. i = 0;
  1234. }
  1235. } else {
  1236. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1237. logsys_loggers[i].syslog_facility = facility;
  1238. }
  1239. syslog_facility_reconf();
  1240. i = 0;
  1241. }
  1242. pthread_mutex_unlock (&logsys_config_mutex);
  1243. return i;
  1244. }
  1245. int logsys_config_syslog_priority_set (
  1246. const char *subsys,
  1247. unsigned int priority)
  1248. {
  1249. int i;
  1250. pthread_mutex_lock (&logsys_config_mutex);
  1251. if (subsys != NULL) {
  1252. i = _logsys_config_subsys_get_unlocked (subsys);
  1253. if (i >= 0) {
  1254. logsys_loggers[i].syslog_priority = priority;
  1255. i = 0;
  1256. }
  1257. } else {
  1258. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1259. logsys_loggers[i].syslog_priority = priority;
  1260. }
  1261. i = 0;
  1262. }
  1263. pthread_mutex_unlock (&logsys_config_mutex);
  1264. return i;
  1265. }
  1266. int logsys_config_logfile_priority_set (
  1267. const char *subsys,
  1268. unsigned int priority)
  1269. {
  1270. int i;
  1271. pthread_mutex_lock (&logsys_config_mutex);
  1272. if (subsys != NULL) {
  1273. i = _logsys_config_subsys_get_unlocked (subsys);
  1274. if (i >= 0) {
  1275. logsys_loggers[i].logfile_priority = priority;
  1276. i = 0;
  1277. }
  1278. } else {
  1279. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1280. logsys_loggers[i].logfile_priority = priority;
  1281. }
  1282. i = 0;
  1283. }
  1284. pthread_mutex_unlock (&logsys_config_mutex);
  1285. return i;
  1286. }
  1287. int logsys_config_debug_set (
  1288. const char *subsys,
  1289. unsigned int debug)
  1290. {
  1291. int i;
  1292. pthread_mutex_lock (&logsys_config_mutex);
  1293. if (subsys != NULL) {
  1294. i = _logsys_config_subsys_get_unlocked (subsys);
  1295. if (i >= 0) {
  1296. switch (debug) {
  1297. case LOGSYS_DEBUG_OFF:
  1298. case LOGSYS_DEBUG_ON:
  1299. logsys_loggers[i].debug = debug;
  1300. logsys_loggers[i].trace1_allowed = 0;
  1301. break;
  1302. case LOGSYS_DEBUG_TRACE:
  1303. logsys_loggers[i].debug = LOGSYS_DEBUG_ON;
  1304. logsys_loggers[i].trace1_allowed = 1;
  1305. break;
  1306. }
  1307. i = 0;
  1308. }
  1309. } else {
  1310. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1311. switch (debug) {
  1312. case LOGSYS_DEBUG_OFF:
  1313. case LOGSYS_DEBUG_ON:
  1314. logsys_loggers[i].debug = debug;
  1315. logsys_loggers[i].trace1_allowed = 0;
  1316. break;
  1317. case LOGSYS_DEBUG_TRACE:
  1318. logsys_loggers[i].debug = LOGSYS_DEBUG_ON;
  1319. logsys_loggers[i].trace1_allowed = 1;
  1320. break;
  1321. }
  1322. }
  1323. i = 0;
  1324. }
  1325. pthread_mutex_unlock (&logsys_config_mutex);
  1326. return i;
  1327. }
  1328. int logsys_facility_id_get (const char *name)
  1329. {
  1330. unsigned int i;
  1331. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1332. if (strcasecmp(name, facilitynames[i].c_name) == 0) {
  1333. return (facilitynames[i].c_val);
  1334. }
  1335. }
  1336. return (-1);
  1337. }
  1338. const char *logsys_facility_name_get (unsigned int facility)
  1339. {
  1340. unsigned int i;
  1341. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1342. if (facility == facilitynames[i].c_val) {
  1343. return (facilitynames[i].c_name);
  1344. }
  1345. }
  1346. return (NULL);
  1347. }
  1348. int logsys_priority_id_get (const char *name)
  1349. {
  1350. unsigned int i;
  1351. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1352. if (strcasecmp(name, prioritynames[i].c_name) == 0) {
  1353. return (prioritynames[i].c_val);
  1354. }
  1355. }
  1356. return (-1);
  1357. }
  1358. const char *logsys_priority_name_get (unsigned int priority)
  1359. {
  1360. unsigned int i;
  1361. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1362. if (priority == prioritynames[i].c_val) {
  1363. return (prioritynames[i].c_name);
  1364. }
  1365. }
  1366. return (NULL);
  1367. }
  1368. int logsys_thread_priority_set (
  1369. int policy,
  1370. const struct sched_param *param,
  1371. unsigned int after_log_ops_yield)
  1372. {
  1373. int res = 0;
  1374. if (param == NULL) {
  1375. return (0);
  1376. }
  1377. #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX)
  1378. if (wthread_active == 0) {
  1379. logsys_sched_policy = policy;
  1380. memcpy(&logsys_sched_param, param, sizeof(struct sched_param));
  1381. logsys_sched_param_queued = 1;
  1382. } else {
  1383. res = pthread_setschedparam (logsys_thread_id, policy, param);
  1384. }
  1385. #endif
  1386. if (after_log_ops_yield > 0) {
  1387. logsys_after_log_ops_yield = after_log_ops_yield;
  1388. }
  1389. return (res);
  1390. }
  1391. int logsys_log_rec_store (const char *filename)
  1392. {
  1393. int fd;
  1394. ssize_t written_size = 0;
  1395. size_t this_write_size;
  1396. uint32_t header_magic=0xFFFFDABB; /* Flight Data Black Box */
  1397. fd = open (filename, O_CREAT|O_RDWR, 0700);
  1398. if (fd < 0) {
  1399. return (-1);
  1400. }
  1401. logsys_flt_lock();
  1402. /* write a header that tells corosync-fplay this is a new-format
  1403. * flightdata file, with timestamps. The header word has the top
  1404. * bit set which makes it larger than any older fdata file so
  1405. * that the tool can recognise old or new files.
  1406. */
  1407. this_write_size = write (fd, &header_magic, sizeof(uint32_t));
  1408. if (this_write_size != sizeof(unsigned int)) {
  1409. goto error_exit;
  1410. }
  1411. written_size += this_write_size;
  1412. this_write_size = write (fd, &flt_data_size, sizeof(uint32_t));
  1413. if (this_write_size != sizeof(unsigned int)) {
  1414. goto error_exit;
  1415. }
  1416. written_size += this_write_size;
  1417. this_write_size = write (fd, flt_data, flt_data_size * sizeof (uint32_t));
  1418. if (this_write_size != (flt_data_size * sizeof(uint32_t))) {
  1419. goto error_exit;
  1420. }
  1421. written_size += this_write_size;
  1422. this_write_size = write (fd, &flt_head, sizeof (uint32_t));
  1423. if (this_write_size != (sizeof(uint32_t))) {
  1424. goto error_exit;
  1425. }
  1426. written_size += this_write_size;
  1427. this_write_size = write (fd, &flt_tail, sizeof (uint32_t));
  1428. if (this_write_size != (sizeof(uint32_t))) {
  1429. goto error_exit;
  1430. }
  1431. written_size += this_write_size;
  1432. if (written_size != ((flt_data_size + 3) * sizeof (uint32_t))) {
  1433. goto error_exit;
  1434. }
  1435. logsys_flt_unlock();
  1436. close (fd);
  1437. return (0);
  1438. error_exit:
  1439. logsys_flt_unlock();
  1440. close (fd);
  1441. return (-1);
  1442. }
  1443. void logsys_atexit (void)
  1444. {
  1445. int res;
  1446. int value;
  1447. struct record *rec;
  1448. if (wthread_active == 0) {
  1449. for (;;) {
  1450. logsys_wthread_lock();
  1451. res = sem_getvalue (&logsys_print_finished, &value);
  1452. if (value == 0) {
  1453. logsys_wthread_unlock();
  1454. return;
  1455. }
  1456. sem_wait (&logsys_print_finished);
  1457. rec = list_entry (logsys_print_finished_records.next, struct record, list);
  1458. list_del (&rec->list);
  1459. logsys_memory_used = logsys_memory_used - strlen (rec->buffer) -
  1460. sizeof (struct record) - 1;
  1461. logsys_wthread_unlock();
  1462. log_printf_to_logs (
  1463. rec->rec_ident,
  1464. rec->file_name,
  1465. rec->function_name,
  1466. rec->file_line,
  1467. rec->buffer);
  1468. free (rec->buffer);
  1469. free (rec);
  1470. }
  1471. } else {
  1472. wthread_should_exit = 1;
  1473. sem_post (&logsys_print_finished);
  1474. pthread_join (logsys_thread_id, NULL);
  1475. }
  1476. }
  1477. void logsys_flush (void)
  1478. {
  1479. }