logsys.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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. if (logsys_loggers[subsysid].mode & (LOGSYS_MODE_OUTPUT_FILE|LOGSYS_MODE_OUTPUT_STDERR)) {
  340. output_buffer[output_buffer_idx++] = '\n';
  341. output_buffer[output_buffer_idx] = '\0';
  342. }
  343. /*
  344. * Output to configured file
  345. */
  346. if (((logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_FILE) &&
  347. logsys_loggers[subsysid].logfile_fp &&
  348. (level <= logsys_loggers[subsysid].logfile_priority)) ||
  349. (logsys_loggers[subsysid].debug != 0)) {
  350. /*
  351. * Output to a file
  352. */
  353. if ((fwrite (output_buffer, strlen (output_buffer), 1,
  354. logsys_loggers[subsysid].logfile_fp) < 1) ||
  355. (fflush (logsys_loggers[subsysid].logfile_fp) == EOF)) {
  356. char tmpbuffer[1024];
  357. /*
  358. * if we are here, it's bad.. it's really really bad.
  359. * Best thing would be to light a candle in a church
  360. * and pray.
  361. */
  362. snprintf(tmpbuffer, sizeof(tmpbuffer),
  363. "LOGSYS EMERGENCY: %s Unable to write to %s.",
  364. logsys_loggers[subsysid].subsys,
  365. logsys_loggers[subsysid].logfile);
  366. pthread_mutex_lock (&logsys_config_mutex);
  367. logsys_close_logfile(subsysid);
  368. logsys_loggers[subsysid].mode &= ~LOGSYS_MODE_OUTPUT_FILE;
  369. pthread_mutex_unlock (&logsys_config_mutex);
  370. log_printf_to_logs(logsys_loggers[subsysid].subsys,
  371. __FILE__, __FUNCTION__, __LINE__,
  372. LOGSYS_LEVEL_EMERG, tmpbuffer);
  373. }
  374. }
  375. /*
  376. * Output to stderr
  377. */
  378. if (((logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_STDERR) &&
  379. (level <= logsys_loggers[subsysid].logfile_priority)) ||
  380. (logsys_loggers[subsysid].debug != 0)) {
  381. if (write (STDERR_FILENO, output_buffer, strlen (output_buffer)) < 0) {
  382. char tmpbuffer[1024];
  383. /*
  384. * if we are here, it's bad.. it's really really bad.
  385. * Best thing would be to light 20 candles for each saint
  386. * in the calendar and pray a lot...
  387. */
  388. pthread_mutex_lock (&logsys_config_mutex);
  389. logsys_loggers[subsysid].mode &= ~LOGSYS_MODE_OUTPUT_STDERR;
  390. pthread_mutex_unlock (&logsys_config_mutex);
  391. snprintf(tmpbuffer, sizeof(tmpbuffer),
  392. "LOGSYS EMERGENCY: %s Unable to write to STDERR.",
  393. logsys_loggers[subsysid].subsys);
  394. log_printf_to_logs(logsys_loggers[subsysid].subsys,
  395. __FILE__, __FUNCTION__, __LINE__,
  396. LOGSYS_LEVEL_EMERG, tmpbuffer);
  397. }
  398. }
  399. }
  400. static void record_print (const char *buf)
  401. {
  402. const int *buf_uint32t = (const int *)buf;
  403. unsigned int rec_size = buf_uint32t[0];
  404. unsigned int rec_ident = buf_uint32t[1];
  405. unsigned int file_line = buf_uint32t[2];
  406. unsigned int level = rec_ident >> 28;
  407. unsigned int i;
  408. unsigned int words_processed;
  409. unsigned int arg_size_idx;
  410. const void *arguments[64];
  411. unsigned int arg_count;
  412. arg_size_idx = 4;
  413. words_processed = 4;
  414. arg_count = 0;
  415. for (i = 0; words_processed < rec_size; i++) {
  416. arguments[arg_count++] = &buf_uint32t[arg_size_idx + 1];
  417. arg_size_idx += buf_uint32t[arg_size_idx] + 1;
  418. words_processed += buf_uint32t[arg_size_idx] + 1;
  419. }
  420. /*
  421. * (char *)arguments[0] -> subsystem
  422. * (char *)arguments[1] -> file_name
  423. * (char *)arguments[2] -> function_name
  424. * (char *)arguments[3] -> message
  425. */
  426. log_printf_to_logs (
  427. (char *)arguments[0],
  428. (char *)arguments[1],
  429. (char *)arguments[2],
  430. file_line,
  431. (level-1),
  432. (char *)arguments[3]);
  433. }
  434. static int record_read (char *buf, int rec_idx, int *log_msg) {
  435. unsigned int rec_size;
  436. unsigned int rec_ident;
  437. int firstcopy, secondcopy;
  438. rec_size = flt_data[rec_idx];
  439. rec_ident = flt_data[(rec_idx + 1) % flt_data_size];
  440. /*
  441. * Not a log record
  442. */
  443. if ((rec_ident & LOGSYS_TAG_LOG) == 0) {
  444. *log_msg = 0;
  445. return ((rec_idx + rec_size) % flt_data_size);
  446. }
  447. /*
  448. * A log record
  449. */
  450. *log_msg = 1;
  451. firstcopy = rec_size;
  452. secondcopy = 0;
  453. if (firstcopy + rec_idx > flt_data_size) {
  454. firstcopy = flt_data_size - rec_idx;
  455. secondcopy -= firstcopy - rec_size;
  456. }
  457. memcpy (&buf[0], &flt_data[rec_idx], firstcopy << 2);
  458. if (secondcopy) {
  459. memcpy (&buf[(firstcopy << 2)], &flt_data[0], secondcopy << 2);
  460. }
  461. return ((rec_idx + rec_size) % flt_data_size);
  462. }
  463. static inline void wthread_signal (void)
  464. {
  465. if (wthread_active == 0) {
  466. return;
  467. }
  468. pthread_mutex_lock (&logsys_cond_mutex);
  469. pthread_cond_signal (&logsys_cond);
  470. pthread_mutex_unlock (&logsys_cond_mutex);
  471. }
  472. static inline void wthread_wait (void)
  473. {
  474. pthread_mutex_lock (&logsys_cond_mutex);
  475. pthread_cond_wait (&logsys_cond, &logsys_cond_mutex);
  476. pthread_mutex_unlock (&logsys_cond_mutex);
  477. }
  478. static inline void wthread_wait_locked (void)
  479. {
  480. pthread_cond_wait (&logsys_cond, &logsys_cond_mutex);
  481. pthread_mutex_unlock (&logsys_cond_mutex);
  482. }
  483. static void *logsys_worker_thread (void *data) __attribute__((__noreturn__));
  484. static void *logsys_worker_thread (void *data)
  485. {
  486. int log_msg;
  487. char buf[COMBINE_BUFFER_SIZE];
  488. /*
  489. * Signal wthread_create that the initialization process may continue
  490. */
  491. wthread_signal ();
  492. logsys_lock();
  493. log_rec_idx = flt_data[FDTAIL_INDEX];
  494. logsys_unlock();
  495. for (;;) {
  496. wthread_wait ();
  497. /*
  498. * Read and copy the logging record index position
  499. * It may have been updated by records_reclaim if
  500. * messages were lost or or log_rec on the first new
  501. * logging record available
  502. */
  503. /*
  504. * Process any pending log messages here
  505. */
  506. for (;;) {
  507. logsys_lock();
  508. if (log_requests_lost > 0) {
  509. printf ("lost %d log requests\n", log_requests_lost);
  510. log_requests_pending -= log_requests_lost;
  511. log_requests_lost = 0;
  512. }
  513. if (log_requests_pending == 0) {
  514. logsys_unlock();
  515. break;
  516. }
  517. log_rec_idx = record_read (buf, log_rec_idx, &log_msg);
  518. if (log_msg) {
  519. log_requests_pending -= 1;
  520. }
  521. logsys_unlock();
  522. /*
  523. * print the stored buffer
  524. */
  525. if (log_msg) {
  526. record_print (buf);
  527. }
  528. }
  529. if (wthread_should_exit) {
  530. pthread_exit (NULL);
  531. }
  532. }
  533. }
  534. static void wthread_create (void)
  535. {
  536. int res;
  537. if (wthread_active) {
  538. return;
  539. }
  540. wthread_active = 1;
  541. pthread_mutex_init (&logsys_cond_mutex, NULL);
  542. pthread_cond_init (&logsys_cond, NULL);
  543. pthread_mutex_lock (&logsys_cond_mutex);
  544. res = pthread_create (&logsys_thread_id, NULL,
  545. logsys_worker_thread, NULL);
  546. /*
  547. * Wait for thread to be started
  548. */
  549. wthread_wait_locked ();
  550. }
  551. static int _logsys_config_subsys_get_unlocked (const char *subsys)
  552. {
  553. unsigned int i;
  554. if (!subsys) {
  555. return LOGSYS_MAX_SUBSYS_COUNT;
  556. }
  557. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  558. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  559. pthread_mutex_unlock (&logsys_config_mutex);
  560. return i;
  561. }
  562. }
  563. return (-1);
  564. }
  565. static void syslog_facility_reconf (void)
  566. {
  567. closelog();
  568. openlog(logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].subsys,
  569. LOG_CONS|LOG_PID,
  570. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].syslog_facility);
  571. }
  572. /*
  573. * this is always invoked within the mutex, so it's safe to parse the
  574. * whole thing as we need.
  575. */
  576. static void logsys_close_logfile (
  577. int subsysid)
  578. {
  579. int i;
  580. if ((logsys_loggers[subsysid].logfile_fp == NULL) &&
  581. (logsys_loggers[subsysid].logfile == NULL)) {
  582. return;
  583. }
  584. /*
  585. * if there is another subsystem or system using the same fp,
  586. * then we clean our own structs, but we can't close the file
  587. * as it is in use by somebody else.
  588. * Only the last users will be allowed to perform the fclose.
  589. */
  590. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  591. if ((logsys_loggers[i].logfile_fp == logsys_loggers[subsysid].logfile_fp) &&
  592. (i != subsysid)) {
  593. logsys_loggers[subsysid].logfile = NULL;
  594. logsys_loggers[subsysid].logfile_fp = NULL;
  595. return;
  596. }
  597. }
  598. /*
  599. * if we are here, we are the last users of that fp, so we can safely
  600. * close it.
  601. */
  602. fclose (logsys_loggers[subsysid].logfile_fp);
  603. logsys_loggers[subsysid].logfile_fp = NULL;
  604. free (logsys_loggers[subsysid].logfile);
  605. logsys_loggers[subsysid].logfile = NULL;
  606. }
  607. /*
  608. * we need a version that can work when somebody else is already
  609. * holding a config mutex lock or we will never get out of here
  610. */
  611. static int logsys_config_file_set_unlocked (
  612. int subsysid,
  613. const char **error_string,
  614. const char *file)
  615. {
  616. static char error_string_response[512];
  617. int i;
  618. logsys_close_logfile(subsysid);
  619. if ((file == NULL) ||
  620. (strcmp(logsys_loggers[subsysid].subsys, "") == 0)) {
  621. return (0);
  622. }
  623. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  624. if ((logsys_loggers[i].logfile != NULL) &&
  625. (strcmp (logsys_loggers[i].logfile, file) == 0) &&
  626. (i != subsysid)) {
  627. logsys_loggers[subsysid].logfile =
  628. logsys_loggers[i].logfile;
  629. logsys_loggers[subsysid].logfile_fp =
  630. logsys_loggers[i].logfile_fp;
  631. return (0);
  632. }
  633. }
  634. logsys_loggers[subsysid].logfile = strdup(file);
  635. if (logsys_loggers[subsysid].logfile == NULL) {
  636. snprintf (error_string_response,
  637. sizeof(error_string_response),
  638. "Unable to allocate memory for logfile '%s'\n",
  639. file);
  640. *error_string = error_string_response;
  641. return (-1);
  642. }
  643. logsys_loggers[subsysid].logfile_fp = fopen (file, "a+");
  644. if (logsys_loggers[subsysid].logfile_fp == NULL) {
  645. free(logsys_loggers[subsysid].logfile);
  646. logsys_loggers[subsysid].logfile = NULL;
  647. snprintf (error_string_response,
  648. sizeof(error_string_response),
  649. "Can't open logfile '%s' for reason (%s).\n",
  650. file, strerror (errno));
  651. *error_string = error_string_response;
  652. return (-1);
  653. }
  654. return (0);
  655. }
  656. static void logsys_subsys_init (
  657. const char *subsys,
  658. int subsysid)
  659. {
  660. if (logsys_system_needs_init == LOGSYS_LOGGER_NEEDS_INIT) {
  661. logsys_loggers[subsysid].init_status =
  662. LOGSYS_LOGGER_NEEDS_INIT;
  663. } else {
  664. memcpy(&logsys_loggers[subsysid],
  665. &logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT],
  666. sizeof(logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT]));
  667. logsys_loggers[subsysid].tags = LOGSYS_TAG_LOG;
  668. logsys_loggers[subsysid].init_status =
  669. LOGSYS_LOGGER_INIT_DONE;
  670. }
  671. strncpy (logsys_loggers[subsysid].subsys, subsys,
  672. LOGSYS_MAX_SUBSYS_NAMELEN);
  673. }
  674. /*
  675. * Internal API - exported
  676. */
  677. int _logsys_system_setup(
  678. const char *mainsystem,
  679. unsigned int mode,
  680. unsigned int debug,
  681. const char *logfile,
  682. int logfile_priority,
  683. int syslog_facility,
  684. int syslog_priority,
  685. unsigned int tags)
  686. {
  687. int i;
  688. const char *errstr;
  689. char tempsubsys[LOGSYS_MAX_SUBSYS_NAMELEN];
  690. i = LOGSYS_MAX_SUBSYS_COUNT;
  691. pthread_mutex_lock (&logsys_config_mutex);
  692. snprintf(logsys_loggers[i].subsys,
  693. LOGSYS_MAX_SUBSYS_NAMELEN,
  694. "%s", mainsystem);
  695. logsys_loggers[i].mode = mode;
  696. logsys_loggers[i].debug = debug;
  697. if ((logfile) && strlen(logfile) > 0) {
  698. logsys_config_file_set_unlocked (i, &errstr, logfile);
  699. }
  700. logsys_loggers[i].logfile_priority = logfile_priority;
  701. logsys_loggers[i].syslog_facility = syslog_facility;
  702. logsys_loggers[i].syslog_priority = syslog_priority;
  703. syslog_facility_reconf();
  704. logsys_loggers[i].tags = tags;
  705. logsys_loggers[i].init_status = LOGSYS_LOGGER_INIT_DONE;
  706. logsys_system_needs_init = LOGSYS_LOGGER_INIT_DONE;
  707. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  708. if ((strcmp (logsys_loggers[i].subsys, "") != 0) &&
  709. (logsys_loggers[i].init_status ==
  710. LOGSYS_LOGGER_NEEDS_INIT)) {
  711. strncpy (tempsubsys, logsys_loggers[i].subsys,
  712. LOGSYS_MAX_SUBSYS_NAMELEN);
  713. logsys_subsys_init(tempsubsys, i);
  714. }
  715. }
  716. pthread_mutex_unlock (&logsys_config_mutex);
  717. return (0);
  718. }
  719. unsigned int _logsys_subsys_create (const char *subsys)
  720. {
  721. int i;
  722. assert (subsys != NULL);
  723. pthread_mutex_lock (&logsys_config_mutex);
  724. i = _logsys_config_subsys_get_unlocked (subsys);
  725. if ((i > -1) && (i < LOGSYS_MAX_SUBSYS_COUNT)) {
  726. pthread_mutex_unlock (&logsys_config_mutex);
  727. return i;
  728. }
  729. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  730. if (strcmp (logsys_loggers[i].subsys, "") == 0) {
  731. logsys_subsys_init(subsys, i);
  732. break;
  733. }
  734. }
  735. assert(i < LOGSYS_MAX_SUBSYS_COUNT);
  736. pthread_mutex_unlock (&logsys_config_mutex);
  737. return i;
  738. }
  739. int _logsys_wthread_create (void)
  740. {
  741. if (((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_FORK) == 0) &&
  742. ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) != 0)) {
  743. wthread_create();
  744. atexit (logsys_atexit);
  745. }
  746. return (0);
  747. }
  748. int _logsys_rec_init (unsigned int size)
  749. {
  750. /*
  751. * First record starts at zero
  752. * Last record ends at zero
  753. */
  754. flt_data = malloc ((size + 2) * sizeof (unsigned int));
  755. if (flt_data == NULL) {
  756. return (-1);
  757. }
  758. flt_data_size = size;
  759. flt_data[FDHEAD_INDEX] = 0;
  760. flt_data[FDTAIL_INDEX] = 0;
  761. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  762. pthread_spin_init (&logsys_idx_spinlock, 0);
  763. #endif
  764. return (0);
  765. }
  766. /*
  767. * u32 RECORD SIZE
  768. * u32 record ident
  769. * u32 arg count
  770. * u32 file line
  771. * u32 subsys length
  772. * buffer null terminated subsys
  773. * u32 filename length
  774. * buffer null terminated filename
  775. * u32 filename length
  776. * buffer null terminated function
  777. * u32 arg1 length
  778. * buffer arg1
  779. * ... repeats length & arg
  780. */
  781. void _logsys_log_rec (
  782. int subsysid,
  783. const char *function_name,
  784. const char *file_name,
  785. int file_line,
  786. unsigned int rec_ident,
  787. ...)
  788. {
  789. va_list ap;
  790. const void *buf_args[64];
  791. unsigned int buf_len[64];
  792. unsigned int i;
  793. unsigned int idx;
  794. unsigned int arguments = 0;
  795. unsigned int record_reclaim_size;
  796. unsigned int index_start;
  797. int words_written;
  798. record_reclaim_size = 0;
  799. /*
  800. * Decode VA Args
  801. */
  802. va_start (ap, rec_ident);
  803. arguments = 3;
  804. for (;;) {
  805. assert (arguments < 64);
  806. buf_args[arguments] = va_arg (ap, void *);
  807. if (buf_args[arguments] == LOGSYS_REC_END) {
  808. break;
  809. }
  810. buf_len[arguments] = va_arg (ap, int);
  811. record_reclaim_size += ((buf_len[arguments] + 3) >> 2) + 1;
  812. arguments++;
  813. }
  814. va_end (ap);
  815. /*
  816. * Encode logsys subsystem identity, filename, and function
  817. */
  818. buf_args[0] = logsys_loggers[subsysid].subsys;
  819. buf_len[0] = strlen (logsys_loggers[subsysid].subsys) + 1;
  820. buf_args[1] = file_name;
  821. buf_len[1] = strlen (file_name) + 1;
  822. buf_args[2] = function_name;
  823. buf_len[2] = strlen (function_name) + 1;
  824. for (i = 0; i < 3; i++) {
  825. record_reclaim_size += ((buf_len[i] + 3) >> 2) + 1;
  826. }
  827. idx = flt_data[FDHEAD_INDEX];
  828. index_start = idx;
  829. /*
  830. * Reclaim data needed for record including 4 words for the header
  831. */
  832. records_reclaim (idx, record_reclaim_size + 4);
  833. /*
  834. * Write record size of zero and rest of header information
  835. */
  836. flt_data[idx++] = 0;
  837. idx_word_step(idx);
  838. flt_data[idx++] = rec_ident;
  839. idx_word_step(idx);
  840. flt_data[idx++] = file_line;
  841. idx_word_step(idx);
  842. flt_data[idx++] = records_written;
  843. idx_word_step(idx);
  844. /*
  845. * Encode all of the arguments into the log message
  846. */
  847. for (i = 0; i < arguments; i++) {
  848. unsigned int bytes;
  849. unsigned int full_words;
  850. unsigned int total_words;
  851. bytes = buf_len[i];
  852. full_words = bytes >> 2;
  853. total_words = (bytes + 3) >> 2;
  854. flt_data[idx++] = total_words;
  855. idx_word_step(idx);
  856. /*
  857. * determine if this is a wrapped write or normal write
  858. */
  859. if (idx + total_words < flt_data_size) {
  860. /*
  861. * dont need to wrap buffer
  862. */
  863. my_memcpy_32bit (&flt_data[idx], buf_args[i], full_words);
  864. if (bytes % 4) {
  865. my_memcpy_8bit ((char *)&flt_data[idx + full_words],
  866. ((const char *)buf_args[i]) + (full_words << 2), bytes % 4);
  867. }
  868. } else {
  869. /*
  870. * need to wrap buffer
  871. */
  872. unsigned int first;
  873. unsigned int second;
  874. first = flt_data_size - idx;
  875. if (first > full_words) {
  876. first = full_words;
  877. }
  878. second = full_words - first;
  879. my_memcpy_32bit (&flt_data[idx],
  880. (const int *)buf_args[i], first);
  881. my_memcpy_32bit (&flt_data[0],
  882. (const int *)(((const unsigned char *)buf_args[i]) + (first << 2)),
  883. second);
  884. if (bytes % 4) {
  885. my_memcpy_8bit ((char *)&flt_data[0 + second],
  886. ((const char *)buf_args[i]) + (full_words << 2), bytes % 4);
  887. }
  888. }
  889. idx += total_words;
  890. idx_buffer_step (idx);
  891. }
  892. words_written = idx - index_start;
  893. if (words_written < 0) {
  894. words_written += flt_data_size;
  895. }
  896. /*
  897. * Commit the write of the record size now that the full record
  898. * is in the memory buffer
  899. */
  900. flt_data[index_start] = words_written;
  901. /*
  902. * If the index of the current head equals the current log_rec_idx,
  903. * and this is not a log_printf operation, set the log_rec_idx to
  904. * the new head position and commit the new head.
  905. */
  906. logsys_lock();
  907. if (rec_ident & LOGSYS_TAG_LOG) {
  908. log_requests_pending += 1;
  909. }
  910. if (log_requests_pending == 0) {
  911. log_rec_idx = idx;
  912. }
  913. flt_data[FDHEAD_INDEX] = idx;
  914. logsys_unlock();
  915. records_written++;
  916. }
  917. void _logsys_log_printf (
  918. int subsysid,
  919. const char *function_name,
  920. const char *file_name,
  921. int file_line,
  922. unsigned int level,
  923. const char *format,
  924. ...)
  925. {
  926. char logsys_print_buffer[COMBINE_BUFFER_SIZE];
  927. unsigned int len;
  928. va_list ap;
  929. if (subsysid <= -1) {
  930. subsysid = LOGSYS_MAX_SUBSYS_COUNT;
  931. }
  932. va_start (ap, format);
  933. len = vsprintf (logsys_print_buffer, format, ap);
  934. va_end (ap);
  935. if (logsys_print_buffer[len - 1] == '\n') {
  936. logsys_print_buffer[len - 1] = '\0';
  937. len -= 1;
  938. }
  939. /*
  940. * Create a log record
  941. */
  942. _logsys_log_rec (subsysid,
  943. function_name,
  944. file_name,
  945. file_line,
  946. (level+1) << 28,
  947. logsys_print_buffer, len + 1,
  948. LOGSYS_REC_END);
  949. if ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) == 0) {
  950. /*
  951. * Output (and block) if the log mode is not threaded otherwise
  952. * expect the worker thread to output the log data once signaled
  953. */
  954. log_printf_to_logs (logsys_loggers[subsysid].subsys,
  955. file_name, function_name, file_line, level,
  956. logsys_print_buffer);
  957. } else {
  958. /*
  959. * Signal worker thread to display logging output
  960. */
  961. wthread_signal ();
  962. }
  963. }
  964. int _logsys_config_subsys_get (const char *subsys)
  965. {
  966. unsigned int i;
  967. pthread_mutex_lock (&logsys_config_mutex);
  968. i = _logsys_config_subsys_get_unlocked (subsys);
  969. pthread_mutex_unlock (&logsys_config_mutex);
  970. return i;
  971. }
  972. /*
  973. * External Configuration and Initialization API
  974. */
  975. void logsys_fork_completed (void)
  976. {
  977. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode &= ~LOGSYS_MODE_FORK;
  978. _logsys_wthread_create ();
  979. }
  980. unsigned int logsys_config_mode_set (const char *subsys, unsigned int mode)
  981. {
  982. int i;
  983. pthread_mutex_lock (&logsys_config_mutex);
  984. if (subsys != NULL) {
  985. i = _logsys_config_subsys_get_unlocked (subsys);
  986. if (i >= 0) {
  987. logsys_loggers[i].mode = mode;
  988. i = 0;
  989. }
  990. } else {
  991. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  992. logsys_loggers[i].mode = mode;
  993. }
  994. i = 0;
  995. }
  996. pthread_mutex_unlock (&logsys_config_mutex);
  997. return i;
  998. }
  999. unsigned int logsys_config_mode_get (const char *subsys)
  1000. {
  1001. int i;
  1002. i = _logsys_config_subsys_get (subsys);
  1003. if (i < 0) {
  1004. return i;
  1005. }
  1006. return logsys_loggers[i].mode;
  1007. }
  1008. unsigned int logsys_config_tags_set (const char *subsys, unsigned int tags)
  1009. {
  1010. int i;
  1011. pthread_mutex_lock (&logsys_config_mutex);
  1012. if (subsys != NULL) {
  1013. i = _logsys_config_subsys_get_unlocked (subsys);
  1014. if (i >= 0) {
  1015. logsys_loggers[i].tags = tags;
  1016. i = 0;
  1017. }
  1018. } else {
  1019. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1020. logsys_loggers[i].tags = tags;
  1021. }
  1022. i = 0;
  1023. }
  1024. pthread_mutex_unlock (&logsys_config_mutex);
  1025. return i;
  1026. }
  1027. unsigned int logsys_config_tags_get (const char *subsys)
  1028. {
  1029. int i;
  1030. i = _logsys_config_subsys_get (subsys);
  1031. if (i < 0) {
  1032. return i;
  1033. }
  1034. return logsys_loggers[i].tags;
  1035. }
  1036. int logsys_config_file_set (
  1037. const char *subsys,
  1038. const char **error_string,
  1039. const char *file)
  1040. {
  1041. int i;
  1042. int res;
  1043. pthread_mutex_lock (&logsys_config_mutex);
  1044. if (subsys != NULL) {
  1045. i = _logsys_config_subsys_get_unlocked (subsys);
  1046. if (i < 0) {
  1047. res = i;
  1048. } else {
  1049. res = logsys_config_file_set_unlocked(i, error_string, file);
  1050. }
  1051. } else {
  1052. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1053. logsys_config_file_set_unlocked(i, error_string, file);
  1054. }
  1055. res = 0;
  1056. }
  1057. pthread_mutex_unlock (&logsys_config_mutex);
  1058. return res;
  1059. }
  1060. int logsys_format_set (const char *format)
  1061. {
  1062. int ret = 0;
  1063. pthread_mutex_lock (&logsys_config_mutex);
  1064. if (format_buffer) {
  1065. free(format_buffer);
  1066. format_buffer = NULL;
  1067. }
  1068. if (format) {
  1069. format_buffer = strdup(format);
  1070. if (format_buffer == NULL) {
  1071. ret = -1;
  1072. }
  1073. } else {
  1074. format_buffer = strdup("[%6s] %b");
  1075. }
  1076. pthread_mutex_unlock (&logsys_config_mutex);
  1077. return ret;
  1078. }
  1079. char *logsys_format_get (void)
  1080. {
  1081. return format_buffer;
  1082. }
  1083. unsigned int logsys_config_syslog_facility_set (
  1084. const char *subsys,
  1085. unsigned int facility)
  1086. {
  1087. int i;
  1088. pthread_mutex_lock (&logsys_config_mutex);
  1089. if (subsys != NULL) {
  1090. i = _logsys_config_subsys_get_unlocked (subsys);
  1091. if (i >= 0) {
  1092. logsys_loggers[i].syslog_facility = facility;
  1093. if (i == LOGSYS_MAX_SUBSYS_COUNT) {
  1094. syslog_facility_reconf();
  1095. }
  1096. i = 0;
  1097. }
  1098. } else {
  1099. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1100. logsys_loggers[i].syslog_facility = facility;
  1101. }
  1102. syslog_facility_reconf();
  1103. i = 0;
  1104. }
  1105. pthread_mutex_unlock (&logsys_config_mutex);
  1106. return i;
  1107. }
  1108. unsigned int logsys_config_syslog_priority_set (
  1109. const char *subsys,
  1110. unsigned int priority)
  1111. {
  1112. int i;
  1113. pthread_mutex_lock (&logsys_config_mutex);
  1114. if (subsys != NULL) {
  1115. i = _logsys_config_subsys_get_unlocked (subsys);
  1116. if (i >= 0) {
  1117. logsys_loggers[i].syslog_priority = priority;
  1118. i = 0;
  1119. }
  1120. } else {
  1121. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1122. logsys_loggers[i].syslog_priority = priority;
  1123. }
  1124. i = 0;
  1125. }
  1126. pthread_mutex_unlock (&logsys_config_mutex);
  1127. return i;
  1128. }
  1129. unsigned int logsys_config_logfile_priority_set (
  1130. const char *subsys,
  1131. unsigned int priority)
  1132. {
  1133. int i;
  1134. pthread_mutex_lock (&logsys_config_mutex);
  1135. if (subsys != NULL) {
  1136. i = _logsys_config_subsys_get_unlocked (subsys);
  1137. if (i >= 0) {
  1138. logsys_loggers[i].logfile_priority = priority;
  1139. i = 0;
  1140. }
  1141. } else {
  1142. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1143. logsys_loggers[i].logfile_priority = priority;
  1144. }
  1145. i = 0;
  1146. }
  1147. pthread_mutex_unlock (&logsys_config_mutex);
  1148. return i;
  1149. }
  1150. unsigned int logsys_config_debug_set (
  1151. const char *subsys,
  1152. unsigned int debug)
  1153. {
  1154. int i;
  1155. pthread_mutex_lock (&logsys_config_mutex);
  1156. if (subsys != NULL) {
  1157. i = _logsys_config_subsys_get_unlocked (subsys);
  1158. if (i >= 0) {
  1159. logsys_loggers[i].debug = debug;
  1160. i = 0;
  1161. }
  1162. } else {
  1163. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1164. logsys_loggers[i].debug = debug;
  1165. }
  1166. i = 0;
  1167. }
  1168. pthread_mutex_unlock (&logsys_config_mutex);
  1169. return i;
  1170. }
  1171. int logsys_facility_id_get (const char *name)
  1172. {
  1173. unsigned int i;
  1174. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1175. if (strcasecmp(name, facilitynames[i].c_name) == 0) {
  1176. return (facilitynames[i].c_val);
  1177. }
  1178. }
  1179. return (-1);
  1180. }
  1181. const char *logsys_facility_name_get (unsigned int facility)
  1182. {
  1183. unsigned int i;
  1184. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1185. if (facility == facilitynames[i].c_val) {
  1186. return (facilitynames[i].c_name);
  1187. }
  1188. }
  1189. return (NULL);
  1190. }
  1191. int logsys_priority_id_get (const char *name)
  1192. {
  1193. unsigned int i;
  1194. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1195. if (strcasecmp(name, prioritynames[i].c_name) == 0) {
  1196. return (prioritynames[i].c_val);
  1197. }
  1198. }
  1199. return (-1);
  1200. }
  1201. const char *logsys_priority_name_get (unsigned int priority)
  1202. {
  1203. unsigned int i;
  1204. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1205. if (priority == prioritynames[i].c_val) {
  1206. return (prioritynames[i].c_name);
  1207. }
  1208. }
  1209. return (NULL);
  1210. }
  1211. int logsys_tag_id_get (const char *name)
  1212. {
  1213. unsigned int i;
  1214. for (i = 0; tagnames[i].c_name != NULL; i++) {
  1215. if (strcasecmp(name, tagnames[i].c_name) == 0) {
  1216. return (tagnames[i].c_val);
  1217. }
  1218. }
  1219. return (-1);
  1220. }
  1221. const char *logsys_tag_name_get (unsigned int tag)
  1222. {
  1223. unsigned int i;
  1224. for (i = 0; tagnames[i].c_name != NULL; i++) {
  1225. if (tag == tagnames[i].c_val) {
  1226. return (tagnames[i].c_name);
  1227. }
  1228. }
  1229. return (NULL);
  1230. }
  1231. int logsys_log_rec_store (const char *filename)
  1232. {
  1233. int fd;
  1234. ssize_t written_size;
  1235. size_t size_to_write = (flt_data_size + 2) * sizeof (unsigned int);
  1236. fd = open (filename, O_CREAT|O_RDWR, 0700);
  1237. if (fd < 0) {
  1238. return (-1);
  1239. }
  1240. written_size = write (fd, flt_data, size_to_write);
  1241. if (close (fd) != 0)
  1242. return (-1);
  1243. if (written_size < 0) {
  1244. return (-1);
  1245. } else if ((size_t)written_size != size_to_write) {
  1246. return (-1);
  1247. }
  1248. return (0);
  1249. }
  1250. void logsys_atexit (void)
  1251. {
  1252. if (wthread_active) {
  1253. wthread_should_exit = 1;
  1254. wthread_signal ();
  1255. pthread_join (logsys_thread_id, NULL);
  1256. }
  1257. }