logsys.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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 inline void wthread_wait_locked (void)
  371. {
  372. pthread_cond_wait (&logsys_cond, &logsys_cond_mutex);
  373. pthread_mutex_unlock (&logsys_cond_mutex);
  374. }
  375. static void *logsys_worker_thread (void *data)
  376. {
  377. int log_msg;
  378. char buf[COMBINE_BUFFER_SIZE];
  379. /*
  380. * Signal wthread_create that the initialization process may continue
  381. */
  382. wthread_signal ();
  383. pthread_spin_lock (&logsys_idx_spinlock);
  384. log_rec_idx = flt_data[FDTAIL_INDEX];
  385. pthread_spin_unlock (&logsys_idx_spinlock);
  386. for (;;) {
  387. wthread_wait ();
  388. /*
  389. * Read and copy the logging record index position
  390. * It may have been updated by records_reclaim if
  391. * messages were lost or or log_rec on the first new
  392. * logging record available
  393. */
  394. /*
  395. * Process any pending log messages here
  396. */
  397. for (;;) {
  398. pthread_spin_lock (&logsys_idx_spinlock);
  399. if (log_requests_lost > 0) {
  400. printf ("lost %d log requests\n", log_requests_lost);
  401. log_requests_pending -= log_requests_lost;
  402. log_requests_lost = 0;
  403. }
  404. if (log_requests_pending == 0) {
  405. pthread_spin_unlock (&logsys_idx_spinlock);
  406. break;
  407. }
  408. log_rec_idx = record_read (buf, log_rec_idx, &log_msg);
  409. if (log_msg) {
  410. log_requests_pending -= 1;
  411. }
  412. pthread_spin_unlock (&logsys_idx_spinlock);
  413. /*
  414. * print the stored buffer
  415. */
  416. if (log_msg) {
  417. record_print (buf);
  418. }
  419. }
  420. if (wthread_should_exit) {
  421. pthread_exit (NULL);
  422. }
  423. }
  424. }
  425. static void wthread_create (void)
  426. {
  427. int res;
  428. if (wthread_active) {
  429. return;
  430. }
  431. wthread_active = 1;
  432. pthread_mutex_init (&logsys_cond_mutex, NULL);
  433. pthread_cond_init (&logsys_cond, NULL);
  434. pthread_mutex_lock (&logsys_cond_mutex);
  435. res = pthread_create (&logsys_thread_id, NULL,
  436. logsys_worker_thread, NULL);
  437. /*
  438. * Wait for thread to be started
  439. */
  440. wthread_wait_locked ();
  441. }
  442. /*
  443. * Internal API - exported
  444. */
  445. void _logsys_nosubsys_set (void)
  446. {
  447. logsys_mode |= LOG_MODE_NOSUBSYS;
  448. }
  449. unsigned int _logsys_subsys_create (
  450. const char *subsys,
  451. unsigned int priority)
  452. {
  453. assert (subsys != NULL);
  454. return logsys_config_subsys_set (
  455. subsys,
  456. LOGSYS_TAG_LOG,
  457. priority);
  458. }
  459. int _logsys_wthread_create (void)
  460. {
  461. if ((logsys_mode & LOG_MODE_FORK) == 0) {
  462. if (logsys_name != NULL) {
  463. openlog (logsys_name, LOG_CONS|LOG_PID, logsys_facility);
  464. }
  465. wthread_create();
  466. atexit (logsys_atexit);
  467. }
  468. return (0);
  469. }
  470. int _logsys_rec_init (unsigned int size)
  471. {
  472. /*
  473. * First record starts at zero
  474. * Last record ends at zero
  475. */
  476. flt_data = malloc ((size + 2) * sizeof (unsigned int));
  477. assert (flt_data != NULL);
  478. flt_data_size = size;
  479. assert (flt_data != NULL);
  480. flt_data[FDHEAD_INDEX] = 0;
  481. flt_data[FDTAIL_INDEX] = 0;
  482. pthread_spin_init (&logsys_idx_spinlock, 0);
  483. return (0);
  484. }
  485. /*
  486. * u32 RECORD SIZE
  487. * u32 record ident
  488. * u32 arg count
  489. * u32 file line
  490. * u32 subsys length
  491. * buffer null terminated subsys
  492. * u32 filename length
  493. * buffer null terminated filename
  494. * u32 filename length
  495. * buffer null terminated function
  496. * u32 arg1 length
  497. * buffer arg1
  498. * ... repeats length & arg
  499. */
  500. void _logsys_log_rec (
  501. int subsys,
  502. char *function_name,
  503. char *file_name,
  504. int file_line,
  505. unsigned int rec_ident,
  506. ...)
  507. {
  508. va_list ap;
  509. void *buf_args[64];
  510. unsigned int buf_len[64];
  511. unsigned int i;
  512. unsigned int idx;
  513. unsigned int arguments = 0;
  514. unsigned int record_reclaim_size;
  515. unsigned int index_start;
  516. int words_written;
  517. record_reclaim_size = 0;
  518. /*
  519. * Decode VA Args
  520. */
  521. va_start (ap, rec_ident);
  522. arguments = 3;
  523. for (;;) {
  524. assert (arguments < 64);
  525. buf_args[arguments] = va_arg (ap, void *);
  526. if (buf_args[arguments] == LOG_REC_END) {
  527. break;
  528. }
  529. buf_len[arguments] = va_arg (ap, int);
  530. record_reclaim_size += ((buf_len[arguments] + 3) >> 2) + 1;
  531. arguments++;
  532. }
  533. va_end (ap);
  534. /*
  535. * Encode logsys subsystem identity, filename, and function
  536. */
  537. buf_args[0] = logsys_loggers[subsys].subsys;
  538. buf_len[0] = strlen (logsys_loggers[subsys].subsys) + 1;
  539. buf_args[1] = file_name;
  540. buf_len[1] = strlen (file_name) + 1;
  541. buf_args[2] = function_name;
  542. buf_len[2] = strlen (function_name) + 1;
  543. for (i = 0; i < 3; i++) {
  544. record_reclaim_size += ((buf_len[i] + 3) >> 2) + 1;
  545. }
  546. idx = flt_data[FDHEAD_INDEX];
  547. index_start = idx;
  548. /*
  549. * Reclaim data needed for record including 4 words for the header
  550. */
  551. records_reclaim (idx, record_reclaim_size + 4);
  552. /*
  553. * Write record size of zero and rest of header information
  554. */
  555. flt_data[idx++] = 0;
  556. idx_word_step(idx);
  557. flt_data[idx++] = rec_ident;
  558. idx_word_step(idx);
  559. flt_data[idx++] = file_line;
  560. idx_word_step(idx);
  561. flt_data[idx++] = records_written;
  562. idx_word_step(idx);
  563. /*
  564. * Encode all of the arguments into the log message
  565. */
  566. for (i = 0; i < arguments; i++) {
  567. unsigned int bytes;
  568. unsigned int full_words;
  569. unsigned int total_words;
  570. bytes = buf_len[i];
  571. full_words = bytes >> 2;
  572. total_words = (bytes + 3) >> 2;
  573. flt_data[idx++] = total_words;
  574. idx_word_step(idx);
  575. /*
  576. * determine if this is a wrapped write or normal write
  577. */
  578. if (idx + total_words < flt_data_size) {
  579. /*
  580. * dont need to wrap buffer
  581. */
  582. my_memcpy_32bit (&flt_data[idx], buf_args[i], full_words);
  583. if (bytes % 4) {
  584. my_memcpy_8bit ((char *)&flt_data[idx + full_words],
  585. ((char *)buf_args[i]) + (full_words << 2), bytes % 4);
  586. }
  587. } else {
  588. /*
  589. * need to wrap buffer
  590. */
  591. unsigned int first;
  592. unsigned int second;
  593. first = flt_data_size - idx;
  594. if (first > full_words) {
  595. first = full_words;
  596. }
  597. second = full_words - first;
  598. my_memcpy_32bit (&flt_data[idx], (int *)buf_args[i], first);
  599. my_memcpy_32bit (&flt_data[0],
  600. (int *)(((unsigned char *)buf_args[i]) + (first << 2)),
  601. second);
  602. if (bytes % 4) {
  603. my_memcpy_8bit ((char *)&flt_data[0 + second],
  604. ((char *)buf_args[i]) + (full_words << 2), bytes % 4);
  605. }
  606. }
  607. idx += total_words;
  608. idx_buffer_step (idx);
  609. }
  610. words_written = idx - index_start;
  611. if (words_written < 0) {
  612. words_written += flt_data_size;
  613. }
  614. /*
  615. * Commit the write of the record size now that the full record
  616. * is in the memory buffer
  617. */
  618. flt_data[index_start] = words_written;
  619. /*
  620. * If the index of the current head equals the current log_rec_idx,
  621. * and this is not a log_printf operation, set the log_rec_idx to
  622. * the new head position and commit the new head.
  623. */
  624. pthread_spin_lock (&logsys_idx_spinlock);
  625. if (rec_ident & LOGSYS_TAG_LOG) {
  626. log_requests_pending += 1;
  627. }
  628. if (log_requests_pending == 0) {
  629. log_rec_idx = idx;
  630. }
  631. flt_data[FDHEAD_INDEX] = idx;
  632. pthread_spin_unlock (&logsys_idx_spinlock);
  633. records_written++;
  634. }
  635. void _logsys_log_printf (
  636. int subsys,
  637. char *function_name,
  638. char *file_name,
  639. int file_line,
  640. unsigned int level,
  641. char *format,
  642. ...)
  643. {
  644. char logsys_print_buffer[COMBINE_BUFFER_SIZE];
  645. unsigned int len;
  646. va_list ap;
  647. if (logsys_mode & LOG_MODE_NOSUBSYS) {
  648. subsys = 0;
  649. }
  650. if (level > logsys_loggers[subsys].priority) {
  651. return;
  652. }
  653. va_start (ap, format);
  654. len = vsprintf (logsys_print_buffer, format, ap);
  655. va_end (ap);
  656. if (logsys_print_buffer[len - 1] == '\n') {
  657. logsys_print_buffer[len - 1] = '\0';
  658. len -= 1;
  659. }
  660. /*
  661. * Create a log record
  662. */
  663. _logsys_log_rec (subsys,
  664. function_name,
  665. file_name,
  666. file_line,
  667. (level+1) << 28,
  668. logsys_print_buffer, len + 1,
  669. LOG_REC_END);
  670. if ((logsys_mode & LOG_MODE_THREADED) == 0) {
  671. /*
  672. * Output (and block) if the log mode is not threaded otherwise
  673. * expect the worker thread to output the log data once signaled
  674. */
  675. log_printf_to_logs (logsys_loggers[subsys].subsys,
  676. function_name, file_name, file_line, level,
  677. logsys_print_buffer);
  678. } else {
  679. /*
  680. * Signal worker thread to display logging output
  681. */
  682. wthread_signal ();
  683. }
  684. }
  685. /*
  686. * External Configuration and Initialization API
  687. */
  688. void logsys_fork_completed (void)
  689. {
  690. logsys_mode &= ~LOG_MODE_FORK;
  691. _logsys_wthread_create ();
  692. }
  693. void logsys_config_mode_set (unsigned int mode)
  694. {
  695. pthread_mutex_lock (&logsys_config_mutex);
  696. logsys_mode = mode;
  697. pthread_mutex_unlock (&logsys_config_mutex);
  698. }
  699. unsigned int logsys_config_mode_get (void)
  700. {
  701. return logsys_mode;
  702. }
  703. int logsys_config_file_set (char **error_string, char *file)
  704. {
  705. static char error_string_response[512];
  706. if (file == NULL) {
  707. return (0);
  708. }
  709. pthread_mutex_lock (&logsys_config_mutex);
  710. if (logsys_mode & LOG_MODE_OUTPUT_FILE) {
  711. logsys_file = file;
  712. if (logsys_file_fp != NULL) {
  713. fclose (logsys_file_fp);
  714. }
  715. logsys_file_fp = fopen (file, "a+");
  716. if (logsys_file_fp == 0) {
  717. sprintf (error_string_response,
  718. "Can't open logfile '%s' for reason (%s).\n",
  719. file, strerror (errno));
  720. *error_string = error_string_response;
  721. pthread_mutex_unlock (&logsys_config_mutex);
  722. return (-1);
  723. }
  724. }
  725. pthread_mutex_unlock (&logsys_config_mutex);
  726. return (0);
  727. }
  728. void logsys_format_set (char *format)
  729. {
  730. pthread_mutex_lock (&logsys_config_mutex);
  731. logsys_format = format;
  732. pthread_mutex_unlock (&logsys_config_mutex);
  733. }
  734. void logsys_config_facility_set (char *name, unsigned int facility)
  735. {
  736. pthread_mutex_lock (&logsys_config_mutex);
  737. logsys_name = name;
  738. logsys_facility = facility;
  739. pthread_mutex_unlock (&logsys_config_mutex);
  740. }
  741. int logsys_facility_id_get (const char *name)
  742. {
  743. unsigned int i;
  744. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  745. if (strcasecmp(name, facilitynames[i].c_name) == 0) {
  746. return (facilitynames[i].c_val);
  747. }
  748. }
  749. return (-1);
  750. }
  751. const char *logsys_facility_name_get (unsigned int facility)
  752. {
  753. unsigned int i;
  754. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  755. if (facility == facilitynames[i].c_val) {
  756. return (facilitynames[i].c_name);
  757. }
  758. }
  759. return (NULL);
  760. }
  761. int logsys_priority_id_get (const char *name)
  762. {
  763. unsigned int i;
  764. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  765. if (strcasecmp(name, prioritynames[i].c_name) == 0) {
  766. return (prioritynames[i].c_val);
  767. }
  768. }
  769. return (-1);
  770. }
  771. const char *logsys_priority_name_get (unsigned int priority)
  772. {
  773. unsigned int i;
  774. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  775. if (priority == prioritynames[i].c_val) {
  776. return (prioritynames[i].c_name);
  777. }
  778. }
  779. return (NULL);
  780. }
  781. unsigned int logsys_config_subsys_set (
  782. const char *subsys,
  783. unsigned int tags,
  784. unsigned int priority)
  785. {
  786. int i;
  787. pthread_mutex_lock (&logsys_config_mutex);
  788. for (i = 0; i < SUBSYS_MAX; i++) {
  789. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  790. logsys_loggers[i].tags = tags;
  791. logsys_loggers[i].priority = priority;
  792. break;
  793. }
  794. }
  795. if (i == SUBSYS_MAX) {
  796. for (i = 0; i < SUBSYS_MAX; i++) {
  797. if (strcmp (logsys_loggers[i].subsys, "") == 0) {
  798. strncpy (logsys_loggers[i].subsys, subsys,
  799. sizeof(logsys_loggers[i].subsys));
  800. logsys_loggers[i].tags = tags;
  801. logsys_loggers[i].priority = priority;
  802. break;
  803. }
  804. }
  805. }
  806. assert(i < SUBSYS_MAX);
  807. pthread_mutex_unlock (&logsys_config_mutex);
  808. return i;
  809. }
  810. int logsys_config_subsys_get (
  811. const char *subsys,
  812. unsigned int *tags,
  813. unsigned int *priority)
  814. {
  815. unsigned int i;
  816. pthread_mutex_lock (&logsys_config_mutex);
  817. for (i = 0; i < SUBSYS_MAX; i++) {
  818. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  819. *tags = logsys_loggers[i].tags;
  820. *priority = logsys_loggers[i].priority;
  821. pthread_mutex_unlock (&logsys_config_mutex);
  822. return i;
  823. }
  824. }
  825. pthread_mutex_unlock (&logsys_config_mutex);
  826. return (-1);
  827. }
  828. int logsys_log_rec_store (char *filename)
  829. {
  830. int fd;
  831. ssize_t written_size;
  832. size_t size_to_write = (flt_data_size + 2) * sizeof (unsigned int);
  833. fd = open (filename, O_CREAT|O_RDWR, 0700);
  834. if (fd == -1) {
  835. return (-1);
  836. }
  837. written_size = write (fd, flt_data, size_to_write);
  838. if (written_size < 0) {
  839. return (-1);
  840. } else if ((size_t)written_size != size_to_write) {
  841. return (-1);
  842. }
  843. return (0);
  844. }
  845. static void logsys_atexit (void)
  846. {
  847. if (wthread_active) {
  848. wthread_should_exit = 1;
  849. wthread_signal ();
  850. pthread_join (logsys_thread_id, NULL);
  851. }
  852. }
  853. void logsys_atsegv (void)
  854. {
  855. if (wthread_active) {
  856. wthread_should_exit = 1;
  857. wthread_signal ();
  858. pthread_join (logsys_thread_id, NULL);
  859. }
  860. }
  861. int logsys_init (
  862. char *name,
  863. int mode,
  864. int facility,
  865. int priority,
  866. char *file,
  867. char *format,
  868. int rec_size)
  869. {
  870. char *errstr;
  871. _logsys_nosubsys_set ();
  872. _logsys_subsys_create (name, priority);
  873. strncpy (logsys_loggers[0].subsys, name,
  874. sizeof (logsys_loggers[0].subsys));
  875. logsys_config_mode_set (mode);
  876. logsys_config_facility_set (name, facility);
  877. logsys_config_file_set (&errstr, file);
  878. logsys_format_set (format);
  879. _logsys_rec_init (rec_size);
  880. _logsys_wthread_create ();
  881. return (0);
  882. }
  883. int logsys_conf (
  884. char *name,
  885. int mode,
  886. int facility,
  887. int priority,
  888. char *file)
  889. {
  890. char *errstr;
  891. _logsys_rec_init (100000);
  892. strncpy (logsys_loggers[0].subsys, name,
  893. sizeof (logsys_loggers[0].subsys));
  894. logsys_config_mode_set (mode);
  895. logsys_config_facility_set (name, facility);
  896. logsys_config_file_set (&errstr, file);
  897. return (0);
  898. }
  899. void logsys_exit (void)
  900. {
  901. }