logsys.c 37 KB

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