logsys.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  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. return i;
  710. }
  711. }
  712. return (-1);
  713. }
  714. static void syslog_facility_reconf (void)
  715. {
  716. closelog();
  717. openlog(logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].subsys,
  718. LOG_CONS|LOG_PID,
  719. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].syslog_facility);
  720. }
  721. /*
  722. * this is always invoked within the mutex, so it's safe to parse the
  723. * whole thing as we need.
  724. */
  725. static void logsys_close_logfile (
  726. int subsysid)
  727. {
  728. int i;
  729. if ((logsys_loggers[subsysid].logfile_fp == NULL) &&
  730. (logsys_loggers[subsysid].logfile == NULL)) {
  731. return;
  732. }
  733. /*
  734. * if there is another subsystem or system using the same fp,
  735. * then we clean our own structs, but we can't close the file
  736. * as it is in use by somebody else.
  737. * Only the last users will be allowed to perform the fclose.
  738. */
  739. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  740. if ((logsys_loggers[i].logfile_fp == logsys_loggers[subsysid].logfile_fp) &&
  741. (i != subsysid)) {
  742. logsys_loggers[subsysid].logfile = NULL;
  743. logsys_loggers[subsysid].logfile_fp = NULL;
  744. return;
  745. }
  746. }
  747. /*
  748. * if we are here, we are the last users of that fp, so we can safely
  749. * close it.
  750. */
  751. fclose (logsys_loggers[subsysid].logfile_fp);
  752. logsys_loggers[subsysid].logfile_fp = NULL;
  753. free (logsys_loggers[subsysid].logfile);
  754. logsys_loggers[subsysid].logfile = NULL;
  755. }
  756. /*
  757. * we need a version that can work when somebody else is already
  758. * holding a config mutex lock or we will never get out of here
  759. */
  760. static int logsys_config_file_set_unlocked (
  761. int subsysid,
  762. const char **error_string,
  763. const char *file)
  764. {
  765. static char error_string_response[512];
  766. int i;
  767. logsys_close_logfile(subsysid);
  768. if ((file == NULL) ||
  769. (strcmp(logsys_loggers[subsysid].subsys, "") == 0)) {
  770. return (0);
  771. }
  772. if (strlen(file) >= PATH_MAX) {
  773. snprintf (error_string_response,
  774. sizeof(error_string_response),
  775. "%s: logfile name exceed maximum system filename lenght\n",
  776. logsys_loggers[subsysid].subsys);
  777. *error_string = error_string_response;
  778. return (-1);
  779. }
  780. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  781. if ((logsys_loggers[i].logfile != NULL) &&
  782. (strcmp (logsys_loggers[i].logfile, file) == 0) &&
  783. (i != subsysid)) {
  784. logsys_loggers[subsysid].logfile =
  785. logsys_loggers[i].logfile;
  786. logsys_loggers[subsysid].logfile_fp =
  787. logsys_loggers[i].logfile_fp;
  788. return (0);
  789. }
  790. }
  791. logsys_loggers[subsysid].logfile = strdup(file);
  792. if (logsys_loggers[subsysid].logfile == NULL) {
  793. snprintf (error_string_response,
  794. sizeof(error_string_response),
  795. "Unable to allocate memory for logfile '%s'\n",
  796. file);
  797. *error_string = error_string_response;
  798. return (-1);
  799. }
  800. logsys_loggers[subsysid].logfile_fp = fopen (file, "a+");
  801. if (logsys_loggers[subsysid].logfile_fp == NULL) {
  802. free(logsys_loggers[subsysid].logfile);
  803. logsys_loggers[subsysid].logfile = NULL;
  804. snprintf (error_string_response,
  805. sizeof(error_string_response),
  806. "Can't open logfile '%s' for reason (%s).\n",
  807. file, strerror (errno));
  808. *error_string = error_string_response;
  809. return (-1);
  810. }
  811. return (0);
  812. }
  813. static void logsys_subsys_init (
  814. const char *subsys,
  815. int subsysid)
  816. {
  817. if (logsys_system_needs_init == LOGSYS_LOGGER_NEEDS_INIT) {
  818. logsys_loggers[subsysid].init_status =
  819. LOGSYS_LOGGER_NEEDS_INIT;
  820. } else {
  821. memcpy(&logsys_loggers[subsysid],
  822. &logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT],
  823. sizeof(logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT]));
  824. logsys_loggers[subsysid].init_status =
  825. LOGSYS_LOGGER_INIT_DONE;
  826. }
  827. strncpy (logsys_loggers[subsysid].subsys, subsys,
  828. LOGSYS_MAX_SUBSYS_NAMELEN);
  829. }
  830. /*
  831. * Internal API - exported
  832. */
  833. int _logsys_system_setup(
  834. const char *mainsystem,
  835. unsigned int mode,
  836. unsigned int debug,
  837. const char *logfile,
  838. int logfile_priority,
  839. int syslog_facility,
  840. int syslog_priority)
  841. {
  842. int i;
  843. const char *errstr;
  844. char tempsubsys[LOGSYS_MAX_SUBSYS_NAMELEN];
  845. if ((mainsystem == NULL) ||
  846. (strlen(mainsystem) >= LOGSYS_MAX_SUBSYS_NAMELEN)) {
  847. return -1;
  848. }
  849. i = LOGSYS_MAX_SUBSYS_COUNT;
  850. pthread_mutex_lock (&logsys_config_mutex);
  851. snprintf(logsys_loggers[i].subsys,
  852. LOGSYS_MAX_SUBSYS_NAMELEN,
  853. "%s", mainsystem);
  854. logsys_loggers[i].mode = mode;
  855. logsys_loggers[i].debug = debug;
  856. if (logsys_config_file_set_unlocked (i, &errstr, logfile) < 0) {
  857. pthread_mutex_unlock (&logsys_config_mutex);
  858. return (-1);
  859. }
  860. logsys_loggers[i].logfile_priority = logfile_priority;
  861. logsys_loggers[i].syslog_facility = syslog_facility;
  862. logsys_loggers[i].syslog_priority = syslog_priority;
  863. syslog_facility_reconf();
  864. logsys_loggers[i].init_status = LOGSYS_LOGGER_INIT_DONE;
  865. logsys_system_needs_init = LOGSYS_LOGGER_INIT_DONE;
  866. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  867. if ((strcmp (logsys_loggers[i].subsys, "") != 0) &&
  868. (logsys_loggers[i].init_status ==
  869. LOGSYS_LOGGER_NEEDS_INIT)) {
  870. strncpy (tempsubsys, logsys_loggers[i].subsys,
  871. LOGSYS_MAX_SUBSYS_NAMELEN);
  872. logsys_subsys_init(tempsubsys, i);
  873. }
  874. }
  875. pthread_mutex_unlock (&logsys_config_mutex);
  876. return (0);
  877. }
  878. unsigned int _logsys_subsys_create (const char *subsys)
  879. {
  880. int i;
  881. if ((subsys == NULL) ||
  882. (strlen(subsys) >= LOGSYS_MAX_SUBSYS_NAMELEN)) {
  883. return -1;
  884. }
  885. pthread_mutex_lock (&logsys_config_mutex);
  886. i = _logsys_config_subsys_get_unlocked (subsys);
  887. if ((i > -1) && (i < LOGSYS_MAX_SUBSYS_COUNT)) {
  888. pthread_mutex_unlock (&logsys_config_mutex);
  889. return i;
  890. }
  891. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  892. if (strcmp (logsys_loggers[i].subsys, "") == 0) {
  893. logsys_subsys_init(subsys, i);
  894. break;
  895. }
  896. }
  897. if (i >= LOGSYS_MAX_SUBSYS_COUNT) {
  898. i = -1;
  899. }
  900. pthread_mutex_unlock (&logsys_config_mutex);
  901. return i;
  902. }
  903. int _logsys_wthread_create (void)
  904. {
  905. if (((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_FORK) == 0) &&
  906. ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) != 0)) {
  907. wthread_create();
  908. atexit (logsys_atexit);
  909. }
  910. return (0);
  911. }
  912. int _logsys_rec_init (unsigned int fltsize)
  913. {
  914. /*
  915. * we need to allocate:
  916. * - requested size +
  917. * 2 extra unsigned ints for HEAD/TAIL tracking
  918. *
  919. * then round it up to the next PAGESIZE
  920. */
  921. size_t flt_real_size;
  922. /*
  923. * XXX: kill me for 1.1 because I am a dirty hack
  924. * temporary workaround that will be replaced by supporting
  925. * 0 byte size flight recorder buffer.
  926. * 0 byte size buffer will enable direct printing to logs
  927. * without flight recoder.
  928. */
  929. if (fltsize < 64000) {
  930. fltsize = 64000;
  931. }
  932. flt_real_size = ROUNDUP(
  933. (fltsize + (2 * sizeof (unsigned int))),
  934. sysconf(_SC_PAGESIZE));
  935. flt_data = malloc (flt_real_size);
  936. if (flt_data == NULL) {
  937. return (-1);
  938. }
  939. /*
  940. * flt_data_size tracks data by ints and not bytes/chars.
  941. *
  942. * the last 2 ints are reserved to store HEAD/TAIL information.
  943. * hide them from the rotating buffer.
  944. */
  945. flt_data_size = ((flt_real_size / sizeof (unsigned int)) - 2);
  946. /*
  947. * First record starts at zero
  948. * Last record ends at zero
  949. */
  950. flt_data[FDHEAD_INDEX] = 0;
  951. flt_data[FDTAIL_INDEX] = 0;
  952. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  953. pthread_spin_init (&logsys_idx_spinlock, 0);
  954. #endif
  955. return (0);
  956. }
  957. /*
  958. * u32 RECORD SIZE
  959. * u32 record ident
  960. * u32 arg count
  961. * u32 file line
  962. * u32 subsys length
  963. * buffer null terminated subsys
  964. * u32 filename length
  965. * buffer null terminated filename
  966. * u32 filename length
  967. * buffer null terminated function
  968. * u32 arg1 length
  969. * buffer arg1
  970. * ... repeats length & arg
  971. */
  972. void _logsys_log_rec (
  973. unsigned int rec_ident,
  974. const char *function_name,
  975. const char *file_name,
  976. int file_line,
  977. ...)
  978. {
  979. va_list ap;
  980. const void *buf_args[FDMAX_ARGS];
  981. unsigned int buf_len[FDMAX_ARGS];
  982. unsigned int i;
  983. unsigned int idx;
  984. unsigned int arguments = 0;
  985. unsigned int record_reclaim_size = 0;
  986. unsigned int index_start;
  987. int words_written;
  988. int subsysid;
  989. subsysid = LOGSYS_DECODE_SUBSYSID(rec_ident);
  990. /*
  991. * Decode VA Args
  992. */
  993. va_start (ap, file_line);
  994. arguments = 3;
  995. for (;;) {
  996. buf_args[arguments] = va_arg (ap, void *);
  997. if (buf_args[arguments] == LOGSYS_REC_END) {
  998. break;
  999. }
  1000. buf_len[arguments] = va_arg (ap, int);
  1001. record_reclaim_size += ((buf_len[arguments] + 3) >> 2) + 1;
  1002. arguments++;
  1003. if (arguments >= FDMAX_ARGS) {
  1004. break;
  1005. }
  1006. }
  1007. va_end (ap);
  1008. /*
  1009. * Encode logsys subsystem identity, filename, and function
  1010. */
  1011. buf_args[0] = logsys_loggers[subsysid].subsys;
  1012. buf_len[0] = strlen (logsys_loggers[subsysid].subsys) + 1;
  1013. buf_args[1] = file_name;
  1014. buf_len[1] = strlen (file_name) + 1;
  1015. buf_args[2] = function_name;
  1016. buf_len[2] = strlen (function_name) + 1;
  1017. for (i = 0; i < 3; i++) {
  1018. record_reclaim_size += ((buf_len[i] + 3) >> 2) + 1;
  1019. }
  1020. idx = flt_data[FDHEAD_INDEX];
  1021. index_start = idx;
  1022. /*
  1023. * Reclaim data needed for record including 4 words for the header
  1024. */
  1025. records_reclaim (idx, record_reclaim_size + 4);
  1026. /*
  1027. * Write record size of zero and rest of header information
  1028. */
  1029. flt_data[idx++] = 0;
  1030. idx_word_step(idx);
  1031. flt_data[idx++] = rec_ident;
  1032. idx_word_step(idx);
  1033. flt_data[idx++] = file_line;
  1034. idx_word_step(idx);
  1035. flt_data[idx++] = records_written;
  1036. idx_word_step(idx);
  1037. /*
  1038. * Encode all of the arguments into the log message
  1039. */
  1040. for (i = 0; i < arguments; i++) {
  1041. unsigned int bytes;
  1042. unsigned int full_words;
  1043. unsigned int total_words;
  1044. bytes = buf_len[i];
  1045. full_words = bytes >> 2;
  1046. total_words = (bytes + 3) >> 2;
  1047. flt_data[idx++] = total_words;
  1048. idx_word_step(idx);
  1049. /*
  1050. * determine if this is a wrapped write or normal write
  1051. */
  1052. if (idx + total_words < flt_data_size) {
  1053. /*
  1054. * dont need to wrap buffer
  1055. */
  1056. my_memcpy_32bit (&flt_data[idx], buf_args[i], full_words);
  1057. if (bytes % 4) {
  1058. my_memcpy_8bit ((char *)&flt_data[idx + full_words],
  1059. ((const char *)buf_args[i]) + (full_words << 2), bytes % 4);
  1060. }
  1061. } else {
  1062. /*
  1063. * need to wrap buffer
  1064. */
  1065. unsigned int first;
  1066. unsigned int second;
  1067. first = flt_data_size - idx;
  1068. if (first > full_words) {
  1069. first = full_words;
  1070. }
  1071. second = full_words - first;
  1072. my_memcpy_32bit (&flt_data[idx],
  1073. (const int *)buf_args[i], first);
  1074. my_memcpy_32bit (&flt_data[0],
  1075. (const int *)(((const unsigned char *)buf_args[i]) + (first << 2)),
  1076. second);
  1077. if (bytes % 4) {
  1078. my_memcpy_8bit ((char *)&flt_data[0 + second],
  1079. ((const char *)buf_args[i]) + (full_words << 2), bytes % 4);
  1080. }
  1081. }
  1082. idx += total_words;
  1083. idx_buffer_step (idx);
  1084. }
  1085. words_written = idx - index_start;
  1086. if (words_written < 0) {
  1087. words_written += flt_data_size;
  1088. }
  1089. /*
  1090. * Commit the write of the record size now that the full record
  1091. * is in the memory buffer
  1092. */
  1093. flt_data[index_start] = words_written;
  1094. /*
  1095. * If the index of the current head equals the current log_rec_idx,
  1096. * and this is not a log_printf operation, set the log_rec_idx to
  1097. * the new head position and commit the new head.
  1098. */
  1099. logsys_lock();
  1100. if (LOGSYS_DECODE_RECID(rec_ident) == LOGSYS_RECID_LOG) {
  1101. log_requests_pending += 1;
  1102. }
  1103. if (log_requests_pending == 0) {
  1104. log_rec_idx = idx;
  1105. }
  1106. flt_data[FDHEAD_INDEX] = idx;
  1107. logsys_unlock();
  1108. records_written++;
  1109. }
  1110. void _logsys_log_vprintf (
  1111. unsigned int rec_ident,
  1112. const char *function_name,
  1113. const char *file_name,
  1114. int file_line,
  1115. const char *format,
  1116. va_list ap)
  1117. {
  1118. char logsys_print_buffer[COMBINE_BUFFER_SIZE];
  1119. unsigned int len;
  1120. unsigned int level;
  1121. int subsysid;
  1122. char * short_file_name;
  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. #ifdef BUILDING_IN_PLACE
  1136. short_file_name = file_name;
  1137. #else
  1138. short_file_name = strrchr (file_name, '/');
  1139. if (short_file_name == NULL)
  1140. short_file_name = file_name;
  1141. else
  1142. short_file_name++; /* move past the "/" */
  1143. #endif /* BUILDING_IN_PLACE */
  1144. /*
  1145. * Create a log record
  1146. */
  1147. _logsys_log_rec (
  1148. rec_ident,
  1149. function_name,
  1150. short_file_name,
  1151. file_line,
  1152. logsys_print_buffer, len + 1,
  1153. LOGSYS_REC_END);
  1154. if ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) == 0) {
  1155. /*
  1156. * Output (and block) if the log mode is not threaded otherwise
  1157. * expect the worker thread to output the log data once signaled
  1158. */
  1159. log_printf_to_logs (rec_ident,
  1160. short_file_name, function_name, file_line,
  1161. logsys_print_buffer);
  1162. } else {
  1163. /*
  1164. * Signal worker thread to display logging output
  1165. */
  1166. wthread_signal ();
  1167. }
  1168. }
  1169. void _logsys_log_printf (
  1170. unsigned int rec_ident,
  1171. const char *function_name,
  1172. const char *file_name,
  1173. int file_line,
  1174. const char *format,
  1175. ...)
  1176. {
  1177. va_list ap;
  1178. va_start (ap, format);
  1179. _logsys_log_vprintf (rec_ident, function_name, file_name, file_line,
  1180. format, ap);
  1181. va_end (ap);
  1182. }
  1183. int _logsys_config_subsys_get (const char *subsys)
  1184. {
  1185. unsigned int i;
  1186. pthread_mutex_lock (&logsys_config_mutex);
  1187. i = _logsys_config_subsys_get_unlocked (subsys);
  1188. pthread_mutex_unlock (&logsys_config_mutex);
  1189. return i;
  1190. }
  1191. /*
  1192. * External Configuration and Initialization API
  1193. */
  1194. void logsys_fork_completed (void)
  1195. {
  1196. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode &= ~LOGSYS_MODE_FORK;
  1197. _logsys_wthread_create ();
  1198. }
  1199. unsigned int logsys_config_mode_set (const char *subsys, unsigned int mode)
  1200. {
  1201. int i;
  1202. pthread_mutex_lock (&logsys_config_mutex);
  1203. if (subsys != NULL) {
  1204. i = _logsys_config_subsys_get_unlocked (subsys);
  1205. if (i >= 0) {
  1206. logsys_loggers[i].mode = mode;
  1207. i = 0;
  1208. }
  1209. } else {
  1210. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1211. logsys_loggers[i].mode = mode;
  1212. }
  1213. i = 0;
  1214. }
  1215. pthread_mutex_unlock (&logsys_config_mutex);
  1216. return i;
  1217. }
  1218. unsigned int logsys_config_mode_get (const char *subsys)
  1219. {
  1220. int i;
  1221. i = _logsys_config_subsys_get (subsys);
  1222. if (i < 0) {
  1223. return i;
  1224. }
  1225. return logsys_loggers[i].mode;
  1226. }
  1227. int logsys_config_file_set (
  1228. const char *subsys,
  1229. const char **error_string,
  1230. const char *file)
  1231. {
  1232. int i;
  1233. int res;
  1234. pthread_mutex_lock (&logsys_config_mutex);
  1235. if (subsys != NULL) {
  1236. i = _logsys_config_subsys_get_unlocked (subsys);
  1237. if (i < 0) {
  1238. res = i;
  1239. } else {
  1240. res = logsys_config_file_set_unlocked(i, error_string, file);
  1241. }
  1242. } else {
  1243. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1244. res = logsys_config_file_set_unlocked(i, error_string, file);
  1245. if (res < 0) {
  1246. break;
  1247. }
  1248. }
  1249. }
  1250. pthread_mutex_unlock (&logsys_config_mutex);
  1251. return res;
  1252. }
  1253. int logsys_format_set (const char *format)
  1254. {
  1255. int ret = 0;
  1256. pthread_mutex_lock (&logsys_config_mutex);
  1257. if (format_buffer) {
  1258. free(format_buffer);
  1259. format_buffer = NULL;
  1260. }
  1261. format_buffer = strdup(format ? format : "%p [%6s] %b");
  1262. if (format_buffer == NULL) {
  1263. ret = -1;
  1264. }
  1265. pthread_mutex_unlock (&logsys_config_mutex);
  1266. return ret;
  1267. }
  1268. char *logsys_format_get (void)
  1269. {
  1270. return format_buffer;
  1271. }
  1272. unsigned int logsys_config_syslog_facility_set (
  1273. const char *subsys,
  1274. unsigned int facility)
  1275. {
  1276. int i;
  1277. pthread_mutex_lock (&logsys_config_mutex);
  1278. if (subsys != NULL) {
  1279. i = _logsys_config_subsys_get_unlocked (subsys);
  1280. if (i >= 0) {
  1281. logsys_loggers[i].syslog_facility = facility;
  1282. if (i == LOGSYS_MAX_SUBSYS_COUNT) {
  1283. syslog_facility_reconf();
  1284. }
  1285. i = 0;
  1286. }
  1287. } else {
  1288. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1289. logsys_loggers[i].syslog_facility = facility;
  1290. }
  1291. syslog_facility_reconf();
  1292. i = 0;
  1293. }
  1294. pthread_mutex_unlock (&logsys_config_mutex);
  1295. return i;
  1296. }
  1297. unsigned int logsys_config_syslog_priority_set (
  1298. const char *subsys,
  1299. unsigned int priority)
  1300. {
  1301. int i;
  1302. pthread_mutex_lock (&logsys_config_mutex);
  1303. if (subsys != NULL) {
  1304. i = _logsys_config_subsys_get_unlocked (subsys);
  1305. if (i >= 0) {
  1306. logsys_loggers[i].syslog_priority = priority;
  1307. i = 0;
  1308. }
  1309. } else {
  1310. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1311. logsys_loggers[i].syslog_priority = priority;
  1312. }
  1313. i = 0;
  1314. }
  1315. pthread_mutex_unlock (&logsys_config_mutex);
  1316. return i;
  1317. }
  1318. unsigned int logsys_config_logfile_priority_set (
  1319. const char *subsys,
  1320. unsigned int priority)
  1321. {
  1322. int i;
  1323. pthread_mutex_lock (&logsys_config_mutex);
  1324. if (subsys != NULL) {
  1325. i = _logsys_config_subsys_get_unlocked (subsys);
  1326. if (i >= 0) {
  1327. logsys_loggers[i].logfile_priority = priority;
  1328. i = 0;
  1329. }
  1330. } else {
  1331. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1332. logsys_loggers[i].logfile_priority = priority;
  1333. }
  1334. i = 0;
  1335. }
  1336. pthread_mutex_unlock (&logsys_config_mutex);
  1337. return i;
  1338. }
  1339. unsigned int logsys_config_debug_set (
  1340. const char *subsys,
  1341. unsigned int debug)
  1342. {
  1343. int i;
  1344. pthread_mutex_lock (&logsys_config_mutex);
  1345. if (subsys != NULL) {
  1346. i = _logsys_config_subsys_get_unlocked (subsys);
  1347. if (i >= 0) {
  1348. logsys_loggers[i].debug = debug;
  1349. i = 0;
  1350. }
  1351. } else {
  1352. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1353. logsys_loggers[i].debug = debug;
  1354. }
  1355. i = 0;
  1356. }
  1357. pthread_mutex_unlock (&logsys_config_mutex);
  1358. return i;
  1359. }
  1360. int logsys_facility_id_get (const char *name)
  1361. {
  1362. unsigned int i;
  1363. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1364. if (strcasecmp(name, facilitynames[i].c_name) == 0) {
  1365. return (facilitynames[i].c_val);
  1366. }
  1367. }
  1368. return (-1);
  1369. }
  1370. const char *logsys_facility_name_get (unsigned int facility)
  1371. {
  1372. unsigned int i;
  1373. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1374. if (facility == facilitynames[i].c_val) {
  1375. return (facilitynames[i].c_name);
  1376. }
  1377. }
  1378. return (NULL);
  1379. }
  1380. int logsys_priority_id_get (const char *name)
  1381. {
  1382. unsigned int i;
  1383. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1384. if (strcasecmp(name, prioritynames[i].c_name) == 0) {
  1385. return (prioritynames[i].c_val);
  1386. }
  1387. }
  1388. return (-1);
  1389. }
  1390. const char *logsys_priority_name_get (unsigned int priority)
  1391. {
  1392. unsigned int i;
  1393. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1394. if (priority == prioritynames[i].c_val) {
  1395. return (prioritynames[i].c_name);
  1396. }
  1397. }
  1398. return (NULL);
  1399. }
  1400. int logsys_thread_priority_set (
  1401. int policy,
  1402. const struct sched_param *param,
  1403. unsigned int after_log_ops_yield)
  1404. {
  1405. int res = 0;
  1406. #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX)
  1407. if (wthread_active == 0) {
  1408. logsys_sched_policy = policy;
  1409. memcpy(&logsys_sched_param, &param, sizeof(struct sched_param));
  1410. logsys_sched_param_queued = 1;
  1411. } else {
  1412. res = pthread_setschedparam (logsys_thread_id, policy, param);
  1413. }
  1414. #endif
  1415. if (after_log_ops_yield > 0) {
  1416. logsys_after_log_ops_yield = after_log_ops_yield;
  1417. }
  1418. return (res);
  1419. }
  1420. int logsys_log_rec_store (const char *filename)
  1421. {
  1422. int fd;
  1423. ssize_t written_size;
  1424. size_t size_to_write = (flt_data_size + 2) * sizeof (unsigned int);
  1425. fd = open (filename, O_CREAT|O_RDWR, 0700);
  1426. if (fd < 0) {
  1427. return (-1);
  1428. }
  1429. written_size = write (fd, &flt_data_size, sizeof(unsigned int));
  1430. if ((written_size < 0) || (written_size != sizeof(unsigned int))) {
  1431. close (fd);
  1432. return (-1);
  1433. }
  1434. written_size = write (fd, flt_data, size_to_write);
  1435. if (close (fd) != 0)
  1436. return (-1);
  1437. if (written_size < 0) {
  1438. return (-1);
  1439. } else if ((size_t)written_size != size_to_write) {
  1440. return (-1);
  1441. }
  1442. return (0);
  1443. }
  1444. void logsys_atexit (void)
  1445. {
  1446. if (wthread_active) {
  1447. wthread_should_exit = 1;
  1448. wthread_signal ();
  1449. pthread_join (logsys_thread_id, NULL);
  1450. }
  1451. }
  1452. void logsys_flush (void)
  1453. {
  1454. wthread_signal ();
  1455. }