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. #define FDMAX_ARGS 64
  158. /* forward declarations */
  159. static void logsys_close_logfile(int subsysid);
  160. #ifdef LOGSYS_DEBUG
  161. static char *decode_mode(int subsysid, char *buf, size_t buflen)
  162. {
  163. memset(buf, 0, buflen);
  164. if (logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_FILE)
  165. snprintf(buf+strlen(buf), buflen, "FILE,");
  166. if (logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_STDERR)
  167. snprintf(buf+strlen(buf), buflen, "STDERR,");
  168. if (logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_SYSLOG)
  169. snprintf(buf+strlen(buf), buflen, "SYSLOG,");
  170. if (subsysid == LOGSYS_MAX_SUBSYS_COUNT) {
  171. if (logsys_loggers[subsysid].mode & LOGSYS_MODE_FORK)
  172. snprintf(buf+strlen(buf), buflen, "FORK,");
  173. if (logsys_loggers[subsysid].mode & LOGSYS_MODE_THREADED)
  174. snprintf(buf+strlen(buf), buflen, "THREADED,");
  175. }
  176. memset(buf+strlen(buf)-1,0,1);
  177. return buf;
  178. }
  179. static const char *decode_debug(int subsysid)
  180. {
  181. if (logsys_loggers[subsysid].debug)
  182. return "on";
  183. return "off";
  184. }
  185. static const char *decode_status(int subsysid)
  186. {
  187. if (!logsys_loggers[subsysid].init_status)
  188. return "INIT_DONE";
  189. return "NEEDS_INIT";
  190. }
  191. static void dump_subsys_config(int subsysid)
  192. {
  193. char modebuf[1024];
  194. fprintf(stderr,
  195. "ID: %d\n"
  196. "subsys: %s\n"
  197. "logfile: %s\n"
  198. "logfile_fp: %p\n"
  199. "mode: %s\n"
  200. "debug: %s\n"
  201. "syslog_fac: %s\n"
  202. "syslog_pri: %s\n"
  203. "logfile_pri: %s\n"
  204. "init_status: %s\n",
  205. subsysid,
  206. logsys_loggers[subsysid].subsys,
  207. logsys_loggers[subsysid].logfile,
  208. logsys_loggers[subsysid].logfile_fp,
  209. decode_mode(subsysid, modebuf, sizeof(modebuf)),
  210. decode_debug(subsysid),
  211. logsys_facility_name_get(logsys_loggers[subsysid].syslog_facility),
  212. logsys_priority_name_get(logsys_loggers[subsysid].syslog_priority),
  213. logsys_priority_name_get(logsys_loggers[subsysid].logfile_priority),
  214. decode_status(subsysid));
  215. }
  216. static void dump_full_config(void)
  217. {
  218. int i;
  219. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  220. if (strlen(logsys_loggers[i].subsys) > 0)
  221. dump_subsys_config(i);
  222. }
  223. }
  224. #endif
  225. /*
  226. * Helpers for _logsys_log_rec functionality
  227. */
  228. static inline void my_memcpy_32bit (int *dest, const int *src, unsigned int words)
  229. {
  230. unsigned int word_idx;
  231. for (word_idx = 0; word_idx < words; word_idx++) {
  232. dest[word_idx] = src[word_idx];
  233. }
  234. }
  235. static inline void my_memcpy_8bit (char *dest, const char *src,
  236. unsigned int bytes)
  237. {
  238. unsigned int byte_idx;
  239. for (byte_idx = 0; byte_idx < bytes; byte_idx++) {
  240. dest[byte_idx] = src[byte_idx];
  241. }
  242. }
  243. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  244. static void logsys_lock (void)
  245. {
  246. pthread_spin_lock (&logsys_idx_spinlock);
  247. }
  248. static void logsys_unlock (void)
  249. {
  250. pthread_spin_unlock (&logsys_idx_spinlock);
  251. }
  252. #else
  253. static void logsys_lock (void)
  254. {
  255. pthread_mutex_lock (&logsys_idx_mutex);
  256. }
  257. static void logsys_unlock (void)
  258. {
  259. pthread_mutex_unlock (&logsys_idx_mutex);
  260. }
  261. #endif
  262. /*
  263. * Before any write operation, a reclaim on the buffer area must be executed
  264. */
  265. static inline void records_reclaim (unsigned int idx, unsigned int words)
  266. {
  267. unsigned int should_reclaim;
  268. should_reclaim = 0;
  269. if ((idx + words) >= flt_data_size) {
  270. logsys_buffer_full = 1;
  271. }
  272. if (logsys_buffer_full == 0) {
  273. return;
  274. }
  275. logsys_lock();
  276. if (flt_data[FDTAIL_INDEX] > flt_data[FDHEAD_INDEX]) {
  277. if (idx + words >= flt_data[FDTAIL_INDEX]) {
  278. should_reclaim = 1;
  279. }
  280. } else {
  281. if ((idx + words) >= (flt_data[FDTAIL_INDEX] + flt_data_size)) {
  282. should_reclaim = 1;
  283. }
  284. }
  285. if (should_reclaim) {
  286. int words_needed = 0;
  287. words_needed = words + 1;
  288. do {
  289. unsigned int old_tail;
  290. words_needed -= flt_data[flt_data[FDTAIL_INDEX]];
  291. old_tail = flt_data[FDTAIL_INDEX];
  292. flt_data[FDTAIL_INDEX] =
  293. (flt_data[FDTAIL_INDEX] +
  294. flt_data[flt_data[FDTAIL_INDEX]]) % (flt_data_size);
  295. if (log_rec_idx == old_tail) {
  296. log_requests_lost += 1;
  297. log_rec_idx = flt_data[FDTAIL_INDEX];
  298. }
  299. } while (words_needed > 0);
  300. }
  301. logsys_unlock();
  302. }
  303. #define idx_word_step(idx) \
  304. do { \
  305. if (idx > (flt_data_size - 1)) { \
  306. idx = 0; \
  307. } \
  308. } while (0);
  309. #define idx_buffer_step(idx) \
  310. do { \
  311. if (idx > (flt_data_size - 1)) { \
  312. idx = ((idx) % (flt_data_size)); \
  313. } \
  314. } while (0);
  315. /*
  316. * Internal threaded logging implementation
  317. */
  318. static inline int strcpy_cutoff (char *dest, const char *src, size_t cutoff,
  319. size_t buf_len)
  320. {
  321. size_t len = strlen (src);
  322. if (buf_len <= 1) {
  323. if (buf_len == 0)
  324. dest[0] = 0;
  325. return 0;
  326. }
  327. if (cutoff == 0) {
  328. cutoff = len;
  329. }
  330. cutoff = MIN (cutoff, buf_len - 1);
  331. len = MIN (len, cutoff);
  332. memcpy (dest, src, len);
  333. memset (dest + len, ' ', cutoff - len);
  334. dest[cutoff] = '\0';
  335. return (cutoff);
  336. }
  337. /*
  338. * %s SUBSYSTEM
  339. * %n FUNCTION NAME
  340. * %f FILENAME
  341. * %l FILELINE
  342. * %p PRIORITY
  343. * %t TIMESTAMP
  344. * %b BUFFER
  345. *
  346. * any number between % and character specify field length to pad or chop
  347. */
  348. static void log_printf_to_logs (
  349. unsigned int rec_ident,
  350. const char *file_name,
  351. const char *function_name,
  352. int file_line,
  353. const char *buffer)
  354. {
  355. char normal_output_buffer[COMBINE_BUFFER_SIZE];
  356. char syslog_output_buffer[COMBINE_BUFFER_SIZE];
  357. char char_time[128];
  358. char line_no[30];
  359. unsigned int format_buffer_idx = 0;
  360. unsigned int normal_output_buffer_idx = 0;
  361. unsigned int syslog_output_buffer_idx = 0;
  362. struct timeval tv;
  363. size_t cutoff;
  364. unsigned int normal_len, syslog_len;
  365. int subsysid;
  366. unsigned int level;
  367. int c;
  368. if (LOGSYS_DECODE_RECID(rec_ident) != LOGSYS_RECID_LOG) {
  369. return;
  370. }
  371. subsysid = LOGSYS_DECODE_SUBSYSID(rec_ident);
  372. level = LOGSYS_DECODE_LEVEL(rec_ident);
  373. while ((c = format_buffer[format_buffer_idx])) {
  374. cutoff = 0;
  375. if (c != '%') {
  376. normal_output_buffer[normal_output_buffer_idx++] = c;
  377. syslog_output_buffer[syslog_output_buffer_idx++] = c;
  378. format_buffer_idx++;
  379. } else {
  380. const char *normal_p, *syslog_p;
  381. format_buffer_idx += 1;
  382. if (isdigit (format_buffer[format_buffer_idx])) {
  383. cutoff = atoi (&format_buffer[format_buffer_idx]);
  384. }
  385. while (isdigit (format_buffer[format_buffer_idx])) {
  386. format_buffer_idx += 1;
  387. }
  388. switch (format_buffer[format_buffer_idx]) {
  389. case 's':
  390. normal_p = logsys_loggers[subsysid].subsys;
  391. syslog_p = logsys_loggers[subsysid].subsys;
  392. break;
  393. case 'n':
  394. normal_p = function_name;
  395. syslog_p = function_name;
  396. break;
  397. case 'f':
  398. normal_p = file_name;
  399. syslog_p = file_name;
  400. break;
  401. case 'l':
  402. sprintf (line_no, "%d", file_line);
  403. normal_p = line_no;
  404. syslog_p = line_no;
  405. break;
  406. case 't':
  407. gettimeofday (&tv, NULL);
  408. (void)strftime (char_time, sizeof (char_time), "%b %d %T", localtime ((time_t *)&tv.tv_sec));
  409. normal_p = char_time;
  410. /*
  411. * syslog does timestamping on its own.
  412. * also strip extra space in case.
  413. */
  414. syslog_p = "";
  415. break;
  416. case 'b':
  417. normal_p = buffer;
  418. syslog_p = buffer;
  419. break;
  420. case 'p':
  421. normal_p = logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].subsys;
  422. syslog_p = "";
  423. break;
  424. default:
  425. normal_p = "";
  426. syslog_p = "";
  427. break;
  428. }
  429. normal_len = strcpy_cutoff (normal_output_buffer + normal_output_buffer_idx,
  430. normal_p, cutoff,
  431. (sizeof (normal_output_buffer)
  432. - normal_output_buffer_idx));
  433. normal_output_buffer_idx += normal_len;
  434. syslog_len = strcpy_cutoff (syslog_output_buffer + syslog_output_buffer_idx,
  435. syslog_p, cutoff,
  436. (sizeof (syslog_output_buffer)
  437. - syslog_output_buffer_idx));
  438. syslog_output_buffer_idx += syslog_len;
  439. format_buffer_idx += 1;
  440. }
  441. if ((normal_output_buffer_idx == sizeof (normal_output_buffer)) ||
  442. (syslog_output_buffer_idx == sizeof (syslog_output_buffer))) {
  443. break;
  444. }
  445. }
  446. normal_output_buffer[normal_output_buffer_idx] = '\0';
  447. syslog_output_buffer[syslog_output_buffer_idx] = '\0';
  448. /*
  449. * Output to syslog
  450. */
  451. if ((logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_SYSLOG) &&
  452. ((level <= logsys_loggers[subsysid].syslog_priority) ||
  453. (logsys_loggers[subsysid].debug != 0))) {
  454. syslog (level | logsys_loggers[subsysid].syslog_facility, "%s", syslog_output_buffer);
  455. }
  456. /*
  457. * Terminate string with \n \0
  458. */
  459. normal_output_buffer[normal_output_buffer_idx++] = '\n';
  460. normal_output_buffer[normal_output_buffer_idx] = '\0';
  461. /*
  462. * Output to configured file
  463. */
  464. if (((logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_FILE) &&
  465. (logsys_loggers[subsysid].logfile_fp != NULL)) &&
  466. ((level <= logsys_loggers[subsysid].logfile_priority) ||
  467. (logsys_loggers[subsysid].debug != 0))) {
  468. /*
  469. * Output to a file
  470. */
  471. if ((fwrite (normal_output_buffer, strlen (normal_output_buffer), 1,
  472. logsys_loggers[subsysid].logfile_fp) < 1) ||
  473. (fflush (logsys_loggers[subsysid].logfile_fp) == EOF)) {
  474. char tmpbuffer[1024];
  475. /*
  476. * if we are here, it's bad.. it's really really bad.
  477. * Best thing would be to light a candle in a church
  478. * and pray.
  479. */
  480. snprintf(tmpbuffer, sizeof(tmpbuffer),
  481. "LOGSYS EMERGENCY: %s Unable to write to %s.",
  482. logsys_loggers[subsysid].subsys,
  483. logsys_loggers[subsysid].logfile);
  484. pthread_mutex_lock (&logsys_config_mutex);
  485. logsys_close_logfile(subsysid);
  486. logsys_loggers[subsysid].mode &= ~LOGSYS_MODE_OUTPUT_FILE;
  487. pthread_mutex_unlock (&logsys_config_mutex);
  488. log_printf_to_logs(
  489. LOGSYS_ENCODE_RECID(
  490. LOGSYS_LEVEL_EMERG,
  491. subsysid,
  492. LOGSYS_RECID_LOG),
  493. __FILE__, __FUNCTION__, __LINE__,
  494. tmpbuffer);
  495. }
  496. }
  497. /*
  498. * Output to stderr
  499. */
  500. if ((logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_STDERR) &&
  501. ((level <= logsys_loggers[subsysid].logfile_priority) ||
  502. (logsys_loggers[subsysid].debug != 0))) {
  503. if (write (STDERR_FILENO, normal_output_buffer, strlen (normal_output_buffer)) < 0) {
  504. char tmpbuffer[1024];
  505. /*
  506. * if we are here, it's bad.. it's really really bad.
  507. * Best thing would be to light 20 candles for each saint
  508. * in the calendar and pray a lot...
  509. */
  510. pthread_mutex_lock (&logsys_config_mutex);
  511. logsys_loggers[subsysid].mode &= ~LOGSYS_MODE_OUTPUT_STDERR;
  512. pthread_mutex_unlock (&logsys_config_mutex);
  513. snprintf(tmpbuffer, sizeof(tmpbuffer),
  514. "LOGSYS EMERGENCY: %s Unable to write to STDERR.",
  515. logsys_loggers[subsysid].subsys);
  516. log_printf_to_logs(
  517. LOGSYS_ENCODE_RECID(
  518. LOGSYS_LEVEL_EMERG,
  519. subsysid,
  520. LOGSYS_RECID_LOG),
  521. __FILE__, __FUNCTION__, __LINE__,
  522. tmpbuffer);
  523. }
  524. }
  525. }
  526. static void record_print (const char *buf)
  527. {
  528. const int *buf_uint32t = (const int *)buf;
  529. unsigned int rec_size = buf_uint32t[0];
  530. unsigned int rec_ident = buf_uint32t[1];
  531. unsigned int file_line = buf_uint32t[2];
  532. unsigned int i;
  533. unsigned int words_processed;
  534. unsigned int arg_size_idx;
  535. const void *arguments[FDMAX_ARGS];
  536. unsigned int arg_count;
  537. arg_size_idx = 4;
  538. words_processed = 4;
  539. arg_count = 0;
  540. for (i = 0; words_processed < rec_size; i++) {
  541. arguments[arg_count++] = &buf_uint32t[arg_size_idx + 1];
  542. arg_size_idx += buf_uint32t[arg_size_idx] + 1;
  543. words_processed += buf_uint32t[arg_size_idx] + 1;
  544. }
  545. /*
  546. * (char *)arguments[0] -> subsystem
  547. * (char *)arguments[1] -> file_name
  548. * (char *)arguments[2] -> function_name
  549. * (char *)arguments[3] -> message
  550. */
  551. log_printf_to_logs (
  552. rec_ident,
  553. (char *)arguments[1],
  554. (char *)arguments[2],
  555. file_line,
  556. (char *)arguments[3]);
  557. }
  558. static int record_read (char *buf, int rec_idx, int *log_msg) {
  559. unsigned int rec_size;
  560. unsigned int rec_ident;
  561. int firstcopy, secondcopy;
  562. rec_size = flt_data[rec_idx];
  563. rec_ident = flt_data[(rec_idx + 1) % flt_data_size];
  564. /*
  565. * Not a log record
  566. */
  567. if (LOGSYS_DECODE_RECID(rec_ident) != LOGSYS_RECID_LOG) {
  568. *log_msg = 0;
  569. return ((rec_idx + rec_size) % flt_data_size);
  570. }
  571. /*
  572. * A log record
  573. */
  574. *log_msg = 1;
  575. firstcopy = rec_size;
  576. secondcopy = 0;
  577. if (firstcopy + rec_idx > flt_data_size) {
  578. firstcopy = flt_data_size - rec_idx;
  579. secondcopy -= firstcopy - rec_size;
  580. }
  581. memcpy (&buf[0], &flt_data[rec_idx], firstcopy << 2);
  582. if (secondcopy) {
  583. memcpy (&buf[(firstcopy << 2)], &flt_data[0], secondcopy << 2);
  584. }
  585. return ((rec_idx + rec_size) % flt_data_size);
  586. }
  587. static inline void wthread_signal (void)
  588. {
  589. if (wthread_active == 0) {
  590. return;
  591. }
  592. pthread_mutex_lock (&logsys_cond_mutex);
  593. pthread_cond_signal (&logsys_cond);
  594. pthread_mutex_unlock (&logsys_cond_mutex);
  595. }
  596. static inline void wthread_wait (void)
  597. {
  598. pthread_mutex_lock (&logsys_cond_mutex);
  599. pthread_cond_wait (&logsys_cond, &logsys_cond_mutex);
  600. pthread_mutex_unlock (&logsys_cond_mutex);
  601. }
  602. static inline void wthread_wait_locked (void)
  603. {
  604. pthread_cond_wait (&logsys_cond, &logsys_cond_mutex);
  605. pthread_mutex_unlock (&logsys_cond_mutex);
  606. }
  607. static void *logsys_worker_thread (void *data) __attribute__((__noreturn__));
  608. static void *logsys_worker_thread (void *data)
  609. {
  610. int log_msg;
  611. char buf[COMBINE_BUFFER_SIZE];
  612. /*
  613. * Signal wthread_create that the initialization process may continue
  614. */
  615. wthread_signal ();
  616. logsys_lock();
  617. log_rec_idx = flt_data[FDTAIL_INDEX];
  618. logsys_unlock();
  619. for (;;) {
  620. wthread_wait ();
  621. /*
  622. * Read and copy the logging record index position
  623. * It may have been updated by records_reclaim if
  624. * messages were lost or or log_rec on the first new
  625. * logging record available
  626. */
  627. /*
  628. * Process any pending log messages here
  629. */
  630. for (;;) {
  631. int yield_counter = 1;
  632. logsys_lock();
  633. if (log_requests_lost > 0) {
  634. printf ("lost %d log requests\n", log_requests_lost);
  635. log_requests_pending -= log_requests_lost;
  636. log_requests_lost = 0;
  637. }
  638. if (log_requests_pending == 0) {
  639. logsys_unlock();
  640. break;
  641. }
  642. log_rec_idx = record_read (buf, log_rec_idx, &log_msg);
  643. if (log_msg) {
  644. log_requests_pending -= 1;
  645. }
  646. logsys_unlock();
  647. /*
  648. * print the stored buffer
  649. */
  650. if (log_msg) {
  651. record_print (buf);
  652. if (yield_counter++ > logsys_after_log_ops_yield) {
  653. yield_counter = 0;
  654. sched_yield ();
  655. }
  656. }
  657. }
  658. if (wthread_should_exit) {
  659. pthread_exit (NULL);
  660. }
  661. }
  662. }
  663. static void wthread_create (void)
  664. {
  665. int res;
  666. if (wthread_active) {
  667. return;
  668. }
  669. wthread_active = 1;
  670. pthread_mutex_init (&logsys_cond_mutex, NULL);
  671. pthread_cond_init (&logsys_cond, NULL);
  672. pthread_mutex_lock (&logsys_cond_mutex);
  673. res = pthread_create (&logsys_thread_id, NULL,
  674. logsys_worker_thread, NULL);
  675. /*
  676. * Wait for thread to be started
  677. */
  678. wthread_wait_locked ();
  679. }
  680. static int _logsys_config_subsys_get_unlocked (const char *subsys)
  681. {
  682. unsigned int i;
  683. if (!subsys) {
  684. return LOGSYS_MAX_SUBSYS_COUNT;
  685. }
  686. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  687. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  688. pthread_mutex_unlock (&logsys_config_mutex);
  689. return i;
  690. }
  691. }
  692. return (-1);
  693. }
  694. static void syslog_facility_reconf (void)
  695. {
  696. closelog();
  697. openlog(logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].subsys,
  698. LOG_CONS|LOG_PID,
  699. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].syslog_facility);
  700. }
  701. /*
  702. * this is always invoked within the mutex, so it's safe to parse the
  703. * whole thing as we need.
  704. */
  705. static void logsys_close_logfile (
  706. int subsysid)
  707. {
  708. int i;
  709. if ((logsys_loggers[subsysid].logfile_fp == NULL) &&
  710. (logsys_loggers[subsysid].logfile == NULL)) {
  711. return;
  712. }
  713. /*
  714. * if there is another subsystem or system using the same fp,
  715. * then we clean our own structs, but we can't close the file
  716. * as it is in use by somebody else.
  717. * Only the last users will be allowed to perform the fclose.
  718. */
  719. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  720. if ((logsys_loggers[i].logfile_fp == logsys_loggers[subsysid].logfile_fp) &&
  721. (i != subsysid)) {
  722. logsys_loggers[subsysid].logfile = NULL;
  723. logsys_loggers[subsysid].logfile_fp = NULL;
  724. return;
  725. }
  726. }
  727. /*
  728. * if we are here, we are the last users of that fp, so we can safely
  729. * close it.
  730. */
  731. fclose (logsys_loggers[subsysid].logfile_fp);
  732. logsys_loggers[subsysid].logfile_fp = NULL;
  733. free (logsys_loggers[subsysid].logfile);
  734. logsys_loggers[subsysid].logfile = NULL;
  735. }
  736. /*
  737. * we need a version that can work when somebody else is already
  738. * holding a config mutex lock or we will never get out of here
  739. */
  740. static int logsys_config_file_set_unlocked (
  741. int subsysid,
  742. const char **error_string,
  743. const char *file)
  744. {
  745. static char error_string_response[512];
  746. int i;
  747. logsys_close_logfile(subsysid);
  748. if ((file == NULL) ||
  749. (strcmp(logsys_loggers[subsysid].subsys, "") == 0)) {
  750. return (0);
  751. }
  752. if (strlen(file) >= PATH_MAX) {
  753. snprintf (error_string_response,
  754. sizeof(error_string_response),
  755. "%s: logfile name exceed maximum system filename lenght\n",
  756. logsys_loggers[subsysid].subsys);
  757. *error_string = error_string_response;
  758. return (-1);
  759. }
  760. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  761. if ((logsys_loggers[i].logfile != NULL) &&
  762. (strcmp (logsys_loggers[i].logfile, file) == 0) &&
  763. (i != subsysid)) {
  764. logsys_loggers[subsysid].logfile =
  765. logsys_loggers[i].logfile;
  766. logsys_loggers[subsysid].logfile_fp =
  767. logsys_loggers[i].logfile_fp;
  768. return (0);
  769. }
  770. }
  771. logsys_loggers[subsysid].logfile = strdup(file);
  772. if (logsys_loggers[subsysid].logfile == NULL) {
  773. snprintf (error_string_response,
  774. sizeof(error_string_response),
  775. "Unable to allocate memory for logfile '%s'\n",
  776. file);
  777. *error_string = error_string_response;
  778. return (-1);
  779. }
  780. logsys_loggers[subsysid].logfile_fp = fopen (file, "a+");
  781. if (logsys_loggers[subsysid].logfile_fp == NULL) {
  782. free(logsys_loggers[subsysid].logfile);
  783. logsys_loggers[subsysid].logfile = NULL;
  784. snprintf (error_string_response,
  785. sizeof(error_string_response),
  786. "Can't open logfile '%s' for reason (%s).\n",
  787. file, strerror (errno));
  788. *error_string = error_string_response;
  789. return (-1);
  790. }
  791. return (0);
  792. }
  793. static void logsys_subsys_init (
  794. const char *subsys,
  795. int subsysid)
  796. {
  797. if (logsys_system_needs_init == LOGSYS_LOGGER_NEEDS_INIT) {
  798. logsys_loggers[subsysid].init_status =
  799. LOGSYS_LOGGER_NEEDS_INIT;
  800. } else {
  801. memcpy(&logsys_loggers[subsysid],
  802. &logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT],
  803. sizeof(logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT]));
  804. logsys_loggers[subsysid].init_status =
  805. LOGSYS_LOGGER_INIT_DONE;
  806. }
  807. strncpy (logsys_loggers[subsysid].subsys, subsys,
  808. LOGSYS_MAX_SUBSYS_NAMELEN);
  809. }
  810. /*
  811. * Internal API - exported
  812. */
  813. int _logsys_system_setup(
  814. const char *mainsystem,
  815. unsigned int mode,
  816. unsigned int debug,
  817. const char *logfile,
  818. int logfile_priority,
  819. int syslog_facility,
  820. int syslog_priority)
  821. {
  822. int i;
  823. const char *errstr;
  824. char tempsubsys[LOGSYS_MAX_SUBSYS_NAMELEN];
  825. if ((mainsystem == NULL) ||
  826. (strlen(mainsystem) >= LOGSYS_MAX_SUBSYS_NAMELEN)) {
  827. return -1;
  828. }
  829. i = LOGSYS_MAX_SUBSYS_COUNT;
  830. pthread_mutex_lock (&logsys_config_mutex);
  831. snprintf(logsys_loggers[i].subsys,
  832. LOGSYS_MAX_SUBSYS_NAMELEN,
  833. "%s", mainsystem);
  834. logsys_loggers[i].mode = mode;
  835. logsys_loggers[i].debug = debug;
  836. if (logsys_config_file_set_unlocked (i, &errstr, logfile) < 0) {
  837. pthread_mutex_unlock (&logsys_config_mutex);
  838. return (-1);
  839. }
  840. logsys_loggers[i].logfile_priority = logfile_priority;
  841. logsys_loggers[i].syslog_facility = syslog_facility;
  842. logsys_loggers[i].syslog_priority = syslog_priority;
  843. syslog_facility_reconf();
  844. logsys_loggers[i].init_status = LOGSYS_LOGGER_INIT_DONE;
  845. logsys_system_needs_init = LOGSYS_LOGGER_INIT_DONE;
  846. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  847. if ((strcmp (logsys_loggers[i].subsys, "") != 0) &&
  848. (logsys_loggers[i].init_status ==
  849. LOGSYS_LOGGER_NEEDS_INIT)) {
  850. strncpy (tempsubsys, logsys_loggers[i].subsys,
  851. LOGSYS_MAX_SUBSYS_NAMELEN);
  852. logsys_subsys_init(tempsubsys, i);
  853. }
  854. }
  855. pthread_mutex_unlock (&logsys_config_mutex);
  856. return (0);
  857. }
  858. unsigned int _logsys_subsys_create (const char *subsys)
  859. {
  860. int i;
  861. if ((subsys == NULL) ||
  862. (strlen(subsys) >= LOGSYS_MAX_SUBSYS_NAMELEN)) {
  863. return -1;
  864. }
  865. pthread_mutex_lock (&logsys_config_mutex);
  866. i = _logsys_config_subsys_get_unlocked (subsys);
  867. if ((i > -1) && (i < LOGSYS_MAX_SUBSYS_COUNT)) {
  868. pthread_mutex_unlock (&logsys_config_mutex);
  869. return i;
  870. }
  871. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  872. if (strcmp (logsys_loggers[i].subsys, "") == 0) {
  873. logsys_subsys_init(subsys, i);
  874. break;
  875. }
  876. }
  877. if (i >= LOGSYS_MAX_SUBSYS_COUNT) {
  878. i = -1;
  879. }
  880. pthread_mutex_unlock (&logsys_config_mutex);
  881. return i;
  882. }
  883. int _logsys_wthread_create (void)
  884. {
  885. if (((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_FORK) == 0) &&
  886. ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) != 0)) {
  887. wthread_create();
  888. atexit (logsys_atexit);
  889. }
  890. return (0);
  891. }
  892. int _logsys_rec_init (unsigned int size)
  893. {
  894. /*
  895. * First record starts at zero
  896. * Last record ends at zero
  897. */
  898. flt_data = malloc ((size + 2) * sizeof (unsigned int));
  899. if (flt_data == NULL) {
  900. return (-1);
  901. }
  902. flt_data_size = size;
  903. flt_data[FDHEAD_INDEX] = 0;
  904. flt_data[FDTAIL_INDEX] = 0;
  905. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  906. pthread_spin_init (&logsys_idx_spinlock, 0);
  907. #endif
  908. return (0);
  909. }
  910. /*
  911. * u32 RECORD SIZE
  912. * u32 record ident
  913. * u32 arg count
  914. * u32 file line
  915. * u32 subsys length
  916. * buffer null terminated subsys
  917. * u32 filename length
  918. * buffer null terminated filename
  919. * u32 filename length
  920. * buffer null terminated function
  921. * u32 arg1 length
  922. * buffer arg1
  923. * ... repeats length & arg
  924. */
  925. void _logsys_log_rec (
  926. unsigned int rec_ident,
  927. const char *function_name,
  928. const char *file_name,
  929. int file_line,
  930. ...)
  931. {
  932. va_list ap;
  933. const void *buf_args[FDMAX_ARGS];
  934. unsigned int buf_len[FDMAX_ARGS];
  935. unsigned int i;
  936. unsigned int idx;
  937. unsigned int arguments = 0;
  938. unsigned int record_reclaim_size = 0;
  939. unsigned int index_start;
  940. int words_written;
  941. int subsysid;
  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 >= FDMAX_ARGS) {
  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. }