logsys.c 38 KB

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