4
0

logsys.c 39 KB

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