logsys.c 25 KB

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