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. if (cutoff <= 0) {
  236. size_t len = strlen (src);
  237. memcpy (dest, src, len + 1);
  238. return (len);
  239. } else {
  240. size_t len;
  241. strncpy (dest, src, cutoff);
  242. dest[cutoff] = '\0';
  243. len = strlen (dest);
  244. if (len != cutoff) {
  245. memset (&dest[len], ' ', cutoff - len);
  246. }
  247. }
  248. return (cutoff);
  249. }
  250. /*
  251. * %s SUBSYSTEM
  252. * %n FUNCTION NAME
  253. * %f FILENAME
  254. * %l FILELINE
  255. * %p PRIORITY
  256. * %t TIMESTAMP
  257. * %b BUFFER
  258. *
  259. * any number between % and character specify field length to pad or chop
  260. */
  261. static void log_printf_to_logs (
  262. const char *subsys,
  263. const char *file_name,
  264. const char *function_name,
  265. int file_line,
  266. unsigned int level,
  267. const char *buffer)
  268. {
  269. char output_buffer[COMBINE_BUFFER_SIZE];
  270. char char_time[128];
  271. char line_no[30];
  272. unsigned int format_buffer_idx = 0;
  273. unsigned int output_buffer_idx = 0;
  274. struct timeval tv;
  275. int cutoff;
  276. unsigned int len;
  277. int subsysid;
  278. subsysid = _logsys_config_subsys_get(subsys);
  279. if (subsysid <= - 1) {
  280. return;
  281. }
  282. while (format_buffer[format_buffer_idx]) {
  283. cutoff = -1;
  284. if (format_buffer[format_buffer_idx] == '%') {
  285. format_buffer_idx += 1;
  286. if (isdigit (format_buffer[format_buffer_idx])) {
  287. cutoff = atoi (&format_buffer[format_buffer_idx]);
  288. }
  289. while (isdigit (format_buffer[format_buffer_idx])) {
  290. format_buffer_idx += 1;
  291. }
  292. switch (format_buffer[format_buffer_idx]) {
  293. case 's':
  294. len = strcpy_cutoff (&output_buffer[output_buffer_idx], subsys, cutoff);
  295. output_buffer_idx += len;
  296. break;
  297. case 'n':
  298. len = strcpy_cutoff (&output_buffer[output_buffer_idx], function_name, cutoff);
  299. output_buffer_idx += len;
  300. break;
  301. case 'f':
  302. len = strcpy_cutoff (&output_buffer[output_buffer_idx], file_name, cutoff);
  303. output_buffer_idx += len;
  304. break;
  305. case 'l':
  306. sprintf (line_no, "%d", file_line);
  307. len = strcpy_cutoff (&output_buffer[output_buffer_idx], line_no, cutoff);
  308. output_buffer_idx += len;
  309. break;
  310. case 'p':
  311. break;
  312. case 't':
  313. gettimeofday (&tv, NULL);
  314. (void)strftime (char_time, sizeof (char_time), "%b %e %k:%M:%S", localtime ((time_t *)&tv.tv_sec));
  315. len = strcpy_cutoff (&output_buffer[output_buffer_idx], char_time, cutoff);
  316. output_buffer_idx += len;
  317. break;
  318. case 'b':
  319. len = strcpy_cutoff (&output_buffer[output_buffer_idx], buffer, cutoff);
  320. output_buffer_idx += len;
  321. break;
  322. }
  323. format_buffer_idx += 1;
  324. } else {
  325. output_buffer[output_buffer_idx++] = format_buffer[format_buffer_idx++];
  326. }
  327. }
  328. output_buffer[output_buffer_idx] = '\0';
  329. /*
  330. * Output to syslog
  331. */
  332. if ((logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_SYSLOG) &&
  333. ((level <= logsys_loggers[subsysid].syslog_priority) ||
  334. (logsys_loggers[subsysid].debug != 0))) {
  335. syslog (level | logsys_loggers[subsysid].syslog_facility, "%s", output_buffer);
  336. }
  337. /*
  338. * Terminate string with \n \0
  339. */
  340. output_buffer[output_buffer_idx++] = '\n';
  341. output_buffer[output_buffer_idx] = '\0';
  342. /*
  343. * Output to configured file
  344. */
  345. if (((logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_FILE) &&
  346. (logsys_loggers[subsysid].logfile_fp != NULL)) &&
  347. ((level <= logsys_loggers[subsysid].logfile_priority) ||
  348. (logsys_loggers[subsysid].debug != 0))) {
  349. /*
  350. * Output to a file
  351. */
  352. if ((fwrite (output_buffer, strlen (output_buffer), 1,
  353. logsys_loggers[subsysid].logfile_fp) < 1) ||
  354. (fflush (logsys_loggers[subsysid].logfile_fp) == EOF)) {
  355. char tmpbuffer[1024];
  356. /*
  357. * if we are here, it's bad.. it's really really bad.
  358. * Best thing would be to light a candle in a church
  359. * and pray.
  360. */
  361. snprintf(tmpbuffer, sizeof(tmpbuffer),
  362. "LOGSYS EMERGENCY: %s Unable to write to %s.",
  363. logsys_loggers[subsysid].subsys,
  364. logsys_loggers[subsysid].logfile);
  365. pthread_mutex_lock (&logsys_config_mutex);
  366. logsys_close_logfile(subsysid);
  367. logsys_loggers[subsysid].mode &= ~LOGSYS_MODE_OUTPUT_FILE;
  368. pthread_mutex_unlock (&logsys_config_mutex);
  369. log_printf_to_logs(logsys_loggers[subsysid].subsys,
  370. __FILE__, __FUNCTION__, __LINE__,
  371. LOGSYS_LEVEL_EMERG, tmpbuffer);
  372. }
  373. }
  374. /*
  375. * Output to stderr
  376. */
  377. if ((logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_STDERR) &&
  378. ((level <= logsys_loggers[subsysid].logfile_priority) ||
  379. (logsys_loggers[subsysid].debug != 0))) {
  380. if (write (STDERR_FILENO, output_buffer, strlen (output_buffer)) < 0) {
  381. char tmpbuffer[1024];
  382. /*
  383. * if we are here, it's bad.. it's really really bad.
  384. * Best thing would be to light 20 candles for each saint
  385. * in the calendar and pray a lot...
  386. */
  387. pthread_mutex_lock (&logsys_config_mutex);
  388. logsys_loggers[subsysid].mode &= ~LOGSYS_MODE_OUTPUT_STDERR;
  389. pthread_mutex_unlock (&logsys_config_mutex);
  390. snprintf(tmpbuffer, sizeof(tmpbuffer),
  391. "LOGSYS EMERGENCY: %s Unable to write to STDERR.",
  392. logsys_loggers[subsysid].subsys);
  393. log_printf_to_logs(logsys_loggers[subsysid].subsys,
  394. __FILE__, __FUNCTION__, __LINE__,
  395. LOGSYS_LEVEL_EMERG, tmpbuffer);
  396. }
  397. }
  398. }
  399. static void record_print (const char *buf)
  400. {
  401. const int *buf_uint32t = (const int *)buf;
  402. unsigned int rec_size = buf_uint32t[0];
  403. unsigned int rec_ident = buf_uint32t[1];
  404. unsigned int file_line = buf_uint32t[2];
  405. unsigned int level = rec_ident >> 28;
  406. unsigned int i;
  407. unsigned int words_processed;
  408. unsigned int arg_size_idx;
  409. const void *arguments[64];
  410. unsigned int arg_count;
  411. arg_size_idx = 4;
  412. words_processed = 4;
  413. arg_count = 0;
  414. for (i = 0; words_processed < rec_size; i++) {
  415. arguments[arg_count++] = &buf_uint32t[arg_size_idx + 1];
  416. arg_size_idx += buf_uint32t[arg_size_idx] + 1;
  417. words_processed += buf_uint32t[arg_size_idx] + 1;
  418. }
  419. /*
  420. * (char *)arguments[0] -> subsystem
  421. * (char *)arguments[1] -> file_name
  422. * (char *)arguments[2] -> function_name
  423. * (char *)arguments[3] -> message
  424. */
  425. log_printf_to_logs (
  426. (char *)arguments[0],
  427. (char *)arguments[1],
  428. (char *)arguments[2],
  429. file_line,
  430. (level-1),
  431. (char *)arguments[3]);
  432. }
  433. static int record_read (char *buf, int rec_idx, int *log_msg) {
  434. unsigned int rec_size;
  435. unsigned int rec_ident;
  436. int firstcopy, secondcopy;
  437. rec_size = flt_data[rec_idx];
  438. rec_ident = flt_data[(rec_idx + 1) % flt_data_size];
  439. /*
  440. * Not a log record
  441. */
  442. if ((rec_ident & LOGSYS_TAG_LOG) == 0) {
  443. *log_msg = 0;
  444. return ((rec_idx + rec_size) % flt_data_size);
  445. }
  446. /*
  447. * A log record
  448. */
  449. *log_msg = 1;
  450. firstcopy = rec_size;
  451. secondcopy = 0;
  452. if (firstcopy + rec_idx > flt_data_size) {
  453. firstcopy = flt_data_size - rec_idx;
  454. secondcopy -= firstcopy - rec_size;
  455. }
  456. memcpy (&buf[0], &flt_data[rec_idx], firstcopy << 2);
  457. if (secondcopy) {
  458. memcpy (&buf[(firstcopy << 2)], &flt_data[0], secondcopy << 2);
  459. }
  460. return ((rec_idx + rec_size) % flt_data_size);
  461. }
  462. static inline void wthread_signal (void)
  463. {
  464. if (wthread_active == 0) {
  465. return;
  466. }
  467. pthread_mutex_lock (&logsys_cond_mutex);
  468. pthread_cond_signal (&logsys_cond);
  469. pthread_mutex_unlock (&logsys_cond_mutex);
  470. }
  471. static inline void wthread_wait (void)
  472. {
  473. pthread_mutex_lock (&logsys_cond_mutex);
  474. pthread_cond_wait (&logsys_cond, &logsys_cond_mutex);
  475. pthread_mutex_unlock (&logsys_cond_mutex);
  476. }
  477. static inline void wthread_wait_locked (void)
  478. {
  479. pthread_cond_wait (&logsys_cond, &logsys_cond_mutex);
  480. pthread_mutex_unlock (&logsys_cond_mutex);
  481. }
  482. static void *logsys_worker_thread (void *data) __attribute__((__noreturn__));
  483. static void *logsys_worker_thread (void *data)
  484. {
  485. int log_msg;
  486. char buf[COMBINE_BUFFER_SIZE];
  487. /*
  488. * Signal wthread_create that the initialization process may continue
  489. */
  490. wthread_signal ();
  491. logsys_lock();
  492. log_rec_idx = flt_data[FDTAIL_INDEX];
  493. logsys_unlock();
  494. for (;;) {
  495. wthread_wait ();
  496. /*
  497. * Read and copy the logging record index position
  498. * It may have been updated by records_reclaim if
  499. * messages were lost or or log_rec on the first new
  500. * logging record available
  501. */
  502. /*
  503. * Process any pending log messages here
  504. */
  505. for (;;) {
  506. int yield_counter = 1;
  507. logsys_lock();
  508. if (log_requests_lost > 0) {
  509. printf ("lost %d log requests\n", log_requests_lost);
  510. log_requests_pending -= log_requests_lost;
  511. log_requests_lost = 0;
  512. }
  513. if (log_requests_pending == 0) {
  514. logsys_unlock();
  515. break;
  516. }
  517. log_rec_idx = record_read (buf, log_rec_idx, &log_msg);
  518. if (log_msg) {
  519. log_requests_pending -= 1;
  520. }
  521. logsys_unlock();
  522. /*
  523. * print the stored buffer
  524. */
  525. if (log_msg) {
  526. record_print (buf);
  527. if (yield_counter++ > logsys_after_log_ops_yield) {
  528. yield_counter = 0;
  529. sched_yield ();
  530. }
  531. }
  532. }
  533. if (wthread_should_exit) {
  534. pthread_exit (NULL);
  535. }
  536. }
  537. }
  538. static void wthread_create (void)
  539. {
  540. int res;
  541. if (wthread_active) {
  542. return;
  543. }
  544. wthread_active = 1;
  545. pthread_mutex_init (&logsys_cond_mutex, NULL);
  546. pthread_cond_init (&logsys_cond, NULL);
  547. pthread_mutex_lock (&logsys_cond_mutex);
  548. res = pthread_create (&logsys_thread_id, NULL,
  549. logsys_worker_thread, NULL);
  550. /*
  551. * Wait for thread to be started
  552. */
  553. wthread_wait_locked ();
  554. }
  555. static int _logsys_config_subsys_get_unlocked (const char *subsys)
  556. {
  557. unsigned int i;
  558. if (!subsys) {
  559. return LOGSYS_MAX_SUBSYS_COUNT;
  560. }
  561. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  562. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  563. pthread_mutex_unlock (&logsys_config_mutex);
  564. return i;
  565. }
  566. }
  567. return (-1);
  568. }
  569. static void syslog_facility_reconf (void)
  570. {
  571. closelog();
  572. openlog(logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].subsys,
  573. LOG_CONS|LOG_PID,
  574. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].syslog_facility);
  575. }
  576. /*
  577. * this is always invoked within the mutex, so it's safe to parse the
  578. * whole thing as we need.
  579. */
  580. static void logsys_close_logfile (
  581. int subsysid)
  582. {
  583. int i;
  584. if ((logsys_loggers[subsysid].logfile_fp == NULL) &&
  585. (logsys_loggers[subsysid].logfile == NULL)) {
  586. return;
  587. }
  588. /*
  589. * if there is another subsystem or system using the same fp,
  590. * then we clean our own structs, but we can't close the file
  591. * as it is in use by somebody else.
  592. * Only the last users will be allowed to perform the fclose.
  593. */
  594. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  595. if ((logsys_loggers[i].logfile_fp == logsys_loggers[subsysid].logfile_fp) &&
  596. (i != subsysid)) {
  597. logsys_loggers[subsysid].logfile = NULL;
  598. logsys_loggers[subsysid].logfile_fp = NULL;
  599. return;
  600. }
  601. }
  602. /*
  603. * if we are here, we are the last users of that fp, so we can safely
  604. * close it.
  605. */
  606. fclose (logsys_loggers[subsysid].logfile_fp);
  607. logsys_loggers[subsysid].logfile_fp = NULL;
  608. free (logsys_loggers[subsysid].logfile);
  609. logsys_loggers[subsysid].logfile = NULL;
  610. }
  611. /*
  612. * we need a version that can work when somebody else is already
  613. * holding a config mutex lock or we will never get out of here
  614. */
  615. static int logsys_config_file_set_unlocked (
  616. int subsysid,
  617. const char **error_string,
  618. const char *file)
  619. {
  620. static char error_string_response[512];
  621. int i;
  622. logsys_close_logfile(subsysid);
  623. if ((file == NULL) ||
  624. (strcmp(logsys_loggers[subsysid].subsys, "") == 0)) {
  625. return (0);
  626. }
  627. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  628. if ((logsys_loggers[i].logfile != NULL) &&
  629. (strcmp (logsys_loggers[i].logfile, file) == 0) &&
  630. (i != subsysid)) {
  631. logsys_loggers[subsysid].logfile =
  632. logsys_loggers[i].logfile;
  633. logsys_loggers[subsysid].logfile_fp =
  634. logsys_loggers[i].logfile_fp;
  635. return (0);
  636. }
  637. }
  638. logsys_loggers[subsysid].logfile = strdup(file);
  639. if (logsys_loggers[subsysid].logfile == NULL) {
  640. snprintf (error_string_response,
  641. sizeof(error_string_response),
  642. "Unable to allocate memory for logfile '%s'\n",
  643. file);
  644. *error_string = error_string_response;
  645. return (-1);
  646. }
  647. logsys_loggers[subsysid].logfile_fp = fopen (file, "a+");
  648. if (logsys_loggers[subsysid].logfile_fp == NULL) {
  649. free(logsys_loggers[subsysid].logfile);
  650. logsys_loggers[subsysid].logfile = NULL;
  651. snprintf (error_string_response,
  652. sizeof(error_string_response),
  653. "Can't open logfile '%s' for reason (%s).\n",
  654. file, strerror (errno));
  655. *error_string = error_string_response;
  656. return (-1);
  657. }
  658. return (0);
  659. }
  660. static void logsys_subsys_init (
  661. const char *subsys,
  662. int subsysid)
  663. {
  664. if (logsys_system_needs_init == LOGSYS_LOGGER_NEEDS_INIT) {
  665. logsys_loggers[subsysid].init_status =
  666. LOGSYS_LOGGER_NEEDS_INIT;
  667. } else {
  668. memcpy(&logsys_loggers[subsysid],
  669. &logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT],
  670. sizeof(logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT]));
  671. logsys_loggers[subsysid].tags = LOGSYS_TAG_LOG;
  672. logsys_loggers[subsysid].init_status =
  673. LOGSYS_LOGGER_INIT_DONE;
  674. }
  675. strncpy (logsys_loggers[subsysid].subsys, subsys,
  676. LOGSYS_MAX_SUBSYS_NAMELEN);
  677. }
  678. /*
  679. * Internal API - exported
  680. */
  681. int _logsys_system_setup(
  682. const char *mainsystem,
  683. unsigned int mode,
  684. unsigned int debug,
  685. const char *logfile,
  686. int logfile_priority,
  687. int syslog_facility,
  688. int syslog_priority,
  689. unsigned int tags)
  690. {
  691. int i;
  692. const char *errstr;
  693. char tempsubsys[LOGSYS_MAX_SUBSYS_NAMELEN];
  694. i = LOGSYS_MAX_SUBSYS_COUNT;
  695. pthread_mutex_lock (&logsys_config_mutex);
  696. snprintf(logsys_loggers[i].subsys,
  697. LOGSYS_MAX_SUBSYS_NAMELEN,
  698. "%s", mainsystem);
  699. logsys_loggers[i].mode = mode;
  700. logsys_loggers[i].debug = debug;
  701. if ((logfile) && strlen(logfile) > 0) {
  702. if (logsys_config_file_set_unlocked (i, &errstr, logfile) < 0) {
  703. pthread_mutex_unlock (&logsys_config_mutex);
  704. return (-1);
  705. }
  706. }
  707. logsys_loggers[i].logfile_priority = logfile_priority;
  708. logsys_loggers[i].syslog_facility = syslog_facility;
  709. logsys_loggers[i].syslog_priority = syslog_priority;
  710. syslog_facility_reconf();
  711. logsys_loggers[i].tags = tags;
  712. logsys_loggers[i].init_status = LOGSYS_LOGGER_INIT_DONE;
  713. logsys_system_needs_init = LOGSYS_LOGGER_INIT_DONE;
  714. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  715. if ((strcmp (logsys_loggers[i].subsys, "") != 0) &&
  716. (logsys_loggers[i].init_status ==
  717. LOGSYS_LOGGER_NEEDS_INIT)) {
  718. strncpy (tempsubsys, logsys_loggers[i].subsys,
  719. LOGSYS_MAX_SUBSYS_NAMELEN);
  720. logsys_subsys_init(tempsubsys, i);
  721. }
  722. }
  723. pthread_mutex_unlock (&logsys_config_mutex);
  724. return (0);
  725. }
  726. unsigned int _logsys_subsys_create (const char *subsys)
  727. {
  728. int i;
  729. if (subsys == NULL) {
  730. return -1;
  731. }
  732. pthread_mutex_lock (&logsys_config_mutex);
  733. i = _logsys_config_subsys_get_unlocked (subsys);
  734. if ((i > -1) && (i < LOGSYS_MAX_SUBSYS_COUNT)) {
  735. pthread_mutex_unlock (&logsys_config_mutex);
  736. return i;
  737. }
  738. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  739. if (strcmp (logsys_loggers[i].subsys, "") == 0) {
  740. logsys_subsys_init(subsys, i);
  741. break;
  742. }
  743. }
  744. if (i >= LOGSYS_MAX_SUBSYS_COUNT) {
  745. i = -1;
  746. }
  747. pthread_mutex_unlock (&logsys_config_mutex);
  748. return i;
  749. }
  750. int _logsys_wthread_create (void)
  751. {
  752. if (((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_FORK) == 0) &&
  753. ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) != 0)) {
  754. wthread_create();
  755. atexit (logsys_atexit);
  756. }
  757. return (0);
  758. }
  759. int _logsys_rec_init (unsigned int size)
  760. {
  761. /*
  762. * First record starts at zero
  763. * Last record ends at zero
  764. */
  765. flt_data = malloc ((size + 2) * sizeof (unsigned int));
  766. if (flt_data == NULL) {
  767. return (-1);
  768. }
  769. flt_data_size = size;
  770. flt_data[FDHEAD_INDEX] = 0;
  771. flt_data[FDTAIL_INDEX] = 0;
  772. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  773. pthread_spin_init (&logsys_idx_spinlock, 0);
  774. #endif
  775. return (0);
  776. }
  777. /*
  778. * u32 RECORD SIZE
  779. * u32 record ident
  780. * u32 arg count
  781. * u32 file line
  782. * u32 subsys length
  783. * buffer null terminated subsys
  784. * u32 filename length
  785. * buffer null terminated filename
  786. * u32 filename length
  787. * buffer null terminated function
  788. * u32 arg1 length
  789. * buffer arg1
  790. * ... repeats length & arg
  791. */
  792. void _logsys_log_rec (
  793. int subsysid,
  794. const char *function_name,
  795. const char *file_name,
  796. int file_line,
  797. unsigned int rec_ident,
  798. ...)
  799. {
  800. va_list ap;
  801. const void *buf_args[64];
  802. unsigned int buf_len[64];
  803. unsigned int i;
  804. unsigned int idx;
  805. unsigned int arguments = 0;
  806. unsigned int record_reclaim_size;
  807. unsigned int index_start;
  808. int words_written;
  809. record_reclaim_size = 0;
  810. /*
  811. * Decode VA Args
  812. */
  813. va_start (ap, rec_ident);
  814. arguments = 3;
  815. for (;;) {
  816. buf_args[arguments] = va_arg (ap, void *);
  817. if (buf_args[arguments] == LOGSYS_REC_END) {
  818. break;
  819. }
  820. buf_len[arguments] = va_arg (ap, int);
  821. record_reclaim_size += ((buf_len[arguments] + 3) >> 2) + 1;
  822. arguments++;
  823. if (arguments >= 64) {
  824. break;
  825. }
  826. }
  827. va_end (ap);
  828. /*
  829. * Encode logsys subsystem identity, filename, and function
  830. */
  831. buf_args[0] = logsys_loggers[subsysid].subsys;
  832. buf_len[0] = strlen (logsys_loggers[subsysid].subsys) + 1;
  833. buf_args[1] = file_name;
  834. buf_len[1] = strlen (file_name) + 1;
  835. buf_args[2] = function_name;
  836. buf_len[2] = strlen (function_name) + 1;
  837. for (i = 0; i < 3; i++) {
  838. record_reclaim_size += ((buf_len[i] + 3) >> 2) + 1;
  839. }
  840. idx = flt_data[FDHEAD_INDEX];
  841. index_start = idx;
  842. /*
  843. * Reclaim data needed for record including 4 words for the header
  844. */
  845. records_reclaim (idx, record_reclaim_size + 4);
  846. /*
  847. * Write record size of zero and rest of header information
  848. */
  849. flt_data[idx++] = 0;
  850. idx_word_step(idx);
  851. flt_data[idx++] = rec_ident;
  852. idx_word_step(idx);
  853. flt_data[idx++] = file_line;
  854. idx_word_step(idx);
  855. flt_data[idx++] = records_written;
  856. idx_word_step(idx);
  857. /*
  858. * Encode all of the arguments into the log message
  859. */
  860. for (i = 0; i < arguments; i++) {
  861. unsigned int bytes;
  862. unsigned int full_words;
  863. unsigned int total_words;
  864. bytes = buf_len[i];
  865. full_words = bytes >> 2;
  866. total_words = (bytes + 3) >> 2;
  867. flt_data[idx++] = total_words;
  868. idx_word_step(idx);
  869. /*
  870. * determine if this is a wrapped write or normal write
  871. */
  872. if (idx + total_words < flt_data_size) {
  873. /*
  874. * dont need to wrap buffer
  875. */
  876. my_memcpy_32bit (&flt_data[idx], buf_args[i], full_words);
  877. if (bytes % 4) {
  878. my_memcpy_8bit ((char *)&flt_data[idx + full_words],
  879. ((const char *)buf_args[i]) + (full_words << 2), bytes % 4);
  880. }
  881. } else {
  882. /*
  883. * need to wrap buffer
  884. */
  885. unsigned int first;
  886. unsigned int second;
  887. first = flt_data_size - idx;
  888. if (first > full_words) {
  889. first = full_words;
  890. }
  891. second = full_words - first;
  892. my_memcpy_32bit (&flt_data[idx],
  893. (const int *)buf_args[i], first);
  894. my_memcpy_32bit (&flt_data[0],
  895. (const int *)(((const unsigned char *)buf_args[i]) + (first << 2)),
  896. second);
  897. if (bytes % 4) {
  898. my_memcpy_8bit ((char *)&flt_data[0 + second],
  899. ((const char *)buf_args[i]) + (full_words << 2), bytes % 4);
  900. }
  901. }
  902. idx += total_words;
  903. idx_buffer_step (idx);
  904. }
  905. words_written = idx - index_start;
  906. if (words_written < 0) {
  907. words_written += flt_data_size;
  908. }
  909. /*
  910. * Commit the write of the record size now that the full record
  911. * is in the memory buffer
  912. */
  913. flt_data[index_start] = words_written;
  914. /*
  915. * If the index of the current head equals the current log_rec_idx,
  916. * and this is not a log_printf operation, set the log_rec_idx to
  917. * the new head position and commit the new head.
  918. */
  919. logsys_lock();
  920. if (rec_ident & LOGSYS_TAG_LOG) {
  921. log_requests_pending += 1;
  922. }
  923. if (log_requests_pending == 0) {
  924. log_rec_idx = idx;
  925. }
  926. flt_data[FDHEAD_INDEX] = idx;
  927. logsys_unlock();
  928. records_written++;
  929. }
  930. void _logsys_log_vprintf (
  931. int subsysid,
  932. const char *function_name,
  933. const char *file_name,
  934. int file_line,
  935. unsigned int level,
  936. const char *format,
  937. va_list ap)
  938. {
  939. char logsys_print_buffer[COMBINE_BUFFER_SIZE];
  940. unsigned int len;
  941. if (subsysid <= -1) {
  942. subsysid = LOGSYS_MAX_SUBSYS_COUNT;
  943. }
  944. if ((level > logsys_loggers[subsysid].syslog_priority) &&
  945. (level > logsys_loggers[subsysid].logfile_priority)) {
  946. return;
  947. }
  948. len = vsprintf (logsys_print_buffer, format, ap);
  949. if (logsys_print_buffer[len - 1] == '\n') {
  950. logsys_print_buffer[len - 1] = '\0';
  951. len -= 1;
  952. }
  953. /*
  954. * Create a log record
  955. */
  956. _logsys_log_rec (subsysid,
  957. function_name,
  958. file_name,
  959. file_line,
  960. (level+1) << 28,
  961. logsys_print_buffer, len + 1,
  962. LOGSYS_REC_END);
  963. if ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) == 0) {
  964. /*
  965. * Output (and block) if the log mode is not threaded otherwise
  966. * expect the worker thread to output the log data once signaled
  967. */
  968. log_printf_to_logs (logsys_loggers[subsysid].subsys,
  969. file_name, function_name, file_line, level,
  970. logsys_print_buffer);
  971. } else {
  972. /*
  973. * Signal worker thread to display logging output
  974. */
  975. wthread_signal ();
  976. }
  977. }
  978. void _logsys_log_printf (
  979. int subsysid,
  980. const char *function_name,
  981. const char *file_name,
  982. int file_line,
  983. unsigned int level,
  984. const char *format,
  985. ...)
  986. {
  987. char logsys_print_buffer[COMBINE_BUFFER_SIZE];
  988. unsigned int len;
  989. va_list ap;
  990. if (subsysid <= -1) {
  991. subsysid = LOGSYS_MAX_SUBSYS_COUNT;
  992. }
  993. if ((level > logsys_loggers[subsysid].syslog_priority) &&
  994. (level > logsys_loggers[subsysid].logfile_priority)) {
  995. return;
  996. }
  997. va_start (ap, format);
  998. len = vsprintf (logsys_print_buffer, format, ap);
  999. va_end (ap);
  1000. if (logsys_print_buffer[len - 1] == '\n') {
  1001. logsys_print_buffer[len - 1] = '\0';
  1002. len -= 1;
  1003. }
  1004. /*
  1005. * Create a log record
  1006. */
  1007. _logsys_log_rec (subsysid,
  1008. function_name,
  1009. file_name,
  1010. file_line,
  1011. (level+1) << 28,
  1012. logsys_print_buffer, len + 1,
  1013. LOGSYS_REC_END);
  1014. if ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) == 0) {
  1015. /*
  1016. * Output (and block) if the log mode is not threaded otherwise
  1017. * expect the worker thread to output the log data once signaled
  1018. */
  1019. log_printf_to_logs (logsys_loggers[subsysid].subsys,
  1020. file_name, function_name, file_line, level,
  1021. logsys_print_buffer);
  1022. } else {
  1023. /*
  1024. * Signal worker thread to display logging output
  1025. */
  1026. wthread_signal ();
  1027. }
  1028. }
  1029. int _logsys_config_subsys_get (const char *subsys)
  1030. {
  1031. unsigned int i;
  1032. pthread_mutex_lock (&logsys_config_mutex);
  1033. i = _logsys_config_subsys_get_unlocked (subsys);
  1034. pthread_mutex_unlock (&logsys_config_mutex);
  1035. return i;
  1036. }
  1037. /*
  1038. * External Configuration and Initialization API
  1039. */
  1040. void logsys_fork_completed (void)
  1041. {
  1042. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode &= ~LOGSYS_MODE_FORK;
  1043. _logsys_wthread_create ();
  1044. }
  1045. unsigned int logsys_config_mode_set (const char *subsys, unsigned int mode)
  1046. {
  1047. int i;
  1048. pthread_mutex_lock (&logsys_config_mutex);
  1049. if (subsys != NULL) {
  1050. i = _logsys_config_subsys_get_unlocked (subsys);
  1051. if (i >= 0) {
  1052. logsys_loggers[i].mode = mode;
  1053. i = 0;
  1054. }
  1055. } else {
  1056. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1057. logsys_loggers[i].mode = mode;
  1058. }
  1059. i = 0;
  1060. }
  1061. pthread_mutex_unlock (&logsys_config_mutex);
  1062. return i;
  1063. }
  1064. unsigned int logsys_config_mode_get (const char *subsys)
  1065. {
  1066. int i;
  1067. i = _logsys_config_subsys_get (subsys);
  1068. if (i < 0) {
  1069. return i;
  1070. }
  1071. return logsys_loggers[i].mode;
  1072. }
  1073. unsigned int logsys_config_tags_set (const char *subsys, unsigned int tags)
  1074. {
  1075. int i;
  1076. pthread_mutex_lock (&logsys_config_mutex);
  1077. if (subsys != NULL) {
  1078. i = _logsys_config_subsys_get_unlocked (subsys);
  1079. if (i >= 0) {
  1080. logsys_loggers[i].tags = tags;
  1081. i = 0;
  1082. }
  1083. } else {
  1084. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1085. logsys_loggers[i].tags = tags;
  1086. }
  1087. i = 0;
  1088. }
  1089. pthread_mutex_unlock (&logsys_config_mutex);
  1090. return i;
  1091. }
  1092. unsigned int logsys_config_tags_get (const char *subsys)
  1093. {
  1094. int i;
  1095. i = _logsys_config_subsys_get (subsys);
  1096. if (i < 0) {
  1097. return i;
  1098. }
  1099. return logsys_loggers[i].tags;
  1100. }
  1101. int logsys_config_file_set (
  1102. const char *subsys,
  1103. const char **error_string,
  1104. const char *file)
  1105. {
  1106. int i;
  1107. int res;
  1108. pthread_mutex_lock (&logsys_config_mutex);
  1109. if (subsys != NULL) {
  1110. i = _logsys_config_subsys_get_unlocked (subsys);
  1111. if (i < 0) {
  1112. res = i;
  1113. } else {
  1114. res = logsys_config_file_set_unlocked(i, error_string, file);
  1115. }
  1116. } else {
  1117. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1118. res = logsys_config_file_set_unlocked(i, error_string, file);
  1119. if (res < 0) {
  1120. break;
  1121. }
  1122. }
  1123. }
  1124. pthread_mutex_unlock (&logsys_config_mutex);
  1125. return res;
  1126. }
  1127. int logsys_format_set (const char *format)
  1128. {
  1129. int ret = 0;
  1130. pthread_mutex_lock (&logsys_config_mutex);
  1131. if (format_buffer) {
  1132. free(format_buffer);
  1133. format_buffer = NULL;
  1134. }
  1135. format_buffer = strdup(format ? format : "[%6s] %b");
  1136. if (format_buffer == NULL) {
  1137. ret = -1;
  1138. }
  1139. pthread_mutex_unlock (&logsys_config_mutex);
  1140. return ret;
  1141. }
  1142. char *logsys_format_get (void)
  1143. {
  1144. return format_buffer;
  1145. }
  1146. unsigned int logsys_config_syslog_facility_set (
  1147. const char *subsys,
  1148. unsigned int facility)
  1149. {
  1150. int i;
  1151. pthread_mutex_lock (&logsys_config_mutex);
  1152. if (subsys != NULL) {
  1153. i = _logsys_config_subsys_get_unlocked (subsys);
  1154. if (i >= 0) {
  1155. logsys_loggers[i].syslog_facility = facility;
  1156. if (i == LOGSYS_MAX_SUBSYS_COUNT) {
  1157. syslog_facility_reconf();
  1158. }
  1159. i = 0;
  1160. }
  1161. } else {
  1162. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1163. logsys_loggers[i].syslog_facility = facility;
  1164. }
  1165. syslog_facility_reconf();
  1166. i = 0;
  1167. }
  1168. pthread_mutex_unlock (&logsys_config_mutex);
  1169. return i;
  1170. }
  1171. unsigned int logsys_config_syslog_priority_set (
  1172. const char *subsys,
  1173. unsigned int priority)
  1174. {
  1175. int i;
  1176. pthread_mutex_lock (&logsys_config_mutex);
  1177. if (subsys != NULL) {
  1178. i = _logsys_config_subsys_get_unlocked (subsys);
  1179. if (i >= 0) {
  1180. logsys_loggers[i].syslog_priority = priority;
  1181. i = 0;
  1182. }
  1183. } else {
  1184. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1185. logsys_loggers[i].syslog_priority = priority;
  1186. }
  1187. i = 0;
  1188. }
  1189. pthread_mutex_unlock (&logsys_config_mutex);
  1190. return i;
  1191. }
  1192. unsigned int logsys_config_logfile_priority_set (
  1193. const char *subsys,
  1194. unsigned int priority)
  1195. {
  1196. int i;
  1197. pthread_mutex_lock (&logsys_config_mutex);
  1198. if (subsys != NULL) {
  1199. i = _logsys_config_subsys_get_unlocked (subsys);
  1200. if (i >= 0) {
  1201. logsys_loggers[i].logfile_priority = priority;
  1202. i = 0;
  1203. }
  1204. } else {
  1205. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1206. logsys_loggers[i].logfile_priority = priority;
  1207. }
  1208. i = 0;
  1209. }
  1210. pthread_mutex_unlock (&logsys_config_mutex);
  1211. return i;
  1212. }
  1213. unsigned int logsys_config_debug_set (
  1214. const char *subsys,
  1215. unsigned int debug)
  1216. {
  1217. int i;
  1218. pthread_mutex_lock (&logsys_config_mutex);
  1219. if (subsys != NULL) {
  1220. i = _logsys_config_subsys_get_unlocked (subsys);
  1221. if (i >= 0) {
  1222. logsys_loggers[i].debug = debug;
  1223. i = 0;
  1224. }
  1225. } else {
  1226. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1227. logsys_loggers[i].debug = debug;
  1228. }
  1229. i = 0;
  1230. }
  1231. pthread_mutex_unlock (&logsys_config_mutex);
  1232. return i;
  1233. }
  1234. int logsys_facility_id_get (const char *name)
  1235. {
  1236. unsigned int i;
  1237. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1238. if (strcasecmp(name, facilitynames[i].c_name) == 0) {
  1239. return (facilitynames[i].c_val);
  1240. }
  1241. }
  1242. return (-1);
  1243. }
  1244. const char *logsys_facility_name_get (unsigned int facility)
  1245. {
  1246. unsigned int i;
  1247. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1248. if (facility == facilitynames[i].c_val) {
  1249. return (facilitynames[i].c_name);
  1250. }
  1251. }
  1252. return (NULL);
  1253. }
  1254. int logsys_priority_id_get (const char *name)
  1255. {
  1256. unsigned int i;
  1257. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1258. if (strcasecmp(name, prioritynames[i].c_name) == 0) {
  1259. return (prioritynames[i].c_val);
  1260. }
  1261. }
  1262. return (-1);
  1263. }
  1264. const char *logsys_priority_name_get (unsigned int priority)
  1265. {
  1266. unsigned int i;
  1267. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1268. if (priority == prioritynames[i].c_val) {
  1269. return (prioritynames[i].c_name);
  1270. }
  1271. }
  1272. return (NULL);
  1273. }
  1274. int logsys_tag_id_get (const char *name)
  1275. {
  1276. unsigned int i;
  1277. for (i = 0; tagnames[i].c_name != NULL; i++) {
  1278. if (strcasecmp(name, tagnames[i].c_name) == 0) {
  1279. return (tagnames[i].c_val);
  1280. }
  1281. }
  1282. return (-1);
  1283. }
  1284. const char *logsys_tag_name_get (unsigned int tag)
  1285. {
  1286. unsigned int i;
  1287. for (i = 0; tagnames[i].c_name != NULL; i++) {
  1288. if (tag == tagnames[i].c_val) {
  1289. return (tagnames[i].c_name);
  1290. }
  1291. }
  1292. return (NULL);
  1293. }
  1294. int logsys_thread_priority_set (
  1295. int policy,
  1296. const struct sched_param *param,
  1297. unsigned int after_log_ops_yield)
  1298. {
  1299. int res = 0;
  1300. if (policy != SCHED_OTHER) {
  1301. res = pthread_setschedparam (logsys_thread_id, policy, param);
  1302. }
  1303. if (after_log_ops_yield > 0) {
  1304. logsys_after_log_ops_yield = after_log_ops_yield;
  1305. }
  1306. return (res);
  1307. }
  1308. int logsys_log_rec_store (const char *filename)
  1309. {
  1310. int fd;
  1311. ssize_t written_size;
  1312. size_t size_to_write = (flt_data_size + 2) * sizeof (unsigned int);
  1313. fd = open (filename, O_CREAT|O_RDWR, 0700);
  1314. if (fd < 0) {
  1315. return (-1);
  1316. }
  1317. written_size = write (fd, flt_data, size_to_write);
  1318. if (close (fd) != 0)
  1319. return (-1);
  1320. if (written_size < 0) {
  1321. return (-1);
  1322. } else if ((size_t)written_size != size_to_write) {
  1323. return (-1);
  1324. }
  1325. return (0);
  1326. }
  1327. void logsys_atexit (void)
  1328. {
  1329. if (wthread_active) {
  1330. wthread_should_exit = 1;
  1331. wthread_signal ();
  1332. pthread_join (logsys_thread_id, NULL);
  1333. }
  1334. }
  1335. void logsys_flush (void)
  1336. {
  1337. wthread_signal ();
  1338. }