logsys.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 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 <stdio.h>
  39. #include <ctype.h>
  40. #include <string.h>
  41. #include <stdarg.h>
  42. #include <sys/time.h>
  43. #include <sys/stat.h>
  44. #include <fcntl.h>
  45. #include <time.h>
  46. #include <errno.h>
  47. #include <sys/types.h>
  48. #include <sys/socket.h>
  49. #include <unistd.h>
  50. #if defined(COROSYNC_LINUX)
  51. #include <linux/un.h>
  52. #endif
  53. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  54. #include <sys/un.h>
  55. #endif
  56. #include <syslog.h>
  57. #include <stdlib.h>
  58. #include <pthread.h>
  59. #include <limits.h>
  60. #include <corosync/engine/logsys.h>
  61. #define YIELD_AFTER_LOG_OPS 10
  62. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  63. /*
  64. * syslog tagnames, priorityanmes, facility names to value mapping
  65. * Some C libraries build this in to their headers, but it is non-portable
  66. * so logsys supplies its own version.
  67. */
  68. struct syslog_names {
  69. const char *c_name;
  70. int c_val;
  71. };
  72. struct syslog_names tagnames[] =
  73. {
  74. { "log", LOGSYS_TAG_LOG },
  75. { "enter", LOGSYS_TAG_ENTER },
  76. { "leave", LOGSYS_TAG_LEAVE },
  77. { "trace1", LOGSYS_TAG_TRACE1 },
  78. { "trace2", LOGSYS_TAG_TRACE2 },
  79. { "trace3", LOGSYS_TAG_TRACE3 },
  80. { "trace4", LOGSYS_TAG_TRACE4 },
  81. { "trace5", LOGSYS_TAG_TRACE5 },
  82. { "trace6", LOGSYS_TAG_TRACE6 },
  83. { "trace7", LOGSYS_TAG_TRACE7 },
  84. { "trace8", LOGSYS_TAG_TRACE8 },
  85. { NULL, -1 }
  86. };
  87. struct syslog_names prioritynames[] =
  88. {
  89. { "alert", LOG_ALERT },
  90. { "crit", LOG_CRIT },
  91. { "debug", LOG_DEBUG },
  92. { "emerg", LOG_EMERG },
  93. { "err", LOG_ERR },
  94. { "error", LOG_ERR },
  95. { "info", LOG_INFO },
  96. { "notice", LOG_NOTICE },
  97. { "warning", LOG_WARNING },
  98. { NULL, -1 }
  99. };
  100. struct syslog_names facilitynames[] =
  101. {
  102. { "auth", LOG_AUTH },
  103. { "cron", LOG_CRON },
  104. { "daemon", LOG_DAEMON },
  105. { "kern", LOG_KERN },
  106. { "lpr", LOG_LPR },
  107. { "mail", LOG_MAIL },
  108. { "news", LOG_NEWS },
  109. { "syslog", LOG_SYSLOG },
  110. { "user", LOG_USER },
  111. { "uucp", LOG_UUCP },
  112. { "local0", LOG_LOCAL0 },
  113. { "local1", LOG_LOCAL1 },
  114. { "local2", LOG_LOCAL2 },
  115. { "local3", LOG_LOCAL3 },
  116. { "local4", LOG_LOCAL4 },
  117. { "local5", LOG_LOCAL5 },
  118. { "local6", LOG_LOCAL6 },
  119. { "local7", LOG_LOCAL7 },
  120. { NULL, -1 }
  121. };
  122. /*
  123. * These are not static so they can be read from the core file
  124. */
  125. int *flt_data;
  126. int flt_data_size;
  127. #define COMBINE_BUFFER_SIZE 2048
  128. /* values for logsys_logger init_status */
  129. #define LOGSYS_LOGGER_INIT_DONE 0
  130. #define LOGSYS_LOGGER_NEEDS_INIT 1
  131. static int logsys_system_needs_init = LOGSYS_LOGGER_NEEDS_INIT;
  132. static int logsys_after_log_ops_yield = 10;
  133. /*
  134. * need unlogical order to preserve 64bit alignment
  135. */
  136. struct logsys_logger {
  137. char subsys[LOGSYS_MAX_SUBSYS_NAMELEN]; /* subsystem name */
  138. char *logfile; /* log to file */
  139. FILE *logfile_fp; /* track file descriptor */
  140. unsigned int mode; /* subsystem mode */
  141. unsigned int debug; /* debug on|off */
  142. unsigned int tags; /* trace tags */
  143. int syslog_facility; /* facility */
  144. int syslog_priority; /* priority */
  145. int logfile_priority; /* priority to file */
  146. int init_status; /* internal field to handle init queues
  147. for subsystems */
  148. };
  149. /*
  150. * operating global variables
  151. */
  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 pthread_cond_t logsys_cond;
  159. static pthread_mutex_t logsys_cond_mutex;
  160. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  161. static pthread_spinlock_t logsys_idx_spinlock;
  162. #else
  163. static pthread_mutex_t logsys_idx_mutex = PTHREAD_MUTEX_INITIALIZER;
  164. #endif
  165. static unsigned int log_rec_idx;
  166. static int logsys_buffer_full = 0;
  167. static char *format_buffer=NULL;
  168. static int log_requests_pending = 0;
  169. static int log_requests_lost = 0;
  170. void *logsys_rec_end;
  171. #define FDHEAD_INDEX (flt_data_size)
  172. #define FDTAIL_INDEX (flt_data_size + 1)
  173. /* forward declarations */
  174. static void logsys_close_logfile(int subsysid);
  175. #ifdef LOGSYS_DEBUG
  176. static char *decode_mode(int subsysid, char *buf, size_t buflen)
  177. {
  178. memset(buf, 0, buflen);
  179. if (logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_FILE)
  180. snprintf(buf+strlen(buf), buflen, "FILE,");
  181. if (logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_STDERR)
  182. snprintf(buf+strlen(buf), buflen, "STDERR,");
  183. if (logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_SYSLOG)
  184. snprintf(buf+strlen(buf), buflen, "SYSLOG,");
  185. if (subsysid == LOGSYS_MAX_SUBSYS_COUNT) {
  186. if (logsys_loggers[subsysid].mode & LOGSYS_MODE_FORK)
  187. snprintf(buf+strlen(buf), buflen, "FORK,");
  188. if (logsys_loggers[subsysid].mode & LOGSYS_MODE_THREADED)
  189. snprintf(buf+strlen(buf), buflen, "THREADED,");
  190. }
  191. memset(buf+strlen(buf)-1,0,1);
  192. return buf;
  193. }
  194. static char *decode_tags(int subsysid, char *buf, size_t buflen)
  195. {
  196. unsigned int i;
  197. memset(buf, 0, buflen);
  198. for (i = 0; tagnames[i].c_name != NULL; i++) {
  199. if (logsys_loggers[subsysid].tags & tagnames[i].c_val) {
  200. snprintf(buf+strlen(buf), buflen, "%s,", tagnames[i].c_name);
  201. }
  202. }
  203. memset(buf+strlen(buf)-1,0,1);
  204. return buf;
  205. }
  206. static const char *decode_debug(int subsysid)
  207. {
  208. if (logsys_loggers[subsysid].debug)
  209. return "on";
  210. return "off";
  211. }
  212. static const char *decode_status(int subsysid)
  213. {
  214. if (!logsys_loggers[subsysid].init_status)
  215. return "INIT_DONE";
  216. return "NEEDS_INIT";
  217. }
  218. static void dump_subsys_config(int subsysid)
  219. {
  220. char modebuf[1024];
  221. char tagbuf[1024];
  222. fprintf(stderr,
  223. "ID: %d\n"
  224. "subsys: %s\n"
  225. "logfile: %s\n"
  226. "logfile_fp: %p\n"
  227. "mode: %s\n"
  228. "debug: %s\n"
  229. "tags: %s\n"
  230. "syslog_fac: %s\n"
  231. "syslog_pri: %s\n"
  232. "logfile_pri: %s\n"
  233. "init_status: %s\n",
  234. subsysid,
  235. logsys_loggers[subsysid].subsys,
  236. logsys_loggers[subsysid].logfile,
  237. logsys_loggers[subsysid].logfile_fp,
  238. decode_mode(subsysid, modebuf, sizeof(modebuf)),
  239. decode_debug(subsysid),
  240. decode_tags(subsysid, tagbuf, sizeof(tagbuf)),
  241. logsys_facility_name_get(logsys_loggers[subsysid].syslog_facility),
  242. logsys_priority_name_get(logsys_loggers[subsysid].syslog_priority),
  243. logsys_priority_name_get(logsys_loggers[subsysid].logfile_priority),
  244. decode_status(subsysid));
  245. }
  246. static void dump_full_config(void)
  247. {
  248. int i;
  249. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  250. if (strlen(logsys_loggers[i].subsys) > 0)
  251. dump_subsys_config(i);
  252. }
  253. }
  254. #endif
  255. /*
  256. * Helpers for _logsys_log_rec functionality
  257. */
  258. static inline void my_memcpy_32bit (int *dest, const int *src, unsigned int words)
  259. {
  260. unsigned int word_idx;
  261. for (word_idx = 0; word_idx < words; word_idx++) {
  262. dest[word_idx] = src[word_idx];
  263. }
  264. }
  265. static inline void my_memcpy_8bit (char *dest, const char *src,
  266. unsigned int bytes)
  267. {
  268. unsigned int byte_idx;
  269. for (byte_idx = 0; byte_idx < bytes; byte_idx++) {
  270. dest[byte_idx] = src[byte_idx];
  271. }
  272. }
  273. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  274. static void logsys_lock (void)
  275. {
  276. pthread_spin_lock (&logsys_idx_spinlock);
  277. }
  278. static void logsys_unlock (void)
  279. {
  280. pthread_spin_unlock (&logsys_idx_spinlock);
  281. }
  282. #else
  283. static void logsys_lock (void)
  284. {
  285. pthread_mutex_lock (&logsys_idx_mutex);
  286. }
  287. static void logsys_unlock (void)
  288. {
  289. pthread_mutex_unlock (&logsys_idx_mutex);
  290. }
  291. #endif
  292. /*
  293. * Before any write operation, a reclaim on the buffer area must be executed
  294. */
  295. static inline void records_reclaim (unsigned int idx, unsigned int words)
  296. {
  297. unsigned int should_reclaim;
  298. should_reclaim = 0;
  299. if ((idx + words) >= flt_data_size) {
  300. logsys_buffer_full = 1;
  301. }
  302. if (logsys_buffer_full == 0) {
  303. return;
  304. }
  305. logsys_lock();
  306. if (flt_data[FDTAIL_INDEX] > flt_data[FDHEAD_INDEX]) {
  307. if (idx + words >= flt_data[FDTAIL_INDEX]) {
  308. should_reclaim = 1;
  309. }
  310. } else {
  311. if ((idx + words) >= (flt_data[FDTAIL_INDEX] + flt_data_size)) {
  312. should_reclaim = 1;
  313. }
  314. }
  315. if (should_reclaim) {
  316. int words_needed = 0;
  317. words_needed = words + 1;
  318. do {
  319. unsigned int old_tail;
  320. words_needed -= flt_data[flt_data[FDTAIL_INDEX]];
  321. old_tail = flt_data[FDTAIL_INDEX];
  322. flt_data[FDTAIL_INDEX] =
  323. (flt_data[FDTAIL_INDEX] +
  324. flt_data[flt_data[FDTAIL_INDEX]]) % (flt_data_size);
  325. if (log_rec_idx == old_tail) {
  326. log_requests_lost += 1;
  327. log_rec_idx = flt_data[FDTAIL_INDEX];
  328. }
  329. } while (words_needed > 0);
  330. }
  331. logsys_unlock();
  332. }
  333. #define idx_word_step(idx) \
  334. do { \
  335. if (idx > (flt_data_size - 1)) { \
  336. idx = 0; \
  337. } \
  338. } while (0);
  339. #define idx_buffer_step(idx) \
  340. do { \
  341. if (idx > (flt_data_size - 1)) { \
  342. idx = ((idx) % (flt_data_size)); \
  343. } \
  344. } while (0);
  345. /*
  346. * Internal threaded logging implementation
  347. */
  348. static inline int strcpy_cutoff (char *dest, const char *src, size_t cutoff,
  349. size_t buf_len)
  350. {
  351. size_t len = strlen (src);
  352. if (buf_len <= 1) {
  353. if (buf_len == 0)
  354. dest[0] = 0;
  355. return 0;
  356. }
  357. if (cutoff == 0) {
  358. cutoff = len;
  359. }
  360. cutoff = MIN (cutoff, buf_len - 1);
  361. len = MIN (len, cutoff);
  362. memcpy (dest, src, len);
  363. memset (dest + len, ' ', cutoff - len);
  364. dest[cutoff] = '\0';
  365. return (cutoff);
  366. }
  367. /*
  368. * %s SUBSYSTEM
  369. * %n FUNCTION NAME
  370. * %f FILENAME
  371. * %l FILELINE
  372. * %p PRIORITY
  373. * %t TIMESTAMP
  374. * %b BUFFER
  375. *
  376. * any number between % and character specify field length to pad or chop
  377. */
  378. static void log_printf_to_logs (
  379. const char *subsys,
  380. const char *file_name,
  381. const char *function_name,
  382. int file_line,
  383. unsigned int level,
  384. unsigned int tag,
  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. int c, i, has_tag = 0;
  399. subsysid = _logsys_config_subsys_get(subsys);
  400. if (subsysid <= - 1) {
  401. return;
  402. }
  403. for (i = 1; tagnames[i].c_name != NULL; i++) {
  404. if (tag & tagnames[i].c_val) {
  405. if ((logsys_loggers[subsysid].tags & tag) == 0) {
  406. return;
  407. } else {
  408. has_tag = 1;
  409. }
  410. }
  411. }
  412. while ((c = format_buffer[format_buffer_idx])) {
  413. cutoff = 0;
  414. if (c != '%') {
  415. normal_output_buffer[normal_output_buffer_idx++] = c;
  416. syslog_output_buffer[syslog_output_buffer_idx++] = c;
  417. format_buffer_idx++;
  418. } else {
  419. const char *normal_p, *syslog_p;
  420. format_buffer_idx += 1;
  421. if (isdigit (format_buffer[format_buffer_idx])) {
  422. cutoff = atoi (&format_buffer[format_buffer_idx]);
  423. }
  424. while (isdigit (format_buffer[format_buffer_idx])) {
  425. format_buffer_idx += 1;
  426. }
  427. switch (format_buffer[format_buffer_idx]) {
  428. case 's':
  429. normal_p = subsys;
  430. syslog_p = subsys;
  431. break;
  432. case 'n':
  433. normal_p = function_name;
  434. syslog_p = function_name;
  435. break;
  436. case 'f':
  437. normal_p = file_name;
  438. syslog_p = file_name;
  439. break;
  440. case 'l':
  441. sprintf (line_no, "%d", file_line);
  442. normal_p = line_no;
  443. syslog_p = line_no;
  444. break;
  445. case 't':
  446. gettimeofday (&tv, NULL);
  447. (void)strftime (char_time, sizeof (char_time), "%b %d %T", localtime ((time_t *)&tv.tv_sec));
  448. normal_p = char_time;
  449. /*
  450. * syslog does timestamping on its own.
  451. * also strip extra space in case.
  452. */
  453. syslog_p = "";
  454. break;
  455. case 'b':
  456. normal_p = buffer;
  457. syslog_p = buffer;
  458. break;
  459. case 'p':
  460. normal_p = logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].subsys;
  461. syslog_p = "";
  462. break;
  463. default:
  464. normal_p = "";
  465. syslog_p = "";
  466. break;
  467. }
  468. normal_len = strcpy_cutoff (normal_output_buffer + normal_output_buffer_idx,
  469. normal_p, cutoff,
  470. (sizeof (normal_output_buffer)
  471. - normal_output_buffer_idx));
  472. normal_output_buffer_idx += normal_len;
  473. syslog_len = strcpy_cutoff (syslog_output_buffer + syslog_output_buffer_idx,
  474. syslog_p, cutoff,
  475. (sizeof (syslog_output_buffer)
  476. - syslog_output_buffer_idx));
  477. syslog_output_buffer_idx += syslog_len;
  478. format_buffer_idx += 1;
  479. }
  480. if ((normal_output_buffer_idx == sizeof (normal_output_buffer)) ||
  481. (syslog_output_buffer_idx == sizeof (syslog_output_buffer))) {
  482. break;
  483. }
  484. }
  485. normal_output_buffer[normal_output_buffer_idx] = '\0';
  486. syslog_output_buffer[syslog_output_buffer_idx] = '\0';
  487. /*
  488. * Output to syslog
  489. */
  490. if ((logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_SYSLOG) &&
  491. ((level <= logsys_loggers[subsysid].syslog_priority) ||
  492. (logsys_loggers[subsysid].debug != 0) || (has_tag > 0))) {
  493. syslog (level | logsys_loggers[subsysid].syslog_facility, "%s", syslog_output_buffer);
  494. }
  495. /*
  496. * Terminate string with \n \0
  497. */
  498. normal_output_buffer[normal_output_buffer_idx++] = '\n';
  499. normal_output_buffer[normal_output_buffer_idx] = '\0';
  500. /*
  501. * Output to configured file
  502. */
  503. if (((logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_FILE) &&
  504. (logsys_loggers[subsysid].logfile_fp != NULL)) &&
  505. ((level <= logsys_loggers[subsysid].logfile_priority) ||
  506. (logsys_loggers[subsysid].debug != 0) || (has_tag > 0))) {
  507. /*
  508. * Output to a file
  509. */
  510. if ((fwrite (normal_output_buffer, strlen (normal_output_buffer), 1,
  511. logsys_loggers[subsysid].logfile_fp) < 1) ||
  512. (fflush (logsys_loggers[subsysid].logfile_fp) == EOF)) {
  513. char tmpbuffer[1024];
  514. /*
  515. * if we are here, it's bad.. it's really really bad.
  516. * Best thing would be to light a candle in a church
  517. * and pray.
  518. */
  519. snprintf(tmpbuffer, sizeof(tmpbuffer),
  520. "LOGSYS EMERGENCY: %s Unable to write to %s.",
  521. logsys_loggers[subsysid].subsys,
  522. logsys_loggers[subsysid].logfile);
  523. pthread_mutex_lock (&logsys_config_mutex);
  524. logsys_close_logfile(subsysid);
  525. logsys_loggers[subsysid].mode &= ~LOGSYS_MODE_OUTPUT_FILE;
  526. pthread_mutex_unlock (&logsys_config_mutex);
  527. log_printf_to_logs(logsys_loggers[subsysid].subsys,
  528. __FILE__, __FUNCTION__, __LINE__,
  529. LOGSYS_LEVEL_EMERG, 0, tmpbuffer);
  530. }
  531. }
  532. /*
  533. * Output to stderr
  534. */
  535. if ((logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_STDERR) &&
  536. ((level <= logsys_loggers[subsysid].logfile_priority) ||
  537. (logsys_loggers[subsysid].debug != 0) || (has_tag > 0))) {
  538. if (write (STDERR_FILENO, normal_output_buffer, strlen (normal_output_buffer)) < 0) {
  539. char tmpbuffer[1024];
  540. /*
  541. * if we are here, it's bad.. it's really really bad.
  542. * Best thing would be to light 20 candles for each saint
  543. * in the calendar and pray a lot...
  544. */
  545. pthread_mutex_lock (&logsys_config_mutex);
  546. logsys_loggers[subsysid].mode &= ~LOGSYS_MODE_OUTPUT_STDERR;
  547. pthread_mutex_unlock (&logsys_config_mutex);
  548. snprintf(tmpbuffer, sizeof(tmpbuffer),
  549. "LOGSYS EMERGENCY: %s Unable to write to STDERR.",
  550. logsys_loggers[subsysid].subsys);
  551. log_printf_to_logs(logsys_loggers[subsysid].subsys,
  552. __FILE__, __FUNCTION__, __LINE__,
  553. LOGSYS_LEVEL_EMERG, 0, tmpbuffer);
  554. }
  555. }
  556. }
  557. static void record_print (const char *buf)
  558. {
  559. const int *buf_uint32t = (const int *)buf;
  560. unsigned int rec_size = buf_uint32t[0];
  561. unsigned int level = buf_uint32t[1];
  562. unsigned int tag = buf_uint32t[2];
  563. unsigned int file_line = buf_uint32t[3];
  564. unsigned int i;
  565. unsigned int words_processed;
  566. unsigned int arg_size_idx;
  567. const void *arguments[64];
  568. unsigned int arg_count;
  569. arg_size_idx = 5;
  570. words_processed = 5;
  571. arg_count = 0;
  572. for (i = 0; words_processed < rec_size; i++) {
  573. arguments[arg_count++] = &buf_uint32t[arg_size_idx + 1];
  574. arg_size_idx += buf_uint32t[arg_size_idx] + 1;
  575. words_processed += buf_uint32t[arg_size_idx] + 1;
  576. }
  577. /*
  578. * (char *)arguments[0] -> subsystem
  579. * (char *)arguments[1] -> file_name
  580. * (char *)arguments[2] -> function_name
  581. * (char *)arguments[3] -> message
  582. */
  583. log_printf_to_logs (
  584. (char *)arguments[0],
  585. (char *)arguments[1],
  586. (char *)arguments[2],
  587. file_line,
  588. level,
  589. tag,
  590. (char *)arguments[3]);
  591. }
  592. static int record_read (char *buf, int rec_idx, int *log_msg) {
  593. unsigned int rec_size;
  594. unsigned int rec_ident;
  595. unsigned int tag;
  596. int firstcopy, secondcopy;
  597. rec_size = flt_data[rec_idx];
  598. rec_ident = flt_data[(rec_idx + 1) % flt_data_size];
  599. tag = flt_data[(rec_idx + 2) % flt_data_size];
  600. /*
  601. * Not a log record
  602. */
  603. if ((tag & LOGSYS_TAG_LOG) == 0) {
  604. *log_msg = 0;
  605. return ((rec_idx + rec_size) % flt_data_size);
  606. }
  607. /*
  608. * A log record
  609. */
  610. *log_msg = 1;
  611. firstcopy = rec_size;
  612. secondcopy = 0;
  613. if (firstcopy + rec_idx > flt_data_size) {
  614. firstcopy = flt_data_size - rec_idx;
  615. secondcopy -= firstcopy - rec_size;
  616. }
  617. memcpy (&buf[0], &flt_data[rec_idx], firstcopy << 2);
  618. if (secondcopy) {
  619. memcpy (&buf[(firstcopy << 2)], &flt_data[0], secondcopy << 2);
  620. }
  621. return ((rec_idx + rec_size) % flt_data_size);
  622. }
  623. static inline void wthread_signal (void)
  624. {
  625. if (wthread_active == 0) {
  626. return;
  627. }
  628. pthread_mutex_lock (&logsys_cond_mutex);
  629. pthread_cond_signal (&logsys_cond);
  630. pthread_mutex_unlock (&logsys_cond_mutex);
  631. }
  632. static inline void wthread_wait (void)
  633. {
  634. pthread_mutex_lock (&logsys_cond_mutex);
  635. pthread_cond_wait (&logsys_cond, &logsys_cond_mutex);
  636. pthread_mutex_unlock (&logsys_cond_mutex);
  637. }
  638. static inline void wthread_wait_locked (void)
  639. {
  640. pthread_cond_wait (&logsys_cond, &logsys_cond_mutex);
  641. pthread_mutex_unlock (&logsys_cond_mutex);
  642. }
  643. static void *logsys_worker_thread (void *data) __attribute__((__noreturn__));
  644. static void *logsys_worker_thread (void *data)
  645. {
  646. int log_msg;
  647. char buf[COMBINE_BUFFER_SIZE];
  648. /*
  649. * Signal wthread_create that the initialization process may continue
  650. */
  651. wthread_signal ();
  652. logsys_lock();
  653. log_rec_idx = flt_data[FDTAIL_INDEX];
  654. logsys_unlock();
  655. for (;;) {
  656. wthread_wait ();
  657. /*
  658. * Read and copy the logging record index position
  659. * It may have been updated by records_reclaim if
  660. * messages were lost or or log_rec on the first new
  661. * logging record available
  662. */
  663. /*
  664. * Process any pending log messages here
  665. */
  666. for (;;) {
  667. int yield_counter = 1;
  668. logsys_lock();
  669. if (log_requests_lost > 0) {
  670. printf ("lost %d log requests\n", log_requests_lost);
  671. log_requests_pending -= log_requests_lost;
  672. log_requests_lost = 0;
  673. }
  674. if (log_requests_pending == 0) {
  675. logsys_unlock();
  676. break;
  677. }
  678. log_rec_idx = record_read (buf, log_rec_idx, &log_msg);
  679. if (log_msg) {
  680. log_requests_pending -= 1;
  681. }
  682. logsys_unlock();
  683. /*
  684. * print the stored buffer
  685. */
  686. if (log_msg) {
  687. record_print (buf);
  688. if (yield_counter++ > logsys_after_log_ops_yield) {
  689. yield_counter = 0;
  690. sched_yield ();
  691. }
  692. }
  693. }
  694. if (wthread_should_exit) {
  695. pthread_exit (NULL);
  696. }
  697. }
  698. }
  699. static void wthread_create (void)
  700. {
  701. int res;
  702. if (wthread_active) {
  703. return;
  704. }
  705. wthread_active = 1;
  706. pthread_mutex_init (&logsys_cond_mutex, NULL);
  707. pthread_cond_init (&logsys_cond, NULL);
  708. pthread_mutex_lock (&logsys_cond_mutex);
  709. res = pthread_create (&logsys_thread_id, NULL,
  710. logsys_worker_thread, NULL);
  711. /*
  712. * Wait for thread to be started
  713. */
  714. wthread_wait_locked ();
  715. }
  716. static int _logsys_config_subsys_get_unlocked (const char *subsys)
  717. {
  718. unsigned int i;
  719. if (!subsys) {
  720. return LOGSYS_MAX_SUBSYS_COUNT;
  721. }
  722. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  723. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  724. pthread_mutex_unlock (&logsys_config_mutex);
  725. return i;
  726. }
  727. }
  728. return (-1);
  729. }
  730. static void syslog_facility_reconf (void)
  731. {
  732. closelog();
  733. openlog(logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].subsys,
  734. LOG_CONS|LOG_PID,
  735. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].syslog_facility);
  736. }
  737. /*
  738. * this is always invoked within the mutex, so it's safe to parse the
  739. * whole thing as we need.
  740. */
  741. static void logsys_close_logfile (
  742. int subsysid)
  743. {
  744. int i;
  745. if ((logsys_loggers[subsysid].logfile_fp == NULL) &&
  746. (logsys_loggers[subsysid].logfile == NULL)) {
  747. return;
  748. }
  749. /*
  750. * if there is another subsystem or system using the same fp,
  751. * then we clean our own structs, but we can't close the file
  752. * as it is in use by somebody else.
  753. * Only the last users will be allowed to perform the fclose.
  754. */
  755. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  756. if ((logsys_loggers[i].logfile_fp == logsys_loggers[subsysid].logfile_fp) &&
  757. (i != subsysid)) {
  758. logsys_loggers[subsysid].logfile = NULL;
  759. logsys_loggers[subsysid].logfile_fp = NULL;
  760. return;
  761. }
  762. }
  763. /*
  764. * if we are here, we are the last users of that fp, so we can safely
  765. * close it.
  766. */
  767. fclose (logsys_loggers[subsysid].logfile_fp);
  768. logsys_loggers[subsysid].logfile_fp = NULL;
  769. free (logsys_loggers[subsysid].logfile);
  770. logsys_loggers[subsysid].logfile = NULL;
  771. }
  772. /*
  773. * we need a version that can work when somebody else is already
  774. * holding a config mutex lock or we will never get out of here
  775. */
  776. static int logsys_config_file_set_unlocked (
  777. int subsysid,
  778. const char **error_string,
  779. const char *file)
  780. {
  781. static char error_string_response[512];
  782. int i;
  783. logsys_close_logfile(subsysid);
  784. if ((file == NULL) ||
  785. (strcmp(logsys_loggers[subsysid].subsys, "") == 0)) {
  786. return (0);
  787. }
  788. if (strlen(file) >= PATH_MAX) {
  789. snprintf (error_string_response,
  790. sizeof(error_string_response),
  791. "%s: logfile name exceed maximum system filename lenght\n",
  792. logsys_loggers[subsysid].subsys);
  793. *error_string = error_string_response;
  794. return (-1);
  795. }
  796. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  797. if ((logsys_loggers[i].logfile != NULL) &&
  798. (strcmp (logsys_loggers[i].logfile, file) == 0) &&
  799. (i != subsysid)) {
  800. logsys_loggers[subsysid].logfile =
  801. logsys_loggers[i].logfile;
  802. logsys_loggers[subsysid].logfile_fp =
  803. logsys_loggers[i].logfile_fp;
  804. return (0);
  805. }
  806. }
  807. logsys_loggers[subsysid].logfile = strdup(file);
  808. if (logsys_loggers[subsysid].logfile == NULL) {
  809. snprintf (error_string_response,
  810. sizeof(error_string_response),
  811. "Unable to allocate memory for logfile '%s'\n",
  812. file);
  813. *error_string = error_string_response;
  814. return (-1);
  815. }
  816. logsys_loggers[subsysid].logfile_fp = fopen (file, "a+");
  817. if (logsys_loggers[subsysid].logfile_fp == NULL) {
  818. free(logsys_loggers[subsysid].logfile);
  819. logsys_loggers[subsysid].logfile = NULL;
  820. snprintf (error_string_response,
  821. sizeof(error_string_response),
  822. "Can't open logfile '%s' for reason (%s).\n",
  823. file, strerror (errno));
  824. *error_string = error_string_response;
  825. return (-1);
  826. }
  827. return (0);
  828. }
  829. static void logsys_subsys_init (
  830. const char *subsys,
  831. int subsysid)
  832. {
  833. if (logsys_system_needs_init == LOGSYS_LOGGER_NEEDS_INIT) {
  834. logsys_loggers[subsysid].init_status =
  835. LOGSYS_LOGGER_NEEDS_INIT;
  836. } else {
  837. memcpy(&logsys_loggers[subsysid],
  838. &logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT],
  839. sizeof(logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT]));
  840. logsys_loggers[subsysid].init_status =
  841. LOGSYS_LOGGER_INIT_DONE;
  842. }
  843. strncpy (logsys_loggers[subsysid].subsys, subsys,
  844. LOGSYS_MAX_SUBSYS_NAMELEN);
  845. }
  846. /*
  847. * Internal API - exported
  848. */
  849. int _logsys_system_setup(
  850. const char *mainsystem,
  851. unsigned int mode,
  852. unsigned int debug,
  853. const char *logfile,
  854. int logfile_priority,
  855. int syslog_facility,
  856. int syslog_priority,
  857. unsigned int tags)
  858. {
  859. int i;
  860. const char *errstr;
  861. char tempsubsys[LOGSYS_MAX_SUBSYS_NAMELEN];
  862. if ((mainsystem == NULL) ||
  863. (strlen(mainsystem) >= LOGSYS_MAX_SUBSYS_NAMELEN)) {
  864. return -1;
  865. }
  866. i = LOGSYS_MAX_SUBSYS_COUNT;
  867. pthread_mutex_lock (&logsys_config_mutex);
  868. snprintf(logsys_loggers[i].subsys,
  869. LOGSYS_MAX_SUBSYS_NAMELEN,
  870. "%s", mainsystem);
  871. logsys_loggers[i].mode = mode;
  872. logsys_loggers[i].debug = debug;
  873. if (logsys_config_file_set_unlocked (i, &errstr, logfile) < 0) {
  874. pthread_mutex_unlock (&logsys_config_mutex);
  875. return (-1);
  876. }
  877. logsys_loggers[i].logfile_priority = logfile_priority;
  878. logsys_loggers[i].syslog_facility = syslog_facility;
  879. logsys_loggers[i].syslog_priority = syslog_priority;
  880. syslog_facility_reconf();
  881. logsys_loggers[i].tags = tags;
  882. logsys_loggers[i].init_status = LOGSYS_LOGGER_INIT_DONE;
  883. logsys_system_needs_init = LOGSYS_LOGGER_INIT_DONE;
  884. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  885. if ((strcmp (logsys_loggers[i].subsys, "") != 0) &&
  886. (logsys_loggers[i].init_status ==
  887. LOGSYS_LOGGER_NEEDS_INIT)) {
  888. strncpy (tempsubsys, logsys_loggers[i].subsys,
  889. LOGSYS_MAX_SUBSYS_NAMELEN);
  890. logsys_subsys_init(tempsubsys, i);
  891. }
  892. }
  893. pthread_mutex_unlock (&logsys_config_mutex);
  894. return (0);
  895. }
  896. unsigned int _logsys_subsys_create (const char *subsys)
  897. {
  898. int i;
  899. if ((subsys == NULL) ||
  900. (strlen(subsys) >= LOGSYS_MAX_SUBSYS_NAMELEN)) {
  901. return -1;
  902. }
  903. pthread_mutex_lock (&logsys_config_mutex);
  904. i = _logsys_config_subsys_get_unlocked (subsys);
  905. if ((i > -1) && (i < LOGSYS_MAX_SUBSYS_COUNT)) {
  906. pthread_mutex_unlock (&logsys_config_mutex);
  907. return i;
  908. }
  909. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  910. if (strcmp (logsys_loggers[i].subsys, "") == 0) {
  911. logsys_subsys_init(subsys, i);
  912. break;
  913. }
  914. }
  915. if (i >= LOGSYS_MAX_SUBSYS_COUNT) {
  916. i = -1;
  917. }
  918. pthread_mutex_unlock (&logsys_config_mutex);
  919. return i;
  920. }
  921. int _logsys_wthread_create (void)
  922. {
  923. if (((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_FORK) == 0) &&
  924. ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) != 0)) {
  925. wthread_create();
  926. atexit (logsys_atexit);
  927. }
  928. return (0);
  929. }
  930. int _logsys_rec_init (unsigned int size)
  931. {
  932. /*
  933. * First record starts at zero
  934. * Last record ends at zero
  935. */
  936. flt_data = malloc ((size + 2) * sizeof (unsigned int));
  937. if (flt_data == NULL) {
  938. return (-1);
  939. }
  940. flt_data_size = size;
  941. flt_data[FDHEAD_INDEX] = 0;
  942. flt_data[FDTAIL_INDEX] = 0;
  943. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  944. pthread_spin_init (&logsys_idx_spinlock, 0);
  945. #endif
  946. return (0);
  947. }
  948. /*
  949. * u32 RECORD SIZE
  950. * u32 record ident
  951. * u32 arg count
  952. * u32 file line
  953. * u32 subsys length
  954. * buffer null terminated subsys
  955. * u32 filename length
  956. * buffer null terminated filename
  957. * u32 filename length
  958. * buffer null terminated function
  959. * u32 arg1 length
  960. * buffer arg1
  961. * ... repeats length & arg
  962. */
  963. void _logsys_log_rec (
  964. int subsysid,
  965. const char *function_name,
  966. const char *file_name,
  967. int file_line,
  968. unsigned int rec_ident,
  969. unsigned int tag,
  970. ...)
  971. {
  972. va_list ap;
  973. const void *buf_args[64];
  974. unsigned int buf_len[64];
  975. unsigned int i;
  976. unsigned int idx;
  977. unsigned int arguments = 0;
  978. unsigned int record_reclaim_size;
  979. unsigned int index_start;
  980. int words_written;
  981. record_reclaim_size = 0;
  982. /*
  983. * Decode VA Args
  984. */
  985. va_start (ap, tag);
  986. arguments = 3;
  987. for (;;) {
  988. buf_args[arguments] = va_arg (ap, void *);
  989. if (buf_args[arguments] == LOGSYS_REC_END) {
  990. break;
  991. }
  992. buf_len[arguments] = va_arg (ap, int);
  993. record_reclaim_size += ((buf_len[arguments] + 3) >> 2) + 1;
  994. arguments++;
  995. if (arguments >= 64) {
  996. break;
  997. }
  998. }
  999. va_end (ap);
  1000. /*
  1001. * Encode logsys subsystem identity, filename, and function
  1002. */
  1003. buf_args[0] = logsys_loggers[subsysid].subsys;
  1004. buf_len[0] = strlen (logsys_loggers[subsysid].subsys) + 1;
  1005. buf_args[1] = file_name;
  1006. buf_len[1] = strlen (file_name) + 1;
  1007. buf_args[2] = function_name;
  1008. buf_len[2] = strlen (function_name) + 1;
  1009. for (i = 0; i < 3; i++) {
  1010. record_reclaim_size += ((buf_len[i] + 3) >> 2) + 1;
  1011. }
  1012. idx = flt_data[FDHEAD_INDEX];
  1013. index_start = idx;
  1014. /*
  1015. * Reclaim data needed for record including 4 words for the header
  1016. */
  1017. records_reclaim (idx, record_reclaim_size + 5);
  1018. /*
  1019. * Write record size of zero and rest of header information
  1020. */
  1021. flt_data[idx++] = 0;
  1022. idx_word_step(idx);
  1023. flt_data[idx++] = rec_ident;
  1024. idx_word_step(idx);
  1025. flt_data[idx++] = tag;
  1026. idx_word_step(idx);
  1027. flt_data[idx++] = file_line;
  1028. idx_word_step(idx);
  1029. flt_data[idx++] = records_written;
  1030. idx_word_step(idx);
  1031. /*
  1032. * Encode all of the arguments into the log message
  1033. */
  1034. for (i = 0; i < arguments; i++) {
  1035. unsigned int bytes;
  1036. unsigned int full_words;
  1037. unsigned int total_words;
  1038. bytes = buf_len[i];
  1039. full_words = bytes >> 2;
  1040. total_words = (bytes + 3) >> 2;
  1041. flt_data[idx++] = total_words;
  1042. idx_word_step(idx);
  1043. /*
  1044. * determine if this is a wrapped write or normal write
  1045. */
  1046. if (idx + total_words < flt_data_size) {
  1047. /*
  1048. * dont need to wrap buffer
  1049. */
  1050. my_memcpy_32bit (&flt_data[idx], buf_args[i], full_words);
  1051. if (bytes % 4) {
  1052. my_memcpy_8bit ((char *)&flt_data[idx + full_words],
  1053. ((const char *)buf_args[i]) + (full_words << 2), bytes % 4);
  1054. }
  1055. } else {
  1056. /*
  1057. * need to wrap buffer
  1058. */
  1059. unsigned int first;
  1060. unsigned int second;
  1061. first = flt_data_size - idx;
  1062. if (first > full_words) {
  1063. first = full_words;
  1064. }
  1065. second = full_words - first;
  1066. my_memcpy_32bit (&flt_data[idx],
  1067. (const int *)buf_args[i], first);
  1068. my_memcpy_32bit (&flt_data[0],
  1069. (const int *)(((const unsigned char *)buf_args[i]) + (first << 2)),
  1070. second);
  1071. if (bytes % 4) {
  1072. my_memcpy_8bit ((char *)&flt_data[0 + second],
  1073. ((const char *)buf_args[i]) + (full_words << 2), bytes % 4);
  1074. }
  1075. }
  1076. idx += total_words;
  1077. idx_buffer_step (idx);
  1078. }
  1079. words_written = idx - index_start;
  1080. if (words_written < 0) {
  1081. words_written += flt_data_size;
  1082. }
  1083. /*
  1084. * Commit the write of the record size now that the full record
  1085. * is in the memory buffer
  1086. */
  1087. flt_data[index_start] = words_written;
  1088. /*
  1089. * If the index of the current head equals the current log_rec_idx,
  1090. * and this is not a log_printf operation, set the log_rec_idx to
  1091. * the new head position and commit the new head.
  1092. */
  1093. logsys_lock();
  1094. if (tag & LOGSYS_TAG_LOG) {
  1095. log_requests_pending += 1;
  1096. }
  1097. if (log_requests_pending == 0) {
  1098. log_rec_idx = idx;
  1099. }
  1100. flt_data[FDHEAD_INDEX] = idx;
  1101. logsys_unlock();
  1102. records_written++;
  1103. }
  1104. void _logsys_log_vprintf (
  1105. int subsysid,
  1106. const char *function_name,
  1107. const char *file_name,
  1108. int file_line,
  1109. unsigned int level,
  1110. unsigned int tag,
  1111. const char *format,
  1112. va_list ap)
  1113. {
  1114. char logsys_print_buffer[COMBINE_BUFFER_SIZE];
  1115. unsigned int len;
  1116. if (subsysid <= -1) {
  1117. subsysid = LOGSYS_MAX_SUBSYS_COUNT;
  1118. }
  1119. if ((level > logsys_loggers[subsysid].syslog_priority) &&
  1120. (level > logsys_loggers[subsysid].logfile_priority) &&
  1121. (logsys_loggers[subsysid].debug == 0)) {
  1122. return;
  1123. }
  1124. len = vsprintf (logsys_print_buffer, format, ap);
  1125. if (logsys_print_buffer[len - 1] == '\n') {
  1126. logsys_print_buffer[len - 1] = '\0';
  1127. len -= 1;
  1128. }
  1129. /*
  1130. * Create a log record
  1131. */
  1132. _logsys_log_rec (subsysid,
  1133. function_name,
  1134. file_name,
  1135. file_line,
  1136. level,
  1137. tag |= LOGSYS_TAG_LOG,
  1138. logsys_print_buffer, len + 1,
  1139. LOGSYS_REC_END);
  1140. if ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) == 0) {
  1141. /*
  1142. * Output (and block) if the log mode is not threaded otherwise
  1143. * expect the worker thread to output the log data once signaled
  1144. */
  1145. log_printf_to_logs (logsys_loggers[subsysid].subsys,
  1146. file_name, function_name, file_line, level, tag,
  1147. logsys_print_buffer);
  1148. } else {
  1149. /*
  1150. * Signal worker thread to display logging output
  1151. */
  1152. wthread_signal ();
  1153. }
  1154. }
  1155. void _logsys_log_printf (
  1156. int subsysid,
  1157. const char *function_name,
  1158. const char *file_name,
  1159. int file_line,
  1160. unsigned int level,
  1161. unsigned int tag,
  1162. const char *format,
  1163. ...)
  1164. {
  1165. va_list ap;
  1166. va_start (ap, format);
  1167. _logsys_log_vprintf (subsysid, function_name, file_name, file_line,
  1168. level, tag, format, ap);
  1169. va_end (ap);
  1170. }
  1171. int _logsys_config_subsys_get (const char *subsys)
  1172. {
  1173. unsigned int i;
  1174. pthread_mutex_lock (&logsys_config_mutex);
  1175. i = _logsys_config_subsys_get_unlocked (subsys);
  1176. pthread_mutex_unlock (&logsys_config_mutex);
  1177. return i;
  1178. }
  1179. /*
  1180. * External Configuration and Initialization API
  1181. */
  1182. void logsys_fork_completed (void)
  1183. {
  1184. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode &= ~LOGSYS_MODE_FORK;
  1185. _logsys_wthread_create ();
  1186. }
  1187. unsigned int logsys_config_mode_set (const char *subsys, unsigned int mode)
  1188. {
  1189. int i;
  1190. pthread_mutex_lock (&logsys_config_mutex);
  1191. if (subsys != NULL) {
  1192. i = _logsys_config_subsys_get_unlocked (subsys);
  1193. if (i >= 0) {
  1194. logsys_loggers[i].mode = mode;
  1195. i = 0;
  1196. }
  1197. } else {
  1198. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1199. logsys_loggers[i].mode = mode;
  1200. }
  1201. i = 0;
  1202. }
  1203. pthread_mutex_unlock (&logsys_config_mutex);
  1204. return i;
  1205. }
  1206. unsigned int logsys_config_mode_get (const char *subsys)
  1207. {
  1208. int i;
  1209. i = _logsys_config_subsys_get (subsys);
  1210. if (i < 0) {
  1211. return i;
  1212. }
  1213. return logsys_loggers[i].mode;
  1214. }
  1215. unsigned int logsys_config_tags_set (const char *subsys, unsigned int tags)
  1216. {
  1217. int i;
  1218. pthread_mutex_lock (&logsys_config_mutex);
  1219. if (subsys != NULL) {
  1220. i = _logsys_config_subsys_get_unlocked (subsys);
  1221. if (i >= 0) {
  1222. logsys_loggers[i].tags = tags;
  1223. i = 0;
  1224. }
  1225. } else {
  1226. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1227. logsys_loggers[i].tags = tags;
  1228. }
  1229. i = 0;
  1230. }
  1231. pthread_mutex_unlock (&logsys_config_mutex);
  1232. return i;
  1233. }
  1234. unsigned int logsys_config_tags_get (const char *subsys)
  1235. {
  1236. int i;
  1237. i = _logsys_config_subsys_get (subsys);
  1238. if (i < 0) {
  1239. return i;
  1240. }
  1241. return logsys_loggers[i].tags;
  1242. }
  1243. int logsys_config_file_set (
  1244. const char *subsys,
  1245. const char **error_string,
  1246. const char *file)
  1247. {
  1248. int i;
  1249. int res;
  1250. pthread_mutex_lock (&logsys_config_mutex);
  1251. if (subsys != NULL) {
  1252. i = _logsys_config_subsys_get_unlocked (subsys);
  1253. if (i < 0) {
  1254. res = i;
  1255. } else {
  1256. res = logsys_config_file_set_unlocked(i, error_string, file);
  1257. }
  1258. } else {
  1259. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1260. res = logsys_config_file_set_unlocked(i, error_string, file);
  1261. if (res < 0) {
  1262. break;
  1263. }
  1264. }
  1265. }
  1266. pthread_mutex_unlock (&logsys_config_mutex);
  1267. return res;
  1268. }
  1269. int logsys_format_set (const char *format)
  1270. {
  1271. int ret = 0;
  1272. pthread_mutex_lock (&logsys_config_mutex);
  1273. if (format_buffer) {
  1274. free(format_buffer);
  1275. format_buffer = NULL;
  1276. }
  1277. format_buffer = strdup(format ? format : "%p [%6s] %b");
  1278. if (format_buffer == NULL) {
  1279. ret = -1;
  1280. }
  1281. pthread_mutex_unlock (&logsys_config_mutex);
  1282. return ret;
  1283. }
  1284. char *logsys_format_get (void)
  1285. {
  1286. return format_buffer;
  1287. }
  1288. unsigned int logsys_config_syslog_facility_set (
  1289. const char *subsys,
  1290. unsigned int facility)
  1291. {
  1292. int i;
  1293. pthread_mutex_lock (&logsys_config_mutex);
  1294. if (subsys != NULL) {
  1295. i = _logsys_config_subsys_get_unlocked (subsys);
  1296. if (i >= 0) {
  1297. logsys_loggers[i].syslog_facility = facility;
  1298. if (i == LOGSYS_MAX_SUBSYS_COUNT) {
  1299. syslog_facility_reconf();
  1300. }
  1301. i = 0;
  1302. }
  1303. } else {
  1304. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1305. logsys_loggers[i].syslog_facility = facility;
  1306. }
  1307. syslog_facility_reconf();
  1308. i = 0;
  1309. }
  1310. pthread_mutex_unlock (&logsys_config_mutex);
  1311. return i;
  1312. }
  1313. unsigned int logsys_config_syslog_priority_set (
  1314. const char *subsys,
  1315. unsigned int priority)
  1316. {
  1317. int i;
  1318. pthread_mutex_lock (&logsys_config_mutex);
  1319. if (subsys != NULL) {
  1320. i = _logsys_config_subsys_get_unlocked (subsys);
  1321. if (i >= 0) {
  1322. logsys_loggers[i].syslog_priority = priority;
  1323. i = 0;
  1324. }
  1325. } else {
  1326. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1327. logsys_loggers[i].syslog_priority = priority;
  1328. }
  1329. i = 0;
  1330. }
  1331. pthread_mutex_unlock (&logsys_config_mutex);
  1332. return i;
  1333. }
  1334. unsigned int logsys_config_logfile_priority_set (
  1335. const char *subsys,
  1336. unsigned int priority)
  1337. {
  1338. int i;
  1339. pthread_mutex_lock (&logsys_config_mutex);
  1340. if (subsys != NULL) {
  1341. i = _logsys_config_subsys_get_unlocked (subsys);
  1342. if (i >= 0) {
  1343. logsys_loggers[i].logfile_priority = priority;
  1344. i = 0;
  1345. }
  1346. } else {
  1347. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1348. logsys_loggers[i].logfile_priority = priority;
  1349. }
  1350. i = 0;
  1351. }
  1352. pthread_mutex_unlock (&logsys_config_mutex);
  1353. return i;
  1354. }
  1355. unsigned int logsys_config_debug_set (
  1356. const char *subsys,
  1357. unsigned int debug)
  1358. {
  1359. int i;
  1360. pthread_mutex_lock (&logsys_config_mutex);
  1361. if (subsys != NULL) {
  1362. i = _logsys_config_subsys_get_unlocked (subsys);
  1363. if (i >= 0) {
  1364. logsys_loggers[i].debug = debug;
  1365. i = 0;
  1366. }
  1367. } else {
  1368. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1369. logsys_loggers[i].debug = debug;
  1370. }
  1371. i = 0;
  1372. }
  1373. pthread_mutex_unlock (&logsys_config_mutex);
  1374. return i;
  1375. }
  1376. int logsys_facility_id_get (const char *name)
  1377. {
  1378. unsigned int i;
  1379. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1380. if (strcasecmp(name, facilitynames[i].c_name) == 0) {
  1381. return (facilitynames[i].c_val);
  1382. }
  1383. }
  1384. return (-1);
  1385. }
  1386. const char *logsys_facility_name_get (unsigned int facility)
  1387. {
  1388. unsigned int i;
  1389. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1390. if (facility == facilitynames[i].c_val) {
  1391. return (facilitynames[i].c_name);
  1392. }
  1393. }
  1394. return (NULL);
  1395. }
  1396. int logsys_priority_id_get (const char *name)
  1397. {
  1398. unsigned int i;
  1399. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1400. if (strcasecmp(name, prioritynames[i].c_name) == 0) {
  1401. return (prioritynames[i].c_val);
  1402. }
  1403. }
  1404. return (-1);
  1405. }
  1406. const char *logsys_priority_name_get (unsigned int priority)
  1407. {
  1408. unsigned int i;
  1409. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1410. if (priority == prioritynames[i].c_val) {
  1411. return (prioritynames[i].c_name);
  1412. }
  1413. }
  1414. return (NULL);
  1415. }
  1416. int logsys_tag_id_get (const char *name)
  1417. {
  1418. unsigned int i;
  1419. for (i = 0; tagnames[i].c_name != NULL; i++) {
  1420. if (strcasecmp(name, tagnames[i].c_name) == 0) {
  1421. return (tagnames[i].c_val);
  1422. }
  1423. }
  1424. return (-1);
  1425. }
  1426. const char *logsys_tag_name_get (unsigned int tag)
  1427. {
  1428. unsigned int i;
  1429. for (i = 0; tagnames[i].c_name != NULL; i++) {
  1430. if (tag == tagnames[i].c_val) {
  1431. return (tagnames[i].c_name);
  1432. }
  1433. }
  1434. return (NULL);
  1435. }
  1436. int logsys_thread_priority_set (
  1437. int policy,
  1438. const struct sched_param *param,
  1439. unsigned int after_log_ops_yield)
  1440. {
  1441. int res = 0;
  1442. if (policy != SCHED_OTHER) {
  1443. res = pthread_setschedparam (logsys_thread_id, policy, param);
  1444. }
  1445. if (after_log_ops_yield > 0) {
  1446. logsys_after_log_ops_yield = after_log_ops_yield;
  1447. }
  1448. return (res);
  1449. }
  1450. int logsys_log_rec_store (const char *filename)
  1451. {
  1452. int fd;
  1453. ssize_t written_size;
  1454. size_t size_to_write = (flt_data_size + 2) * sizeof (unsigned int);
  1455. fd = open (filename, O_CREAT|O_RDWR, 0700);
  1456. if (fd < 0) {
  1457. return (-1);
  1458. }
  1459. written_size = write (fd, flt_data, size_to_write);
  1460. if (close (fd) != 0)
  1461. return (-1);
  1462. if (written_size < 0) {
  1463. return (-1);
  1464. } else if ((size_t)written_size != size_to_write) {
  1465. return (-1);
  1466. }
  1467. return (0);
  1468. }
  1469. void logsys_atexit (void)
  1470. {
  1471. if (wthread_active) {
  1472. wthread_should_exit = 1;
  1473. wthread_signal ();
  1474. pthread_join (logsys_thread_id, NULL);
  1475. }
  1476. }
  1477. void logsys_flush (void)
  1478. {
  1479. wthread_signal ();
  1480. }