4
0

logsys.c 38 KB

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