logsys.c 39 KB

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