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