logsys.c 23 KB

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