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