logsys.c 25 KB

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