logsys.c 39 KB

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