logsys.c 38 KB

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