logsys.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  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. sem_wait (&logsys_print_finished);
  573. logsys_wthread_lock();
  574. if (wthread_should_exit) {
  575. int value;
  576. res = sem_getvalue (&logsys_print_finished, &value);
  577. if (value == 0) {
  578. logsys_wthread_unlock();
  579. pthread_exit (NULL);
  580. }
  581. }
  582. rec = list_entry (logsys_print_finished_records.next, struct record, list);
  583. list_del (&rec->list);
  584. logsys_memory_used = logsys_memory_used - strlen (rec->buffer) -
  585. sizeof (struct record) - 1;
  586. dropped = logsys_dropped_messages;
  587. logsys_dropped_messages = 0;
  588. logsys_wthread_unlock();
  589. if (dropped) {
  590. printf ("%d messages lost\n", dropped);
  591. }
  592. log_printf_to_logs (
  593. rec->rec_ident,
  594. rec->file_name,
  595. rec->function_name,
  596. rec->file_line,
  597. rec->buffer);
  598. free (rec->buffer);
  599. free (rec);
  600. }
  601. }
  602. static void wthread_create (void)
  603. {
  604. int res;
  605. if (wthread_active) {
  606. return;
  607. }
  608. wthread_active = 1;
  609. /*
  610. * TODO: propagate pthread_create errors back to the caller
  611. */
  612. res = pthread_create (&logsys_thread_id, NULL,
  613. logsys_worker_thread, NULL);
  614. sem_wait (&logsys_thread_start);
  615. if (res == 0) {
  616. if (logsys_sched_param_queued == 1) {
  617. /*
  618. * TODO: propagate logsys_thread_priority_set errors back to
  619. * the caller
  620. */
  621. res = logsys_thread_priority_set (
  622. logsys_sched_policy,
  623. &logsys_sched_param,
  624. logsys_after_log_ops_yield);
  625. logsys_sched_param_queued = 0;
  626. }
  627. } else {
  628. wthread_active = 0;
  629. }
  630. }
  631. static int _logsys_config_subsys_get_unlocked (const char *subsys)
  632. {
  633. unsigned int i;
  634. if (!subsys) {
  635. return LOGSYS_MAX_SUBSYS_COUNT;
  636. }
  637. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  638. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  639. return i;
  640. }
  641. }
  642. return (-1);
  643. }
  644. static void syslog_facility_reconf (void)
  645. {
  646. closelog();
  647. openlog(logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].subsys,
  648. LOG_CONS|LOG_PID,
  649. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].syslog_facility);
  650. }
  651. /*
  652. * this is always invoked within the mutex, so it's safe to parse the
  653. * whole thing as we need.
  654. */
  655. static void logsys_close_logfile (
  656. int subsysid)
  657. {
  658. int i;
  659. if ((logsys_loggers[subsysid].logfile_fp == NULL) &&
  660. (logsys_loggers[subsysid].logfile == NULL)) {
  661. return;
  662. }
  663. /*
  664. * if there is another subsystem or system using the same fp,
  665. * then we clean our own structs, but we can't close the file
  666. * as it is in use by somebody else.
  667. * Only the last users will be allowed to perform the fclose.
  668. */
  669. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  670. if ((logsys_loggers[i].logfile_fp == logsys_loggers[subsysid].logfile_fp) &&
  671. (i != subsysid)) {
  672. logsys_loggers[subsysid].logfile = NULL;
  673. logsys_loggers[subsysid].logfile_fp = NULL;
  674. return;
  675. }
  676. }
  677. /*
  678. * if we are here, we are the last users of that fp, so we can safely
  679. * close it.
  680. */
  681. fclose (logsys_loggers[subsysid].logfile_fp);
  682. logsys_loggers[subsysid].logfile_fp = NULL;
  683. free (logsys_loggers[subsysid].logfile);
  684. logsys_loggers[subsysid].logfile = NULL;
  685. }
  686. /*
  687. * we need a version that can work when somebody else is already
  688. * holding a config mutex lock or we will never get out of here
  689. */
  690. static int logsys_config_file_set_unlocked (
  691. int subsysid,
  692. const char **error_string,
  693. const char *file)
  694. {
  695. static char error_string_response[512];
  696. int i;
  697. logsys_close_logfile(subsysid);
  698. if ((file == NULL) ||
  699. (strcmp(logsys_loggers[subsysid].subsys, "") == 0)) {
  700. return (0);
  701. }
  702. if (strlen(file) >= PATH_MAX) {
  703. snprintf (error_string_response,
  704. sizeof(error_string_response),
  705. "%s: logfile name exceed maximum system filename lenght\n",
  706. logsys_loggers[subsysid].subsys);
  707. *error_string = error_string_response;
  708. return (-1);
  709. }
  710. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  711. if ((logsys_loggers[i].logfile != NULL) &&
  712. (strcmp (logsys_loggers[i].logfile, file) == 0) &&
  713. (i != subsysid)) {
  714. logsys_loggers[subsysid].logfile =
  715. logsys_loggers[i].logfile;
  716. logsys_loggers[subsysid].logfile_fp =
  717. logsys_loggers[i].logfile_fp;
  718. return (0);
  719. }
  720. }
  721. logsys_loggers[subsysid].logfile = strdup(file);
  722. if (logsys_loggers[subsysid].logfile == NULL) {
  723. snprintf (error_string_response,
  724. sizeof(error_string_response),
  725. "Unable to allocate memory for logfile '%s'\n",
  726. file);
  727. *error_string = error_string_response;
  728. return (-1);
  729. }
  730. logsys_loggers[subsysid].logfile_fp = fopen (file, "a+");
  731. if (logsys_loggers[subsysid].logfile_fp == NULL) {
  732. char error_str[100];
  733. strerror_r (errno, error_str, 100);
  734. free(logsys_loggers[subsysid].logfile);
  735. logsys_loggers[subsysid].logfile = NULL;
  736. snprintf (error_string_response,
  737. sizeof(error_string_response),
  738. "Can't open logfile '%s' for reason (%s).\n",
  739. file, error_str);
  740. *error_string = error_string_response;
  741. return (-1);
  742. }
  743. return (0);
  744. }
  745. static void logsys_subsys_init (
  746. const char *subsys,
  747. int subsysid)
  748. {
  749. if (logsys_system_needs_init == LOGSYS_LOGGER_NEEDS_INIT) {
  750. logsys_loggers[subsysid].init_status =
  751. LOGSYS_LOGGER_NEEDS_INIT;
  752. } else {
  753. memcpy(&logsys_loggers[subsysid],
  754. &logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT],
  755. sizeof(logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT]));
  756. logsys_loggers[subsysid].init_status =
  757. LOGSYS_LOGGER_INIT_DONE;
  758. }
  759. strncpy (logsys_loggers[subsysid].subsys, subsys,
  760. LOGSYS_MAX_SUBSYS_NAMELEN);
  761. }
  762. /*
  763. * Internal API - exported
  764. */
  765. int _logsys_system_setup(
  766. const char *mainsystem,
  767. unsigned int mode,
  768. unsigned int debug,
  769. const char *logfile,
  770. int logfile_priority,
  771. int syslog_facility,
  772. int syslog_priority)
  773. {
  774. int i;
  775. const char *errstr;
  776. char tempsubsys[LOGSYS_MAX_SUBSYS_NAMELEN];
  777. if ((mainsystem == NULL) ||
  778. (strlen(mainsystem) >= LOGSYS_MAX_SUBSYS_NAMELEN)) {
  779. return -1;
  780. }
  781. i = LOGSYS_MAX_SUBSYS_COUNT;
  782. pthread_mutex_lock (&logsys_config_mutex);
  783. snprintf(logsys_loggers[i].subsys,
  784. LOGSYS_MAX_SUBSYS_NAMELEN,
  785. "%s", mainsystem);
  786. logsys_loggers[i].mode = mode;
  787. logsys_loggers[i].debug = debug;
  788. if (logsys_config_file_set_unlocked (i, &errstr, logfile) < 0) {
  789. pthread_mutex_unlock (&logsys_config_mutex);
  790. return (-1);
  791. }
  792. logsys_loggers[i].logfile_priority = logfile_priority;
  793. logsys_loggers[i].syslog_facility = syslog_facility;
  794. logsys_loggers[i].syslog_priority = syslog_priority;
  795. syslog_facility_reconf();
  796. logsys_loggers[i].init_status = LOGSYS_LOGGER_INIT_DONE;
  797. logsys_system_needs_init = LOGSYS_LOGGER_INIT_DONE;
  798. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  799. if ((strcmp (logsys_loggers[i].subsys, "") != 0) &&
  800. (logsys_loggers[i].init_status ==
  801. LOGSYS_LOGGER_NEEDS_INIT)) {
  802. strncpy (tempsubsys, logsys_loggers[i].subsys,
  803. LOGSYS_MAX_SUBSYS_NAMELEN);
  804. logsys_subsys_init(tempsubsys, i);
  805. }
  806. }
  807. pthread_mutex_unlock (&logsys_config_mutex);
  808. return (0);
  809. }
  810. int _logsys_subsys_create (const char *subsys)
  811. {
  812. int i;
  813. if ((subsys == NULL) ||
  814. (strlen(subsys) >= LOGSYS_MAX_SUBSYS_NAMELEN)) {
  815. return -1;
  816. }
  817. pthread_mutex_lock (&logsys_config_mutex);
  818. i = _logsys_config_subsys_get_unlocked (subsys);
  819. if ((i > -1) && (i < LOGSYS_MAX_SUBSYS_COUNT)) {
  820. pthread_mutex_unlock (&logsys_config_mutex);
  821. return i;
  822. }
  823. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  824. if (strcmp (logsys_loggers[i].subsys, "") == 0) {
  825. logsys_subsys_init(subsys, i);
  826. break;
  827. }
  828. }
  829. if (i >= LOGSYS_MAX_SUBSYS_COUNT) {
  830. i = -1;
  831. }
  832. pthread_mutex_unlock (&logsys_config_mutex);
  833. return i;
  834. }
  835. int _logsys_wthread_create (void)
  836. {
  837. if (((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_FORK) == 0) &&
  838. ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) != 0)) {
  839. wthread_create();
  840. }
  841. return (0);
  842. }
  843. int _logsys_rec_init (unsigned int fltsize)
  844. {
  845. size_t flt_real_size;
  846. sem_init (&logsys_thread_start, 0, 0);
  847. sem_init (&logsys_print_finished, 0, 0);
  848. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  849. pthread_spin_init (&logsys_flt_spinlock, 1);
  850. pthread_spin_init (&logsys_wthread_spinlock, 1);
  851. #endif
  852. /*
  853. * XXX: kill me for 1.1 because I am a dirty hack
  854. * temporary workaround that will be replaced by supporting
  855. * 0 byte size flight recorder buffer.
  856. * 0 byte size buffer will enable direct printing to logs
  857. * without flight recoder.
  858. */
  859. if (fltsize < 64000) {
  860. fltsize = 64000;
  861. }
  862. flt_real_size = ROUNDUP(fltsize, sysconf(_SC_PAGESIZE));
  863. circular_memory_map ((void **)&flt_data, flt_real_size);
  864. memset (flt_data, 0, flt_real_size * 2);
  865. /*
  866. * flt_data_size tracks data by ints and not bytes/chars.
  867. */
  868. flt_data_size = flt_real_size / sizeof (uint32_t);
  869. /*
  870. * First record starts at zero
  871. * Last record ends at zero
  872. */
  873. flt_head = 0;
  874. flt_tail = 0;
  875. return (0);
  876. }
  877. /*
  878. * u32 RECORD SIZE
  879. * u32 record ident
  880. * u32 arg count
  881. * u32 file line
  882. * u32 subsys length
  883. * buffer null terminated subsys
  884. * u32 filename length
  885. * buffer null terminated filename
  886. * u32 filename length
  887. * buffer null terminated function
  888. * u32 arg1 length
  889. * buffer arg1
  890. * ... repeats length & arg
  891. */
  892. void _logsys_log_rec (
  893. unsigned int rec_ident,
  894. const char *function_name,
  895. const char *file_name,
  896. int file_line,
  897. ...)
  898. {
  899. va_list ap;
  900. const void *buf_args[FDMAX_ARGS];
  901. unsigned int buf_len[FDMAX_ARGS];
  902. unsigned int i;
  903. unsigned int idx;
  904. unsigned int arguments = 0;
  905. unsigned int record_reclaim_size = 0;
  906. unsigned int index_start;
  907. int words_written;
  908. int subsysid;
  909. subsysid = LOGSYS_DECODE_SUBSYSID(rec_ident);
  910. /*
  911. * Decode VA Args
  912. */
  913. va_start (ap, file_line);
  914. arguments = 3;
  915. for (;;) {
  916. buf_args[arguments] = va_arg (ap, void *);
  917. if (buf_args[arguments] == LOGSYS_REC_END) {
  918. break;
  919. }
  920. buf_len[arguments] = va_arg (ap, int);
  921. record_reclaim_size += ((buf_len[arguments] + 3) >> 2) + 1;
  922. arguments++;
  923. if (arguments >= FDMAX_ARGS) {
  924. break;
  925. }
  926. }
  927. va_end (ap);
  928. /*
  929. * Encode logsys subsystem identity, filename, and function
  930. */
  931. buf_args[0] = logsys_loggers[subsysid].subsys;
  932. buf_len[0] = strlen (logsys_loggers[subsysid].subsys) + 1;
  933. buf_args[1] = file_name;
  934. buf_len[1] = strlen (file_name) + 1;
  935. buf_args[2] = function_name;
  936. buf_len[2] = strlen (function_name) + 1;
  937. for (i = 0; i < 3; i++) {
  938. record_reclaim_size += ((buf_len[i] + 3) >> 2) + 1;
  939. }
  940. logsys_flt_lock();
  941. idx = flt_head;
  942. index_start = idx;
  943. /*
  944. * Reclaim data needed for record including 4 words for the header
  945. */
  946. records_reclaim (idx, record_reclaim_size + 4);
  947. /*
  948. * Write record size of zero and rest of header information
  949. */
  950. flt_data[idx++] = 0;
  951. idx_word_step(idx);
  952. flt_data[idx++] = rec_ident;
  953. idx_word_step(idx);
  954. flt_data[idx++] = file_line;
  955. idx_word_step(idx);
  956. flt_data[idx++] = records_written;
  957. idx_word_step(idx);
  958. /*
  959. * Encode all of the arguments into the log message
  960. */
  961. for (i = 0; i < arguments; i++) {
  962. unsigned int bytes;
  963. unsigned int total_words;
  964. bytes = buf_len[i];
  965. total_words = (bytes + 3) >> 2;
  966. flt_data[idx++] = total_words;
  967. idx_word_step(idx);
  968. memcpy (&flt_data[idx], buf_args[i], buf_len[i]);
  969. idx += total_words;
  970. idx_buffer_step (idx);
  971. }
  972. words_written = idx - index_start;
  973. if (words_written < 0) {
  974. words_written += flt_data_size;
  975. }
  976. /*
  977. * Commit the write of the record size now that the full record
  978. * is in the memory buffer
  979. */
  980. flt_data[index_start] = words_written;
  981. flt_head = idx;
  982. logsys_flt_unlock();
  983. records_written++;
  984. }
  985. void _logsys_log_vprintf (
  986. unsigned int rec_ident,
  987. const char *function_name,
  988. const char *file_name,
  989. int file_line,
  990. const char *format,
  991. va_list ap)
  992. {
  993. char logsys_print_buffer[COMBINE_BUFFER_SIZE];
  994. unsigned int len;
  995. unsigned int level;
  996. int subsysid;
  997. const char *short_file_name;
  998. subsysid = LOGSYS_DECODE_SUBSYSID(rec_ident);
  999. level = LOGSYS_DECODE_LEVEL(rec_ident);
  1000. len = vsprintf (logsys_print_buffer, format, ap);
  1001. if (logsys_print_buffer[len - 1] == '\n') {
  1002. logsys_print_buffer[len - 1] = '\0';
  1003. len -= 1;
  1004. }
  1005. #ifdef BUILDING_IN_PLACE
  1006. short_file_name = file_name;
  1007. #else
  1008. short_file_name = strrchr (file_name, '/');
  1009. if (short_file_name == NULL)
  1010. short_file_name = file_name;
  1011. else
  1012. short_file_name++; /* move past the "/" */
  1013. #endif /* BUILDING_IN_PLACE */
  1014. /*
  1015. * Create a log record
  1016. */
  1017. _logsys_log_rec (
  1018. rec_ident,
  1019. function_name,
  1020. short_file_name,
  1021. file_line,
  1022. logsys_print_buffer, len + 1,
  1023. LOGSYS_REC_END);
  1024. /*
  1025. * If logsys is not going to print a message to a log target don't
  1026. * queue one
  1027. */
  1028. if ((level > logsys_loggers[subsysid].syslog_priority &&
  1029. level > logsys_loggers[subsysid].logfile_priority &&
  1030. logsys_loggers[subsysid].debug == 0) ||
  1031. (level == LOGSYS_LEVEL_DEBUG &&
  1032. logsys_loggers[subsysid].debug == 0)) {
  1033. return;
  1034. }
  1035. if ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) == 0) {
  1036. /*
  1037. * Output (and block) if the log mode is not threaded otherwise
  1038. * expect the worker thread to output the log data once signaled
  1039. */
  1040. log_printf_to_logs (rec_ident,
  1041. short_file_name,
  1042. function_name,
  1043. file_line,
  1044. logsys_print_buffer);
  1045. } else {
  1046. /*
  1047. * Signal worker thread to display logging output
  1048. */
  1049. log_printf_to_logs_wthread (rec_ident,
  1050. short_file_name,
  1051. function_name,
  1052. file_line,
  1053. logsys_print_buffer);
  1054. }
  1055. }
  1056. void _logsys_log_printf (
  1057. unsigned int rec_ident,
  1058. const char *function_name,
  1059. const char *file_name,
  1060. int file_line,
  1061. const char *format,
  1062. ...)
  1063. {
  1064. va_list ap;
  1065. va_start (ap, format);
  1066. _logsys_log_vprintf (rec_ident, function_name, file_name, file_line,
  1067. format, ap);
  1068. va_end (ap);
  1069. }
  1070. int _logsys_config_subsys_get (const char *subsys)
  1071. {
  1072. unsigned int i;
  1073. pthread_mutex_lock (&logsys_config_mutex);
  1074. i = _logsys_config_subsys_get_unlocked (subsys);
  1075. pthread_mutex_unlock (&logsys_config_mutex);
  1076. return i;
  1077. }
  1078. /*
  1079. * External Configuration and Initialization API
  1080. */
  1081. void logsys_fork_completed (void)
  1082. {
  1083. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode &= ~LOGSYS_MODE_FORK;
  1084. _logsys_wthread_create ();
  1085. }
  1086. int logsys_config_mode_set (const char *subsys, unsigned int mode)
  1087. {
  1088. int i;
  1089. pthread_mutex_lock (&logsys_config_mutex);
  1090. if (subsys != NULL) {
  1091. i = _logsys_config_subsys_get_unlocked (subsys);
  1092. if (i >= 0) {
  1093. logsys_loggers[i].mode = mode;
  1094. i = 0;
  1095. }
  1096. } else {
  1097. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1098. logsys_loggers[i].mode = mode;
  1099. }
  1100. i = 0;
  1101. }
  1102. pthread_mutex_unlock (&logsys_config_mutex);
  1103. return i;
  1104. }
  1105. unsigned int logsys_config_mode_get (const char *subsys)
  1106. {
  1107. int i;
  1108. i = _logsys_config_subsys_get (subsys);
  1109. if (i < 0) {
  1110. return i;
  1111. }
  1112. return logsys_loggers[i].mode;
  1113. }
  1114. int logsys_config_file_set (
  1115. const char *subsys,
  1116. const char **error_string,
  1117. const char *file)
  1118. {
  1119. int i;
  1120. int res;
  1121. pthread_mutex_lock (&logsys_config_mutex);
  1122. if (subsys != NULL) {
  1123. i = _logsys_config_subsys_get_unlocked (subsys);
  1124. if (i < 0) {
  1125. res = i;
  1126. } else {
  1127. res = logsys_config_file_set_unlocked(i, error_string, file);
  1128. }
  1129. } else {
  1130. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1131. res = logsys_config_file_set_unlocked(i, error_string, file);
  1132. if (res < 0) {
  1133. break;
  1134. }
  1135. }
  1136. }
  1137. pthread_mutex_unlock (&logsys_config_mutex);
  1138. return res;
  1139. }
  1140. int logsys_format_set (const char *format)
  1141. {
  1142. int ret = 0;
  1143. pthread_mutex_lock (&logsys_config_mutex);
  1144. if (format_buffer) {
  1145. free(format_buffer);
  1146. format_buffer = NULL;
  1147. }
  1148. format_buffer = strdup(format ? format : "%p [%6s] %b");
  1149. if (format_buffer == NULL) {
  1150. ret = -1;
  1151. }
  1152. pthread_mutex_unlock (&logsys_config_mutex);
  1153. return ret;
  1154. }
  1155. char *logsys_format_get (void)
  1156. {
  1157. return format_buffer;
  1158. }
  1159. int logsys_config_syslog_facility_set (
  1160. const char *subsys,
  1161. unsigned int facility)
  1162. {
  1163. int i;
  1164. pthread_mutex_lock (&logsys_config_mutex);
  1165. if (subsys != NULL) {
  1166. i = _logsys_config_subsys_get_unlocked (subsys);
  1167. if (i >= 0) {
  1168. logsys_loggers[i].syslog_facility = facility;
  1169. if (i == LOGSYS_MAX_SUBSYS_COUNT) {
  1170. syslog_facility_reconf();
  1171. }
  1172. i = 0;
  1173. }
  1174. } else {
  1175. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1176. logsys_loggers[i].syslog_facility = facility;
  1177. }
  1178. syslog_facility_reconf();
  1179. i = 0;
  1180. }
  1181. pthread_mutex_unlock (&logsys_config_mutex);
  1182. return i;
  1183. }
  1184. int logsys_config_syslog_priority_set (
  1185. const char *subsys,
  1186. unsigned int priority)
  1187. {
  1188. int i;
  1189. pthread_mutex_lock (&logsys_config_mutex);
  1190. if (subsys != NULL) {
  1191. i = _logsys_config_subsys_get_unlocked (subsys);
  1192. if (i >= 0) {
  1193. logsys_loggers[i].syslog_priority = priority;
  1194. i = 0;
  1195. }
  1196. } else {
  1197. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1198. logsys_loggers[i].syslog_priority = priority;
  1199. }
  1200. i = 0;
  1201. }
  1202. pthread_mutex_unlock (&logsys_config_mutex);
  1203. return i;
  1204. }
  1205. int logsys_config_logfile_priority_set (
  1206. const char *subsys,
  1207. unsigned int priority)
  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].logfile_priority = priority;
  1215. i = 0;
  1216. }
  1217. } else {
  1218. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1219. logsys_loggers[i].logfile_priority = priority;
  1220. }
  1221. i = 0;
  1222. }
  1223. pthread_mutex_unlock (&logsys_config_mutex);
  1224. return i;
  1225. }
  1226. int logsys_config_debug_set (
  1227. const char *subsys,
  1228. unsigned int debug)
  1229. {
  1230. int i;
  1231. pthread_mutex_lock (&logsys_config_mutex);
  1232. if (subsys != NULL) {
  1233. i = _logsys_config_subsys_get_unlocked (subsys);
  1234. if (i >= 0) {
  1235. logsys_loggers[i].debug = debug;
  1236. i = 0;
  1237. }
  1238. } else {
  1239. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1240. logsys_loggers[i].debug = debug;
  1241. }
  1242. i = 0;
  1243. }
  1244. pthread_mutex_unlock (&logsys_config_mutex);
  1245. return i;
  1246. }
  1247. int logsys_facility_id_get (const char *name)
  1248. {
  1249. unsigned int i;
  1250. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1251. if (strcasecmp(name, facilitynames[i].c_name) == 0) {
  1252. return (facilitynames[i].c_val);
  1253. }
  1254. }
  1255. return (-1);
  1256. }
  1257. const char *logsys_facility_name_get (unsigned int facility)
  1258. {
  1259. unsigned int i;
  1260. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1261. if (facility == facilitynames[i].c_val) {
  1262. return (facilitynames[i].c_name);
  1263. }
  1264. }
  1265. return (NULL);
  1266. }
  1267. int logsys_priority_id_get (const char *name)
  1268. {
  1269. unsigned int i;
  1270. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1271. if (strcasecmp(name, prioritynames[i].c_name) == 0) {
  1272. return (prioritynames[i].c_val);
  1273. }
  1274. }
  1275. return (-1);
  1276. }
  1277. const char *logsys_priority_name_get (unsigned int priority)
  1278. {
  1279. unsigned int i;
  1280. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1281. if (priority == prioritynames[i].c_val) {
  1282. return (prioritynames[i].c_name);
  1283. }
  1284. }
  1285. return (NULL);
  1286. }
  1287. int logsys_thread_priority_set (
  1288. int policy,
  1289. const struct sched_param *param,
  1290. unsigned int after_log_ops_yield)
  1291. {
  1292. int res = 0;
  1293. #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX)
  1294. if (wthread_active == 0) {
  1295. logsys_sched_policy = policy;
  1296. memcpy(&logsys_sched_param, param, sizeof(struct sched_param));
  1297. logsys_sched_param_queued = 1;
  1298. } else {
  1299. res = pthread_setschedparam (logsys_thread_id, policy, param);
  1300. }
  1301. #endif
  1302. if (after_log_ops_yield > 0) {
  1303. logsys_after_log_ops_yield = after_log_ops_yield;
  1304. }
  1305. return (res);
  1306. }
  1307. int logsys_log_rec_store (const char *filename)
  1308. {
  1309. int fd;
  1310. ssize_t written_size;
  1311. size_t size_to_write = (flt_data_size + 2) * sizeof (unsigned int);
  1312. fd = open (filename, O_CREAT|O_RDWR, 0700);
  1313. if (fd < 0) {
  1314. return (-1);
  1315. }
  1316. written_size = write (fd, &flt_data_size, sizeof(unsigned int));
  1317. if ((written_size < 0) || (written_size != sizeof(unsigned int))) {
  1318. close (fd);
  1319. return (-1);
  1320. }
  1321. written_size = write (fd, flt_data, flt_data_size * sizeof (unsigned int));
  1322. written_size += write (fd, &flt_head, sizeof (uint32_t));
  1323. written_size += write (fd, &flt_tail, sizeof (uint32_t));
  1324. if (close (fd) != 0)
  1325. return (-1);
  1326. if (written_size < 0) {
  1327. return (-1);
  1328. } else if ((size_t)written_size != size_to_write) {
  1329. return (-1);
  1330. }
  1331. return (0);
  1332. }
  1333. void logsys_atexit (void)
  1334. {
  1335. int res;
  1336. int value;
  1337. struct record *rec;
  1338. if (wthread_active == 0) {
  1339. for (;;) {
  1340. logsys_wthread_lock();
  1341. res = sem_getvalue (&logsys_print_finished, &value);
  1342. if (value == 0) {
  1343. logsys_wthread_unlock();
  1344. return;
  1345. }
  1346. sem_wait (&logsys_print_finished);
  1347. rec = list_entry (logsys_print_finished_records.next, struct record, list);
  1348. list_del (&rec->list);
  1349. logsys_memory_used = logsys_memory_used - strlen (rec->buffer) -
  1350. sizeof (struct record) - 1;
  1351. logsys_wthread_unlock();
  1352. log_printf_to_logs (
  1353. rec->rec_ident,
  1354. rec->file_name,
  1355. rec->function_name,
  1356. rec->file_line,
  1357. rec->buffer);
  1358. free (rec->buffer);
  1359. free (rec);
  1360. }
  1361. } else {
  1362. wthread_should_exit = 1;
  1363. sem_post (&logsys_print_finished);
  1364. pthread_join (logsys_thread_id, NULL);
  1365. }
  1366. }
  1367. void logsys_flush (void)
  1368. {
  1369. }