logsys.c 38 KB

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