logsys.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  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_printf (
  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. ...)
  930. {
  931. char logsys_print_buffer[COMBINE_BUFFER_SIZE];
  932. unsigned int len;
  933. va_list ap;
  934. if (subsysid <= -1) {
  935. subsysid = LOGSYS_MAX_SUBSYS_COUNT;
  936. }
  937. if ((level > logsys_loggers[subsysid].syslog_priority) &&
  938. (level > logsys_loggers[subsysid].logfile_priority)) {
  939. return;
  940. }
  941. va_start (ap, format);
  942. len = vsprintf (logsys_print_buffer, format, ap);
  943. va_end (ap);
  944. if (logsys_print_buffer[len - 1] == '\n') {
  945. logsys_print_buffer[len - 1] = '\0';
  946. len -= 1;
  947. }
  948. /*
  949. * Create a log record
  950. */
  951. _logsys_log_rec (subsysid,
  952. function_name,
  953. file_name,
  954. file_line,
  955. (level+1) << 28,
  956. logsys_print_buffer, len + 1,
  957. LOGSYS_REC_END);
  958. if ((logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode & LOGSYS_MODE_THREADED) == 0) {
  959. /*
  960. * Output (and block) if the log mode is not threaded otherwise
  961. * expect the worker thread to output the log data once signaled
  962. */
  963. log_printf_to_logs (logsys_loggers[subsysid].subsys,
  964. file_name, function_name, file_line, level,
  965. logsys_print_buffer);
  966. } else {
  967. /*
  968. * Signal worker thread to display logging output
  969. */
  970. wthread_signal ();
  971. }
  972. }
  973. int _logsys_config_subsys_get (const char *subsys)
  974. {
  975. unsigned int i;
  976. pthread_mutex_lock (&logsys_config_mutex);
  977. i = _logsys_config_subsys_get_unlocked (subsys);
  978. pthread_mutex_unlock (&logsys_config_mutex);
  979. return i;
  980. }
  981. /*
  982. * External Configuration and Initialization API
  983. */
  984. void logsys_fork_completed (void)
  985. {
  986. logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode &= ~LOGSYS_MODE_FORK;
  987. _logsys_wthread_create ();
  988. }
  989. unsigned int logsys_config_mode_set (const char *subsys, unsigned int mode)
  990. {
  991. int i;
  992. pthread_mutex_lock (&logsys_config_mutex);
  993. if (subsys != NULL) {
  994. i = _logsys_config_subsys_get_unlocked (subsys);
  995. if (i >= 0) {
  996. logsys_loggers[i].mode = mode;
  997. i = 0;
  998. }
  999. } else {
  1000. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1001. logsys_loggers[i].mode = mode;
  1002. }
  1003. i = 0;
  1004. }
  1005. pthread_mutex_unlock (&logsys_config_mutex);
  1006. return i;
  1007. }
  1008. unsigned int logsys_config_mode_get (const char *subsys)
  1009. {
  1010. int i;
  1011. i = _logsys_config_subsys_get (subsys);
  1012. if (i < 0) {
  1013. return i;
  1014. }
  1015. return logsys_loggers[i].mode;
  1016. }
  1017. unsigned int logsys_config_tags_set (const char *subsys, unsigned int tags)
  1018. {
  1019. int i;
  1020. pthread_mutex_lock (&logsys_config_mutex);
  1021. if (subsys != NULL) {
  1022. i = _logsys_config_subsys_get_unlocked (subsys);
  1023. if (i >= 0) {
  1024. logsys_loggers[i].tags = tags;
  1025. i = 0;
  1026. }
  1027. } else {
  1028. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1029. logsys_loggers[i].tags = tags;
  1030. }
  1031. i = 0;
  1032. }
  1033. pthread_mutex_unlock (&logsys_config_mutex);
  1034. return i;
  1035. }
  1036. unsigned int logsys_config_tags_get (const char *subsys)
  1037. {
  1038. int i;
  1039. i = _logsys_config_subsys_get (subsys);
  1040. if (i < 0) {
  1041. return i;
  1042. }
  1043. return logsys_loggers[i].tags;
  1044. }
  1045. int logsys_config_file_set (
  1046. const char *subsys,
  1047. const char **error_string,
  1048. const char *file)
  1049. {
  1050. int i;
  1051. int res;
  1052. pthread_mutex_lock (&logsys_config_mutex);
  1053. if (subsys != NULL) {
  1054. i = _logsys_config_subsys_get_unlocked (subsys);
  1055. if (i < 0) {
  1056. res = i;
  1057. } else {
  1058. res = logsys_config_file_set_unlocked(i, error_string, file);
  1059. }
  1060. } else {
  1061. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1062. res = logsys_config_file_set_unlocked(i, error_string, file);
  1063. if (res < 0) {
  1064. break;
  1065. }
  1066. }
  1067. }
  1068. pthread_mutex_unlock (&logsys_config_mutex);
  1069. return res;
  1070. }
  1071. int logsys_format_set (const char *format)
  1072. {
  1073. int ret = 0;
  1074. pthread_mutex_lock (&logsys_config_mutex);
  1075. if (format_buffer) {
  1076. free(format_buffer);
  1077. format_buffer = NULL;
  1078. }
  1079. format_buffer = strdup(format ? format : "[%6s] %b");
  1080. if (format_buffer == NULL) {
  1081. ret = -1;
  1082. }
  1083. pthread_mutex_unlock (&logsys_config_mutex);
  1084. return ret;
  1085. }
  1086. char *logsys_format_get (void)
  1087. {
  1088. return format_buffer;
  1089. }
  1090. unsigned int logsys_config_syslog_facility_set (
  1091. const char *subsys,
  1092. unsigned int facility)
  1093. {
  1094. int i;
  1095. pthread_mutex_lock (&logsys_config_mutex);
  1096. if (subsys != NULL) {
  1097. i = _logsys_config_subsys_get_unlocked (subsys);
  1098. if (i >= 0) {
  1099. logsys_loggers[i].syslog_facility = facility;
  1100. if (i == LOGSYS_MAX_SUBSYS_COUNT) {
  1101. syslog_facility_reconf();
  1102. }
  1103. i = 0;
  1104. }
  1105. } else {
  1106. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1107. logsys_loggers[i].syslog_facility = facility;
  1108. }
  1109. syslog_facility_reconf();
  1110. i = 0;
  1111. }
  1112. pthread_mutex_unlock (&logsys_config_mutex);
  1113. return i;
  1114. }
  1115. unsigned int logsys_config_syslog_priority_set (
  1116. const char *subsys,
  1117. unsigned int priority)
  1118. {
  1119. int i;
  1120. pthread_mutex_lock (&logsys_config_mutex);
  1121. if (subsys != NULL) {
  1122. i = _logsys_config_subsys_get_unlocked (subsys);
  1123. if (i >= 0) {
  1124. logsys_loggers[i].syslog_priority = priority;
  1125. i = 0;
  1126. }
  1127. } else {
  1128. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1129. logsys_loggers[i].syslog_priority = priority;
  1130. }
  1131. i = 0;
  1132. }
  1133. pthread_mutex_unlock (&logsys_config_mutex);
  1134. return i;
  1135. }
  1136. unsigned int logsys_config_logfile_priority_set (
  1137. const char *subsys,
  1138. unsigned int priority)
  1139. {
  1140. int i;
  1141. pthread_mutex_lock (&logsys_config_mutex);
  1142. if (subsys != NULL) {
  1143. i = _logsys_config_subsys_get_unlocked (subsys);
  1144. if (i >= 0) {
  1145. logsys_loggers[i].logfile_priority = priority;
  1146. i = 0;
  1147. }
  1148. } else {
  1149. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1150. logsys_loggers[i].logfile_priority = priority;
  1151. }
  1152. i = 0;
  1153. }
  1154. pthread_mutex_unlock (&logsys_config_mutex);
  1155. return i;
  1156. }
  1157. unsigned int logsys_config_debug_set (
  1158. const char *subsys,
  1159. unsigned int debug)
  1160. {
  1161. int i;
  1162. pthread_mutex_lock (&logsys_config_mutex);
  1163. if (subsys != NULL) {
  1164. i = _logsys_config_subsys_get_unlocked (subsys);
  1165. if (i >= 0) {
  1166. logsys_loggers[i].debug = debug;
  1167. i = 0;
  1168. }
  1169. } else {
  1170. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  1171. logsys_loggers[i].debug = debug;
  1172. }
  1173. i = 0;
  1174. }
  1175. pthread_mutex_unlock (&logsys_config_mutex);
  1176. return i;
  1177. }
  1178. int logsys_facility_id_get (const char *name)
  1179. {
  1180. unsigned int i;
  1181. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1182. if (strcasecmp(name, facilitynames[i].c_name) == 0) {
  1183. return (facilitynames[i].c_val);
  1184. }
  1185. }
  1186. return (-1);
  1187. }
  1188. const char *logsys_facility_name_get (unsigned int facility)
  1189. {
  1190. unsigned int i;
  1191. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  1192. if (facility == facilitynames[i].c_val) {
  1193. return (facilitynames[i].c_name);
  1194. }
  1195. }
  1196. return (NULL);
  1197. }
  1198. int logsys_priority_id_get (const char *name)
  1199. {
  1200. unsigned int i;
  1201. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1202. if (strcasecmp(name, prioritynames[i].c_name) == 0) {
  1203. return (prioritynames[i].c_val);
  1204. }
  1205. }
  1206. return (-1);
  1207. }
  1208. const char *logsys_priority_name_get (unsigned int priority)
  1209. {
  1210. unsigned int i;
  1211. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  1212. if (priority == prioritynames[i].c_val) {
  1213. return (prioritynames[i].c_name);
  1214. }
  1215. }
  1216. return (NULL);
  1217. }
  1218. int logsys_tag_id_get (const char *name)
  1219. {
  1220. unsigned int i;
  1221. for (i = 0; tagnames[i].c_name != NULL; i++) {
  1222. if (strcasecmp(name, tagnames[i].c_name) == 0) {
  1223. return (tagnames[i].c_val);
  1224. }
  1225. }
  1226. return (-1);
  1227. }
  1228. const char *logsys_tag_name_get (unsigned int tag)
  1229. {
  1230. unsigned int i;
  1231. for (i = 0; tagnames[i].c_name != NULL; i++) {
  1232. if (tag == tagnames[i].c_val) {
  1233. return (tagnames[i].c_name);
  1234. }
  1235. }
  1236. return (NULL);
  1237. }
  1238. int logsys_log_rec_store (const char *filename)
  1239. {
  1240. int fd;
  1241. ssize_t written_size;
  1242. size_t size_to_write = (flt_data_size + 2) * sizeof (unsigned int);
  1243. fd = open (filename, O_CREAT|O_RDWR, 0700);
  1244. if (fd < 0) {
  1245. return (-1);
  1246. }
  1247. written_size = write (fd, flt_data, size_to_write);
  1248. if (close (fd) != 0)
  1249. return (-1);
  1250. if (written_size < 0) {
  1251. return (-1);
  1252. } else if ((size_t)written_size != size_to_write) {
  1253. return (-1);
  1254. }
  1255. return (0);
  1256. }
  1257. void logsys_atexit (void)
  1258. {
  1259. if (wthread_active) {
  1260. wthread_should_exit = 1;
  1261. wthread_signal ();
  1262. pthread_join (logsys_thread_id, NULL);
  1263. }
  1264. }
  1265. void logsys_flush (void)
  1266. {
  1267. wthread_signal ();
  1268. }