logsys.c 38 KB

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