logsys.c 25 KB

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