logsys.c 39 KB

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