logsys.c 25 KB

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