logsys.c 38 KB

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