logsys.c 24 KB

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