logsys.c 38 KB

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