logsys.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  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. * Author: Fabio M. Di Nitto (fdinitto@redhat.com)
  8. *
  9. * All rights reserved.
  10. *
  11. * This software licensed under BSD license, the text of which follows:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * - Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  22. * contributors may be used to endorse or promote products derived from this
  23. * software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  35. * THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <config.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. #define YIELD_AFTER_LOG_OPS 10
  62. /*
  63. * similar to syslog facilities/priorities tables,
  64. * make a tag table for internal use
  65. */
  66. #ifdef SYSLOG_NAMES
  67. struct syslog_names {
  68. const char *c_name;
  69. int c_val;
  70. };
  71. struct syslog_names tagnames[] =
  72. {
  73. { "log", LOGSYS_TAG_LOG },
  74. { "enter", LOGSYS_TAG_ENTER },
  75. { "leave", LOGSYS_TAG_LEAVE },
  76. { "trace1", LOGSYS_TAG_TRACE1 },
  77. { "trace2", LOGSYS_TAG_TRACE2 },
  78. { "trace3", LOGSYS_TAG_TRACE3 },
  79. { "trace4", LOGSYS_TAG_TRACE4 },
  80. { "trace5", LOGSYS_TAG_TRACE5 },
  81. { "trace6", LOGSYS_TAG_TRACE6 },
  82. { "trace7", LOGSYS_TAG_TRACE7 },
  83. { "trace8", LOGSYS_TAG_TRACE8 },
  84. { NULL, -1 }
  85. };
  86. #endif
  87. /*
  88. * These are not static so they can be read from the core file
  89. */
  90. int *flt_data;
  91. int flt_data_size;
  92. #define COMBINE_BUFFER_SIZE 2048
  93. /* values for logsys_logger init_status */
  94. #define LOGSYS_LOGGER_INIT_DONE 0
  95. #define LOGSYS_LOGGER_NEEDS_INIT 1
  96. static int logsys_system_needs_init = LOGSYS_LOGGER_NEEDS_INIT;
  97. static int logsys_after_log_ops_yield = 10;
  98. /*
  99. * need unlogical order to preserve 64bit alignment
  100. */
  101. struct logsys_logger {
  102. char subsys[LOGSYS_MAX_SUBSYS_NAMELEN]; /* subsystem name */
  103. char *logfile; /* log to file */
  104. FILE *logfile_fp; /* track file descriptor */
  105. unsigned int mode; /* subsystem mode */
  106. unsigned int debug; /* debug on|off */
  107. unsigned int tags; /* trace tags */
  108. int syslog_facility; /* facility */
  109. int syslog_priority; /* priority */
  110. int logfile_priority; /* priority to file */
  111. int init_status; /* internal field to handle init queues
  112. for subsystems */
  113. };
  114. /*
  115. * operating global variables
  116. */
  117. static struct logsys_logger logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT + 1];
  118. static int wthread_active = 0;
  119. static int wthread_should_exit = 0;
  120. static pthread_mutex_t logsys_config_mutex = PTHREAD_MUTEX_INITIALIZER;
  121. static unsigned int records_written = 1;
  122. static pthread_t logsys_thread_id;
  123. static pthread_cond_t logsys_cond;
  124. static pthread_mutex_t logsys_cond_mutex;
  125. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  126. static pthread_spinlock_t logsys_idx_spinlock;
  127. #else
  128. static pthread_mutex_t logsys_idx_mutex = PTHREAD_MUTEX_INITIALIZER;
  129. #endif
  130. static unsigned int log_rec_idx;
  131. static int logsys_buffer_full = 0;
  132. static char *format_buffer=NULL;
  133. static int log_requests_pending = 0;
  134. static int log_requests_lost = 0;
  135. void *logsys_rec_end;
  136. #define FDHEAD_INDEX (flt_data_size)
  137. #define FDTAIL_INDEX (flt_data_size + 1)
  138. /* forward declarations */
  139. static void logsys_close_logfile(int subsysid);
  140. /*
  141. * Helpers for _logsys_log_rec functionality
  142. */
  143. static inline void my_memcpy_32bit (int *dest, const int *src, unsigned int words)
  144. {
  145. unsigned int word_idx;
  146. for (word_idx = 0; word_idx < words; word_idx++) {
  147. dest[word_idx] = src[word_idx];
  148. }
  149. }
  150. static inline void my_memcpy_8bit (char *dest, const char *src,
  151. unsigned int bytes)
  152. {
  153. unsigned int byte_idx;
  154. for (byte_idx = 0; byte_idx < bytes; byte_idx++) {
  155. dest[byte_idx] = src[byte_idx];
  156. }
  157. }
  158. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  159. static void logsys_lock (void)
  160. {
  161. pthread_spin_lock (&logsys_idx_spinlock);
  162. }
  163. static void logsys_unlock (void)
  164. {
  165. pthread_spin_unlock (&logsys_idx_spinlock);
  166. }
  167. #else
  168. static void logsys_lock (void)
  169. {
  170. pthread_mutex_lock (&logsys_idx_mutex);
  171. }
  172. static void logsys_unlock (void)
  173. {
  174. pthread_mutex_unlock (&logsys_idx_mutex);
  175. }
  176. #endif
  177. /*
  178. * Before any write operation, a reclaim on the buffer area must be executed
  179. */
  180. static inline void records_reclaim (unsigned int idx, unsigned int words)
  181. {
  182. unsigned int should_reclaim;
  183. should_reclaim = 0;
  184. if ((idx + words) >= flt_data_size) {
  185. logsys_buffer_full = 1;
  186. }
  187. if (logsys_buffer_full == 0) {
  188. return;
  189. }
  190. logsys_lock();
  191. if (flt_data[FDTAIL_INDEX] > flt_data[FDHEAD_INDEX]) {
  192. if (idx + words >= flt_data[FDTAIL_INDEX]) {
  193. should_reclaim = 1;
  194. }
  195. } else {
  196. if ((idx + words) >= (flt_data[FDTAIL_INDEX] + flt_data_size)) {
  197. should_reclaim = 1;
  198. }
  199. }
  200. if (should_reclaim) {
  201. int words_needed = 0;
  202. words_needed = words + 1;
  203. do {
  204. unsigned int old_tail;
  205. words_needed -= flt_data[flt_data[FDTAIL_INDEX]];
  206. old_tail = flt_data[FDTAIL_INDEX];
  207. flt_data[FDTAIL_INDEX] =
  208. (flt_data[FDTAIL_INDEX] +
  209. flt_data[flt_data[FDTAIL_INDEX]]) % (flt_data_size);
  210. if (log_rec_idx == old_tail) {
  211. log_requests_lost += 1;
  212. log_rec_idx = flt_data[FDTAIL_INDEX];
  213. }
  214. } while (words_needed > 0);
  215. }
  216. logsys_unlock();
  217. }
  218. #define idx_word_step(idx) \
  219. do { \
  220. if (idx > (flt_data_size - 1)) { \
  221. idx = 0; \
  222. } \
  223. } while (0);
  224. #define idx_buffer_step(idx) \
  225. do { \
  226. if (idx > (flt_data_size - 1)) { \
  227. idx = ((idx) % (flt_data_size)); \
  228. } \
  229. } while (0);
  230. /*
  231. * Internal threaded logging implementation
  232. */
  233. static inline int strcpy_cutoff (char *dest, const char *src, int cutoff)
  234. {
  235. unsigned int len;
  236. if (cutoff <= 0) {
  237. strcpy (dest, src);
  238. return (strlen (dest));
  239. } else {
  240. strncpy (dest, src, cutoff);
  241. dest[cutoff] = '\0';
  242. len = strlen (dest);
  243. if (len != cutoff) {
  244. memset (&dest[len], ' ', cutoff - len);
  245. }
  246. }
  247. return (cutoff);
  248. }
  249. /*
  250. * %s SUBSYSTEM
  251. * %n FUNCTION NAME
  252. * %f FILENAME
  253. * %l FILELINE
  254. * %p PRIORITY
  255. * %t TIMESTAMP
  256. * %b BUFFER
  257. *
  258. * any number between % and character specify field length to pad or chop
  259. */
  260. static void log_printf_to_logs (
  261. const char *subsys,
  262. const char *file_name,
  263. const char *function_name,
  264. int file_line,
  265. unsigned int level,
  266. const char *buffer)
  267. {
  268. char output_buffer[COMBINE_BUFFER_SIZE];
  269. char char_time[128];
  270. char line_no[30];
  271. unsigned int format_buffer_idx = 0;
  272. unsigned int output_buffer_idx = 0;
  273. struct timeval tv;
  274. int cutoff;
  275. unsigned int len;
  276. int subsysid;
  277. subsysid = _logsys_config_subsys_get(subsys);
  278. if (subsysid <= - 1) {
  279. return;
  280. }
  281. while (format_buffer[format_buffer_idx]) {
  282. cutoff = -1;
  283. if (format_buffer[format_buffer_idx] == '%') {
  284. format_buffer_idx += 1;
  285. if (isdigit (format_buffer[format_buffer_idx])) {
  286. cutoff = atoi (&format_buffer[format_buffer_idx]);
  287. }
  288. while (isdigit (format_buffer[format_buffer_idx])) {
  289. format_buffer_idx += 1;
  290. }
  291. switch (format_buffer[format_buffer_idx]) {
  292. case 's':
  293. len = strcpy_cutoff (&output_buffer[output_buffer_idx], subsys, cutoff);
  294. output_buffer_idx += len;
  295. break;
  296. case 'n':
  297. len = strcpy_cutoff (&output_buffer[output_buffer_idx], function_name, cutoff);
  298. output_buffer_idx += len;
  299. break;
  300. case 'f':
  301. len = strcpy_cutoff (&output_buffer[output_buffer_idx], file_name, cutoff);
  302. output_buffer_idx += len;
  303. break;
  304. case 'l':
  305. sprintf (line_no, "%d", file_line);
  306. len = strcpy_cutoff (&output_buffer[output_buffer_idx], line_no, cutoff);
  307. output_buffer_idx += len;
  308. break;
  309. case 'p':
  310. break;
  311. case 't':
  312. gettimeofday (&tv, NULL);
  313. (void)strftime (char_time, sizeof (char_time), "%b %e %k:%M:%S", localtime ((time_t *)&tv.tv_sec));
  314. len = strcpy_cutoff (&output_buffer[output_buffer_idx], char_time, cutoff);
  315. output_buffer_idx += len;
  316. break;
  317. case 'b':
  318. len = strcpy_cutoff (&output_buffer[output_buffer_idx], buffer, cutoff);
  319. output_buffer_idx += len;
  320. break;
  321. }
  322. format_buffer_idx += 1;
  323. } else {
  324. output_buffer[output_buffer_idx++] = format_buffer[format_buffer_idx++];
  325. }
  326. }
  327. output_buffer[output_buffer_idx] = '\0';
  328. /*
  329. * Output to syslog
  330. */
  331. if ((logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_SYSLOG) &&
  332. ((level <= logsys_loggers[subsysid].syslog_priority) ||
  333. (logsys_loggers[subsysid].debug != 0))) {
  334. syslog (level | logsys_loggers[subsysid].syslog_facility, "%s", output_buffer);
  335. }
  336. /*
  337. * Terminate string with \n \0
  338. */
  339. output_buffer[output_buffer_idx++] = '\n';
  340. output_buffer[output_buffer_idx] = '\0';
  341. /*
  342. * Output to configured file
  343. */
  344. if (((logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_FILE) &&
  345. (logsys_loggers[subsysid].logfile_fp != NULL)) &&
  346. ((level <= logsys_loggers[subsysid].logfile_priority) ||
  347. (logsys_loggers[subsysid].debug != 0))) {
  348. /*
  349. * Output to a file
  350. */
  351. if ((fwrite (output_buffer, strlen (output_buffer), 1,
  352. logsys_loggers[subsysid].logfile_fp) < 1) ||
  353. (fflush (logsys_loggers[subsysid].logfile_fp) == EOF)) {
  354. char tmpbuffer[1024];
  355. /*
  356. * if we are here, it's bad.. it's really really bad.
  357. * Best thing would be to light a candle in a church
  358. * and pray.
  359. */
  360. snprintf(tmpbuffer, sizeof(tmpbuffer),
  361. "LOGSYS EMERGENCY: %s Unable to write to %s.",
  362. logsys_loggers[subsysid].subsys,
  363. logsys_loggers[subsysid].logfile);
  364. pthread_mutex_lock (&logsys_config_mutex);
  365. logsys_close_logfile(subsysid);
  366. logsys_loggers[subsysid].mode &= ~LOGSYS_MODE_OUTPUT_FILE;
  367. pthread_mutex_unlock (&logsys_config_mutex);
  368. log_printf_to_logs(logsys_loggers[subsysid].subsys,
  369. __FILE__, __FUNCTION__, __LINE__,
  370. LOGSYS_LEVEL_EMERG, tmpbuffer);
  371. }
  372. }
  373. /*
  374. * Output to stderr
  375. */
  376. if ((logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_STDERR) &&
  377. ((level <= logsys_loggers[subsysid].logfile_priority) ||
  378. (logsys_loggers[subsysid].debug != 0))) {
  379. if (write (STDERR_FILENO, output_buffer, strlen (output_buffer)) < 0) {
  380. char tmpbuffer[1024];
  381. /*
  382. * if we are here, it's bad.. it's really really bad.
  383. * Best thing would be to light 20 candles for each saint
  384. * in the calendar and pray a lot...
  385. */
  386. pthread_mutex_lock (&logsys_config_mutex);
  387. logsys_loggers[subsysid].mode &= ~LOGSYS_MODE_OUTPUT_STDERR;
  388. pthread_mutex_unlock (&logsys_config_mutex);
  389. snprintf(tmpbuffer, sizeof(tmpbuffer),
  390. "LOGSYS EMERGENCY: %s Unable to write to STDERR.",
  391. logsys_loggers[subsysid].subsys);
  392. log_printf_to_logs(logsys_loggers[subsysid].subsys,
  393. __FILE__, __FUNCTION__, __LINE__,
  394. LOGSYS_LEVEL_EMERG, tmpbuffer);
  395. }
  396. }
  397. }
  398. static void record_print (const char *buf)
  399. {
  400. const int *buf_uint32t = (const int *)buf;
  401. unsigned int rec_size = buf_uint32t[0];
  402. unsigned int rec_ident = buf_uint32t[1];
  403. unsigned int file_line = buf_uint32t[2];
  404. unsigned int level = rec_ident >> 28;
  405. unsigned int i;
  406. unsigned int words_processed;
  407. unsigned int arg_size_idx;
  408. const void *arguments[64];
  409. unsigned int arg_count;
  410. arg_size_idx = 4;
  411. words_processed = 4;
  412. arg_count = 0;
  413. for (i = 0; words_processed < rec_size; i++) {
  414. arguments[arg_count++] = &buf_uint32t[arg_size_idx + 1];
  415. arg_size_idx += buf_uint32t[arg_size_idx] + 1;
  416. words_processed += buf_uint32t[arg_size_idx] + 1;
  417. }
  418. /*
  419. * (char *)arguments[0] -> subsystem
  420. * (char *)arguments[1] -> file_name
  421. * (char *)arguments[2] -> function_name
  422. * (char *)arguments[3] -> message
  423. */
  424. log_printf_to_logs (
  425. (char *)arguments[0],
  426. (char *)arguments[1],
  427. (char *)arguments[2],
  428. file_line,
  429. (level-1),
  430. (char *)arguments[3]);
  431. }
  432. static int record_read (char *buf, int rec_idx, int *log_msg) {
  433. unsigned int rec_size;
  434. unsigned int rec_ident;
  435. int firstcopy, secondcopy;
  436. rec_size = flt_data[rec_idx];
  437. rec_ident = flt_data[(rec_idx + 1) % flt_data_size];
  438. /*
  439. * Not a log record
  440. */
  441. if ((rec_ident & LOGSYS_TAG_LOG) == 0) {
  442. *log_msg = 0;
  443. return ((rec_idx + rec_size) % flt_data_size);
  444. }
  445. /*
  446. * A log record
  447. */
  448. *log_msg = 1;
  449. firstcopy = rec_size;
  450. secondcopy = 0;
  451. if (firstcopy + rec_idx > flt_data_size) {
  452. firstcopy = flt_data_size - rec_idx;
  453. secondcopy -= firstcopy - rec_size;
  454. }
  455. memcpy (&buf[0], &flt_data[rec_idx], firstcopy << 2);
  456. if (secondcopy) {
  457. memcpy (&buf[(firstcopy << 2)], &flt_data[0], secondcopy << 2);
  458. }
  459. return ((rec_idx + rec_size) % flt_data_size);
  460. }
  461. static inline void wthread_signal (void)
  462. {
  463. if (wthread_active == 0) {
  464. return;
  465. }
  466. pthread_mutex_lock (&logsys_cond_mutex);
  467. pthread_cond_signal (&logsys_cond);
  468. pthread_mutex_unlock (&logsys_cond_mutex);
  469. }
  470. static inline void wthread_wait (void)
  471. {
  472. pthread_mutex_lock (&logsys_cond_mutex);
  473. pthread_cond_wait (&logsys_cond, &logsys_cond_mutex);
  474. pthread_mutex_unlock (&logsys_cond_mutex);
  475. }
  476. static inline void wthread_wait_locked (void)
  477. {
  478. pthread_cond_wait (&logsys_cond, &logsys_cond_mutex);
  479. pthread_mutex_unlock (&logsys_cond_mutex);
  480. }
  481. static void *logsys_worker_thread (void *data) __attribute__((__noreturn__));
  482. static void *logsys_worker_thread (void *data)
  483. {
  484. int log_msg;
  485. char buf[COMBINE_BUFFER_SIZE];
  486. /*
  487. * Signal wthread_create that the initialization process may continue
  488. */
  489. wthread_signal ();
  490. logsys_lock();
  491. log_rec_idx = flt_data[FDTAIL_INDEX];
  492. logsys_unlock();
  493. for (;;) {
  494. wthread_wait ();
  495. /*
  496. * Read and copy the logging record index position
  497. * It may have been updated by records_reclaim if
  498. * messages were lost or or log_rec on the first new
  499. * logging record available
  500. */
  501. /*
  502. * Process any pending log messages here
  503. */
  504. for (;;) {
  505. int yield_counter = 1;
  506. logsys_lock();
  507. if (log_requests_lost > 0) {
  508. printf ("lost %d log requests\n", log_requests_lost);
  509. log_requests_pending -= log_requests_lost;
  510. log_requests_lost = 0;
  511. }
  512. if (log_requests_pending == 0) {
  513. logsys_unlock();
  514. break;
  515. }
  516. log_rec_idx = record_read (buf, log_rec_idx, &log_msg);
  517. if (log_msg) {
  518. log_requests_pending -= 1;
  519. }
  520. logsys_unlock();
  521. /*
  522. * print the stored buffer
  523. */
  524. if (log_msg) {
  525. record_print (buf);
  526. if (yield_counter++ > logsys_after_log_ops_yield) {
  527. yield_counter = 0;
  528. sched_yield ();
  529. }
  530. }
  531. }
  532. if (wthread_should_exit) {
  533. pthread_exit (NULL);
  534. }
  535. }
  536. }
  537. static void wthread_create (void)
  538. {
  539. int res;
  540. if (wthread_active) {
  541. return;
  542. }
  543. wthread_active = 1;
  544. pthread_mutex_init (&logsys_cond_mutex, NULL);
  545. pthread_cond_init (&logsys_cond, NULL);
  546. pthread_mutex_lock (&logsys_cond_mutex);
  547. res = pthread_create (&logsys_thread_id, NULL,
  548. logsys_worker_thread, NULL);
  549. /*
  550. * Wait for thread to be started
  551. */
  552. wthread_wait_locked ();
  553. }
  554. static int _logsys_config_subsys_get_unlocked (const char *subsys)
  555. {
  556. unsigned int i;
  557. if (!subsys) {
  558. return LOGSYS_MAX_SUBSYS_COUNT;
  559. }
  560. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  561. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  562. pthread_mutex_unlock (&logsys_config_mutex);
  563. return i;
  564. }
  565. }
  566. return (-1);
  567. }
  568. static void syslog_facility_reconf (void)
  569. {
  570. closelog();
  571. openlog(logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].subsys,
  572. LOG_CONS|LOG_PID,
  573. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].syslog_facility);
  574. }
  575. /*
  576. * this is always invoked within the mutex, so it's safe to parse the
  577. * whole thing as we need.
  578. */
  579. static void logsys_close_logfile (
  580. int subsysid)
  581. {
  582. int i;
  583. if ((logsys_loggers[subsysid].logfile_fp == NULL) &&
  584. (logsys_loggers[subsysid].logfile == NULL)) {
  585. return;
  586. }
  587. /*
  588. * if there is another subsystem or system using the same fp,
  589. * then we clean our own structs, but we can't close the file
  590. * as it is in use by somebody else.
  591. * Only the last users will be allowed to perform the fclose.
  592. */
  593. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  594. if ((logsys_loggers[i].logfile_fp == logsys_loggers[subsysid].logfile_fp) &&
  595. (i != subsysid)) {
  596. logsys_loggers[subsysid].logfile = NULL;
  597. logsys_loggers[subsysid].logfile_fp = NULL;
  598. return;
  599. }
  600. }
  601. /*
  602. * if we are here, we are the last users of that fp, so we can safely
  603. * close it.
  604. */
  605. fclose (logsys_loggers[subsysid].logfile_fp);
  606. logsys_loggers[subsysid].logfile_fp = NULL;
  607. free (logsys_loggers[subsysid].logfile);
  608. logsys_loggers[subsysid].logfile = NULL;
  609. }
  610. /*
  611. * we need a version that can work when somebody else is already
  612. * holding a config mutex lock or we will never get out of here
  613. */
  614. static int logsys_config_file_set_unlocked (
  615. int subsysid,
  616. const char **error_string,
  617. const char *file)
  618. {
  619. static char error_string_response[512];
  620. int i;
  621. logsys_close_logfile(subsysid);
  622. if ((file == NULL) ||
  623. (strcmp(logsys_loggers[subsysid].subsys, "") == 0)) {
  624. return (0);
  625. }
  626. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  627. if ((logsys_loggers[i].logfile != NULL) &&
  628. (strcmp (logsys_loggers[i].logfile, file) == 0) &&
  629. (i != subsysid)) {
  630. logsys_loggers[subsysid].logfile =
  631. logsys_loggers[i].logfile;
  632. logsys_loggers[subsysid].logfile_fp =
  633. logsys_loggers[i].logfile_fp;
  634. return (0);
  635. }
  636. }
  637. logsys_loggers[subsysid].logfile = strdup(file);
  638. if (logsys_loggers[subsysid].logfile == NULL) {
  639. snprintf (error_string_response,
  640. sizeof(error_string_response),
  641. "Unable to allocate memory for logfile '%s'\n",
  642. file);
  643. *error_string = error_string_response;
  644. return (-1);
  645. }
  646. logsys_loggers[subsysid].logfile_fp = fopen (file, "a+");
  647. if (logsys_loggers[subsysid].logfile_fp == NULL) {
  648. free(logsys_loggers[subsysid].logfile);
  649. logsys_loggers[subsysid].logfile = NULL;
  650. snprintf (error_string_response,
  651. sizeof(error_string_response),
  652. "Can't open logfile '%s' for reason (%s).\n",
  653. file, strerror (errno));
  654. *error_string = error_string_response;
  655. return (-1);
  656. }
  657. return (0);
  658. }
  659. static void logsys_subsys_init (
  660. const char *subsys,
  661. int subsysid)
  662. {
  663. if (logsys_system_needs_init == LOGSYS_LOGGER_NEEDS_INIT) {
  664. logsys_loggers[subsysid].init_status =
  665. LOGSYS_LOGGER_NEEDS_INIT;
  666. } else {
  667. memcpy(&logsys_loggers[subsysid],
  668. &logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT],
  669. sizeof(logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT]));
  670. logsys_loggers[subsysid].tags = LOGSYS_TAG_LOG;
  671. logsys_loggers[subsysid].init_status =
  672. LOGSYS_LOGGER_INIT_DONE;
  673. }
  674. strncpy (logsys_loggers[subsysid].subsys, subsys,
  675. LOGSYS_MAX_SUBSYS_NAMELEN);
  676. }
  677. /*
  678. * Internal API - exported
  679. */
  680. int _logsys_system_setup(
  681. const char *mainsystem,
  682. unsigned int mode,
  683. unsigned int debug,
  684. const char *logfile,
  685. int logfile_priority,
  686. int syslog_facility,
  687. int syslog_priority,
  688. unsigned int tags)
  689. {
  690. int i;
  691. const char *errstr;
  692. char tempsubsys[LOGSYS_MAX_SUBSYS_NAMELEN];
  693. i = LOGSYS_MAX_SUBSYS_COUNT;
  694. pthread_mutex_lock (&logsys_config_mutex);
  695. snprintf(logsys_loggers[i].subsys,
  696. LOGSYS_MAX_SUBSYS_NAMELEN,
  697. "%s", mainsystem);
  698. logsys_loggers[i].mode = mode;
  699. logsys_loggers[i].debug = debug;
  700. if ((logfile) && strlen(logfile) > 0) {
  701. if (logsys_config_file_set_unlocked (i, &errstr, logfile) < 0) {
  702. pthread_mutex_unlock (&logsys_config_mutex);
  703. return (-1);
  704. }
  705. }
  706. logsys_loggers[i].logfile_priority = logfile_priority;
  707. logsys_loggers[i].syslog_facility = syslog_facility;
  708. logsys_loggers[i].syslog_priority = syslog_priority;
  709. syslog_facility_reconf();
  710. logsys_loggers[i].tags = tags;
  711. logsys_loggers[i].init_status = LOGSYS_LOGGER_INIT_DONE;
  712. logsys_system_needs_init = LOGSYS_LOGGER_INIT_DONE;
  713. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  714. if ((strcmp (logsys_loggers[i].subsys, "") != 0) &&
  715. (logsys_loggers[i].init_status ==
  716. LOGSYS_LOGGER_NEEDS_INIT)) {
  717. strncpy (tempsubsys, logsys_loggers[i].subsys,
  718. LOGSYS_MAX_SUBSYS_NAMELEN);
  719. logsys_subsys_init(tempsubsys, i);
  720. }
  721. }
  722. pthread_mutex_unlock (&logsys_config_mutex);
  723. return (0);
  724. }
  725. unsigned int _logsys_subsys_create (const char *subsys)
  726. {
  727. int i;
  728. if (subsys == NULL) {
  729. return -1;
  730. }
  731. pthread_mutex_lock (&logsys_config_mutex);
  732. i = _logsys_config_subsys_get_unlocked (subsys);
  733. if ((i > -1) && (i < LOGSYS_MAX_SUBSYS_COUNT)) {
  734. pthread_mutex_unlock (&logsys_config_mutex);
  735. return i;
  736. }
  737. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  738. if (strcmp (logsys_loggers[i].subsys, "") == 0) {
  739. logsys_subsys_init(subsys, i);
  740. break;
  741. }
  742. }
  743. if (i >= LOGSYS_MAX_SUBSYS_COUNT) {
  744. i = -1;
  745. }
  746. pthread_mutex_unlock (&logsys_config_mutex);
  747. return i;
  748. }
  749. int _logsys_wthread_create (void)
  750. {
  751. if (((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_FORK) == 0) &&
  752. ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) != 0)) {
  753. wthread_create();
  754. atexit (logsys_atexit);
  755. }
  756. return (0);
  757. }
  758. int _logsys_rec_init (unsigned int size)
  759. {
  760. /*
  761. * First record starts at zero
  762. * Last record ends at zero
  763. */
  764. flt_data = malloc ((size + 2) * sizeof (unsigned int));
  765. if (flt_data == NULL) {
  766. return (-1);
  767. }
  768. flt_data_size = size;
  769. flt_data[FDHEAD_INDEX] = 0;
  770. flt_data[FDTAIL_INDEX] = 0;
  771. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  772. pthread_spin_init (&logsys_idx_spinlock, 0);
  773. #endif
  774. return (0);
  775. }
  776. /*
  777. * u32 RECORD SIZE
  778. * u32 record ident
  779. * u32 arg count
  780. * u32 file line
  781. * u32 subsys length
  782. * buffer null terminated subsys
  783. * u32 filename length
  784. * buffer null terminated filename
  785. * u32 filename length
  786. * buffer null terminated function
  787. * u32 arg1 length
  788. * buffer arg1
  789. * ... repeats length & arg
  790. */
  791. void _logsys_log_rec (
  792. int subsysid,
  793. const char *function_name,
  794. const char *file_name,
  795. int file_line,
  796. unsigned int rec_ident,
  797. ...)
  798. {
  799. va_list ap;
  800. const void *buf_args[64];
  801. unsigned int buf_len[64];
  802. unsigned int i;
  803. unsigned int idx;
  804. unsigned int arguments = 0;
  805. unsigned int record_reclaim_size;
  806. unsigned int index_start;
  807. int words_written;
  808. record_reclaim_size = 0;
  809. /*
  810. * Decode VA Args
  811. */
  812. va_start (ap, rec_ident);
  813. arguments = 3;
  814. for (;;) {
  815. buf_args[arguments] = va_arg (ap, void *);
  816. if (buf_args[arguments] == LOGSYS_REC_END) {
  817. break;
  818. }
  819. buf_len[arguments] = va_arg (ap, int);
  820. record_reclaim_size += ((buf_len[arguments] + 3) >> 2) + 1;
  821. arguments++;
  822. if (arguments >= 64) {
  823. break;
  824. }
  825. }
  826. va_end (ap);
  827. /*
  828. * Encode logsys subsystem identity, filename, and function
  829. */
  830. buf_args[0] = logsys_loggers[subsysid].subsys;
  831. buf_len[0] = strlen (logsys_loggers[subsysid].subsys) + 1;
  832. buf_args[1] = file_name;
  833. buf_len[1] = strlen (file_name) + 1;
  834. buf_args[2] = function_name;
  835. buf_len[2] = strlen (function_name) + 1;
  836. for (i = 0; i < 3; i++) {
  837. record_reclaim_size += ((buf_len[i] + 3) >> 2) + 1;
  838. }
  839. idx = flt_data[FDHEAD_INDEX];
  840. index_start = idx;
  841. /*
  842. * Reclaim data needed for record including 4 words for the header
  843. */
  844. records_reclaim (idx, record_reclaim_size + 4);
  845. /*
  846. * Write record size of zero and rest of header information
  847. */
  848. flt_data[idx++] = 0;
  849. idx_word_step(idx);
  850. flt_data[idx++] = rec_ident;
  851. idx_word_step(idx);
  852. flt_data[idx++] = file_line;
  853. idx_word_step(idx);
  854. flt_data[idx++] = records_written;
  855. idx_word_step(idx);
  856. /*
  857. * Encode all of the arguments into the log message
  858. */
  859. for (i = 0; i < arguments; i++) {
  860. unsigned int bytes;
  861. unsigned int full_words;
  862. unsigned int total_words;
  863. bytes = buf_len[i];
  864. full_words = bytes >> 2;
  865. total_words = (bytes + 3) >> 2;
  866. flt_data[idx++] = total_words;
  867. idx_word_step(idx);
  868. /*
  869. * determine if this is a wrapped write or normal write
  870. */
  871. if (idx + total_words < flt_data_size) {
  872. /*
  873. * dont need to wrap buffer
  874. */
  875. my_memcpy_32bit (&flt_data[idx], buf_args[i], full_words);
  876. if (bytes % 4) {
  877. my_memcpy_8bit ((char *)&flt_data[idx + full_words],
  878. ((const char *)buf_args[i]) + (full_words << 2), bytes % 4);
  879. }
  880. } else {
  881. /*
  882. * need to wrap buffer
  883. */
  884. unsigned int first;
  885. unsigned int second;
  886. first = flt_data_size - idx;
  887. if (first > full_words) {
  888. first = full_words;
  889. }
  890. second = full_words - first;
  891. my_memcpy_32bit (&flt_data[idx],
  892. (const int *)buf_args[i], first);
  893. my_memcpy_32bit (&flt_data[0],
  894. (const int *)(((const unsigned char *)buf_args[i]) + (first << 2)),
  895. second);
  896. if (bytes % 4) {
  897. my_memcpy_8bit ((char *)&flt_data[0 + second],
  898. ((const char *)buf_args[i]) + (full_words << 2), bytes % 4);
  899. }
  900. }
  901. idx += total_words;
  902. idx_buffer_step (idx);
  903. }
  904. words_written = idx - index_start;
  905. if (words_written < 0) {
  906. words_written += flt_data_size;
  907. }
  908. /*
  909. * Commit the write of the record size now that the full record
  910. * is in the memory buffer
  911. */
  912. flt_data[index_start] = words_written;
  913. /*
  914. * If the index of the current head equals the current log_rec_idx,
  915. * and this is not a log_printf operation, set the log_rec_idx to
  916. * the new head position and commit the new head.
  917. */
  918. logsys_lock();
  919. if (rec_ident & LOGSYS_TAG_LOG) {
  920. log_requests_pending += 1;
  921. }
  922. if (log_requests_pending == 0) {
  923. log_rec_idx = idx;
  924. }
  925. flt_data[FDHEAD_INDEX] = idx;
  926. logsys_unlock();
  927. records_written++;
  928. }
  929. void _logsys_log_vprintf (
  930. int subsysid,
  931. const char *function_name,
  932. const char *file_name,
  933. int file_line,
  934. unsigned int level,
  935. const char *format,
  936. va_list ap)
  937. {
  938. char logsys_print_buffer[COMBINE_BUFFER_SIZE];
  939. unsigned int len;
  940. if (subsysid <= -1) {
  941. subsysid = LOGSYS_MAX_SUBSYS_COUNT;
  942. }
  943. if ((level > logsys_loggers[subsysid].syslog_priority) &&
  944. (level > logsys_loggers[subsysid].logfile_priority)) {
  945. return;
  946. }
  947. len = vsprintf (logsys_print_buffer, format, ap);
  948. if (logsys_print_buffer[len - 1] == '\n') {
  949. logsys_print_buffer[len - 1] = '\0';
  950. len -= 1;
  951. }
  952. /*
  953. * Create a log record
  954. */
  955. _logsys_log_rec (subsysid,
  956. function_name,
  957. file_name,
  958. file_line,
  959. (level+1) << 28,
  960. logsys_print_buffer, len + 1,
  961. LOGSYS_REC_END);
  962. if ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) == 0) {
  963. /*
  964. * Output (and block) if the log mode is not threaded otherwise
  965. * expect the worker thread to output the log data once signaled
  966. */
  967. log_printf_to_logs (logsys_loggers[subsysid].subsys,
  968. file_name, function_name, file_line, level,
  969. logsys_print_buffer);
  970. } else {
  971. /*
  972. * Signal worker thread to display logging output
  973. */
  974. wthread_signal ();
  975. }
  976. }
  977. void _logsys_log_printf (
  978. int subsysid,
  979. const char *function_name,
  980. const char *file_name,
  981. int file_line,
  982. unsigned int level,
  983. const char *format,
  984. ...)
  985. {
  986. char logsys_print_buffer[COMBINE_BUFFER_SIZE];
  987. unsigned int len;
  988. va_list ap;
  989. if (subsysid <= -1) {
  990. subsysid = LOGSYS_MAX_SUBSYS_COUNT;
  991. }
  992. if ((level > logsys_loggers[subsysid].syslog_priority) &&
  993. (level > logsys_loggers[subsysid].logfile_priority)) {
  994. return;
  995. }
  996. va_start (ap, format);
  997. len = vsprintf (logsys_print_buffer, format, ap);
  998. va_end (ap);
  999. if (logsys_print_buffer[len - 1] == '\n') {
  1000. logsys_print_buffer[len - 1] = '\0';
  1001. len -= 1;
  1002. }
  1003. /*
  1004. * Create a log record
  1005. */
  1006. _logsys_log_rec (subsysid,
  1007. function_name,
  1008. file_name,
  1009. file_line,
  1010. (level+1) << 28,
  1011. logsys_print_buffer, len + 1,
  1012. LOGSYS_REC_END);
  1013. if ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) == 0) {
  1014. /*
  1015. * Output (and block) if the log mode is not threaded otherwise
  1016. * expect the worker thread to output the log data once signaled
  1017. */
  1018. log_printf_to_logs (logsys_loggers[subsysid].subsys,
  1019. file_name, function_name, file_line, level,
  1020. logsys_print_buffer);
  1021. } else {
  1022. /*
  1023. * Signal worker thread to display logging output
  1024. */
  1025. wthread_signal ();
  1026. }
  1027. }
  1028. int _logsys_config_subsys_get (const char *subsys)
  1029. {
  1030. unsigned int i;
  1031. pthread_mutex_lock (&logsys_config_mutex);
  1032. i = _logsys_config_subsys_get_unlocked (subsys);
  1033. pthread_mutex_unlock (&logsys_config_mutex);
  1034. return i;
  1035. }
  1036. /*
  1037. * External Configuration and Initialization API
  1038. */
  1039. void logsys_fork_completed (void)
  1040. {
  1041. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode &= ~LOGSYS_MODE_FORK;
  1042. _logsys_wthread_create ();
  1043. }
  1044. unsigned int logsys_config_mode_set (const char *subsys, unsigned int mode)
  1045. {
  1046. int i;
  1047. pthread_mutex_lock (&logsys_config_mutex);
  1048. if (subsys != NULL) {
  1049. i = _logsys_config_subsys_get_unlocked (subsys);
  1050. if (i >= 0) {
  1051. logsys_loggers[i].mode = mode;
  1052. i = 0;
  1053. }
  1054. } else {
  1055. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1056. logsys_loggers[i].mode = mode;
  1057. }
  1058. i = 0;
  1059. }
  1060. pthread_mutex_unlock (&logsys_config_mutex);
  1061. return i;
  1062. }
  1063. unsigned int logsys_config_mode_get (const char *subsys)
  1064. {
  1065. int i;
  1066. i = _logsys_config_subsys_get (subsys);
  1067. if (i < 0) {
  1068. return i;
  1069. }
  1070. return logsys_loggers[i].mode;
  1071. }
  1072. unsigned int logsys_config_tags_set (const char *subsys, unsigned int tags)
  1073. {
  1074. int i;
  1075. pthread_mutex_lock (&logsys_config_mutex);
  1076. if (subsys != NULL) {
  1077. i = _logsys_config_subsys_get_unlocked (subsys);
  1078. if (i >= 0) {
  1079. logsys_loggers[i].tags = tags;
  1080. i = 0;
  1081. }
  1082. } else {
  1083. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1084. logsys_loggers[i].tags = tags;
  1085. }
  1086. i = 0;
  1087. }
  1088. pthread_mutex_unlock (&logsys_config_mutex);
  1089. return i;
  1090. }
  1091. unsigned int logsys_config_tags_get (const char *subsys)
  1092. {
  1093. int i;
  1094. i = _logsys_config_subsys_get (subsys);
  1095. if (i < 0) {
  1096. return i;
  1097. }
  1098. return logsys_loggers[i].tags;
  1099. }
  1100. int logsys_config_file_set (
  1101. const char *subsys,
  1102. const char **error_string,
  1103. const char *file)
  1104. {
  1105. int i;
  1106. int res;
  1107. pthread_mutex_lock (&logsys_config_mutex);
  1108. if (subsys != NULL) {
  1109. i = _logsys_config_subsys_get_unlocked (subsys);
  1110. if (i < 0) {
  1111. res = i;
  1112. } else {
  1113. res = logsys_config_file_set_unlocked(i, error_string, file);
  1114. }
  1115. } else {
  1116. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1117. res = logsys_config_file_set_unlocked(i, error_string, file);
  1118. if (res < 0) {
  1119. break;
  1120. }
  1121. }
  1122. }
  1123. pthread_mutex_unlock (&logsys_config_mutex);
  1124. return res;
  1125. }
  1126. int logsys_format_set (const char *format)
  1127. {
  1128. int ret = 0;
  1129. pthread_mutex_lock (&logsys_config_mutex);
  1130. if (format_buffer) {
  1131. free(format_buffer);
  1132. format_buffer = NULL;
  1133. }
  1134. format_buffer = strdup(format ? format : "[%6s] %b");
  1135. if (format_buffer == NULL) {
  1136. ret = -1;
  1137. }
  1138. pthread_mutex_unlock (&logsys_config_mutex);
  1139. return ret;
  1140. }
  1141. char *logsys_format_get (void)
  1142. {
  1143. return format_buffer;
  1144. }
  1145. unsigned int logsys_config_syslog_facility_set (
  1146. const char *subsys,
  1147. unsigned int facility)
  1148. {
  1149. int i;
  1150. pthread_mutex_lock (&logsys_config_mutex);
  1151. if (subsys != NULL) {
  1152. i = _logsys_config_subsys_get_unlocked (subsys);
  1153. if (i >= 0) {
  1154. logsys_loggers[i].syslog_facility = facility;
  1155. if (i == LOGSYS_MAX_SUBSYS_COUNT) {
  1156. syslog_facility_reconf();
  1157. }
  1158. i = 0;
  1159. }
  1160. } else {
  1161. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1162. logsys_loggers[i].syslog_facility = facility;
  1163. }
  1164. syslog_facility_reconf();
  1165. i = 0;
  1166. }
  1167. pthread_mutex_unlock (&logsys_config_mutex);
  1168. return i;
  1169. }
  1170. unsigned int logsys_config_syslog_priority_set (
  1171. const char *subsys,
  1172. unsigned int priority)
  1173. {
  1174. int i;
  1175. pthread_mutex_lock (&logsys_config_mutex);
  1176. if (subsys != NULL) {
  1177. i = _logsys_config_subsys_get_unlocked (subsys);
  1178. if (i >= 0) {
  1179. logsys_loggers[i].syslog_priority = priority;
  1180. i = 0;
  1181. }
  1182. } else {
  1183. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1184. logsys_loggers[i].syslog_priority = priority;
  1185. }
  1186. i = 0;
  1187. }
  1188. pthread_mutex_unlock (&logsys_config_mutex);
  1189. return i;
  1190. }
  1191. unsigned int logsys_config_logfile_priority_set (
  1192. const char *subsys,
  1193. unsigned int priority)
  1194. {
  1195. int i;
  1196. pthread_mutex_lock (&logsys_config_mutex);
  1197. if (subsys != NULL) {
  1198. i = _logsys_config_subsys_get_unlocked (subsys);
  1199. if (i >= 0) {
  1200. logsys_loggers[i].logfile_priority = priority;
  1201. i = 0;
  1202. }
  1203. } else {
  1204. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1205. logsys_loggers[i].logfile_priority = priority;
  1206. }
  1207. i = 0;
  1208. }
  1209. pthread_mutex_unlock (&logsys_config_mutex);
  1210. return i;
  1211. }
  1212. unsigned int logsys_config_debug_set (
  1213. const char *subsys,
  1214. unsigned int debug)
  1215. {
  1216. int i;
  1217. pthread_mutex_lock (&logsys_config_mutex);
  1218. if (subsys != NULL) {
  1219. i = _logsys_config_subsys_get_unlocked (subsys);
  1220. if (i >= 0) {
  1221. logsys_loggers[i].debug = debug;
  1222. i = 0;
  1223. }
  1224. } else {
  1225. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1226. logsys_loggers[i].debug = debug;
  1227. }
  1228. i = 0;
  1229. }
  1230. pthread_mutex_unlock (&logsys_config_mutex);
  1231. return i;
  1232. }
  1233. int logsys_facility_id_get (const char *name)
  1234. {
  1235. unsigned int i;
  1236. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1237. if (strcasecmp(name, facilitynames[i].c_name) == 0) {
  1238. return (facilitynames[i].c_val);
  1239. }
  1240. }
  1241. return (-1);
  1242. }
  1243. const char *logsys_facility_name_get (unsigned int facility)
  1244. {
  1245. unsigned int i;
  1246. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1247. if (facility == facilitynames[i].c_val) {
  1248. return (facilitynames[i].c_name);
  1249. }
  1250. }
  1251. return (NULL);
  1252. }
  1253. int logsys_priority_id_get (const char *name)
  1254. {
  1255. unsigned int i;
  1256. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1257. if (strcasecmp(name, prioritynames[i].c_name) == 0) {
  1258. return (prioritynames[i].c_val);
  1259. }
  1260. }
  1261. return (-1);
  1262. }
  1263. const char *logsys_priority_name_get (unsigned int priority)
  1264. {
  1265. unsigned int i;
  1266. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1267. if (priority == prioritynames[i].c_val) {
  1268. return (prioritynames[i].c_name);
  1269. }
  1270. }
  1271. return (NULL);
  1272. }
  1273. int logsys_tag_id_get (const char *name)
  1274. {
  1275. unsigned int i;
  1276. for (i = 0; tagnames[i].c_name != NULL; i++) {
  1277. if (strcasecmp(name, tagnames[i].c_name) == 0) {
  1278. return (tagnames[i].c_val);
  1279. }
  1280. }
  1281. return (-1);
  1282. }
  1283. const char *logsys_tag_name_get (unsigned int tag)
  1284. {
  1285. unsigned int i;
  1286. for (i = 0; tagnames[i].c_name != NULL; i++) {
  1287. if (tag == tagnames[i].c_val) {
  1288. return (tagnames[i].c_name);
  1289. }
  1290. }
  1291. return (NULL);
  1292. }
  1293. int logsys_thread_priority_set (
  1294. int policy,
  1295. const struct sched_param *param,
  1296. unsigned int after_log_ops_yield)
  1297. {
  1298. int res = 0;
  1299. if (policy != SCHED_OTHER) {
  1300. res = pthread_setschedparam (logsys_thread_id, policy, param);
  1301. }
  1302. if (after_log_ops_yield > 0) {
  1303. logsys_after_log_ops_yield = after_log_ops_yield;
  1304. }
  1305. return (res);
  1306. }
  1307. int logsys_log_rec_store (const char *filename)
  1308. {
  1309. int fd;
  1310. ssize_t written_size;
  1311. size_t size_to_write = (flt_data_size + 2) * sizeof (unsigned int);
  1312. fd = open (filename, O_CREAT|O_RDWR, 0700);
  1313. if (fd < 0) {
  1314. return (-1);
  1315. }
  1316. written_size = write (fd, flt_data, size_to_write);
  1317. if (close (fd) != 0)
  1318. return (-1);
  1319. if (written_size < 0) {
  1320. return (-1);
  1321. } else if ((size_t)written_size != size_to_write) {
  1322. return (-1);
  1323. }
  1324. return (0);
  1325. }
  1326. void logsys_atexit (void)
  1327. {
  1328. if (wthread_active) {
  1329. wthread_should_exit = 1;
  1330. wthread_signal ();
  1331. pthread_join (logsys_thread_id, NULL);
  1332. }
  1333. }
  1334. void logsys_flush (void)
  1335. {
  1336. wthread_signal ();
  1337. }