logsys.c 38 KB

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