logsys.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  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. memset (buf, 0, sizeof (buf));
  629. /*
  630. * Signal wthread_create that the initialization process may continue
  631. */
  632. wthread_signal ();
  633. logsys_lock();
  634. log_rec_idx = flt_data[FDTAIL_INDEX];
  635. logsys_unlock();
  636. for (;;) {
  637. wthread_wait ();
  638. /*
  639. * Read and copy the logging record index position
  640. * It may have been updated by records_reclaim if
  641. * messages were lost or or log_rec on the first new
  642. * logging record available
  643. */
  644. /*
  645. * Process any pending log messages here
  646. */
  647. for (;;) {
  648. int yield_counter = 1;
  649. logsys_lock();
  650. if (log_requests_lost > 0) {
  651. printf ("lost %d log requests\n", log_requests_lost);
  652. log_requests_pending -= log_requests_lost;
  653. log_requests_lost = 0;
  654. }
  655. if (log_requests_pending == 0) {
  656. logsys_unlock();
  657. break;
  658. }
  659. log_rec_idx = record_read (buf, log_rec_idx, &log_msg);
  660. if (log_msg) {
  661. log_requests_pending -= 1;
  662. }
  663. logsys_unlock();
  664. /*
  665. * print the stored buffer
  666. */
  667. if (log_msg) {
  668. record_print (buf);
  669. if (yield_counter++ > logsys_after_log_ops_yield) {
  670. yield_counter = 0;
  671. sched_yield ();
  672. }
  673. }
  674. }
  675. if (wthread_should_exit) {
  676. pthread_exit (NULL);
  677. }
  678. }
  679. }
  680. static void wthread_create (void)
  681. {
  682. int res;
  683. if (wthread_active) {
  684. return;
  685. }
  686. wthread_active = 1;
  687. pthread_mutex_init (&logsys_cond_mutex, NULL);
  688. pthread_cond_init (&logsys_cond, NULL);
  689. pthread_mutex_lock (&logsys_cond_mutex);
  690. /*
  691. * TODO: propagate pthread_create errors back to the caller
  692. */
  693. res = pthread_create (&logsys_thread_id, NULL,
  694. logsys_worker_thread, NULL);
  695. if (res == 0) {
  696. /*
  697. * Wait for thread to be started
  698. */
  699. wthread_wait_locked ();
  700. if (logsys_sched_param_queued == 1) {
  701. /*
  702. * TODO: propagate logsys_thread_priority_set errors back to
  703. * the caller
  704. */
  705. res = logsys_thread_priority_set (logsys_sched_policy,
  706. &logsys_sched_param,
  707. logsys_after_log_ops_yield);
  708. logsys_sched_param_queued = 0;
  709. }
  710. } else {
  711. wthread_active = 0;
  712. }
  713. }
  714. static int _logsys_config_subsys_get_unlocked (const char *subsys)
  715. {
  716. unsigned int i;
  717. if (!subsys) {
  718. return LOGSYS_MAX_SUBSYS_COUNT;
  719. }
  720. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  721. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  722. return i;
  723. }
  724. }
  725. return (-1);
  726. }
  727. static void syslog_facility_reconf (void)
  728. {
  729. closelog();
  730. openlog(logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].subsys,
  731. LOG_CONS|LOG_PID,
  732. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].syslog_facility);
  733. }
  734. /*
  735. * this is always invoked within the mutex, so it's safe to parse the
  736. * whole thing as we need.
  737. */
  738. static void logsys_close_logfile (
  739. int subsysid)
  740. {
  741. int i;
  742. if ((logsys_loggers[subsysid].logfile_fp == NULL) &&
  743. (logsys_loggers[subsysid].logfile == NULL)) {
  744. return;
  745. }
  746. /*
  747. * if there is another subsystem or system using the same fp,
  748. * then we clean our own structs, but we can't close the file
  749. * as it is in use by somebody else.
  750. * Only the last users will be allowed to perform the fclose.
  751. */
  752. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  753. if ((logsys_loggers[i].logfile_fp == logsys_loggers[subsysid].logfile_fp) &&
  754. (i != subsysid)) {
  755. logsys_loggers[subsysid].logfile = NULL;
  756. logsys_loggers[subsysid].logfile_fp = NULL;
  757. return;
  758. }
  759. }
  760. /*
  761. * if we are here, we are the last users of that fp, so we can safely
  762. * close it.
  763. */
  764. fclose (logsys_loggers[subsysid].logfile_fp);
  765. logsys_loggers[subsysid].logfile_fp = NULL;
  766. free (logsys_loggers[subsysid].logfile);
  767. logsys_loggers[subsysid].logfile = NULL;
  768. }
  769. /*
  770. * we need a version that can work when somebody else is already
  771. * holding a config mutex lock or we will never get out of here
  772. */
  773. static int logsys_config_file_set_unlocked (
  774. int subsysid,
  775. const char **error_string,
  776. const char *file)
  777. {
  778. static char error_string_response[512];
  779. int i;
  780. logsys_close_logfile(subsysid);
  781. if ((file == NULL) ||
  782. (strcmp(logsys_loggers[subsysid].subsys, "") == 0)) {
  783. return (0);
  784. }
  785. if (strlen(file) >= PATH_MAX) {
  786. snprintf (error_string_response,
  787. sizeof(error_string_response),
  788. "%s: logfile name exceed maximum system filename lenght\n",
  789. logsys_loggers[subsysid].subsys);
  790. *error_string = error_string_response;
  791. return (-1);
  792. }
  793. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  794. if ((logsys_loggers[i].logfile != NULL) &&
  795. (strcmp (logsys_loggers[i].logfile, file) == 0) &&
  796. (i != subsysid)) {
  797. logsys_loggers[subsysid].logfile =
  798. logsys_loggers[i].logfile;
  799. logsys_loggers[subsysid].logfile_fp =
  800. logsys_loggers[i].logfile_fp;
  801. return (0);
  802. }
  803. }
  804. logsys_loggers[subsysid].logfile = strdup(file);
  805. if (logsys_loggers[subsysid].logfile == NULL) {
  806. snprintf (error_string_response,
  807. sizeof(error_string_response),
  808. "Unable to allocate memory for logfile '%s'\n",
  809. file);
  810. *error_string = error_string_response;
  811. return (-1);
  812. }
  813. logsys_loggers[subsysid].logfile_fp = fopen (file, "a+");
  814. if (logsys_loggers[subsysid].logfile_fp == NULL) {
  815. char error_str[100];
  816. strerror_r (errno, error_str, 100);
  817. free(logsys_loggers[subsysid].logfile);
  818. logsys_loggers[subsysid].logfile = NULL;
  819. snprintf (error_string_response,
  820. sizeof(error_string_response),
  821. "Can't open logfile '%s' for reason (%s).\n",
  822. file, error_str);
  823. *error_string = error_string_response;
  824. return (-1);
  825. }
  826. return (0);
  827. }
  828. static void logsys_subsys_init (
  829. const char *subsys,
  830. int subsysid)
  831. {
  832. if (logsys_system_needs_init == LOGSYS_LOGGER_NEEDS_INIT) {
  833. logsys_loggers[subsysid].init_status =
  834. LOGSYS_LOGGER_NEEDS_INIT;
  835. } else {
  836. memcpy(&logsys_loggers[subsysid],
  837. &logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT],
  838. sizeof(logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT]));
  839. logsys_loggers[subsysid].init_status =
  840. LOGSYS_LOGGER_INIT_DONE;
  841. }
  842. strncpy (logsys_loggers[subsysid].subsys, subsys,
  843. LOGSYS_MAX_SUBSYS_NAMELEN);
  844. }
  845. /*
  846. * Internal API - exported
  847. */
  848. int _logsys_system_setup(
  849. const char *mainsystem,
  850. unsigned int mode,
  851. unsigned int debug,
  852. const char *logfile,
  853. int logfile_priority,
  854. int syslog_facility,
  855. int syslog_priority)
  856. {
  857. int i;
  858. const char *errstr;
  859. char tempsubsys[LOGSYS_MAX_SUBSYS_NAMELEN];
  860. if ((mainsystem == NULL) ||
  861. (strlen(mainsystem) >= LOGSYS_MAX_SUBSYS_NAMELEN)) {
  862. return -1;
  863. }
  864. i = LOGSYS_MAX_SUBSYS_COUNT;
  865. pthread_mutex_lock (&logsys_config_mutex);
  866. snprintf(logsys_loggers[i].subsys,
  867. LOGSYS_MAX_SUBSYS_NAMELEN,
  868. "%s", mainsystem);
  869. logsys_loggers[i].mode = mode;
  870. logsys_loggers[i].debug = debug;
  871. if (logsys_config_file_set_unlocked (i, &errstr, logfile) < 0) {
  872. pthread_mutex_unlock (&logsys_config_mutex);
  873. return (-1);
  874. }
  875. logsys_loggers[i].logfile_priority = logfile_priority;
  876. logsys_loggers[i].syslog_facility = syslog_facility;
  877. logsys_loggers[i].syslog_priority = syslog_priority;
  878. syslog_facility_reconf();
  879. logsys_loggers[i].init_status = LOGSYS_LOGGER_INIT_DONE;
  880. logsys_system_needs_init = LOGSYS_LOGGER_INIT_DONE;
  881. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  882. if ((strcmp (logsys_loggers[i].subsys, "") != 0) &&
  883. (logsys_loggers[i].init_status ==
  884. LOGSYS_LOGGER_NEEDS_INIT)) {
  885. strncpy (tempsubsys, logsys_loggers[i].subsys,
  886. LOGSYS_MAX_SUBSYS_NAMELEN);
  887. logsys_subsys_init(tempsubsys, i);
  888. }
  889. }
  890. pthread_mutex_unlock (&logsys_config_mutex);
  891. return (0);
  892. }
  893. unsigned int _logsys_subsys_create (const char *subsys)
  894. {
  895. int i;
  896. if ((subsys == NULL) ||
  897. (strlen(subsys) >= LOGSYS_MAX_SUBSYS_NAMELEN)) {
  898. return -1;
  899. }
  900. pthread_mutex_lock (&logsys_config_mutex);
  901. i = _logsys_config_subsys_get_unlocked (subsys);
  902. if ((i > -1) && (i < LOGSYS_MAX_SUBSYS_COUNT)) {
  903. pthread_mutex_unlock (&logsys_config_mutex);
  904. return i;
  905. }
  906. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  907. if (strcmp (logsys_loggers[i].subsys, "") == 0) {
  908. logsys_subsys_init(subsys, i);
  909. break;
  910. }
  911. }
  912. if (i >= LOGSYS_MAX_SUBSYS_COUNT) {
  913. i = -1;
  914. }
  915. pthread_mutex_unlock (&logsys_config_mutex);
  916. return i;
  917. }
  918. int _logsys_wthread_create (void)
  919. {
  920. if (((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_FORK) == 0) &&
  921. ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) != 0)) {
  922. wthread_create();
  923. }
  924. return (0);
  925. }
  926. int _logsys_rec_init (unsigned int fltsize)
  927. {
  928. /*
  929. * we need to allocate:
  930. * - requested size +
  931. * 2 extra unsigned ints for HEAD/TAIL tracking
  932. *
  933. * then round it up to the next PAGESIZE
  934. */
  935. size_t flt_real_size;
  936. /*
  937. * XXX: kill me for 1.1 because I am a dirty hack
  938. * temporary workaround that will be replaced by supporting
  939. * 0 byte size flight recorder buffer.
  940. * 0 byte size buffer will enable direct printing to logs
  941. * without flight recoder.
  942. */
  943. if (fltsize < 64000) {
  944. fltsize = 64000;
  945. }
  946. flt_real_size = ROUNDUP(
  947. (fltsize + (2 * sizeof (unsigned int))),
  948. sysconf(_SC_PAGESIZE));
  949. flt_data = malloc (flt_real_size);
  950. if (flt_data == NULL) {
  951. return (-1);
  952. }
  953. memset (flt_data, 0, flt_real_size);
  954. /*
  955. * flt_data_size tracks data by ints and not bytes/chars.
  956. *
  957. * the last 2 ints are reserved to store HEAD/TAIL information.
  958. * hide them from the rotating buffer.
  959. */
  960. flt_data_size = ((flt_real_size / sizeof (unsigned int)) - 2);
  961. /*
  962. * First record starts at zero
  963. * Last record ends at zero
  964. */
  965. flt_data[FDHEAD_INDEX] = 0;
  966. flt_data[FDTAIL_INDEX] = 0;
  967. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  968. pthread_spin_init (&logsys_idx_spinlock, 0);
  969. #endif
  970. return (0);
  971. }
  972. /*
  973. * u32 RECORD SIZE
  974. * u32 record ident
  975. * u32 arg count
  976. * u32 file line
  977. * u32 subsys length
  978. * buffer null terminated subsys
  979. * u32 filename length
  980. * buffer null terminated filename
  981. * u32 filename length
  982. * buffer null terminated function
  983. * u32 arg1 length
  984. * buffer arg1
  985. * ... repeats length & arg
  986. */
  987. void _logsys_log_rec (
  988. unsigned int rec_ident,
  989. const char *function_name,
  990. const char *file_name,
  991. int file_line,
  992. ...)
  993. {
  994. va_list ap;
  995. const void *buf_args[FDMAX_ARGS];
  996. unsigned int buf_len[FDMAX_ARGS];
  997. unsigned int i;
  998. unsigned int idx;
  999. unsigned int arguments = 0;
  1000. unsigned int record_reclaim_size = 0;
  1001. unsigned int index_start;
  1002. int words_written;
  1003. int subsysid;
  1004. subsysid = LOGSYS_DECODE_SUBSYSID(rec_ident);
  1005. /*
  1006. * Decode VA Args
  1007. */
  1008. va_start (ap, file_line);
  1009. arguments = 3;
  1010. for (;;) {
  1011. buf_args[arguments] = va_arg (ap, void *);
  1012. if (buf_args[arguments] == LOGSYS_REC_END) {
  1013. break;
  1014. }
  1015. buf_len[arguments] = va_arg (ap, int);
  1016. record_reclaim_size += ((buf_len[arguments] + 3) >> 2) + 1;
  1017. arguments++;
  1018. if (arguments >= FDMAX_ARGS) {
  1019. break;
  1020. }
  1021. }
  1022. va_end (ap);
  1023. /*
  1024. * Encode logsys subsystem identity, filename, and function
  1025. */
  1026. buf_args[0] = logsys_loggers[subsysid].subsys;
  1027. buf_len[0] = strlen (logsys_loggers[subsysid].subsys) + 1;
  1028. buf_args[1] = file_name;
  1029. buf_len[1] = strlen (file_name) + 1;
  1030. buf_args[2] = function_name;
  1031. buf_len[2] = strlen (function_name) + 1;
  1032. for (i = 0; i < 3; i++) {
  1033. record_reclaim_size += ((buf_len[i] + 3) >> 2) + 1;
  1034. }
  1035. idx = flt_data[FDHEAD_INDEX];
  1036. index_start = idx;
  1037. /*
  1038. * Reclaim data needed for record including 4 words for the header
  1039. */
  1040. records_reclaim (idx, record_reclaim_size + 4);
  1041. /*
  1042. * Write record size of zero and rest of header information
  1043. */
  1044. flt_data[idx++] = 0;
  1045. idx_word_step(idx);
  1046. flt_data[idx++] = rec_ident;
  1047. idx_word_step(idx);
  1048. flt_data[idx++] = file_line;
  1049. idx_word_step(idx);
  1050. flt_data[idx++] = records_written;
  1051. idx_word_step(idx);
  1052. /*
  1053. * Encode all of the arguments into the log message
  1054. */
  1055. for (i = 0; i < arguments; i++) {
  1056. unsigned int bytes;
  1057. unsigned int full_words;
  1058. unsigned int total_words;
  1059. bytes = buf_len[i];
  1060. full_words = bytes >> 2;
  1061. total_words = (bytes + 3) >> 2;
  1062. flt_data[idx++] = total_words;
  1063. idx_word_step(idx);
  1064. /*
  1065. * determine if this is a wrapped write or normal write
  1066. */
  1067. if (idx + total_words < flt_data_size) {
  1068. /*
  1069. * dont need to wrap buffer
  1070. */
  1071. my_memcpy_32bit (&flt_data[idx], buf_args[i], full_words);
  1072. if (bytes % 4) {
  1073. my_memcpy_8bit ((char *)&flt_data[idx + full_words],
  1074. ((const char *)buf_args[i]) + (full_words << 2), bytes % 4);
  1075. }
  1076. } else {
  1077. /*
  1078. * need to wrap buffer
  1079. */
  1080. unsigned int first;
  1081. unsigned int second;
  1082. first = flt_data_size - idx;
  1083. if (first > full_words) {
  1084. first = full_words;
  1085. }
  1086. second = full_words - first;
  1087. my_memcpy_32bit (&flt_data[idx],
  1088. (const int *)buf_args[i], first);
  1089. my_memcpy_32bit (&flt_data[0],
  1090. (const int *)(((const unsigned char *)buf_args[i]) + (first << 2)),
  1091. second);
  1092. if (bytes % 4) {
  1093. my_memcpy_8bit ((char *)&flt_data[0 + second],
  1094. ((const char *)buf_args[i]) + (full_words << 2), bytes % 4);
  1095. }
  1096. }
  1097. idx += total_words;
  1098. idx_buffer_step (idx);
  1099. }
  1100. words_written = idx - index_start;
  1101. if (words_written < 0) {
  1102. words_written += flt_data_size;
  1103. }
  1104. /*
  1105. * Commit the write of the record size now that the full record
  1106. * is in the memory buffer
  1107. */
  1108. flt_data[index_start] = words_written;
  1109. /*
  1110. * If the index of the current head equals the current log_rec_idx,
  1111. * and this is not a log_printf operation, set the log_rec_idx to
  1112. * the new head position and commit the new head.
  1113. */
  1114. logsys_lock();
  1115. if (LOGSYS_DECODE_RECID(rec_ident) == LOGSYS_RECID_LOG) {
  1116. log_requests_pending += 1;
  1117. }
  1118. if (log_requests_pending == 0) {
  1119. log_rec_idx = idx;
  1120. }
  1121. flt_data[FDHEAD_INDEX] = idx;
  1122. logsys_unlock();
  1123. records_written++;
  1124. }
  1125. void _logsys_log_vprintf (
  1126. unsigned int rec_ident,
  1127. const char *function_name,
  1128. const char *file_name,
  1129. int file_line,
  1130. const char *format,
  1131. va_list ap)
  1132. {
  1133. char logsys_print_buffer[COMBINE_BUFFER_SIZE];
  1134. unsigned int len;
  1135. unsigned int level;
  1136. int subsysid;
  1137. const char *short_file_name;
  1138. subsysid = LOGSYS_DECODE_SUBSYSID(rec_ident);
  1139. level = LOGSYS_DECODE_LEVEL(rec_ident);
  1140. if ((level > logsys_loggers[subsysid].syslog_priority) &&
  1141. (level > logsys_loggers[subsysid].logfile_priority) &&
  1142. (logsys_loggers[subsysid].debug == 0)) {
  1143. return;
  1144. }
  1145. len = vsprintf (logsys_print_buffer, format, ap);
  1146. if (logsys_print_buffer[len - 1] == '\n') {
  1147. logsys_print_buffer[len - 1] = '\0';
  1148. len -= 1;
  1149. }
  1150. #ifdef BUILDING_IN_PLACE
  1151. short_file_name = file_name;
  1152. #else
  1153. short_file_name = strrchr (file_name, '/');
  1154. if (short_file_name == NULL)
  1155. short_file_name = file_name;
  1156. else
  1157. short_file_name++; /* move past the "/" */
  1158. #endif /* BUILDING_IN_PLACE */
  1159. /*
  1160. * Create a log record
  1161. */
  1162. _logsys_log_rec (
  1163. rec_ident,
  1164. function_name,
  1165. short_file_name,
  1166. file_line,
  1167. logsys_print_buffer, len + 1,
  1168. LOGSYS_REC_END);
  1169. if ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) == 0) {
  1170. /*
  1171. * Output (and block) if the log mode is not threaded otherwise
  1172. * expect the worker thread to output the log data once signaled
  1173. */
  1174. log_printf_to_logs (rec_ident,
  1175. short_file_name, function_name, file_line,
  1176. logsys_print_buffer);
  1177. } else {
  1178. /*
  1179. * Signal worker thread to display logging output
  1180. */
  1181. wthread_signal ();
  1182. }
  1183. }
  1184. void _logsys_log_printf (
  1185. unsigned int rec_ident,
  1186. const char *function_name,
  1187. const char *file_name,
  1188. int file_line,
  1189. const char *format,
  1190. ...)
  1191. {
  1192. va_list ap;
  1193. va_start (ap, format);
  1194. _logsys_log_vprintf (rec_ident, function_name, file_name, file_line,
  1195. format, ap);
  1196. va_end (ap);
  1197. }
  1198. int _logsys_config_subsys_get (const char *subsys)
  1199. {
  1200. unsigned int i;
  1201. pthread_mutex_lock (&logsys_config_mutex);
  1202. i = _logsys_config_subsys_get_unlocked (subsys);
  1203. pthread_mutex_unlock (&logsys_config_mutex);
  1204. return i;
  1205. }
  1206. /*
  1207. * External Configuration and Initialization API
  1208. */
  1209. void logsys_fork_completed (void)
  1210. {
  1211. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode &= ~LOGSYS_MODE_FORK;
  1212. _logsys_wthread_create ();
  1213. }
  1214. unsigned int logsys_config_mode_set (const char *subsys, unsigned int mode)
  1215. {
  1216. int i;
  1217. pthread_mutex_lock (&logsys_config_mutex);
  1218. if (subsys != NULL) {
  1219. i = _logsys_config_subsys_get_unlocked (subsys);
  1220. if (i >= 0) {
  1221. logsys_loggers[i].mode = mode;
  1222. i = 0;
  1223. }
  1224. } else {
  1225. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1226. logsys_loggers[i].mode = mode;
  1227. }
  1228. i = 0;
  1229. }
  1230. pthread_mutex_unlock (&logsys_config_mutex);
  1231. return i;
  1232. }
  1233. unsigned int logsys_config_mode_get (const char *subsys)
  1234. {
  1235. int i;
  1236. i = _logsys_config_subsys_get (subsys);
  1237. if (i < 0) {
  1238. return i;
  1239. }
  1240. return logsys_loggers[i].mode;
  1241. }
  1242. int logsys_config_file_set (
  1243. const char *subsys,
  1244. const char **error_string,
  1245. const char *file)
  1246. {
  1247. int i;
  1248. int res;
  1249. pthread_mutex_lock (&logsys_config_mutex);
  1250. if (subsys != NULL) {
  1251. i = _logsys_config_subsys_get_unlocked (subsys);
  1252. if (i < 0) {
  1253. res = i;
  1254. } else {
  1255. res = logsys_config_file_set_unlocked(i, error_string, file);
  1256. }
  1257. } else {
  1258. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1259. res = logsys_config_file_set_unlocked(i, error_string, file);
  1260. if (res < 0) {
  1261. break;
  1262. }
  1263. }
  1264. }
  1265. pthread_mutex_unlock (&logsys_config_mutex);
  1266. return res;
  1267. }
  1268. int logsys_format_set (const char *format)
  1269. {
  1270. int ret = 0;
  1271. pthread_mutex_lock (&logsys_config_mutex);
  1272. if (format_buffer) {
  1273. free(format_buffer);
  1274. format_buffer = NULL;
  1275. }
  1276. format_buffer = strdup(format ? format : "%p [%6s] %b");
  1277. if (format_buffer == NULL) {
  1278. ret = -1;
  1279. }
  1280. pthread_mutex_unlock (&logsys_config_mutex);
  1281. return ret;
  1282. }
  1283. char *logsys_format_get (void)
  1284. {
  1285. return format_buffer;
  1286. }
  1287. unsigned int logsys_config_syslog_facility_set (
  1288. const char *subsys,
  1289. unsigned int facility)
  1290. {
  1291. int i;
  1292. pthread_mutex_lock (&logsys_config_mutex);
  1293. if (subsys != NULL) {
  1294. i = _logsys_config_subsys_get_unlocked (subsys);
  1295. if (i >= 0) {
  1296. logsys_loggers[i].syslog_facility = facility;
  1297. if (i == LOGSYS_MAX_SUBSYS_COUNT) {
  1298. syslog_facility_reconf();
  1299. }
  1300. i = 0;
  1301. }
  1302. } else {
  1303. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1304. logsys_loggers[i].syslog_facility = facility;
  1305. }
  1306. syslog_facility_reconf();
  1307. i = 0;
  1308. }
  1309. pthread_mutex_unlock (&logsys_config_mutex);
  1310. return i;
  1311. }
  1312. unsigned int logsys_config_syslog_priority_set (
  1313. const char *subsys,
  1314. unsigned int priority)
  1315. {
  1316. int i;
  1317. pthread_mutex_lock (&logsys_config_mutex);
  1318. if (subsys != NULL) {
  1319. i = _logsys_config_subsys_get_unlocked (subsys);
  1320. if (i >= 0) {
  1321. logsys_loggers[i].syslog_priority = priority;
  1322. i = 0;
  1323. }
  1324. } else {
  1325. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1326. logsys_loggers[i].syslog_priority = priority;
  1327. }
  1328. i = 0;
  1329. }
  1330. pthread_mutex_unlock (&logsys_config_mutex);
  1331. return i;
  1332. }
  1333. unsigned int logsys_config_logfile_priority_set (
  1334. const char *subsys,
  1335. unsigned int priority)
  1336. {
  1337. int i;
  1338. pthread_mutex_lock (&logsys_config_mutex);
  1339. if (subsys != NULL) {
  1340. i = _logsys_config_subsys_get_unlocked (subsys);
  1341. if (i >= 0) {
  1342. logsys_loggers[i].logfile_priority = priority;
  1343. i = 0;
  1344. }
  1345. } else {
  1346. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1347. logsys_loggers[i].logfile_priority = priority;
  1348. }
  1349. i = 0;
  1350. }
  1351. pthread_mutex_unlock (&logsys_config_mutex);
  1352. return i;
  1353. }
  1354. unsigned int logsys_config_debug_set (
  1355. const char *subsys,
  1356. unsigned int debug)
  1357. {
  1358. int i;
  1359. pthread_mutex_lock (&logsys_config_mutex);
  1360. if (subsys != NULL) {
  1361. i = _logsys_config_subsys_get_unlocked (subsys);
  1362. if (i >= 0) {
  1363. logsys_loggers[i].debug = debug;
  1364. i = 0;
  1365. }
  1366. } else {
  1367. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1368. logsys_loggers[i].debug = debug;
  1369. }
  1370. i = 0;
  1371. }
  1372. pthread_mutex_unlock (&logsys_config_mutex);
  1373. return i;
  1374. }
  1375. int logsys_facility_id_get (const char *name)
  1376. {
  1377. unsigned int i;
  1378. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1379. if (strcasecmp(name, facilitynames[i].c_name) == 0) {
  1380. return (facilitynames[i].c_val);
  1381. }
  1382. }
  1383. return (-1);
  1384. }
  1385. const char *logsys_facility_name_get (unsigned int facility)
  1386. {
  1387. unsigned int i;
  1388. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1389. if (facility == facilitynames[i].c_val) {
  1390. return (facilitynames[i].c_name);
  1391. }
  1392. }
  1393. return (NULL);
  1394. }
  1395. int logsys_priority_id_get (const char *name)
  1396. {
  1397. unsigned int i;
  1398. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1399. if (strcasecmp(name, prioritynames[i].c_name) == 0) {
  1400. return (prioritynames[i].c_val);
  1401. }
  1402. }
  1403. return (-1);
  1404. }
  1405. const char *logsys_priority_name_get (unsigned int priority)
  1406. {
  1407. unsigned int i;
  1408. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1409. if (priority == prioritynames[i].c_val) {
  1410. return (prioritynames[i].c_name);
  1411. }
  1412. }
  1413. return (NULL);
  1414. }
  1415. int logsys_thread_priority_set (
  1416. int policy,
  1417. const struct sched_param *param,
  1418. unsigned int after_log_ops_yield)
  1419. {
  1420. int res = 0;
  1421. #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX)
  1422. if (wthread_active == 0) {
  1423. logsys_sched_policy = policy;
  1424. memcpy(&logsys_sched_param, &param, sizeof(struct sched_param));
  1425. logsys_sched_param_queued = 1;
  1426. } else {
  1427. res = pthread_setschedparam (logsys_thread_id, policy, param);
  1428. }
  1429. #endif
  1430. if (after_log_ops_yield > 0) {
  1431. logsys_after_log_ops_yield = after_log_ops_yield;
  1432. }
  1433. return (res);
  1434. }
  1435. int logsys_log_rec_store (const char *filename)
  1436. {
  1437. int fd;
  1438. ssize_t written_size;
  1439. size_t size_to_write = (flt_data_size + 2) * sizeof (unsigned int);
  1440. fd = open (filename, O_CREAT|O_RDWR, 0700);
  1441. if (fd < 0) {
  1442. return (-1);
  1443. }
  1444. written_size = write (fd, &flt_data_size, sizeof(unsigned int));
  1445. if ((written_size < 0) || (written_size != sizeof(unsigned int))) {
  1446. close (fd);
  1447. return (-1);
  1448. }
  1449. written_size = write (fd, flt_data, size_to_write);
  1450. if (close (fd) != 0)
  1451. return (-1);
  1452. if (written_size < 0) {
  1453. return (-1);
  1454. } else if ((size_t)written_size != size_to_write) {
  1455. return (-1);
  1456. }
  1457. return (0);
  1458. }
  1459. void logsys_atexit (void)
  1460. {
  1461. if (wthread_active) {
  1462. wthread_should_exit = 1;
  1463. wthread_signal ();
  1464. pthread_join (logsys_thread_id, NULL);
  1465. }
  1466. }
  1467. void logsys_flush (void)
  1468. {
  1469. wthread_signal ();
  1470. }