4
0

logsys.c 39 KB

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