logsys.c 39 KB

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