logsys.c 25 KB

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