logsys.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712
  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. }
  913. memset (flt_data, 0, flt_real_size * 2);
  914. /*
  915. * flt_data_size tracks data by ints and not bytes/chars.
  916. */
  917. flt_data_size = flt_real_size / sizeof (uint32_t);
  918. /*
  919. * First record starts at zero
  920. * Last record ends at zero
  921. */
  922. flt_head = 0;
  923. flt_tail = 0;
  924. return (0);
  925. }
  926. /*
  927. * u32 RECORD SIZE
  928. * u32 record ident
  929. * u32 arg count
  930. * u32 file line
  931. * u32 subsys length
  932. * buffer null terminated subsys
  933. * u32 filename length
  934. * buffer null terminated filename
  935. * u32 filename length
  936. * buffer null terminated function
  937. * u32 arg1 length
  938. * buffer arg1
  939. * ... repeats length & arg
  940. */
  941. void _logsys_log_rec (
  942. unsigned int rec_ident,
  943. const char *function_name,
  944. const char *file_name,
  945. int file_line,
  946. ...)
  947. {
  948. va_list ap;
  949. const void *buf_args[FDMAX_ARGS];
  950. unsigned int buf_len[FDMAX_ARGS];
  951. unsigned int i;
  952. unsigned int idx;
  953. unsigned int arguments = 0;
  954. unsigned int record_reclaim_size = 0;
  955. unsigned int index_start;
  956. int words_written;
  957. int subsysid;
  958. subsysid = LOGSYS_DECODE_SUBSYSID(rec_ident);
  959. /*
  960. * Decode VA Args
  961. */
  962. va_start (ap, file_line);
  963. arguments = 3;
  964. for (;;) {
  965. buf_args[arguments] = va_arg (ap, void *);
  966. if (buf_args[arguments] == LOGSYS_REC_END) {
  967. break;
  968. }
  969. buf_len[arguments] = va_arg (ap, int);
  970. record_reclaim_size += ((buf_len[arguments] + 3) >> 2) + 1;
  971. arguments++;
  972. if (arguments >= FDMAX_ARGS) {
  973. break;
  974. }
  975. }
  976. va_end (ap);
  977. /*
  978. * Encode logsys subsystem identity, filename, and function
  979. */
  980. buf_args[0] = logsys_loggers[subsysid].subsys;
  981. buf_len[0] = strlen (logsys_loggers[subsysid].subsys) + 1;
  982. buf_args[1] = file_name;
  983. buf_len[1] = strlen (file_name) + 1;
  984. buf_args[2] = function_name;
  985. buf_len[2] = strlen (function_name) + 1;
  986. for (i = 0; i < 3; i++) {
  987. record_reclaim_size += ((buf_len[i] + 3) >> 2) + 1;
  988. }
  989. logsys_flt_lock();
  990. idx = flt_head;
  991. index_start = idx;
  992. /*
  993. * Reclaim data needed for record including 4 words for the header
  994. */
  995. records_reclaim (idx, record_reclaim_size + 4);
  996. /*
  997. * Write record size of zero and rest of header information
  998. */
  999. flt_data[idx++] = 0;
  1000. idx_word_step(idx);
  1001. flt_data[idx++] = rec_ident;
  1002. idx_word_step(idx);
  1003. flt_data[idx++] = file_line;
  1004. idx_word_step(idx);
  1005. flt_data[idx++] = records_written;
  1006. idx_word_step(idx);
  1007. /*
  1008. * Encode all of the arguments into the log message
  1009. */
  1010. for (i = 0; i < arguments; i++) {
  1011. unsigned int bytes;
  1012. unsigned int total_words;
  1013. bytes = buf_len[i];
  1014. total_words = (bytes + 3) >> 2;
  1015. flt_data[idx++] = total_words;
  1016. idx_word_step(idx);
  1017. memcpy (&flt_data[idx], buf_args[i], buf_len[i]);
  1018. idx += total_words;
  1019. idx_buffer_step (idx);
  1020. }
  1021. words_written = idx - index_start;
  1022. if (words_written < 0) {
  1023. words_written += flt_data_size;
  1024. }
  1025. /*
  1026. * Commit the write of the record size now that the full record
  1027. * is in the memory buffer
  1028. */
  1029. flt_data[index_start] = words_written;
  1030. flt_head = idx;
  1031. logsys_flt_unlock();
  1032. records_written++;
  1033. }
  1034. void _logsys_log_vprintf (
  1035. unsigned int rec_ident,
  1036. const char *function_name,
  1037. const char *file_name,
  1038. int file_line,
  1039. const char *format,
  1040. va_list ap)
  1041. {
  1042. char logsys_print_buffer[COMBINE_BUFFER_SIZE];
  1043. unsigned int len;
  1044. unsigned int level;
  1045. int subsysid;
  1046. const char *short_file_name;
  1047. subsysid = LOGSYS_DECODE_SUBSYSID(rec_ident);
  1048. level = LOGSYS_DECODE_LEVEL(rec_ident);
  1049. len = vsnprintf (logsys_print_buffer, sizeof (logsys_print_buffer), format, ap);
  1050. if (logsys_print_buffer[len - 1] == '\n') {
  1051. logsys_print_buffer[len - 1] = '\0';
  1052. len -= 1;
  1053. }
  1054. #ifdef BUILDING_IN_PLACE
  1055. short_file_name = file_name;
  1056. #else
  1057. short_file_name = strrchr (file_name, '/');
  1058. if (short_file_name == NULL)
  1059. short_file_name = file_name;
  1060. else
  1061. short_file_name++; /* move past the "/" */
  1062. #endif /* BUILDING_IN_PLACE */
  1063. if (startup_pid == 0 || startup_pid == getpid()) {
  1064. /*
  1065. * Create a log record if we are really true corosync
  1066. * process (not fork of some service) or if we didn't finished
  1067. * initialization yet.
  1068. */
  1069. _logsys_log_rec (
  1070. rec_ident,
  1071. function_name,
  1072. short_file_name,
  1073. file_line,
  1074. logsys_print_buffer, len + 1,
  1075. LOGSYS_REC_END);
  1076. }
  1077. /*
  1078. * If logsys is not going to print a message to a log target don't
  1079. * queue one
  1080. */
  1081. if ((level > logsys_loggers[subsysid].syslog_priority &&
  1082. level > logsys_loggers[subsysid].logfile_priority &&
  1083. logsys_loggers[subsysid].debug == 0) ||
  1084. (level == LOGSYS_LEVEL_DEBUG &&
  1085. logsys_loggers[subsysid].debug == 0)) {
  1086. return;
  1087. }
  1088. if ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) == 0) {
  1089. /*
  1090. * Output (and block) if the log mode is not threaded otherwise
  1091. * expect the worker thread to output the log data once signaled
  1092. */
  1093. log_printf_to_logs (rec_ident,
  1094. short_file_name,
  1095. function_name,
  1096. file_line,
  1097. logsys_print_buffer);
  1098. } else {
  1099. /*
  1100. * Signal worker thread to display logging output
  1101. */
  1102. log_printf_to_logs_wthread (rec_ident,
  1103. short_file_name,
  1104. function_name,
  1105. file_line,
  1106. logsys_print_buffer);
  1107. }
  1108. }
  1109. void _logsys_log_printf (
  1110. unsigned int rec_ident,
  1111. const char *function_name,
  1112. const char *file_name,
  1113. int file_line,
  1114. const char *format,
  1115. ...)
  1116. {
  1117. va_list ap;
  1118. va_start (ap, format);
  1119. _logsys_log_vprintf (rec_ident, function_name, file_name, file_line,
  1120. format, ap);
  1121. va_end (ap);
  1122. }
  1123. int _logsys_config_subsys_get (const char *subsys)
  1124. {
  1125. unsigned int i;
  1126. pthread_mutex_lock (&logsys_config_mutex);
  1127. i = _logsys_config_subsys_get_unlocked (subsys);
  1128. pthread_mutex_unlock (&logsys_config_mutex);
  1129. return i;
  1130. }
  1131. /*
  1132. * External Configuration and Initialization API
  1133. */
  1134. void logsys_fork_completed (void)
  1135. {
  1136. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode &= ~LOGSYS_MODE_FORK;
  1137. startup_pid = getpid();
  1138. (void)_logsys_wthread_create ();
  1139. }
  1140. int logsys_config_mode_set (const char *subsys, unsigned int mode)
  1141. {
  1142. int i;
  1143. pthread_mutex_lock (&logsys_config_mutex);
  1144. if (subsys != NULL) {
  1145. i = _logsys_config_subsys_get_unlocked (subsys);
  1146. if (i >= 0) {
  1147. logsys_loggers[i].mode = mode;
  1148. i = 0;
  1149. }
  1150. } else {
  1151. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1152. logsys_loggers[i].mode = mode;
  1153. }
  1154. i = 0;
  1155. }
  1156. pthread_mutex_unlock (&logsys_config_mutex);
  1157. return i;
  1158. }
  1159. unsigned int logsys_config_mode_get (const char *subsys)
  1160. {
  1161. int i;
  1162. i = _logsys_config_subsys_get (subsys);
  1163. if (i < 0) {
  1164. return i;
  1165. }
  1166. return logsys_loggers[i].mode;
  1167. }
  1168. int logsys_config_file_set (
  1169. const char *subsys,
  1170. const char **error_string,
  1171. const char *file)
  1172. {
  1173. int i;
  1174. int res;
  1175. pthread_mutex_lock (&logsys_config_mutex);
  1176. if (subsys != NULL) {
  1177. i = _logsys_config_subsys_get_unlocked (subsys);
  1178. if (i < 0) {
  1179. res = i;
  1180. } else {
  1181. res = logsys_config_file_set_unlocked(i, error_string, file);
  1182. }
  1183. } else {
  1184. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1185. res = logsys_config_file_set_unlocked(i, error_string, file);
  1186. if (res < 0) {
  1187. break;
  1188. }
  1189. }
  1190. }
  1191. pthread_mutex_unlock (&logsys_config_mutex);
  1192. return res;
  1193. }
  1194. int logsys_format_set (const char *format)
  1195. {
  1196. int ret = 0;
  1197. pthread_mutex_lock (&logsys_config_mutex);
  1198. if (format_buffer) {
  1199. free(format_buffer);
  1200. format_buffer = NULL;
  1201. }
  1202. format_buffer = strdup(format ? format : "%p [%6s] %b");
  1203. if (format_buffer == NULL) {
  1204. ret = -1;
  1205. }
  1206. pthread_mutex_unlock (&logsys_config_mutex);
  1207. return ret;
  1208. }
  1209. char *logsys_format_get (void)
  1210. {
  1211. return format_buffer;
  1212. }
  1213. int logsys_config_syslog_facility_set (
  1214. const char *subsys,
  1215. unsigned int facility)
  1216. {
  1217. int i;
  1218. pthread_mutex_lock (&logsys_config_mutex);
  1219. if (subsys != NULL) {
  1220. i = _logsys_config_subsys_get_unlocked (subsys);
  1221. if (i >= 0) {
  1222. logsys_loggers[i].syslog_facility = facility;
  1223. if (i == LOGSYS_MAX_SUBSYS_COUNT) {
  1224. syslog_facility_reconf();
  1225. }
  1226. i = 0;
  1227. }
  1228. } else {
  1229. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1230. logsys_loggers[i].syslog_facility = facility;
  1231. }
  1232. syslog_facility_reconf();
  1233. i = 0;
  1234. }
  1235. pthread_mutex_unlock (&logsys_config_mutex);
  1236. return i;
  1237. }
  1238. int logsys_config_syslog_priority_set (
  1239. const char *subsys,
  1240. unsigned int priority)
  1241. {
  1242. int i;
  1243. pthread_mutex_lock (&logsys_config_mutex);
  1244. if (subsys != NULL) {
  1245. i = _logsys_config_subsys_get_unlocked (subsys);
  1246. if (i >= 0) {
  1247. logsys_loggers[i].syslog_priority = priority;
  1248. i = 0;
  1249. }
  1250. } else {
  1251. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1252. logsys_loggers[i].syslog_priority = priority;
  1253. }
  1254. i = 0;
  1255. }
  1256. pthread_mutex_unlock (&logsys_config_mutex);
  1257. return i;
  1258. }
  1259. int logsys_config_logfile_priority_set (
  1260. const char *subsys,
  1261. unsigned int priority)
  1262. {
  1263. int i;
  1264. pthread_mutex_lock (&logsys_config_mutex);
  1265. if (subsys != NULL) {
  1266. i = _logsys_config_subsys_get_unlocked (subsys);
  1267. if (i >= 0) {
  1268. logsys_loggers[i].logfile_priority = priority;
  1269. i = 0;
  1270. }
  1271. } else {
  1272. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1273. logsys_loggers[i].logfile_priority = priority;
  1274. }
  1275. i = 0;
  1276. }
  1277. pthread_mutex_unlock (&logsys_config_mutex);
  1278. return i;
  1279. }
  1280. int logsys_config_debug_set (
  1281. const char *subsys,
  1282. unsigned int debug)
  1283. {
  1284. int i;
  1285. pthread_mutex_lock (&logsys_config_mutex);
  1286. if (subsys != NULL) {
  1287. i = _logsys_config_subsys_get_unlocked (subsys);
  1288. if (i >= 0) {
  1289. switch (debug) {
  1290. case LOGSYS_DEBUG_OFF:
  1291. case LOGSYS_DEBUG_ON:
  1292. logsys_loggers[i].debug = debug;
  1293. logsys_loggers[i].trace1_allowed = 0;
  1294. break;
  1295. case LOGSYS_DEBUG_TRACE:
  1296. logsys_loggers[i].debug = LOGSYS_DEBUG_ON;
  1297. logsys_loggers[i].trace1_allowed = 1;
  1298. break;
  1299. }
  1300. i = 0;
  1301. }
  1302. } else {
  1303. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1304. switch (debug) {
  1305. case LOGSYS_DEBUG_OFF:
  1306. case LOGSYS_DEBUG_ON:
  1307. logsys_loggers[i].debug = debug;
  1308. logsys_loggers[i].trace1_allowed = 0;
  1309. break;
  1310. case LOGSYS_DEBUG_TRACE:
  1311. logsys_loggers[i].debug = LOGSYS_DEBUG_ON;
  1312. logsys_loggers[i].trace1_allowed = 1;
  1313. break;
  1314. }
  1315. }
  1316. i = 0;
  1317. }
  1318. pthread_mutex_unlock (&logsys_config_mutex);
  1319. return i;
  1320. }
  1321. int logsys_facility_id_get (const char *name)
  1322. {
  1323. unsigned int i;
  1324. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1325. if (strcasecmp(name, facilitynames[i].c_name) == 0) {
  1326. return (facilitynames[i].c_val);
  1327. }
  1328. }
  1329. return (-1);
  1330. }
  1331. const char *logsys_facility_name_get (unsigned int facility)
  1332. {
  1333. unsigned int i;
  1334. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1335. if (facility == facilitynames[i].c_val) {
  1336. return (facilitynames[i].c_name);
  1337. }
  1338. }
  1339. return (NULL);
  1340. }
  1341. int logsys_priority_id_get (const char *name)
  1342. {
  1343. unsigned int i;
  1344. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1345. if (strcasecmp(name, prioritynames[i].c_name) == 0) {
  1346. return (prioritynames[i].c_val);
  1347. }
  1348. }
  1349. return (-1);
  1350. }
  1351. const char *logsys_priority_name_get (unsigned int priority)
  1352. {
  1353. unsigned int i;
  1354. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1355. if (priority == prioritynames[i].c_val) {
  1356. return (prioritynames[i].c_name);
  1357. }
  1358. }
  1359. return (NULL);
  1360. }
  1361. int logsys_thread_priority_set (
  1362. int policy,
  1363. const struct sched_param *param,
  1364. unsigned int after_log_ops_yield)
  1365. {
  1366. int res = 0;
  1367. if (param == NULL) {
  1368. return (0);
  1369. }
  1370. #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX)
  1371. if (wthread_active == 0) {
  1372. logsys_sched_policy = policy;
  1373. memcpy(&logsys_sched_param, param, sizeof(struct sched_param));
  1374. logsys_sched_param_queued = 1;
  1375. } else {
  1376. res = pthread_setschedparam (logsys_thread_id, policy, param);
  1377. }
  1378. #endif
  1379. if (after_log_ops_yield > 0) {
  1380. logsys_after_log_ops_yield = after_log_ops_yield;
  1381. }
  1382. return (res);
  1383. }
  1384. int logsys_log_rec_store (const char *filename)
  1385. {
  1386. int fd;
  1387. ssize_t written_size = 0;
  1388. size_t this_write_size;
  1389. fd = open (filename, O_CREAT|O_RDWR, 0700);
  1390. if (fd < 0) {
  1391. return (-1);
  1392. }
  1393. logsys_flt_lock();
  1394. this_write_size = write (fd, &flt_data_size, sizeof(uint32_t));
  1395. if (this_write_size != sizeof(unsigned int)) {
  1396. goto error_exit;
  1397. }
  1398. written_size += this_write_size;
  1399. this_write_size = write (fd, flt_data, flt_data_size * sizeof (uint32_t));
  1400. if (this_write_size != (flt_data_size * sizeof(uint32_t))) {
  1401. goto error_exit;
  1402. }
  1403. written_size += this_write_size;
  1404. this_write_size = write (fd, &flt_head, sizeof (uint32_t));
  1405. if (this_write_size != (sizeof(uint32_t))) {
  1406. goto error_exit;
  1407. }
  1408. written_size += this_write_size;
  1409. this_write_size = write (fd, &flt_tail, sizeof (uint32_t));
  1410. if (this_write_size != (sizeof(uint32_t))) {
  1411. goto error_exit;
  1412. }
  1413. written_size += this_write_size;
  1414. if (written_size != ((flt_data_size + 3) * sizeof (uint32_t))) {
  1415. goto error_exit;
  1416. }
  1417. logsys_flt_unlock();
  1418. close (fd);
  1419. return (0);
  1420. error_exit:
  1421. logsys_flt_unlock();
  1422. close (fd);
  1423. return (-1);
  1424. }
  1425. void logsys_atexit (void)
  1426. {
  1427. int res;
  1428. int value;
  1429. struct record *rec;
  1430. if (wthread_active == 0) {
  1431. for (;;) {
  1432. logsys_wthread_lock();
  1433. res = sem_getvalue (&logsys_print_finished, &value);
  1434. if (value == 0) {
  1435. logsys_wthread_unlock();
  1436. return;
  1437. }
  1438. sem_wait (&logsys_print_finished);
  1439. rec = list_entry (logsys_print_finished_records.next, struct record, list);
  1440. list_del (&rec->list);
  1441. logsys_memory_used = logsys_memory_used - strlen (rec->buffer) -
  1442. sizeof (struct record) - 1;
  1443. logsys_wthread_unlock();
  1444. log_printf_to_logs (
  1445. rec->rec_ident,
  1446. rec->file_name,
  1447. rec->function_name,
  1448. rec->file_line,
  1449. rec->buffer);
  1450. free (rec->buffer);
  1451. free (rec);
  1452. }
  1453. } else {
  1454. wthread_should_exit = 1;
  1455. sem_post (&logsys_print_finished);
  1456. pthread_join (logsys_thread_id, NULL);
  1457. }
  1458. }
  1459. void logsys_flush (void)
  1460. {
  1461. }