4
0

logsys.c 40 KB

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