logsys.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 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 <config.h>
  37. #include <assert.h>
  38. #include <stdio.h>
  39. #include <ctype.h>
  40. #include <string.h>
  41. #include <stdarg.h>
  42. #include <sys/time.h>
  43. #include <sys/stat.h>
  44. #include <fcntl.h>
  45. #include <time.h>
  46. #include <errno.h>
  47. #include <sys/types.h>
  48. #include <sys/socket.h>
  49. #include <unistd.h>
  50. #if defined(COROSYNC_LINUX)
  51. #include <linux/un.h>
  52. #endif
  53. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  54. #include <sys/un.h>
  55. #endif
  56. #define SYSLOG_NAMES
  57. #include <syslog.h>
  58. #include <stdlib.h>
  59. #include <pthread.h>
  60. #include <corosync/engine/logsys.h>
  61. /* similar to syslog facilities/priorities tables,
  62. * make a tag table for internal use
  63. */
  64. #ifdef SYSLOG_NAMES
  65. struct syslog_names {
  66. const char *c_name;
  67. int c_val;
  68. };
  69. struct syslog_names tagnames[] =
  70. {
  71. { "log", LOGSYS_TAG_LOG },
  72. { "enter", LOGSYS_TAG_ENTER },
  73. { "leave", LOGSYS_TAG_LEAVE },
  74. { "trace1", LOGSYS_TAG_TRACE1 },
  75. { "trace2", LOGSYS_TAG_TRACE2 },
  76. { "trace3", LOGSYS_TAG_TRACE3 },
  77. { "trace4", LOGSYS_TAG_TRACE4 },
  78. { "trace5", LOGSYS_TAG_TRACE5 },
  79. { "trace6", LOGSYS_TAG_TRACE6 },
  80. { "trace7", LOGSYS_TAG_TRACE7 },
  81. { "trace8", LOGSYS_TAG_TRACE8 },
  82. { NULL, -1 }
  83. };
  84. #endif
  85. /*
  86. * These are not static so they can be read from the core file
  87. */
  88. int *flt_data;
  89. int flt_data_size;
  90. #define SUBSYS_MAX 32
  91. #define COMBINE_BUFFER_SIZE 2048
  92. struct logsys_logger {
  93. char subsys[64];
  94. unsigned int priority;
  95. unsigned int tags;
  96. unsigned int mode;
  97. };
  98. /*
  99. * Configuration parameters for logging system
  100. */
  101. static const char *logsys_name = NULL;
  102. static unsigned int logsys_mode = LOG_MODE_NOSUBSYS;
  103. static const char *logsys_file = NULL;
  104. static FILE *logsys_file_fp = NULL;
  105. static int logsys_facility = LOG_DAEMON;
  106. /*
  107. * operating global variables
  108. */
  109. static struct logsys_logger logsys_loggers[SUBSYS_MAX];
  110. static int wthread_active = 0;
  111. static int wthread_should_exit = 0;
  112. static pthread_mutex_t logsys_config_mutex = PTHREAD_MUTEX_INITIALIZER;
  113. static unsigned int records_written = 1;
  114. static pthread_t logsys_thread_id;
  115. static pthread_cond_t logsys_cond;
  116. static pthread_mutex_t logsys_cond_mutex;
  117. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  118. static pthread_spinlock_t logsys_idx_spinlock;
  119. #else
  120. static pthread_mutex_t logsys_idx_mutex = PTHREAD_MUTEX_INITIALIZER;
  121. #endif
  122. static unsigned int log_rec_idx;
  123. static int logsys_buffer_full = 0;
  124. static char *format_buffer=NULL;
  125. static int log_requests_pending = 0;
  126. static int log_requests_lost = 0;
  127. void *logsys_rec_end;
  128. #define FDHEAD_INDEX (flt_data_size)
  129. #define FDTAIL_INDEX (flt_data_size + 1)
  130. struct log_data {
  131. unsigned int syslog_pos;
  132. unsigned int priority;
  133. char *log_string;
  134. };
  135. static void logsys_atexit (void);
  136. /*
  137. * Helpers for _logsys_log_rec functionality
  138. */
  139. static inline void my_memcpy_32bit (int *dest, const int *src, unsigned int words)
  140. {
  141. unsigned int word_idx;
  142. for (word_idx = 0; word_idx < words; word_idx++) {
  143. dest[word_idx] = src[word_idx];
  144. }
  145. }
  146. static inline void my_memcpy_8bit (char *dest, const char *src,
  147. unsigned int bytes)
  148. {
  149. unsigned int byte_idx;
  150. for (byte_idx = 0; byte_idx < bytes; byte_idx++) {
  151. dest[byte_idx] = src[byte_idx];
  152. }
  153. }
  154. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  155. static void logsys_lock (void)
  156. {
  157. pthread_spin_lock (&logsys_idx_spinlock);
  158. }
  159. static void logsys_unlock (void)
  160. {
  161. pthread_spin_unlock (&logsys_idx_spinlock);
  162. }
  163. #else
  164. static void logsys_lock (void)
  165. {
  166. pthread_mutex_lock (&logsys_idx_mutex);
  167. }
  168. static void logsys_unlock (void)
  169. {
  170. pthread_mutex_unlock (&logsys_idx_mutex);
  171. }
  172. #endif
  173. /*
  174. * Before any write operation, a reclaim on the buffer area must be executed
  175. */
  176. static inline void records_reclaim (unsigned int idx, unsigned int words)
  177. {
  178. unsigned int should_reclaim;
  179. should_reclaim = 0;
  180. if ((idx + words) >= flt_data_size) {
  181. logsys_buffer_full = 1;
  182. }
  183. if (logsys_buffer_full == 0) {
  184. return;
  185. }
  186. logsys_lock();
  187. if (flt_data[FDTAIL_INDEX] > flt_data[FDHEAD_INDEX]) {
  188. if (idx + words >= flt_data[FDTAIL_INDEX]) {
  189. should_reclaim = 1;
  190. }
  191. } else {
  192. if ((idx + words) >= (flt_data[FDTAIL_INDEX] + flt_data_size)) {
  193. should_reclaim = 1;
  194. }
  195. }
  196. if (should_reclaim) {
  197. int words_needed = 0;
  198. words_needed = words + 1;
  199. do {
  200. unsigned int old_tail;
  201. words_needed -= flt_data[flt_data[FDTAIL_INDEX]];
  202. old_tail = flt_data[FDTAIL_INDEX];
  203. flt_data[FDTAIL_INDEX] =
  204. (flt_data[FDTAIL_INDEX] +
  205. flt_data[flt_data[FDTAIL_INDEX]]) % (flt_data_size);
  206. if (log_rec_idx == old_tail) {
  207. log_requests_lost += 1;
  208. log_rec_idx = flt_data[FDTAIL_INDEX];
  209. }
  210. } while (words_needed > 0);
  211. }
  212. logsys_unlock();
  213. }
  214. #define idx_word_step(idx) \
  215. do { \
  216. if (idx > (flt_data_size - 1)) { \
  217. idx = 0; \
  218. } \
  219. } while (0);
  220. #define idx_buffer_step(idx) \
  221. do { \
  222. if (idx > (flt_data_size - 1)) { \
  223. idx = ((idx) % (flt_data_size)); \
  224. } \
  225. } while (0);
  226. /*
  227. * Internal threaded logging implementation
  228. */
  229. static inline int strcpy_cutoff (char *dest, const char *src, int cutoff)
  230. {
  231. unsigned int len;
  232. if (cutoff == -1) {
  233. strcpy (dest, src);
  234. return (strlen (dest));
  235. } else {
  236. assert (cutoff > 0);
  237. strncpy (dest, src, cutoff);
  238. dest[cutoff] = '\0';
  239. len = strlen (dest);
  240. if (len != cutoff) {
  241. memset (&dest[len], ' ', cutoff - len);
  242. }
  243. }
  244. return (cutoff);
  245. }
  246. /*
  247. * %s SUBSYSTEM
  248. * %n FUNCTION NAME
  249. * %f FILENAME
  250. * %l FILELINE
  251. * %p PRIORITY
  252. * %t TIMESTAMP
  253. * %b BUFFER
  254. *
  255. * any number between % and character specify field length to pad or chop
  256. */
  257. static void log_printf_to_logs (
  258. const char *subsys,
  259. const char *function_name,
  260. const char *file_name,
  261. int file_line,
  262. unsigned int level,
  263. char *buffer)
  264. {
  265. char output_buffer[COMBINE_BUFFER_SIZE];
  266. char char_time[128];
  267. char line_no[30];
  268. unsigned int format_buffer_idx = 0;
  269. unsigned int output_buffer_idx = 0;
  270. struct timeval tv;
  271. int cutoff;
  272. unsigned int len;
  273. while (format_buffer[format_buffer_idx]) {
  274. cutoff = -1;
  275. if (format_buffer[format_buffer_idx] == '%') {
  276. format_buffer_idx += 1;
  277. if (isdigit (format_buffer[format_buffer_idx])) {
  278. cutoff = atoi (&format_buffer[format_buffer_idx]);
  279. }
  280. while (isdigit (format_buffer[format_buffer_idx])) {
  281. format_buffer_idx += 1;
  282. }
  283. switch (format_buffer[format_buffer_idx]) {
  284. case 's':
  285. len = strcpy_cutoff (&output_buffer[output_buffer_idx], subsys, cutoff);
  286. output_buffer_idx += len;
  287. break;
  288. case 'n':
  289. len = strcpy_cutoff (&output_buffer[output_buffer_idx], function_name, cutoff);
  290. output_buffer_idx += len;
  291. break;
  292. case 'l':
  293. sprintf (line_no, "%d", file_line);
  294. len = strcpy_cutoff (&output_buffer[output_buffer_idx], line_no, cutoff);
  295. output_buffer_idx += len;
  296. break;
  297. case 'p':
  298. break;
  299. case 't':
  300. gettimeofday (&tv, NULL);
  301. (void)strftime (char_time, sizeof (char_time), "%b %e %k:%M:%S", localtime ((time_t *)&tv.tv_sec));
  302. len = strcpy_cutoff (&output_buffer[output_buffer_idx], char_time, cutoff);
  303. output_buffer_idx += len;
  304. break;
  305. case 'b':
  306. len = strcpy_cutoff (&output_buffer[output_buffer_idx], buffer, cutoff);
  307. output_buffer_idx += len;
  308. break;
  309. }
  310. format_buffer_idx += 1;
  311. } else {
  312. output_buffer[output_buffer_idx++] = format_buffer[format_buffer_idx++];
  313. }
  314. }
  315. output_buffer[output_buffer_idx] = '\0';
  316. /*
  317. * Output to syslog
  318. */
  319. if (logsys_mode & LOG_MODE_OUTPUT_SYSLOG) {
  320. syslog (level, "%s", output_buffer);
  321. }
  322. /*
  323. * Terminate string with \n \0
  324. */
  325. if (logsys_mode & (LOG_MODE_OUTPUT_FILE|LOG_MODE_OUTPUT_STDERR)) {
  326. output_buffer[output_buffer_idx++] = '\n';
  327. output_buffer[output_buffer_idx] = '\0';
  328. }
  329. /*
  330. * Output to configured file
  331. */
  332. if ((logsys_mode & LOG_MODE_OUTPUT_FILE) && logsys_file_fp) {
  333. /*
  334. * Output to a file
  335. */
  336. (void)fwrite (output_buffer, strlen (output_buffer), 1, logsys_file_fp);
  337. fflush (logsys_file_fp);
  338. }
  339. /*
  340. * Output to stderr
  341. */
  342. if (logsys_mode & LOG_MODE_OUTPUT_STDERR) {
  343. (void)write (STDERR_FILENO, output_buffer, strlen (output_buffer));
  344. }
  345. }
  346. static void record_print (char *buf)
  347. {
  348. int *buf_uint32t = (int *)buf;
  349. unsigned int rec_size = buf_uint32t[0];
  350. unsigned int rec_ident = buf_uint32t[1];
  351. unsigned int file_line = buf_uint32t[2];
  352. unsigned int level = rec_ident >> 28;
  353. unsigned int i;
  354. unsigned int words_processed;
  355. unsigned int arg_size_idx;
  356. void *arguments[64];
  357. unsigned int arg_count;
  358. arg_size_idx = 4;
  359. words_processed = 4;
  360. arg_count = 0;
  361. for (i = 0; words_processed < rec_size; i++) {
  362. arguments[arg_count++] = &buf_uint32t[arg_size_idx + 1];
  363. arg_size_idx += buf_uint32t[arg_size_idx] + 1;
  364. words_processed += buf_uint32t[arg_size_idx] + 1;
  365. }
  366. log_printf_to_logs (
  367. (char *)arguments[0],
  368. (char *)arguments[1],
  369. (char *)arguments[2],
  370. file_line,
  371. level,
  372. (char *)arguments[3]);
  373. }
  374. static int record_read (char *buf, int rec_idx, int *log_msg) {
  375. unsigned int rec_size;
  376. unsigned int rec_ident;
  377. int firstcopy, secondcopy;
  378. rec_size = flt_data[rec_idx];
  379. rec_ident = flt_data[(rec_idx + 1) % flt_data_size];
  380. /*
  381. * Not a log record
  382. */
  383. if ((rec_ident & LOGSYS_TAG_LOG) == 0) {
  384. *log_msg = 0;
  385. return ((rec_idx + rec_size) % flt_data_size);
  386. }
  387. /*
  388. * A log record
  389. */
  390. *log_msg = 1;
  391. firstcopy = rec_size;
  392. secondcopy = 0;
  393. if (firstcopy + rec_idx > flt_data_size) {
  394. firstcopy = flt_data_size - rec_idx;
  395. secondcopy -= firstcopy - rec_size;
  396. }
  397. memcpy (&buf[0], &flt_data[rec_idx], firstcopy << 2);
  398. if (secondcopy) {
  399. memcpy (&buf[(firstcopy << 2)], &flt_data[0], secondcopy << 2);
  400. }
  401. return ((rec_idx + rec_size) % flt_data_size);
  402. }
  403. static inline void wthread_signal (void)
  404. {
  405. if (wthread_active == 0) {
  406. return;
  407. }
  408. pthread_mutex_lock (&logsys_cond_mutex);
  409. pthread_cond_signal (&logsys_cond);
  410. pthread_mutex_unlock (&logsys_cond_mutex);
  411. }
  412. static inline void wthread_wait (void)
  413. {
  414. pthread_mutex_lock (&logsys_cond_mutex);
  415. pthread_cond_wait (&logsys_cond, &logsys_cond_mutex);
  416. pthread_mutex_unlock (&logsys_cond_mutex);
  417. }
  418. static inline void wthread_wait_locked (void)
  419. {
  420. pthread_cond_wait (&logsys_cond, &logsys_cond_mutex);
  421. pthread_mutex_unlock (&logsys_cond_mutex);
  422. }
  423. static void *logsys_worker_thread (void *data) __attribute__((__noreturn__));
  424. static void *logsys_worker_thread (void *data)
  425. {
  426. int log_msg;
  427. char buf[COMBINE_BUFFER_SIZE];
  428. /*
  429. * Signal wthread_create that the initialization process may continue
  430. */
  431. wthread_signal ();
  432. logsys_lock();
  433. log_rec_idx = flt_data[FDTAIL_INDEX];
  434. logsys_unlock();
  435. for (;;) {
  436. wthread_wait ();
  437. /*
  438. * Read and copy the logging record index position
  439. * It may have been updated by records_reclaim if
  440. * messages were lost or or log_rec on the first new
  441. * logging record available
  442. */
  443. /*
  444. * Process any pending log messages here
  445. */
  446. for (;;) {
  447. logsys_lock();
  448. if (log_requests_lost > 0) {
  449. printf ("lost %d log requests\n", log_requests_lost);
  450. log_requests_pending -= log_requests_lost;
  451. log_requests_lost = 0;
  452. }
  453. if (log_requests_pending == 0) {
  454. logsys_unlock();
  455. break;
  456. }
  457. log_rec_idx = record_read (buf, log_rec_idx, &log_msg);
  458. if (log_msg) {
  459. log_requests_pending -= 1;
  460. }
  461. logsys_unlock();
  462. /*
  463. * print the stored buffer
  464. */
  465. if (log_msg) {
  466. record_print (buf);
  467. }
  468. }
  469. if (wthread_should_exit) {
  470. pthread_exit (NULL);
  471. }
  472. }
  473. }
  474. static void wthread_create (void)
  475. {
  476. int res;
  477. if (wthread_active) {
  478. return;
  479. }
  480. wthread_active = 1;
  481. pthread_mutex_init (&logsys_cond_mutex, NULL);
  482. pthread_cond_init (&logsys_cond, NULL);
  483. pthread_mutex_lock (&logsys_cond_mutex);
  484. res = pthread_create (&logsys_thread_id, NULL,
  485. logsys_worker_thread, NULL);
  486. /*
  487. * Wait for thread to be started
  488. */
  489. wthread_wait_locked ();
  490. }
  491. /*
  492. * Internal API - exported
  493. */
  494. void _logsys_nosubsys_set (void)
  495. {
  496. logsys_mode |= LOG_MODE_NOSUBSYS;
  497. }
  498. unsigned int _logsys_subsys_create (
  499. const char *subsys,
  500. unsigned int priority)
  501. {
  502. assert (subsys != NULL);
  503. return logsys_config_subsys_set (
  504. subsys,
  505. LOGSYS_TAG_LOG,
  506. priority);
  507. }
  508. int _logsys_wthread_create (void)
  509. {
  510. if ((logsys_mode & LOG_MODE_FORK) == 0) {
  511. if (logsys_name != NULL) {
  512. openlog (logsys_name, LOG_CONS|LOG_PID, logsys_facility);
  513. }
  514. wthread_create();
  515. atexit (logsys_atexit);
  516. }
  517. return (0);
  518. }
  519. int _logsys_rec_init (unsigned int size)
  520. {
  521. /*
  522. * First record starts at zero
  523. * Last record ends at zero
  524. */
  525. flt_data = malloc ((size + 2) * sizeof (unsigned int));
  526. assert (flt_data != NULL);
  527. flt_data_size = size;
  528. assert (flt_data != NULL);
  529. flt_data[FDHEAD_INDEX] = 0;
  530. flt_data[FDTAIL_INDEX] = 0;
  531. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  532. pthread_spin_init (&logsys_idx_spinlock, 0);
  533. #endif
  534. return (0);
  535. }
  536. /*
  537. * u32 RECORD SIZE
  538. * u32 record ident
  539. * u32 arg count
  540. * u32 file line
  541. * u32 subsys length
  542. * buffer null terminated subsys
  543. * u32 filename length
  544. * buffer null terminated filename
  545. * u32 filename length
  546. * buffer null terminated function
  547. * u32 arg1 length
  548. * buffer arg1
  549. * ... repeats length & arg
  550. */
  551. void _logsys_log_rec (
  552. int subsys,
  553. const char *function_name,
  554. const char *file_name,
  555. int file_line,
  556. unsigned int rec_ident,
  557. ...)
  558. {
  559. va_list ap;
  560. const void *buf_args[64];
  561. unsigned int buf_len[64];
  562. unsigned int i;
  563. unsigned int idx;
  564. unsigned int arguments = 0;
  565. unsigned int record_reclaim_size;
  566. unsigned int index_start;
  567. int words_written;
  568. record_reclaim_size = 0;
  569. /*
  570. * Decode VA Args
  571. */
  572. va_start (ap, rec_ident);
  573. arguments = 3;
  574. for (;;) {
  575. assert (arguments < 64);
  576. buf_args[arguments] = va_arg (ap, void *);
  577. if (buf_args[arguments] == LOG_REC_END) {
  578. break;
  579. }
  580. buf_len[arguments] = va_arg (ap, int);
  581. record_reclaim_size += ((buf_len[arguments] + 3) >> 2) + 1;
  582. arguments++;
  583. }
  584. va_end (ap);
  585. /*
  586. * Encode logsys subsystem identity, filename, and function
  587. */
  588. buf_args[0] = logsys_loggers[subsys].subsys;
  589. buf_len[0] = strlen (logsys_loggers[subsys].subsys) + 1;
  590. buf_args[1] = file_name;
  591. buf_len[1] = strlen (file_name) + 1;
  592. buf_args[2] = function_name;
  593. buf_len[2] = strlen (function_name) + 1;
  594. for (i = 0; i < 3; i++) {
  595. record_reclaim_size += ((buf_len[i] + 3) >> 2) + 1;
  596. }
  597. idx = flt_data[FDHEAD_INDEX];
  598. index_start = idx;
  599. /*
  600. * Reclaim data needed for record including 4 words for the header
  601. */
  602. records_reclaim (idx, record_reclaim_size + 4);
  603. /*
  604. * Write record size of zero and rest of header information
  605. */
  606. flt_data[idx++] = 0;
  607. idx_word_step(idx);
  608. flt_data[idx++] = rec_ident;
  609. idx_word_step(idx);
  610. flt_data[idx++] = file_line;
  611. idx_word_step(idx);
  612. flt_data[idx++] = records_written;
  613. idx_word_step(idx);
  614. /*
  615. * Encode all of the arguments into the log message
  616. */
  617. for (i = 0; i < arguments; i++) {
  618. unsigned int bytes;
  619. unsigned int full_words;
  620. unsigned int total_words;
  621. bytes = buf_len[i];
  622. full_words = bytes >> 2;
  623. total_words = (bytes + 3) >> 2;
  624. flt_data[idx++] = total_words;
  625. idx_word_step(idx);
  626. /*
  627. * determine if this is a wrapped write or normal write
  628. */
  629. if (idx + total_words < flt_data_size) {
  630. /*
  631. * dont need to wrap buffer
  632. */
  633. my_memcpy_32bit (&flt_data[idx], buf_args[i], full_words);
  634. if (bytes % 4) {
  635. my_memcpy_8bit ((char *)&flt_data[idx + full_words],
  636. ((const char *)buf_args[i]) + (full_words << 2), bytes % 4);
  637. }
  638. } else {
  639. /*
  640. * need to wrap buffer
  641. */
  642. unsigned int first;
  643. unsigned int second;
  644. first = flt_data_size - idx;
  645. if (first > full_words) {
  646. first = full_words;
  647. }
  648. second = full_words - first;
  649. my_memcpy_32bit (&flt_data[idx],
  650. (const int *)buf_args[i], first);
  651. my_memcpy_32bit (&flt_data[0],
  652. (const int *)(((const unsigned char *)buf_args[i]) + (first << 2)),
  653. second);
  654. if (bytes % 4) {
  655. my_memcpy_8bit ((char *)&flt_data[0 + second],
  656. ((const char *)buf_args[i]) + (full_words << 2), bytes % 4);
  657. }
  658. }
  659. idx += total_words;
  660. idx_buffer_step (idx);
  661. }
  662. words_written = idx - index_start;
  663. if (words_written < 0) {
  664. words_written += flt_data_size;
  665. }
  666. /*
  667. * Commit the write of the record size now that the full record
  668. * is in the memory buffer
  669. */
  670. flt_data[index_start] = words_written;
  671. /*
  672. * If the index of the current head equals the current log_rec_idx,
  673. * and this is not a log_printf operation, set the log_rec_idx to
  674. * the new head position and commit the new head.
  675. */
  676. logsys_lock();
  677. if (rec_ident & LOGSYS_TAG_LOG) {
  678. log_requests_pending += 1;
  679. }
  680. if (log_requests_pending == 0) {
  681. log_rec_idx = idx;
  682. }
  683. flt_data[FDHEAD_INDEX] = idx;
  684. logsys_unlock();
  685. records_written++;
  686. }
  687. void _logsys_log_printf (
  688. int subsys,
  689. const char *function_name,
  690. const char *file_name,
  691. int file_line,
  692. unsigned int level,
  693. const char *format,
  694. ...)
  695. {
  696. char logsys_print_buffer[COMBINE_BUFFER_SIZE];
  697. unsigned int len;
  698. va_list ap;
  699. if (logsys_mode & LOG_MODE_NOSUBSYS) {
  700. subsys = 0;
  701. }
  702. if (level > logsys_loggers[subsys].priority) {
  703. return;
  704. }
  705. va_start (ap, format);
  706. len = vsprintf (logsys_print_buffer, format, ap);
  707. va_end (ap);
  708. if (logsys_print_buffer[len - 1] == '\n') {
  709. logsys_print_buffer[len - 1] = '\0';
  710. len -= 1;
  711. }
  712. /*
  713. * Create a log record
  714. */
  715. _logsys_log_rec (subsys,
  716. function_name,
  717. file_name,
  718. file_line,
  719. (level+1) << 28,
  720. logsys_print_buffer, len + 1,
  721. LOG_REC_END);
  722. if ((logsys_mode & LOG_MODE_THREADED) == 0) {
  723. /*
  724. * Output (and block) if the log mode is not threaded otherwise
  725. * expect the worker thread to output the log data once signaled
  726. */
  727. log_printf_to_logs (logsys_loggers[subsys].subsys,
  728. function_name, file_name, file_line, level,
  729. logsys_print_buffer);
  730. } else {
  731. /*
  732. * Signal worker thread to display logging output
  733. */
  734. wthread_signal ();
  735. }
  736. }
  737. /*
  738. * External Configuration and Initialization API
  739. */
  740. void logsys_fork_completed (void)
  741. {
  742. logsys_mode &= ~LOG_MODE_FORK;
  743. _logsys_wthread_create ();
  744. }
  745. void logsys_config_mode_set (unsigned int mode)
  746. {
  747. pthread_mutex_lock (&logsys_config_mutex);
  748. logsys_mode = mode;
  749. pthread_mutex_unlock (&logsys_config_mutex);
  750. }
  751. unsigned int logsys_config_mode_get (void)
  752. {
  753. return logsys_mode;
  754. }
  755. static void logsys_close_logfile()
  756. {
  757. if (logsys_file_fp != NULL) {
  758. fclose (logsys_file_fp);
  759. logsys_file_fp = NULL;
  760. }
  761. }
  762. int logsys_config_file_set (const char **error_string, const char *file)
  763. {
  764. static char error_string_response[512];
  765. if (file == NULL) {
  766. logsys_close_logfile();
  767. return (0);
  768. }
  769. pthread_mutex_lock (&logsys_config_mutex);
  770. if (logsys_mode & LOG_MODE_OUTPUT_FILE) {
  771. logsys_file = file;
  772. logsys_close_logfile();
  773. logsys_file_fp = fopen (file, "a+");
  774. if (logsys_file_fp == 0) {
  775. sprintf (error_string_response,
  776. "Can't open logfile '%s' for reason (%s).\n",
  777. file, strerror (errno));
  778. *error_string = error_string_response;
  779. pthread_mutex_unlock (&logsys_config_mutex);
  780. return (-1);
  781. }
  782. } else
  783. logsys_close_logfile();
  784. pthread_mutex_unlock (&logsys_config_mutex);
  785. return (0);
  786. }
  787. void logsys_format_set (char *format)
  788. {
  789. pthread_mutex_lock (&logsys_config_mutex);
  790. if (format_buffer) {
  791. free(format_buffer);
  792. format_buffer = NULL;
  793. }
  794. if (format) {
  795. format_buffer = strdup(format);
  796. } else {
  797. format_buffer = strdup("[%6s] %b");
  798. }
  799. pthread_mutex_unlock (&logsys_config_mutex);
  800. }
  801. char *logsys_format_get (void)
  802. {
  803. return format_buffer;
  804. }
  805. void logsys_config_facility_set (const char *name, unsigned int facility)
  806. {
  807. pthread_mutex_lock (&logsys_config_mutex);
  808. logsys_name = name;
  809. logsys_facility = facility;
  810. pthread_mutex_unlock (&logsys_config_mutex);
  811. }
  812. int logsys_facility_id_get (const char *name)
  813. {
  814. unsigned int i;
  815. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  816. if (strcasecmp(name, facilitynames[i].c_name) == 0) {
  817. return (facilitynames[i].c_val);
  818. }
  819. }
  820. return (-1);
  821. }
  822. const char *logsys_facility_name_get (unsigned int facility)
  823. {
  824. unsigned int i;
  825. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  826. if (facility == facilitynames[i].c_val) {
  827. return (facilitynames[i].c_name);
  828. }
  829. }
  830. return (NULL);
  831. }
  832. int logsys_priority_id_get (const char *name)
  833. {
  834. unsigned int i;
  835. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  836. if (strcasecmp(name, prioritynames[i].c_name) == 0) {
  837. return (prioritynames[i].c_val);
  838. }
  839. }
  840. return (-1);
  841. }
  842. const char *logsys_priority_name_get (unsigned int priority)
  843. {
  844. unsigned int i;
  845. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  846. if (priority == prioritynames[i].c_val) {
  847. return (prioritynames[i].c_name);
  848. }
  849. }
  850. return (NULL);
  851. }
  852. int logsys_tag_id_get (const char *name)
  853. {
  854. unsigned int i;
  855. for (i = 0; tagnames[i].c_name != NULL; i++) {
  856. if (strcasecmp(name, tagnames[i].c_name) == 0) {
  857. return (tagnames[i].c_val);
  858. }
  859. }
  860. return (-1);
  861. }
  862. const char *logsys_tag_name_get (unsigned int tag)
  863. {
  864. unsigned int i;
  865. for (i = 0; tagnames[i].c_name != NULL; i++) {
  866. if (tag == tagnames[i].c_val) {
  867. return (tagnames[i].c_name);
  868. }
  869. }
  870. return (NULL);
  871. }
  872. unsigned int logsys_config_subsys_set (
  873. const char *subsys,
  874. unsigned int tags,
  875. unsigned int priority)
  876. {
  877. int i;
  878. pthread_mutex_lock (&logsys_config_mutex);
  879. for (i = 0; i < SUBSYS_MAX; i++) {
  880. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  881. logsys_loggers[i].tags = tags;
  882. logsys_loggers[i].priority = priority;
  883. break;
  884. }
  885. }
  886. if (i == SUBSYS_MAX) {
  887. for (i = 0; i < SUBSYS_MAX; i++) {
  888. if (strcmp (logsys_loggers[i].subsys, "") == 0) {
  889. strncpy (logsys_loggers[i].subsys, subsys,
  890. sizeof(logsys_loggers[i].subsys));
  891. logsys_loggers[i].tags = tags;
  892. logsys_loggers[i].priority = priority;
  893. break;
  894. }
  895. }
  896. }
  897. assert(i < SUBSYS_MAX);
  898. pthread_mutex_unlock (&logsys_config_mutex);
  899. return i;
  900. }
  901. int logsys_config_subsys_get (
  902. const char *subsys,
  903. unsigned int *tags,
  904. unsigned int *priority)
  905. {
  906. unsigned int i;
  907. pthread_mutex_lock (&logsys_config_mutex);
  908. for (i = 0; i < SUBSYS_MAX; i++) {
  909. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  910. *tags = logsys_loggers[i].tags;
  911. *priority = logsys_loggers[i].priority;
  912. pthread_mutex_unlock (&logsys_config_mutex);
  913. return i;
  914. }
  915. }
  916. pthread_mutex_unlock (&logsys_config_mutex);
  917. return (-1);
  918. }
  919. int logsys_log_rec_store (const char *filename)
  920. {
  921. int fd;
  922. ssize_t written_size;
  923. size_t size_to_write = (flt_data_size + 2) * sizeof (unsigned int);
  924. fd = open (filename, O_CREAT|O_RDWR, 0700);
  925. if (fd == -1) {
  926. return (-1);
  927. }
  928. written_size = write (fd, flt_data, size_to_write);
  929. if (close (fd) != 0)
  930. return (-1);
  931. if (written_size < 0) {
  932. return (-1);
  933. } else if ((size_t)written_size != size_to_write) {
  934. return (-1);
  935. }
  936. return (0);
  937. }
  938. static void logsys_atexit (void)
  939. {
  940. if (wthread_active) {
  941. wthread_should_exit = 1;
  942. wthread_signal ();
  943. pthread_join (logsys_thread_id, NULL);
  944. }
  945. }
  946. void logsys_atsegv (void)
  947. {
  948. if (wthread_active) {
  949. wthread_should_exit = 1;
  950. wthread_signal ();
  951. pthread_join (logsys_thread_id, NULL);
  952. }
  953. }
  954. int logsys_init (
  955. const char *name,
  956. int mode,
  957. int facility,
  958. int priority,
  959. const char *file,
  960. char *format,
  961. int rec_size)
  962. {
  963. const char *errstr;
  964. _logsys_nosubsys_set ();
  965. _logsys_subsys_create (name, priority);
  966. strncpy (logsys_loggers[0].subsys, name,
  967. sizeof (logsys_loggers[0].subsys));
  968. logsys_config_mode_set (mode);
  969. logsys_config_facility_set (name, facility);
  970. logsys_config_file_set (&errstr, file);
  971. logsys_format_set (format);
  972. _logsys_rec_init (rec_size);
  973. _logsys_wthread_create ();
  974. return (0);
  975. }
  976. int logsys_conf (
  977. char *name,
  978. int mode,
  979. int facility,
  980. int priority,
  981. char *file)
  982. {
  983. const char *errstr;
  984. _logsys_rec_init (100000);
  985. strncpy (logsys_loggers[0].subsys, name,
  986. sizeof (logsys_loggers[0].subsys));
  987. logsys_config_mode_set (mode);
  988. logsys_config_facility_set (name, facility);
  989. logsys_config_file_set (&errstr, file);
  990. return (0);
  991. }
  992. void logsys_exit (void)
  993. {
  994. }