logsys.c 24 KB

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