logsys.c 36 KB

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