logsys.c 38 KB

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