logsys.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  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. if (logsys_config_file_set_unlocked (i, &errstr, logfile) < 0) {
  699. pthread_mutex_unlock (&logsys_config_mutex);
  700. return (-1);
  701. }
  702. }
  703. logsys_loggers[i].logfile_priority = logfile_priority;
  704. logsys_loggers[i].syslog_facility = syslog_facility;
  705. logsys_loggers[i].syslog_priority = syslog_priority;
  706. syslog_facility_reconf();
  707. logsys_loggers[i].tags = tags;
  708. logsys_loggers[i].init_status = LOGSYS_LOGGER_INIT_DONE;
  709. logsys_system_needs_init = LOGSYS_LOGGER_INIT_DONE;
  710. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  711. if ((strcmp (logsys_loggers[i].subsys, "") != 0) &&
  712. (logsys_loggers[i].init_status ==
  713. LOGSYS_LOGGER_NEEDS_INIT)) {
  714. strncpy (tempsubsys, logsys_loggers[i].subsys,
  715. LOGSYS_MAX_SUBSYS_NAMELEN);
  716. logsys_subsys_init(tempsubsys, i);
  717. }
  718. }
  719. pthread_mutex_unlock (&logsys_config_mutex);
  720. return (0);
  721. }
  722. unsigned int _logsys_subsys_create (const char *subsys)
  723. {
  724. int i;
  725. assert (subsys != NULL);
  726. pthread_mutex_lock (&logsys_config_mutex);
  727. i = _logsys_config_subsys_get_unlocked (subsys);
  728. if ((i > -1) && (i < LOGSYS_MAX_SUBSYS_COUNT)) {
  729. pthread_mutex_unlock (&logsys_config_mutex);
  730. return i;
  731. }
  732. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  733. if (strcmp (logsys_loggers[i].subsys, "") == 0) {
  734. logsys_subsys_init(subsys, i);
  735. break;
  736. }
  737. }
  738. assert(i < LOGSYS_MAX_SUBSYS_COUNT);
  739. pthread_mutex_unlock (&logsys_config_mutex);
  740. return i;
  741. }
  742. int _logsys_wthread_create (void)
  743. {
  744. if (((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_FORK) == 0) &&
  745. ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) != 0)) {
  746. wthread_create();
  747. atexit (logsys_atexit);
  748. }
  749. return (0);
  750. }
  751. int _logsys_rec_init (unsigned int size)
  752. {
  753. /*
  754. * First record starts at zero
  755. * Last record ends at zero
  756. */
  757. flt_data = malloc ((size + 2) * sizeof (unsigned int));
  758. if (flt_data == NULL) {
  759. return (-1);
  760. }
  761. flt_data_size = size;
  762. flt_data[FDHEAD_INDEX] = 0;
  763. flt_data[FDTAIL_INDEX] = 0;
  764. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  765. pthread_spin_init (&logsys_idx_spinlock, 0);
  766. #endif
  767. return (0);
  768. }
  769. /*
  770. * u32 RECORD SIZE
  771. * u32 record ident
  772. * u32 arg count
  773. * u32 file line
  774. * u32 subsys length
  775. * buffer null terminated subsys
  776. * u32 filename length
  777. * buffer null terminated filename
  778. * u32 filename length
  779. * buffer null terminated function
  780. * u32 arg1 length
  781. * buffer arg1
  782. * ... repeats length & arg
  783. */
  784. void _logsys_log_rec (
  785. int subsysid,
  786. const char *function_name,
  787. const char *file_name,
  788. int file_line,
  789. unsigned int rec_ident,
  790. ...)
  791. {
  792. va_list ap;
  793. const void *buf_args[64];
  794. unsigned int buf_len[64];
  795. unsigned int i;
  796. unsigned int idx;
  797. unsigned int arguments = 0;
  798. unsigned int record_reclaim_size;
  799. unsigned int index_start;
  800. int words_written;
  801. record_reclaim_size = 0;
  802. /*
  803. * Decode VA Args
  804. */
  805. va_start (ap, rec_ident);
  806. arguments = 3;
  807. for (;;) {
  808. assert (arguments < 64);
  809. buf_args[arguments] = va_arg (ap, void *);
  810. if (buf_args[arguments] == LOGSYS_REC_END) {
  811. break;
  812. }
  813. buf_len[arguments] = va_arg (ap, int);
  814. record_reclaim_size += ((buf_len[arguments] + 3) >> 2) + 1;
  815. arguments++;
  816. }
  817. va_end (ap);
  818. /*
  819. * Encode logsys subsystem identity, filename, and function
  820. */
  821. buf_args[0] = logsys_loggers[subsysid].subsys;
  822. buf_len[0] = strlen (logsys_loggers[subsysid].subsys) + 1;
  823. buf_args[1] = file_name;
  824. buf_len[1] = strlen (file_name) + 1;
  825. buf_args[2] = function_name;
  826. buf_len[2] = strlen (function_name) + 1;
  827. for (i = 0; i < 3; i++) {
  828. record_reclaim_size += ((buf_len[i] + 3) >> 2) + 1;
  829. }
  830. idx = flt_data[FDHEAD_INDEX];
  831. index_start = idx;
  832. /*
  833. * Reclaim data needed for record including 4 words for the header
  834. */
  835. records_reclaim (idx, record_reclaim_size + 4);
  836. /*
  837. * Write record size of zero and rest of header information
  838. */
  839. flt_data[idx++] = 0;
  840. idx_word_step(idx);
  841. flt_data[idx++] = rec_ident;
  842. idx_word_step(idx);
  843. flt_data[idx++] = file_line;
  844. idx_word_step(idx);
  845. flt_data[idx++] = records_written;
  846. idx_word_step(idx);
  847. /*
  848. * Encode all of the arguments into the log message
  849. */
  850. for (i = 0; i < arguments; i++) {
  851. unsigned int bytes;
  852. unsigned int full_words;
  853. unsigned int total_words;
  854. bytes = buf_len[i];
  855. full_words = bytes >> 2;
  856. total_words = (bytes + 3) >> 2;
  857. flt_data[idx++] = total_words;
  858. idx_word_step(idx);
  859. /*
  860. * determine if this is a wrapped write or normal write
  861. */
  862. if (idx + total_words < flt_data_size) {
  863. /*
  864. * dont need to wrap buffer
  865. */
  866. my_memcpy_32bit (&flt_data[idx], buf_args[i], full_words);
  867. if (bytes % 4) {
  868. my_memcpy_8bit ((char *)&flt_data[idx + full_words],
  869. ((const char *)buf_args[i]) + (full_words << 2), bytes % 4);
  870. }
  871. } else {
  872. /*
  873. * need to wrap buffer
  874. */
  875. unsigned int first;
  876. unsigned int second;
  877. first = flt_data_size - idx;
  878. if (first > full_words) {
  879. first = full_words;
  880. }
  881. second = full_words - first;
  882. my_memcpy_32bit (&flt_data[idx],
  883. (const int *)buf_args[i], first);
  884. my_memcpy_32bit (&flt_data[0],
  885. (const int *)(((const unsigned char *)buf_args[i]) + (first << 2)),
  886. second);
  887. if (bytes % 4) {
  888. my_memcpy_8bit ((char *)&flt_data[0 + second],
  889. ((const char *)buf_args[i]) + (full_words << 2), bytes % 4);
  890. }
  891. }
  892. idx += total_words;
  893. idx_buffer_step (idx);
  894. }
  895. words_written = idx - index_start;
  896. if (words_written < 0) {
  897. words_written += flt_data_size;
  898. }
  899. /*
  900. * Commit the write of the record size now that the full record
  901. * is in the memory buffer
  902. */
  903. flt_data[index_start] = words_written;
  904. /*
  905. * If the index of the current head equals the current log_rec_idx,
  906. * and this is not a log_printf operation, set the log_rec_idx to
  907. * the new head position and commit the new head.
  908. */
  909. logsys_lock();
  910. if (rec_ident & LOGSYS_TAG_LOG) {
  911. log_requests_pending += 1;
  912. }
  913. if (log_requests_pending == 0) {
  914. log_rec_idx = idx;
  915. }
  916. flt_data[FDHEAD_INDEX] = idx;
  917. logsys_unlock();
  918. records_written++;
  919. }
  920. void _logsys_log_printf (
  921. int subsysid,
  922. const char *function_name,
  923. const char *file_name,
  924. int file_line,
  925. unsigned int level,
  926. const char *format,
  927. ...)
  928. {
  929. char logsys_print_buffer[COMBINE_BUFFER_SIZE];
  930. unsigned int len;
  931. va_list ap;
  932. if (subsysid <= -1) {
  933. subsysid = LOGSYS_MAX_SUBSYS_COUNT;
  934. }
  935. if ((level > logsys_loggers[subsysid].syslog_priority) &&
  936. (level > logsys_loggers[subsysid].logfile_priority)) {
  937. return;
  938. }
  939. va_start (ap, format);
  940. len = vsprintf (logsys_print_buffer, format, ap);
  941. va_end (ap);
  942. if (logsys_print_buffer[len - 1] == '\n') {
  943. logsys_print_buffer[len - 1] = '\0';
  944. len -= 1;
  945. }
  946. /*
  947. * Create a log record
  948. */
  949. _logsys_log_rec (subsysid,
  950. function_name,
  951. file_name,
  952. file_line,
  953. (level+1) << 28,
  954. logsys_print_buffer, len + 1,
  955. LOGSYS_REC_END);
  956. if ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) == 0) {
  957. /*
  958. * Output (and block) if the log mode is not threaded otherwise
  959. * expect the worker thread to output the log data once signaled
  960. */
  961. log_printf_to_logs (logsys_loggers[subsysid].subsys,
  962. file_name, function_name, file_line, level,
  963. logsys_print_buffer);
  964. } else {
  965. /*
  966. * Signal worker thread to display logging output
  967. */
  968. wthread_signal ();
  969. }
  970. }
  971. int _logsys_config_subsys_get (const char *subsys)
  972. {
  973. unsigned int i;
  974. pthread_mutex_lock (&logsys_config_mutex);
  975. i = _logsys_config_subsys_get_unlocked (subsys);
  976. pthread_mutex_unlock (&logsys_config_mutex);
  977. return i;
  978. }
  979. /*
  980. * External Configuration and Initialization API
  981. */
  982. void logsys_fork_completed (void)
  983. {
  984. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode &= ~LOGSYS_MODE_FORK;
  985. _logsys_wthread_create ();
  986. }
  987. unsigned int logsys_config_mode_set (const char *subsys, unsigned int mode)
  988. {
  989. int i;
  990. pthread_mutex_lock (&logsys_config_mutex);
  991. if (subsys != NULL) {
  992. i = _logsys_config_subsys_get_unlocked (subsys);
  993. if (i >= 0) {
  994. logsys_loggers[i].mode = mode;
  995. i = 0;
  996. }
  997. } else {
  998. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  999. logsys_loggers[i].mode = mode;
  1000. }
  1001. i = 0;
  1002. }
  1003. pthread_mutex_unlock (&logsys_config_mutex);
  1004. return i;
  1005. }
  1006. unsigned int logsys_config_mode_get (const char *subsys)
  1007. {
  1008. int i;
  1009. i = _logsys_config_subsys_get (subsys);
  1010. if (i < 0) {
  1011. return i;
  1012. }
  1013. return logsys_loggers[i].mode;
  1014. }
  1015. unsigned int logsys_config_tags_set (const char *subsys, unsigned int tags)
  1016. {
  1017. int i;
  1018. pthread_mutex_lock (&logsys_config_mutex);
  1019. if (subsys != NULL) {
  1020. i = _logsys_config_subsys_get_unlocked (subsys);
  1021. if (i >= 0) {
  1022. logsys_loggers[i].tags = tags;
  1023. i = 0;
  1024. }
  1025. } else {
  1026. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1027. logsys_loggers[i].tags = tags;
  1028. }
  1029. i = 0;
  1030. }
  1031. pthread_mutex_unlock (&logsys_config_mutex);
  1032. return i;
  1033. }
  1034. unsigned int logsys_config_tags_get (const char *subsys)
  1035. {
  1036. int i;
  1037. i = _logsys_config_subsys_get (subsys);
  1038. if (i < 0) {
  1039. return i;
  1040. }
  1041. return logsys_loggers[i].tags;
  1042. }
  1043. int logsys_config_file_set (
  1044. const char *subsys,
  1045. const char **error_string,
  1046. const char *file)
  1047. {
  1048. int i;
  1049. int res;
  1050. pthread_mutex_lock (&logsys_config_mutex);
  1051. if (subsys != NULL) {
  1052. i = _logsys_config_subsys_get_unlocked (subsys);
  1053. if (i < 0) {
  1054. res = i;
  1055. } else {
  1056. res = logsys_config_file_set_unlocked(i, error_string, file);
  1057. }
  1058. } else {
  1059. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1060. res = logsys_config_file_set_unlocked(i, error_string, file);
  1061. if (res < 0) {
  1062. break;
  1063. }
  1064. }
  1065. }
  1066. pthread_mutex_unlock (&logsys_config_mutex);
  1067. return res;
  1068. }
  1069. int logsys_format_set (const char *format)
  1070. {
  1071. int ret = 0;
  1072. pthread_mutex_lock (&logsys_config_mutex);
  1073. if (format_buffer) {
  1074. free(format_buffer);
  1075. format_buffer = NULL;
  1076. }
  1077. format_buffer = strdup(format ? format : "[%6s] %b");
  1078. if (format_buffer == NULL) {
  1079. ret = -1;
  1080. }
  1081. pthread_mutex_unlock (&logsys_config_mutex);
  1082. return ret;
  1083. }
  1084. char *logsys_format_get (void)
  1085. {
  1086. return format_buffer;
  1087. }
  1088. unsigned int logsys_config_syslog_facility_set (
  1089. const char *subsys,
  1090. unsigned int facility)
  1091. {
  1092. int i;
  1093. pthread_mutex_lock (&logsys_config_mutex);
  1094. if (subsys != NULL) {
  1095. i = _logsys_config_subsys_get_unlocked (subsys);
  1096. if (i >= 0) {
  1097. logsys_loggers[i].syslog_facility = facility;
  1098. if (i == LOGSYS_MAX_SUBSYS_COUNT) {
  1099. syslog_facility_reconf();
  1100. }
  1101. i = 0;
  1102. }
  1103. } else {
  1104. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1105. logsys_loggers[i].syslog_facility = facility;
  1106. }
  1107. syslog_facility_reconf();
  1108. i = 0;
  1109. }
  1110. pthread_mutex_unlock (&logsys_config_mutex);
  1111. return i;
  1112. }
  1113. unsigned int logsys_config_syslog_priority_set (
  1114. const char *subsys,
  1115. unsigned int priority)
  1116. {
  1117. int i;
  1118. pthread_mutex_lock (&logsys_config_mutex);
  1119. if (subsys != NULL) {
  1120. i = _logsys_config_subsys_get_unlocked (subsys);
  1121. if (i >= 0) {
  1122. logsys_loggers[i].syslog_priority = priority;
  1123. i = 0;
  1124. }
  1125. } else {
  1126. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1127. logsys_loggers[i].syslog_priority = priority;
  1128. }
  1129. i = 0;
  1130. }
  1131. pthread_mutex_unlock (&logsys_config_mutex);
  1132. return i;
  1133. }
  1134. unsigned int logsys_config_logfile_priority_set (
  1135. const char *subsys,
  1136. unsigned int priority)
  1137. {
  1138. int i;
  1139. pthread_mutex_lock (&logsys_config_mutex);
  1140. if (subsys != NULL) {
  1141. i = _logsys_config_subsys_get_unlocked (subsys);
  1142. if (i >= 0) {
  1143. logsys_loggers[i].logfile_priority = priority;
  1144. i = 0;
  1145. }
  1146. } else {
  1147. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1148. logsys_loggers[i].logfile_priority = priority;
  1149. }
  1150. i = 0;
  1151. }
  1152. pthread_mutex_unlock (&logsys_config_mutex);
  1153. return i;
  1154. }
  1155. unsigned int logsys_config_debug_set (
  1156. const char *subsys,
  1157. unsigned int debug)
  1158. {
  1159. int i;
  1160. pthread_mutex_lock (&logsys_config_mutex);
  1161. if (subsys != NULL) {
  1162. i = _logsys_config_subsys_get_unlocked (subsys);
  1163. if (i >= 0) {
  1164. logsys_loggers[i].debug = debug;
  1165. i = 0;
  1166. }
  1167. } else {
  1168. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1169. logsys_loggers[i].debug = debug;
  1170. }
  1171. i = 0;
  1172. }
  1173. pthread_mutex_unlock (&logsys_config_mutex);
  1174. return i;
  1175. }
  1176. int logsys_facility_id_get (const char *name)
  1177. {
  1178. unsigned int i;
  1179. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1180. if (strcasecmp(name, facilitynames[i].c_name) == 0) {
  1181. return (facilitynames[i].c_val);
  1182. }
  1183. }
  1184. return (-1);
  1185. }
  1186. const char *logsys_facility_name_get (unsigned int facility)
  1187. {
  1188. unsigned int i;
  1189. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1190. if (facility == facilitynames[i].c_val) {
  1191. return (facilitynames[i].c_name);
  1192. }
  1193. }
  1194. return (NULL);
  1195. }
  1196. int logsys_priority_id_get (const char *name)
  1197. {
  1198. unsigned int i;
  1199. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1200. if (strcasecmp(name, prioritynames[i].c_name) == 0) {
  1201. return (prioritynames[i].c_val);
  1202. }
  1203. }
  1204. return (-1);
  1205. }
  1206. const char *logsys_priority_name_get (unsigned int priority)
  1207. {
  1208. unsigned int i;
  1209. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1210. if (priority == prioritynames[i].c_val) {
  1211. return (prioritynames[i].c_name);
  1212. }
  1213. }
  1214. return (NULL);
  1215. }
  1216. int logsys_tag_id_get (const char *name)
  1217. {
  1218. unsigned int i;
  1219. for (i = 0; tagnames[i].c_name != NULL; i++) {
  1220. if (strcasecmp(name, tagnames[i].c_name) == 0) {
  1221. return (tagnames[i].c_val);
  1222. }
  1223. }
  1224. return (-1);
  1225. }
  1226. const char *logsys_tag_name_get (unsigned int tag)
  1227. {
  1228. unsigned int i;
  1229. for (i = 0; tagnames[i].c_name != NULL; i++) {
  1230. if (tag == tagnames[i].c_val) {
  1231. return (tagnames[i].c_name);
  1232. }
  1233. }
  1234. return (NULL);
  1235. }
  1236. int logsys_log_rec_store (const char *filename)
  1237. {
  1238. int fd;
  1239. ssize_t written_size;
  1240. size_t size_to_write = (flt_data_size + 2) * sizeof (unsigned int);
  1241. fd = open (filename, O_CREAT|O_RDWR, 0700);
  1242. if (fd < 0) {
  1243. return (-1);
  1244. }
  1245. written_size = write (fd, flt_data, size_to_write);
  1246. if (close (fd) != 0)
  1247. return (-1);
  1248. if (written_size < 0) {
  1249. return (-1);
  1250. } else if ((size_t)written_size != size_to_write) {
  1251. return (-1);
  1252. }
  1253. return (0);
  1254. }
  1255. void logsys_atexit (void)
  1256. {
  1257. if (wthread_active) {
  1258. wthread_should_exit = 1;
  1259. wthread_signal ();
  1260. pthread_join (logsys_thread_id, NULL);
  1261. }
  1262. }