logsys.c 37 KB

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