logsys.c 33 KB

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