logsys.c 39 KB

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