logsys.c 35 KB

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