logsys.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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)
  758. format_buffer = format;
  759. else
  760. format_buffer = "[%6s] %b";
  761. pthread_mutex_unlock (&logsys_config_mutex);
  762. }
  763. void logsys_config_facility_set (char *name, unsigned int facility)
  764. {
  765. pthread_mutex_lock (&logsys_config_mutex);
  766. logsys_name = name;
  767. logsys_facility = facility;
  768. pthread_mutex_unlock (&logsys_config_mutex);
  769. }
  770. int logsys_facility_id_get (const char *name)
  771. {
  772. unsigned int i;
  773. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  774. if (strcasecmp(name, facilitynames[i].c_name) == 0) {
  775. return (facilitynames[i].c_val);
  776. }
  777. }
  778. return (-1);
  779. }
  780. const char *logsys_facility_name_get (unsigned int facility)
  781. {
  782. unsigned int i;
  783. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  784. if (facility == facilitynames[i].c_val) {
  785. return (facilitynames[i].c_name);
  786. }
  787. }
  788. return (NULL);
  789. }
  790. int logsys_priority_id_get (const char *name)
  791. {
  792. unsigned int i;
  793. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  794. if (strcasecmp(name, prioritynames[i].c_name) == 0) {
  795. return (prioritynames[i].c_val);
  796. }
  797. }
  798. return (-1);
  799. }
  800. const char *logsys_priority_name_get (unsigned int priority)
  801. {
  802. unsigned int i;
  803. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  804. if (priority == prioritynames[i].c_val) {
  805. return (prioritynames[i].c_name);
  806. }
  807. }
  808. return (NULL);
  809. }
  810. int logsys_tag_id_get (const char *name)
  811. {
  812. unsigned int i;
  813. for (i = 0; tagnames[i].c_name != NULL; i++) {
  814. if (strcasecmp(name, tagnames[i].c_name) == 0) {
  815. return (tagnames[i].c_val);
  816. }
  817. }
  818. return (-1);
  819. }
  820. const char *logsys_tag_name_get (unsigned int tag)
  821. {
  822. unsigned int i;
  823. for (i = 0; tagnames[i].c_name != NULL; i++) {
  824. if (tag == tagnames[i].c_val) {
  825. return (tagnames[i].c_name);
  826. }
  827. }
  828. return (NULL);
  829. }
  830. unsigned int logsys_config_subsys_set (
  831. const char *subsys,
  832. unsigned int tags,
  833. unsigned int priority)
  834. {
  835. int i;
  836. pthread_mutex_lock (&logsys_config_mutex);
  837. for (i = 0; i < SUBSYS_MAX; i++) {
  838. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  839. logsys_loggers[i].tags = tags;
  840. logsys_loggers[i].priority = priority;
  841. break;
  842. }
  843. }
  844. if (i == SUBSYS_MAX) {
  845. for (i = 0; i < SUBSYS_MAX; i++) {
  846. if (strcmp (logsys_loggers[i].subsys, "") == 0) {
  847. strncpy (logsys_loggers[i].subsys, subsys,
  848. sizeof(logsys_loggers[i].subsys));
  849. logsys_loggers[i].tags = tags;
  850. logsys_loggers[i].priority = priority;
  851. break;
  852. }
  853. }
  854. }
  855. assert(i < SUBSYS_MAX);
  856. pthread_mutex_unlock (&logsys_config_mutex);
  857. return i;
  858. }
  859. int logsys_config_subsys_get (
  860. const char *subsys,
  861. unsigned int *tags,
  862. unsigned int *priority)
  863. {
  864. unsigned int i;
  865. pthread_mutex_lock (&logsys_config_mutex);
  866. for (i = 0; i < SUBSYS_MAX; i++) {
  867. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  868. *tags = logsys_loggers[i].tags;
  869. *priority = logsys_loggers[i].priority;
  870. pthread_mutex_unlock (&logsys_config_mutex);
  871. return i;
  872. }
  873. }
  874. pthread_mutex_unlock (&logsys_config_mutex);
  875. return (-1);
  876. }
  877. int logsys_log_rec_store (char *filename)
  878. {
  879. int fd;
  880. ssize_t written_size;
  881. size_t size_to_write = (flt_data_size + 2) * sizeof (unsigned int);
  882. fd = open (filename, O_CREAT|O_RDWR, 0700);
  883. if (fd == -1) {
  884. return (-1);
  885. }
  886. written_size = write (fd, flt_data, size_to_write);
  887. if (written_size < 0) {
  888. return (-1);
  889. } else if ((size_t)written_size != size_to_write) {
  890. return (-1);
  891. }
  892. return (0);
  893. }
  894. static void logsys_atexit (void)
  895. {
  896. if (wthread_active) {
  897. wthread_should_exit = 1;
  898. wthread_signal ();
  899. pthread_join (logsys_thread_id, NULL);
  900. }
  901. }
  902. void logsys_atsegv (void)
  903. {
  904. if (wthread_active) {
  905. wthread_should_exit = 1;
  906. wthread_signal ();
  907. pthread_join (logsys_thread_id, NULL);
  908. }
  909. }
  910. int logsys_init (
  911. char *name,
  912. int mode,
  913. int facility,
  914. int priority,
  915. char *file,
  916. char *format,
  917. int rec_size)
  918. {
  919. char *errstr;
  920. _logsys_nosubsys_set ();
  921. _logsys_subsys_create (name, priority);
  922. strncpy (logsys_loggers[0].subsys, name,
  923. sizeof (logsys_loggers[0].subsys));
  924. logsys_config_mode_set (mode);
  925. logsys_config_facility_set (name, facility);
  926. logsys_config_file_set (&errstr, file);
  927. logsys_format_set (format);
  928. _logsys_rec_init (rec_size);
  929. _logsys_wthread_create ();
  930. return (0);
  931. }
  932. int logsys_conf (
  933. char *name,
  934. int mode,
  935. int facility,
  936. int priority,
  937. char *file)
  938. {
  939. char *errstr;
  940. _logsys_rec_init (100000);
  941. strncpy (logsys_loggers[0].subsys, name,
  942. sizeof (logsys_loggers[0].subsys));
  943. logsys_config_mode_set (mode);
  944. logsys_config_facility_set (name, facility);
  945. logsys_config_file_set (&errstr, file);
  946. return (0);
  947. }
  948. void logsys_exit (void)
  949. {
  950. }