logsys.c 36 KB

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