logsys.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 Red Hat, Inc.
  4. *
  5. * Author: Steven Dake (sdake@redhat.com)
  6. * Author: Lon Hohberger (lhh@redhat.com)
  7. * Author: Fabio M. Di Nitto (fdinitto@redhat.com)
  8. *
  9. * All rights reserved.
  10. *
  11. * This software licensed under BSD license, the text of which follows:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * - Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  22. * contributors may be used to endorse or promote products derived from this
  23. * software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  35. * THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <config.h>
  38. #include <assert.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. #define SYSLOG_NAMES
  58. #include <syslog.h>
  59. #include <stdlib.h>
  60. #include <pthread.h>
  61. #include <corosync/engine/logsys.h>
  62. /*
  63. * similar to syslog facilities/priorities tables,
  64. * make a tag table for internal use
  65. */
  66. #ifdef SYSLOG_NAMES
  67. struct syslog_names {
  68. const char *c_name;
  69. int c_val;
  70. };
  71. struct syslog_names tagnames[] =
  72. {
  73. { "log", LOGSYS_TAG_LOG },
  74. { "enter", LOGSYS_TAG_ENTER },
  75. { "leave", LOGSYS_TAG_LEAVE },
  76. { "trace1", LOGSYS_TAG_TRACE1 },
  77. { "trace2", LOGSYS_TAG_TRACE2 },
  78. { "trace3", LOGSYS_TAG_TRACE3 },
  79. { "trace4", LOGSYS_TAG_TRACE4 },
  80. { "trace5", LOGSYS_TAG_TRACE5 },
  81. { "trace6", LOGSYS_TAG_TRACE6 },
  82. { "trace7", LOGSYS_TAG_TRACE7 },
  83. { "trace8", LOGSYS_TAG_TRACE8 },
  84. { NULL, -1 }
  85. };
  86. #endif
  87. /*
  88. * These are not static so they can be read from the core file
  89. */
  90. int *flt_data;
  91. int flt_data_size;
  92. #define COMBINE_BUFFER_SIZE 2048
  93. /* values for logsys_logger init_status */
  94. #define LOGSYS_LOGGER_INIT_DONE 0
  95. #define LOGSYS_LOGGER_NEEDS_INIT 1
  96. static int logsys_system_needs_init = LOGSYS_LOGGER_NEEDS_INIT;
  97. /*
  98. * need unlogical order to preserve 64bit alignment
  99. */
  100. struct logsys_logger {
  101. char subsys[LOGSYS_MAX_SUBSYS_NAMELEN]; /* subsystem name */
  102. char *logfile; /* log to file */
  103. FILE *logfile_fp; /* track file descriptor */
  104. unsigned int mode; /* subsystem mode */
  105. unsigned int debug; /* debug on|off */
  106. unsigned int tags; /* trace tags */
  107. int syslog_facility; /* facility */
  108. int syslog_priority; /* priority */
  109. int logfile_priority; /* priority to file */
  110. int init_status; /* internal field to handle init queues
  111. for subsystems */
  112. };
  113. /*
  114. * operating global variables
  115. */
  116. static struct logsys_logger logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT + 1];
  117. static int wthread_active = 0;
  118. static int wthread_should_exit = 0;
  119. static pthread_mutex_t logsys_config_mutex = PTHREAD_MUTEX_INITIALIZER;
  120. static unsigned int records_written = 1;
  121. static pthread_t logsys_thread_id;
  122. static pthread_cond_t logsys_cond;
  123. static pthread_mutex_t logsys_cond_mutex;
  124. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  125. static pthread_spinlock_t logsys_idx_spinlock;
  126. #else
  127. static pthread_mutex_t logsys_idx_mutex = PTHREAD_MUTEX_INITIALIZER;
  128. #endif
  129. static unsigned int log_rec_idx;
  130. static int logsys_buffer_full = 0;
  131. static char *format_buffer=NULL;
  132. static int log_requests_pending = 0;
  133. static int log_requests_lost = 0;
  134. void *logsys_rec_end;
  135. #define FDHEAD_INDEX (flt_data_size)
  136. #define FDTAIL_INDEX (flt_data_size + 1)
  137. /* forward declarations */
  138. static void logsys_close_logfile(int subsysid);
  139. /*
  140. * Helpers for _logsys_log_rec functionality
  141. */
  142. static inline void my_memcpy_32bit (int *dest, const int *src, unsigned int words)
  143. {
  144. unsigned int word_idx;
  145. for (word_idx = 0; word_idx < words; word_idx++) {
  146. dest[word_idx] = src[word_idx];
  147. }
  148. }
  149. static inline void my_memcpy_8bit (char *dest, const char *src,
  150. unsigned int bytes)
  151. {
  152. unsigned int byte_idx;
  153. for (byte_idx = 0; byte_idx < bytes; byte_idx++) {
  154. dest[byte_idx] = src[byte_idx];
  155. }
  156. }
  157. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  158. static void logsys_lock (void)
  159. {
  160. pthread_spin_lock (&logsys_idx_spinlock);
  161. }
  162. static void logsys_unlock (void)
  163. {
  164. pthread_spin_unlock (&logsys_idx_spinlock);
  165. }
  166. #else
  167. static void logsys_lock (void)
  168. {
  169. pthread_mutex_lock (&logsys_idx_mutex);
  170. }
  171. static void logsys_unlock (void)
  172. {
  173. pthread_mutex_unlock (&logsys_idx_mutex);
  174. }
  175. #endif
  176. /*
  177. * Before any write operation, a reclaim on the buffer area must be executed
  178. */
  179. static inline void records_reclaim (unsigned int idx, unsigned int words)
  180. {
  181. unsigned int should_reclaim;
  182. should_reclaim = 0;
  183. if ((idx + words) >= flt_data_size) {
  184. logsys_buffer_full = 1;
  185. }
  186. if (logsys_buffer_full == 0) {
  187. return;
  188. }
  189. logsys_lock();
  190. if (flt_data[FDTAIL_INDEX] > flt_data[FDHEAD_INDEX]) {
  191. if (idx + words >= flt_data[FDTAIL_INDEX]) {
  192. should_reclaim = 1;
  193. }
  194. } else {
  195. if ((idx + words) >= (flt_data[FDTAIL_INDEX] + flt_data_size)) {
  196. should_reclaim = 1;
  197. }
  198. }
  199. if (should_reclaim) {
  200. int words_needed = 0;
  201. words_needed = words + 1;
  202. do {
  203. unsigned int old_tail;
  204. words_needed -= flt_data[flt_data[FDTAIL_INDEX]];
  205. old_tail = flt_data[FDTAIL_INDEX];
  206. flt_data[FDTAIL_INDEX] =
  207. (flt_data[FDTAIL_INDEX] +
  208. flt_data[flt_data[FDTAIL_INDEX]]) % (flt_data_size);
  209. if (log_rec_idx == old_tail) {
  210. log_requests_lost += 1;
  211. log_rec_idx = flt_data[FDTAIL_INDEX];
  212. }
  213. } while (words_needed > 0);
  214. }
  215. logsys_unlock();
  216. }
  217. #define idx_word_step(idx) \
  218. do { \
  219. if (idx > (flt_data_size - 1)) { \
  220. idx = 0; \
  221. } \
  222. } while (0);
  223. #define idx_buffer_step(idx) \
  224. do { \
  225. if (idx > (flt_data_size - 1)) { \
  226. idx = ((idx) % (flt_data_size)); \
  227. } \
  228. } while (0);
  229. /*
  230. * Internal threaded logging implementation
  231. */
  232. static inline int strcpy_cutoff (char *dest, const char *src, int cutoff)
  233. {
  234. unsigned int len;
  235. if (cutoff == -1) {
  236. strcpy (dest, src);
  237. return (strlen (dest));
  238. } else {
  239. assert (cutoff > 0);
  240. strncpy (dest, src, cutoff);
  241. dest[cutoff] = '\0';
  242. len = strlen (dest);
  243. if (len != cutoff) {
  244. memset (&dest[len], ' ', cutoff - len);
  245. }
  246. }
  247. return (cutoff);
  248. }
  249. /*
  250. * %s SUBSYSTEM
  251. * %n FUNCTION NAME
  252. * %f FILENAME
  253. * %l FILELINE
  254. * %p PRIORITY
  255. * %t TIMESTAMP
  256. * %b BUFFER
  257. *
  258. * any number between % and character specify field length to pad or chop
  259. */
  260. static void log_printf_to_logs (
  261. const char *subsys,
  262. const char *file_name,
  263. const char *function_name,
  264. int file_line,
  265. unsigned int level,
  266. const char *buffer)
  267. {
  268. char output_buffer[COMBINE_BUFFER_SIZE];
  269. char char_time[128];
  270. char line_no[30];
  271. unsigned int format_buffer_idx = 0;
  272. unsigned int output_buffer_idx = 0;
  273. struct timeval tv;
  274. int cutoff;
  275. unsigned int len;
  276. int subsysid;
  277. subsysid = _logsys_config_subsys_get(subsys);
  278. if (subsysid <= - 1) {
  279. return;
  280. }
  281. while (format_buffer[format_buffer_idx]) {
  282. cutoff = -1;
  283. if (format_buffer[format_buffer_idx] == '%') {
  284. format_buffer_idx += 1;
  285. if (isdigit (format_buffer[format_buffer_idx])) {
  286. cutoff = atoi (&format_buffer[format_buffer_idx]);
  287. }
  288. while (isdigit (format_buffer[format_buffer_idx])) {
  289. format_buffer_idx += 1;
  290. }
  291. switch (format_buffer[format_buffer_idx]) {
  292. case 's':
  293. len = strcpy_cutoff (&output_buffer[output_buffer_idx], subsys, cutoff);
  294. output_buffer_idx += len;
  295. break;
  296. case 'n':
  297. len = strcpy_cutoff (&output_buffer[output_buffer_idx], function_name, cutoff);
  298. output_buffer_idx += len;
  299. break;
  300. case 'f':
  301. len = strcpy_cutoff (&output_buffer[output_buffer_idx], file_name, cutoff);
  302. output_buffer_idx += len;
  303. break;
  304. case 'l':
  305. sprintf (line_no, "%d", file_line);
  306. len = strcpy_cutoff (&output_buffer[output_buffer_idx], line_no, cutoff);
  307. output_buffer_idx += len;
  308. break;
  309. case 'p':
  310. break;
  311. case 't':
  312. gettimeofday (&tv, NULL);
  313. (void)strftime (char_time, sizeof (char_time), "%b %e %k:%M:%S", localtime ((time_t *)&tv.tv_sec));
  314. len = strcpy_cutoff (&output_buffer[output_buffer_idx], char_time, cutoff);
  315. output_buffer_idx += len;
  316. break;
  317. case 'b':
  318. len = strcpy_cutoff (&output_buffer[output_buffer_idx], buffer, cutoff);
  319. output_buffer_idx += len;
  320. break;
  321. }
  322. format_buffer_idx += 1;
  323. } else {
  324. output_buffer[output_buffer_idx++] = format_buffer[format_buffer_idx++];
  325. }
  326. }
  327. output_buffer[output_buffer_idx] = '\0';
  328. /*
  329. * Output to syslog
  330. */
  331. if ((logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_SYSLOG) &&
  332. ((level <= logsys_loggers[subsysid].syslog_priority) ||
  333. (logsys_loggers[subsysid].debug != 0))) {
  334. syslog (level | logsys_loggers[subsysid].syslog_facility, "%s", output_buffer);
  335. }
  336. /*
  337. * Terminate string with \n \0
  338. */
  339. output_buffer[output_buffer_idx++] = '\n';
  340. output_buffer[output_buffer_idx] = '\0';
  341. /*
  342. * Output to configured file
  343. */
  344. if (((logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_FILE) &&
  345. (logsys_loggers[subsysid].logfile_fp != NULL)) &&
  346. ((level <= logsys_loggers[subsysid].logfile_priority) ||
  347. (logsys_loggers[subsysid].debug != 0))) {
  348. /*
  349. * Output to a file
  350. */
  351. if ((fwrite (output_buffer, strlen (output_buffer), 1,
  352. logsys_loggers[subsysid].logfile_fp) < 1) ||
  353. (fflush (logsys_loggers[subsysid].logfile_fp) == EOF)) {
  354. char tmpbuffer[1024];
  355. /*
  356. * if we are here, it's bad.. it's really really bad.
  357. * Best thing would be to light a candle in a church
  358. * and pray.
  359. */
  360. snprintf(tmpbuffer, sizeof(tmpbuffer),
  361. "LOGSYS EMERGENCY: %s Unable to write to %s.",
  362. logsys_loggers[subsysid].subsys,
  363. logsys_loggers[subsysid].logfile);
  364. pthread_mutex_lock (&logsys_config_mutex);
  365. logsys_close_logfile(subsysid);
  366. logsys_loggers[subsysid].mode &= ~LOGSYS_MODE_OUTPUT_FILE;
  367. pthread_mutex_unlock (&logsys_config_mutex);
  368. log_printf_to_logs(logsys_loggers[subsysid].subsys,
  369. __FILE__, __FUNCTION__, __LINE__,
  370. LOGSYS_LEVEL_EMERG, tmpbuffer);
  371. }
  372. }
  373. /*
  374. * Output to stderr
  375. */
  376. if ((logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_STDERR) &&
  377. ((level <= logsys_loggers[subsysid].logfile_priority) ||
  378. (logsys_loggers[subsysid].debug != 0))) {
  379. if (write (STDERR_FILENO, output_buffer, strlen (output_buffer)) < 0) {
  380. char tmpbuffer[1024];
  381. /*
  382. * if we are here, it's bad.. it's really really bad.
  383. * Best thing would be to light 20 candles for each saint
  384. * in the calendar and pray a lot...
  385. */
  386. pthread_mutex_lock (&logsys_config_mutex);
  387. logsys_loggers[subsysid].mode &= ~LOGSYS_MODE_OUTPUT_STDERR;
  388. pthread_mutex_unlock (&logsys_config_mutex);
  389. snprintf(tmpbuffer, sizeof(tmpbuffer),
  390. "LOGSYS EMERGENCY: %s Unable to write to STDERR.",
  391. logsys_loggers[subsysid].subsys);
  392. log_printf_to_logs(logsys_loggers[subsysid].subsys,
  393. __FILE__, __FUNCTION__, __LINE__,
  394. LOGSYS_LEVEL_EMERG, tmpbuffer);
  395. }
  396. }
  397. }
  398. static void record_print (const char *buf)
  399. {
  400. const int *buf_uint32t = (const int *)buf;
  401. unsigned int rec_size = buf_uint32t[0];
  402. unsigned int rec_ident = buf_uint32t[1];
  403. unsigned int file_line = buf_uint32t[2];
  404. unsigned int level = rec_ident >> 28;
  405. unsigned int i;
  406. unsigned int words_processed;
  407. unsigned int arg_size_idx;
  408. const void *arguments[64];
  409. unsigned int arg_count;
  410. arg_size_idx = 4;
  411. words_processed = 4;
  412. arg_count = 0;
  413. for (i = 0; words_processed < rec_size; i++) {
  414. arguments[arg_count++] = &buf_uint32t[arg_size_idx + 1];
  415. arg_size_idx += buf_uint32t[arg_size_idx] + 1;
  416. words_processed += buf_uint32t[arg_size_idx] + 1;
  417. }
  418. /*
  419. * (char *)arguments[0] -> subsystem
  420. * (char *)arguments[1] -> file_name
  421. * (char *)arguments[2] -> function_name
  422. * (char *)arguments[3] -> message
  423. */
  424. log_printf_to_logs (
  425. (char *)arguments[0],
  426. (char *)arguments[1],
  427. (char *)arguments[2],
  428. file_line,
  429. (level-1),
  430. (char *)arguments[3]);
  431. }
  432. static int record_read (char *buf, int rec_idx, int *log_msg) {
  433. unsigned int rec_size;
  434. unsigned int rec_ident;
  435. int firstcopy, secondcopy;
  436. rec_size = flt_data[rec_idx];
  437. rec_ident = flt_data[(rec_idx + 1) % flt_data_size];
  438. /*
  439. * Not a log record
  440. */
  441. if ((rec_ident & LOGSYS_TAG_LOG) == 0) {
  442. *log_msg = 0;
  443. return ((rec_idx + rec_size) % flt_data_size);
  444. }
  445. /*
  446. * A log record
  447. */
  448. *log_msg = 1;
  449. firstcopy = rec_size;
  450. secondcopy = 0;
  451. if (firstcopy + rec_idx > flt_data_size) {
  452. firstcopy = flt_data_size - rec_idx;
  453. secondcopy -= firstcopy - rec_size;
  454. }
  455. memcpy (&buf[0], &flt_data[rec_idx], firstcopy << 2);
  456. if (secondcopy) {
  457. memcpy (&buf[(firstcopy << 2)], &flt_data[0], secondcopy << 2);
  458. }
  459. return ((rec_idx + rec_size) % flt_data_size);
  460. }
  461. static inline void wthread_signal (void)
  462. {
  463. if (wthread_active == 0) {
  464. return;
  465. }
  466. pthread_mutex_lock (&logsys_cond_mutex);
  467. pthread_cond_signal (&logsys_cond);
  468. pthread_mutex_unlock (&logsys_cond_mutex);
  469. }
  470. static inline void wthread_wait (void)
  471. {
  472. pthread_mutex_lock (&logsys_cond_mutex);
  473. pthread_cond_wait (&logsys_cond, &logsys_cond_mutex);
  474. pthread_mutex_unlock (&logsys_cond_mutex);
  475. }
  476. static inline void wthread_wait_locked (void)
  477. {
  478. pthread_cond_wait (&logsys_cond, &logsys_cond_mutex);
  479. pthread_mutex_unlock (&logsys_cond_mutex);
  480. }
  481. static void *logsys_worker_thread (void *data) __attribute__((__noreturn__));
  482. static void *logsys_worker_thread (void *data)
  483. {
  484. int log_msg;
  485. char buf[COMBINE_BUFFER_SIZE];
  486. /*
  487. * Signal wthread_create that the initialization process may continue
  488. */
  489. wthread_signal ();
  490. logsys_lock();
  491. log_rec_idx = flt_data[FDTAIL_INDEX];
  492. logsys_unlock();
  493. for (;;) {
  494. wthread_wait ();
  495. /*
  496. * Read and copy the logging record index position
  497. * It may have been updated by records_reclaim if
  498. * messages were lost or or log_rec on the first new
  499. * logging record available
  500. */
  501. /*
  502. * Process any pending log messages here
  503. */
  504. for (;;) {
  505. logsys_lock();
  506. if (log_requests_lost > 0) {
  507. printf ("lost %d log requests\n", log_requests_lost);
  508. log_requests_pending -= log_requests_lost;
  509. log_requests_lost = 0;
  510. }
  511. if (log_requests_pending == 0) {
  512. logsys_unlock();
  513. break;
  514. }
  515. log_rec_idx = record_read (buf, log_rec_idx, &log_msg);
  516. if (log_msg) {
  517. log_requests_pending -= 1;
  518. }
  519. logsys_unlock();
  520. /*
  521. * print the stored buffer
  522. */
  523. if (log_msg) {
  524. record_print (buf);
  525. }
  526. }
  527. if (wthread_should_exit) {
  528. pthread_exit (NULL);
  529. }
  530. }
  531. }
  532. static void wthread_create (void)
  533. {
  534. int res;
  535. if (wthread_active) {
  536. return;
  537. }
  538. wthread_active = 1;
  539. pthread_mutex_init (&logsys_cond_mutex, NULL);
  540. pthread_cond_init (&logsys_cond, NULL);
  541. pthread_mutex_lock (&logsys_cond_mutex);
  542. res = pthread_create (&logsys_thread_id, NULL,
  543. logsys_worker_thread, NULL);
  544. /*
  545. * Wait for thread to be started
  546. */
  547. wthread_wait_locked ();
  548. }
  549. static int _logsys_config_subsys_get_unlocked (const char *subsys)
  550. {
  551. unsigned int i;
  552. if (!subsys) {
  553. return LOGSYS_MAX_SUBSYS_COUNT;
  554. }
  555. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  556. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  557. pthread_mutex_unlock (&logsys_config_mutex);
  558. return i;
  559. }
  560. }
  561. return (-1);
  562. }
  563. static void syslog_facility_reconf (void)
  564. {
  565. closelog();
  566. openlog(logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].subsys,
  567. LOG_CONS|LOG_PID,
  568. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].syslog_facility);
  569. }
  570. /*
  571. * this is always invoked within the mutex, so it's safe to parse the
  572. * whole thing as we need.
  573. */
  574. static void logsys_close_logfile (
  575. int subsysid)
  576. {
  577. int i;
  578. if ((logsys_loggers[subsysid].logfile_fp == NULL) &&
  579. (logsys_loggers[subsysid].logfile == NULL)) {
  580. return;
  581. }
  582. /*
  583. * if there is another subsystem or system using the same fp,
  584. * then we clean our own structs, but we can't close the file
  585. * as it is in use by somebody else.
  586. * Only the last users will be allowed to perform the fclose.
  587. */
  588. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  589. if ((logsys_loggers[i].logfile_fp == logsys_loggers[subsysid].logfile_fp) &&
  590. (i != subsysid)) {
  591. logsys_loggers[subsysid].logfile = NULL;
  592. logsys_loggers[subsysid].logfile_fp = NULL;
  593. return;
  594. }
  595. }
  596. /*
  597. * if we are here, we are the last users of that fp, so we can safely
  598. * close it.
  599. */
  600. fclose (logsys_loggers[subsysid].logfile_fp);
  601. logsys_loggers[subsysid].logfile_fp = NULL;
  602. free (logsys_loggers[subsysid].logfile);
  603. logsys_loggers[subsysid].logfile = NULL;
  604. }
  605. /*
  606. * we need a version that can work when somebody else is already
  607. * holding a config mutex lock or we will never get out of here
  608. */
  609. static int logsys_config_file_set_unlocked (
  610. int subsysid,
  611. const char **error_string,
  612. const char *file)
  613. {
  614. static char error_string_response[512];
  615. int i;
  616. logsys_close_logfile(subsysid);
  617. if ((file == NULL) ||
  618. (strcmp(logsys_loggers[subsysid].subsys, "") == 0)) {
  619. return (0);
  620. }
  621. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  622. if ((logsys_loggers[i].logfile != NULL) &&
  623. (strcmp (logsys_loggers[i].logfile, file) == 0) &&
  624. (i != subsysid)) {
  625. logsys_loggers[subsysid].logfile =
  626. logsys_loggers[i].logfile;
  627. logsys_loggers[subsysid].logfile_fp =
  628. logsys_loggers[i].logfile_fp;
  629. return (0);
  630. }
  631. }
  632. logsys_loggers[subsysid].logfile = strdup(file);
  633. if (logsys_loggers[subsysid].logfile == NULL) {
  634. snprintf (error_string_response,
  635. sizeof(error_string_response),
  636. "Unable to allocate memory for logfile '%s'\n",
  637. file);
  638. *error_string = error_string_response;
  639. return (-1);
  640. }
  641. logsys_loggers[subsysid].logfile_fp = fopen (file, "a+");
  642. if (logsys_loggers[subsysid].logfile_fp == NULL) {
  643. free(logsys_loggers[subsysid].logfile);
  644. logsys_loggers[subsysid].logfile = NULL;
  645. snprintf (error_string_response,
  646. sizeof(error_string_response),
  647. "Can't open logfile '%s' for reason (%s).\n",
  648. file, strerror (errno));
  649. *error_string = error_string_response;
  650. return (-1);
  651. }
  652. return (0);
  653. }
  654. static void logsys_subsys_init (
  655. const char *subsys,
  656. int subsysid)
  657. {
  658. if (logsys_system_needs_init == LOGSYS_LOGGER_NEEDS_INIT) {
  659. logsys_loggers[subsysid].init_status =
  660. LOGSYS_LOGGER_NEEDS_INIT;
  661. } else {
  662. memcpy(&logsys_loggers[subsysid],
  663. &logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT],
  664. sizeof(logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT]));
  665. logsys_loggers[subsysid].tags = LOGSYS_TAG_LOG;
  666. logsys_loggers[subsysid].init_status =
  667. LOGSYS_LOGGER_INIT_DONE;
  668. }
  669. strncpy (logsys_loggers[subsysid].subsys, subsys,
  670. LOGSYS_MAX_SUBSYS_NAMELEN);
  671. }
  672. /*
  673. * Internal API - exported
  674. */
  675. int _logsys_system_setup(
  676. const char *mainsystem,
  677. unsigned int mode,
  678. unsigned int debug,
  679. const char *logfile,
  680. int logfile_priority,
  681. int syslog_facility,
  682. int syslog_priority,
  683. unsigned int tags)
  684. {
  685. int i;
  686. const char *errstr;
  687. char tempsubsys[LOGSYS_MAX_SUBSYS_NAMELEN];
  688. i = LOGSYS_MAX_SUBSYS_COUNT;
  689. pthread_mutex_lock (&logsys_config_mutex);
  690. snprintf(logsys_loggers[i].subsys,
  691. LOGSYS_MAX_SUBSYS_NAMELEN,
  692. "%s", mainsystem);
  693. logsys_loggers[i].mode = mode;
  694. logsys_loggers[i].debug = debug;
  695. if ((logfile) && strlen(logfile) > 0) {
  696. if (logsys_config_file_set_unlocked (i, &errstr, logfile) < 0) {
  697. pthread_mutex_unlock (&logsys_config_mutex);
  698. return (-1);
  699. }
  700. }
  701. logsys_loggers[i].logfile_priority = logfile_priority;
  702. logsys_loggers[i].syslog_facility = syslog_facility;
  703. logsys_loggers[i].syslog_priority = syslog_priority;
  704. syslog_facility_reconf();
  705. logsys_loggers[i].tags = tags;
  706. logsys_loggers[i].init_status = LOGSYS_LOGGER_INIT_DONE;
  707. logsys_system_needs_init = LOGSYS_LOGGER_INIT_DONE;
  708. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  709. if ((strcmp (logsys_loggers[i].subsys, "") != 0) &&
  710. (logsys_loggers[i].init_status ==
  711. LOGSYS_LOGGER_NEEDS_INIT)) {
  712. strncpy (tempsubsys, logsys_loggers[i].subsys,
  713. LOGSYS_MAX_SUBSYS_NAMELEN);
  714. logsys_subsys_init(tempsubsys, i);
  715. }
  716. }
  717. pthread_mutex_unlock (&logsys_config_mutex);
  718. return (0);
  719. }
  720. unsigned int _logsys_subsys_create (const char *subsys)
  721. {
  722. int i;
  723. assert (subsys != NULL);
  724. pthread_mutex_lock (&logsys_config_mutex);
  725. i = _logsys_config_subsys_get_unlocked (subsys);
  726. if ((i > -1) && (i < LOGSYS_MAX_SUBSYS_COUNT)) {
  727. pthread_mutex_unlock (&logsys_config_mutex);
  728. return i;
  729. }
  730. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  731. if (strcmp (logsys_loggers[i].subsys, "") == 0) {
  732. logsys_subsys_init(subsys, i);
  733. break;
  734. }
  735. }
  736. assert(i < LOGSYS_MAX_SUBSYS_COUNT);
  737. pthread_mutex_unlock (&logsys_config_mutex);
  738. return i;
  739. }
  740. int _logsys_wthread_create (void)
  741. {
  742. if (((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_FORK) == 0) &&
  743. ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) != 0)) {
  744. wthread_create();
  745. atexit (logsys_atexit);
  746. }
  747. return (0);
  748. }
  749. int _logsys_rec_init (unsigned int size)
  750. {
  751. /*
  752. * First record starts at zero
  753. * Last record ends at zero
  754. */
  755. flt_data = malloc ((size + 2) * sizeof (unsigned int));
  756. if (flt_data == NULL) {
  757. return (-1);
  758. }
  759. flt_data_size = size;
  760. flt_data[FDHEAD_INDEX] = 0;
  761. flt_data[FDTAIL_INDEX] = 0;
  762. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  763. pthread_spin_init (&logsys_idx_spinlock, 0);
  764. #endif
  765. return (0);
  766. }
  767. /*
  768. * u32 RECORD SIZE
  769. * u32 record ident
  770. * u32 arg count
  771. * u32 file line
  772. * u32 subsys length
  773. * buffer null terminated subsys
  774. * u32 filename length
  775. * buffer null terminated filename
  776. * u32 filename length
  777. * buffer null terminated function
  778. * u32 arg1 length
  779. * buffer arg1
  780. * ... repeats length & arg
  781. */
  782. void _logsys_log_rec (
  783. int subsysid,
  784. const char *function_name,
  785. const char *file_name,
  786. int file_line,
  787. unsigned int rec_ident,
  788. ...)
  789. {
  790. va_list ap;
  791. const void *buf_args[64];
  792. unsigned int buf_len[64];
  793. unsigned int i;
  794. unsigned int idx;
  795. unsigned int arguments = 0;
  796. unsigned int record_reclaim_size;
  797. unsigned int index_start;
  798. int words_written;
  799. record_reclaim_size = 0;
  800. /*
  801. * Decode VA Args
  802. */
  803. va_start (ap, rec_ident);
  804. arguments = 3;
  805. for (;;) {
  806. assert (arguments < 64);
  807. buf_args[arguments] = va_arg (ap, void *);
  808. if (buf_args[arguments] == LOGSYS_REC_END) {
  809. break;
  810. }
  811. buf_len[arguments] = va_arg (ap, int);
  812. record_reclaim_size += ((buf_len[arguments] + 3) >> 2) + 1;
  813. arguments++;
  814. }
  815. va_end (ap);
  816. /*
  817. * Encode logsys subsystem identity, filename, and function
  818. */
  819. buf_args[0] = logsys_loggers[subsysid].subsys;
  820. buf_len[0] = strlen (logsys_loggers[subsysid].subsys) + 1;
  821. buf_args[1] = file_name;
  822. buf_len[1] = strlen (file_name) + 1;
  823. buf_args[2] = function_name;
  824. buf_len[2] = strlen (function_name) + 1;
  825. for (i = 0; i < 3; i++) {
  826. record_reclaim_size += ((buf_len[i] + 3) >> 2) + 1;
  827. }
  828. idx = flt_data[FDHEAD_INDEX];
  829. index_start = idx;
  830. /*
  831. * Reclaim data needed for record including 4 words for the header
  832. */
  833. records_reclaim (idx, record_reclaim_size + 4);
  834. /*
  835. * Write record size of zero and rest of header information
  836. */
  837. flt_data[idx++] = 0;
  838. idx_word_step(idx);
  839. flt_data[idx++] = rec_ident;
  840. idx_word_step(idx);
  841. flt_data[idx++] = file_line;
  842. idx_word_step(idx);
  843. flt_data[idx++] = records_written;
  844. idx_word_step(idx);
  845. /*
  846. * Encode all of the arguments into the log message
  847. */
  848. for (i = 0; i < arguments; i++) {
  849. unsigned int bytes;
  850. unsigned int full_words;
  851. unsigned int total_words;
  852. bytes = buf_len[i];
  853. full_words = bytes >> 2;
  854. total_words = (bytes + 3) >> 2;
  855. flt_data[idx++] = total_words;
  856. idx_word_step(idx);
  857. /*
  858. * determine if this is a wrapped write or normal write
  859. */
  860. if (idx + total_words < flt_data_size) {
  861. /*
  862. * dont need to wrap buffer
  863. */
  864. my_memcpy_32bit (&flt_data[idx], buf_args[i], full_words);
  865. if (bytes % 4) {
  866. my_memcpy_8bit ((char *)&flt_data[idx + full_words],
  867. ((const char *)buf_args[i]) + (full_words << 2), bytes % 4);
  868. }
  869. } else {
  870. /*
  871. * need to wrap buffer
  872. */
  873. unsigned int first;
  874. unsigned int second;
  875. first = flt_data_size - idx;
  876. if (first > full_words) {
  877. first = full_words;
  878. }
  879. second = full_words - first;
  880. my_memcpy_32bit (&flt_data[idx],
  881. (const int *)buf_args[i], first);
  882. my_memcpy_32bit (&flt_data[0],
  883. (const int *)(((const unsigned char *)buf_args[i]) + (first << 2)),
  884. second);
  885. if (bytes % 4) {
  886. my_memcpy_8bit ((char *)&flt_data[0 + second],
  887. ((const char *)buf_args[i]) + (full_words << 2), bytes % 4);
  888. }
  889. }
  890. idx += total_words;
  891. idx_buffer_step (idx);
  892. }
  893. words_written = idx - index_start;
  894. if (words_written < 0) {
  895. words_written += flt_data_size;
  896. }
  897. /*
  898. * Commit the write of the record size now that the full record
  899. * is in the memory buffer
  900. */
  901. flt_data[index_start] = words_written;
  902. /*
  903. * If the index of the current head equals the current log_rec_idx,
  904. * and this is not a log_printf operation, set the log_rec_idx to
  905. * the new head position and commit the new head.
  906. */
  907. logsys_lock();
  908. if (rec_ident & LOGSYS_TAG_LOG) {
  909. log_requests_pending += 1;
  910. }
  911. if (log_requests_pending == 0) {
  912. log_rec_idx = idx;
  913. }
  914. flt_data[FDHEAD_INDEX] = idx;
  915. logsys_unlock();
  916. records_written++;
  917. }
  918. void _logsys_log_printf (
  919. int subsysid,
  920. const char *function_name,
  921. const char *file_name,
  922. int file_line,
  923. unsigned int level,
  924. const char *format,
  925. ...)
  926. {
  927. char logsys_print_buffer[COMBINE_BUFFER_SIZE];
  928. unsigned int len;
  929. va_list ap;
  930. if (subsysid <= -1) {
  931. subsysid = LOGSYS_MAX_SUBSYS_COUNT;
  932. }
  933. if ((level > logsys_loggers[subsysid].syslog_priority) &&
  934. (level > logsys_loggers[subsysid].logfile_priority)) {
  935. return;
  936. }
  937. va_start (ap, format);
  938. len = vsprintf (logsys_print_buffer, format, ap);
  939. va_end (ap);
  940. if (logsys_print_buffer[len - 1] == '\n') {
  941. logsys_print_buffer[len - 1] = '\0';
  942. len -= 1;
  943. }
  944. /*
  945. * Create a log record
  946. */
  947. _logsys_log_rec (subsysid,
  948. function_name,
  949. file_name,
  950. file_line,
  951. (level+1) << 28,
  952. logsys_print_buffer, len + 1,
  953. LOGSYS_REC_END);
  954. if ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) == 0) {
  955. /*
  956. * Output (and block) if the log mode is not threaded otherwise
  957. * expect the worker thread to output the log data once signaled
  958. */
  959. log_printf_to_logs (logsys_loggers[subsysid].subsys,
  960. file_name, function_name, file_line, level,
  961. logsys_print_buffer);
  962. } else {
  963. /*
  964. * Signal worker thread to display logging output
  965. */
  966. wthread_signal ();
  967. }
  968. }
  969. int _logsys_config_subsys_get (const char *subsys)
  970. {
  971. unsigned int i;
  972. pthread_mutex_lock (&logsys_config_mutex);
  973. i = _logsys_config_subsys_get_unlocked (subsys);
  974. pthread_mutex_unlock (&logsys_config_mutex);
  975. return i;
  976. }
  977. /*
  978. * External Configuration and Initialization API
  979. */
  980. void logsys_fork_completed (void)
  981. {
  982. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode &= ~LOGSYS_MODE_FORK;
  983. _logsys_wthread_create ();
  984. }
  985. unsigned int logsys_config_mode_set (const char *subsys, unsigned int mode)
  986. {
  987. int i;
  988. pthread_mutex_lock (&logsys_config_mutex);
  989. if (subsys != NULL) {
  990. i = _logsys_config_subsys_get_unlocked (subsys);
  991. if (i >= 0) {
  992. logsys_loggers[i].mode = mode;
  993. i = 0;
  994. }
  995. } else {
  996. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  997. logsys_loggers[i].mode = mode;
  998. }
  999. i = 0;
  1000. }
  1001. pthread_mutex_unlock (&logsys_config_mutex);
  1002. return i;
  1003. }
  1004. unsigned int logsys_config_mode_get (const char *subsys)
  1005. {
  1006. int i;
  1007. i = _logsys_config_subsys_get (subsys);
  1008. if (i < 0) {
  1009. return i;
  1010. }
  1011. return logsys_loggers[i].mode;
  1012. }
  1013. unsigned int logsys_config_tags_set (const char *subsys, unsigned int tags)
  1014. {
  1015. int i;
  1016. pthread_mutex_lock (&logsys_config_mutex);
  1017. if (subsys != NULL) {
  1018. i = _logsys_config_subsys_get_unlocked (subsys);
  1019. if (i >= 0) {
  1020. logsys_loggers[i].tags = tags;
  1021. i = 0;
  1022. }
  1023. } else {
  1024. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1025. logsys_loggers[i].tags = tags;
  1026. }
  1027. i = 0;
  1028. }
  1029. pthread_mutex_unlock (&logsys_config_mutex);
  1030. return i;
  1031. }
  1032. unsigned int logsys_config_tags_get (const char *subsys)
  1033. {
  1034. int i;
  1035. i = _logsys_config_subsys_get (subsys);
  1036. if (i < 0) {
  1037. return i;
  1038. }
  1039. return logsys_loggers[i].tags;
  1040. }
  1041. int logsys_config_file_set (
  1042. const char *subsys,
  1043. const char **error_string,
  1044. const char *file)
  1045. {
  1046. int i;
  1047. int res;
  1048. pthread_mutex_lock (&logsys_config_mutex);
  1049. if (subsys != NULL) {
  1050. i = _logsys_config_subsys_get_unlocked (subsys);
  1051. if (i < 0) {
  1052. res = i;
  1053. } else {
  1054. res = logsys_config_file_set_unlocked(i, error_string, file);
  1055. }
  1056. } else {
  1057. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1058. res = logsys_config_file_set_unlocked(i, error_string, file);
  1059. if (res < 0) {
  1060. break;
  1061. }
  1062. }
  1063. }
  1064. pthread_mutex_unlock (&logsys_config_mutex);
  1065. return res;
  1066. }
  1067. int logsys_format_set (const char *format)
  1068. {
  1069. int ret = 0;
  1070. pthread_mutex_lock (&logsys_config_mutex);
  1071. if (format_buffer) {
  1072. free(format_buffer);
  1073. format_buffer = NULL;
  1074. }
  1075. format_buffer = strdup(format ? format : "[%6s] %b");
  1076. if (format_buffer == NULL) {
  1077. ret = -1;
  1078. }
  1079. pthread_mutex_unlock (&logsys_config_mutex);
  1080. return ret;
  1081. }
  1082. char *logsys_format_get (void)
  1083. {
  1084. return format_buffer;
  1085. }
  1086. unsigned int logsys_config_syslog_facility_set (
  1087. const char *subsys,
  1088. unsigned int facility)
  1089. {
  1090. int i;
  1091. pthread_mutex_lock (&logsys_config_mutex);
  1092. if (subsys != NULL) {
  1093. i = _logsys_config_subsys_get_unlocked (subsys);
  1094. if (i >= 0) {
  1095. logsys_loggers[i].syslog_facility = facility;
  1096. if (i == LOGSYS_MAX_SUBSYS_COUNT) {
  1097. syslog_facility_reconf();
  1098. }
  1099. i = 0;
  1100. }
  1101. } else {
  1102. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1103. logsys_loggers[i].syslog_facility = facility;
  1104. }
  1105. syslog_facility_reconf();
  1106. i = 0;
  1107. }
  1108. pthread_mutex_unlock (&logsys_config_mutex);
  1109. return i;
  1110. }
  1111. unsigned int logsys_config_syslog_priority_set (
  1112. const char *subsys,
  1113. unsigned int priority)
  1114. {
  1115. int i;
  1116. pthread_mutex_lock (&logsys_config_mutex);
  1117. if (subsys != NULL) {
  1118. i = _logsys_config_subsys_get_unlocked (subsys);
  1119. if (i >= 0) {
  1120. logsys_loggers[i].syslog_priority = priority;
  1121. i = 0;
  1122. }
  1123. } else {
  1124. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1125. logsys_loggers[i].syslog_priority = priority;
  1126. }
  1127. i = 0;
  1128. }
  1129. pthread_mutex_unlock (&logsys_config_mutex);
  1130. return i;
  1131. }
  1132. unsigned int logsys_config_logfile_priority_set (
  1133. const char *subsys,
  1134. unsigned int priority)
  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].logfile_priority = priority;
  1142. i = 0;
  1143. }
  1144. } else {
  1145. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1146. logsys_loggers[i].logfile_priority = priority;
  1147. }
  1148. i = 0;
  1149. }
  1150. pthread_mutex_unlock (&logsys_config_mutex);
  1151. return i;
  1152. }
  1153. unsigned int logsys_config_debug_set (
  1154. const char *subsys,
  1155. unsigned int debug)
  1156. {
  1157. int i;
  1158. pthread_mutex_lock (&logsys_config_mutex);
  1159. if (subsys != NULL) {
  1160. i = _logsys_config_subsys_get_unlocked (subsys);
  1161. if (i >= 0) {
  1162. logsys_loggers[i].debug = debug;
  1163. i = 0;
  1164. }
  1165. } else {
  1166. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1167. logsys_loggers[i].debug = debug;
  1168. }
  1169. i = 0;
  1170. }
  1171. pthread_mutex_unlock (&logsys_config_mutex);
  1172. return i;
  1173. }
  1174. int logsys_facility_id_get (const char *name)
  1175. {
  1176. unsigned int i;
  1177. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1178. if (strcasecmp(name, facilitynames[i].c_name) == 0) {
  1179. return (facilitynames[i].c_val);
  1180. }
  1181. }
  1182. return (-1);
  1183. }
  1184. const char *logsys_facility_name_get (unsigned int facility)
  1185. {
  1186. unsigned int i;
  1187. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1188. if (facility == facilitynames[i].c_val) {
  1189. return (facilitynames[i].c_name);
  1190. }
  1191. }
  1192. return (NULL);
  1193. }
  1194. int logsys_priority_id_get (const char *name)
  1195. {
  1196. unsigned int i;
  1197. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1198. if (strcasecmp(name, prioritynames[i].c_name) == 0) {
  1199. return (prioritynames[i].c_val);
  1200. }
  1201. }
  1202. return (-1);
  1203. }
  1204. const char *logsys_priority_name_get (unsigned int priority)
  1205. {
  1206. unsigned int i;
  1207. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1208. if (priority == prioritynames[i].c_val) {
  1209. return (prioritynames[i].c_name);
  1210. }
  1211. }
  1212. return (NULL);
  1213. }
  1214. int logsys_tag_id_get (const char *name)
  1215. {
  1216. unsigned int i;
  1217. for (i = 0; tagnames[i].c_name != NULL; i++) {
  1218. if (strcasecmp(name, tagnames[i].c_name) == 0) {
  1219. return (tagnames[i].c_val);
  1220. }
  1221. }
  1222. return (-1);
  1223. }
  1224. const char *logsys_tag_name_get (unsigned int tag)
  1225. {
  1226. unsigned int i;
  1227. for (i = 0; tagnames[i].c_name != NULL; i++) {
  1228. if (tag == tagnames[i].c_val) {
  1229. return (tagnames[i].c_name);
  1230. }
  1231. }
  1232. return (NULL);
  1233. }
  1234. int logsys_log_rec_store (const char *filename)
  1235. {
  1236. int fd;
  1237. ssize_t written_size;
  1238. size_t size_to_write = (flt_data_size + 2) * sizeof (unsigned int);
  1239. fd = open (filename, O_CREAT|O_RDWR, 0700);
  1240. if (fd < 0) {
  1241. return (-1);
  1242. }
  1243. written_size = write (fd, flt_data, size_to_write);
  1244. if (close (fd) != 0)
  1245. return (-1);
  1246. if (written_size < 0) {
  1247. return (-1);
  1248. } else if ((size_t)written_size != size_to_write) {
  1249. return (-1);
  1250. }
  1251. return (0);
  1252. }
  1253. void logsys_atexit (void)
  1254. {
  1255. if (wthread_active) {
  1256. wthread_should_exit = 1;
  1257. wthread_signal ();
  1258. pthread_join (logsys_thread_id, NULL);
  1259. }
  1260. }
  1261. void logsys_flush (void)
  1262. {
  1263. wthread_signal ();
  1264. }