logsys.c 39 KB

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