4
0

logsys.c 24 KB

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