logsys.c 23 KB

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