logsys.c 24 KB

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