logsys.c 39 KB

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