logsys.c 39 KB

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