logsys.c 38 KB

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