log.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /***********************************************************************
  13. * log.h
  14. *
  15. * structures related to logging facility.
  16. *
  17. *************************************************************************/
  18. #include <stdio.h>
  19. #ifdef LINUX
  20. #ifndef _XOPEN_SOURCE
  21. #define _XOPEN_SOURCE /* glibc2 needs this */
  22. #endif
  23. #ifndef __USE_XOPEN
  24. #define __USE_XOPEN
  25. #endif
  26. #endif
  27. #include <time.h>
  28. #include <stdarg.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <errno.h>
  32. #ifdef LINUX
  33. #include <sys/statfs.h>
  34. #else
  35. #include <sys/statvfs.h>
  36. #endif
  37. #include <fcntl.h>
  38. #include "prio.h"
  39. #include "prprf.h"
  40. #include "slap.h"
  41. #include "slapi-plugin.h"
  42. #define LOG_MB_IN_BYTES (1024 * 1024)
  43. #define LOG_SUCCESS 0 /* fine & dandy */
  44. #define LOG_CONTINUE LOG_SUCCESS
  45. #define LOG_ERROR 1 /* default error case */
  46. #define LOG_EXCEEDED 2 /*err: > max logs allowed */
  47. #define LOG_ROTATE 3 /*ok; go to the next log */
  48. #define LOG_UNABLE_TO_OPENFILE 4
  49. #define LOG_DONE 5
  50. #define LOG_UNIT_UNKNOWN 0
  51. #define LOG_UNIT_MONTHS 1
  52. #define LOG_UNIT_WEEKS 2
  53. #define LOG_UNIT_DAYS 3
  54. #define LOG_UNIT_HOURS 4
  55. #define LOG_UNIT_MINS 5
  56. #define LOGFILE_NEW 0
  57. #define LOGFILE_REOPENED 1
  58. #define LOG_UNIT_TYPE_UNKNOWN "unknown"
  59. #define LOG_UNIT_TYPE_MONTHS "month"
  60. #define LOG_UNIT_TYPE_WEEKS "week"
  61. #define LOG_UNIT_TYPE_DAYS "day"
  62. #define LOG_UNIT_TYPE_HOURS "hour"
  63. #define LOG_UNIT_TYPE_MINUTES "minute"
  64. #define LOG_BUFFER_MAXSIZE 512 * 1024
  65. #define PREVLOGFILE "Previous Log File:"
  66. /* see log.c for why this is done */
  67. typedef PRFileDesc *LOGFD;
  68. struct logfileinfo {
  69. PRInt64 l_size; /* size is in bytes */
  70. time_t l_ctime; /* log creation time*/
  71. struct logfileinfo *l_next; /* next log */
  72. };
  73. typedef struct logfileinfo LogFileInfo;
  74. struct logbufinfo {
  75. char *top; /* beginning of the buffer */
  76. char *current; /* current pointer into buffer */
  77. size_t maxsize; /* size of buffer */
  78. PRLock *lock; /* lock for access logging */
  79. PRInt32 refcount; /* Reference count for buffer copies */
  80. };
  81. typedef struct logbufinfo LogBufferInfo;
  82. struct logging_opts {
  83. /* These are access log specific */
  84. int log_access_state;
  85. int log_access_mode; /* access mode */
  86. int log_access_maxnumlogs; /* Number of logs */
  87. PRInt64 log_access_maxlogsize; /* max log size in bytes*/
  88. int log_access_rotationtime; /* time in units. */
  89. int log_access_rotationunit; /* time in units. */
  90. int log_access_rotationtime_secs; /* time in seconds */
  91. int log_access_rotationsync_enabled;/* 0 or 1*/
  92. int log_access_rotationsynchour; /* 0-23 */
  93. int log_access_rotationsyncmin; /* 0-59 */
  94. time_t log_access_rotationsyncclock; /* clock in seconds */
  95. PRInt64 log_access_maxdiskspace; /* space in bytes */
  96. PRInt64 log_access_minfreespace; /* free space in bytes */
  97. int log_access_exptime; /* time */
  98. int log_access_exptimeunit; /* unit time */
  99. int log_access_exptime_secs; /* time in secs */
  100. int log_access_level; /* access log level */
  101. char *log_access_file; /* access log file path */
  102. LOGFD log_access_fdes; /* fp for the cur access log */
  103. unsigned int log_numof_access_logs; /* number of logs */
  104. time_t log_access_ctime; /* log creation time */
  105. LogFileInfo *log_access_logchain; /* all the logs info */
  106. char *log_accessinfo_file; /* access log rotation info file */
  107. LogBufferInfo *log_access_buffer; /* buffer for access log */
  108. /* These are error log specific */
  109. int log_error_state;
  110. int log_error_mode; /* access mode */
  111. int log_error_maxnumlogs; /* Number of logs */
  112. PRInt64 log_error_maxlogsize; /* max log size in bytes*/
  113. int log_error_rotationtime; /* time in units. */
  114. int log_error_rotationunit; /* time in units. */
  115. int log_error_rotationtime_secs; /* time in seconds */
  116. int log_error_rotationsync_enabled;/* 0 or 1*/
  117. int log_error_rotationsynchour; /* 0-23 */
  118. int log_error_rotationsyncmin; /* 0-59 */
  119. time_t log_error_rotationsyncclock; /* clock in seconds */
  120. PRInt64 log_error_maxdiskspace; /* space in bytes */
  121. PRInt64 log_error_minfreespace; /* free space in bytes */
  122. int log_error_exptime; /* time */
  123. int log_error_exptimeunit; /* unit time */
  124. int log_error_exptime_secs; /* time in secs */
  125. char *log_error_file; /* error log file path */
  126. LOGFD log_error_fdes; /* fp for the cur error log */
  127. unsigned int log_numof_error_logs; /* number of logs */
  128. time_t log_error_ctime; /* log creation time */
  129. LogFileInfo *log_error_logchain; /* all the logs info */
  130. char *log_errorinfo_file; /* error log rotation info file */
  131. Slapi_RWLock *log_error_rwlock; /* lock on error*/
  132. /* These are audit log specific */
  133. int log_audit_state;
  134. int log_audit_mode; /* access mode */
  135. int log_audit_maxnumlogs; /* Number of logs */
  136. PRInt64 log_audit_maxlogsize; /* max log size in bytes*/
  137. int log_audit_rotationtime; /* time in units. */
  138. int log_audit_rotationunit; /* time in units. */
  139. int log_audit_rotationtime_secs; /* time in seconds */
  140. int log_audit_rotationsync_enabled;/* 0 or 1*/
  141. int log_audit_rotationsynchour; /* 0-23 */
  142. int log_audit_rotationsyncmin; /* 0-59 */
  143. time_t log_audit_rotationsyncclock; /* clock in seconds */
  144. PRInt64 log_audit_maxdiskspace; /* space in bytes */
  145. PRInt64 log_audit_minfreespace; /* free space in bytes */
  146. int log_audit_exptime; /* time */
  147. int log_audit_exptimeunit; /* unit time */
  148. int log_audit_exptime_secs; /* time in secs */
  149. char *log_audit_file; /* aufit log name */
  150. LOGFD log_audit_fdes; /* audit log fdes */
  151. unsigned int log_numof_audit_logs; /* number of logs */
  152. time_t log_audit_ctime; /* log creation time */
  153. LogFileInfo *log_audit_logchain; /* all the logs info */
  154. char *log_auditinfo_file; /* audit log rotation info file */
  155. Slapi_RWLock *log_audit_rwlock; /* lock on audit*/
  156. /* These are auditfail log specific */
  157. int log_auditfail_state;
  158. int log_auditfail_mode; /* access mode */
  159. int log_auditfail_maxnumlogs; /* Number of logs */
  160. PRInt64 log_auditfail_maxlogsize; /* max log size in bytes*/
  161. int log_auditfail_rotationtime; /* time in units. */
  162. int log_auditfail_rotationunit; /* time in units. */
  163. int log_auditfail_rotationtime_secs; /* time in seconds */
  164. int log_auditfail_rotationsync_enabled;/* 0 or 1*/
  165. int log_auditfail_rotationsynchour; /* 0-23 */
  166. int log_auditfail_rotationsyncmin; /* 0-59 */
  167. time_t log_auditfail_rotationsyncclock; /* clock in seconds */
  168. PRInt64 log_auditfail_maxdiskspace; /* space in bytes */
  169. PRInt64 log_auditfail_minfreespace; /* free space in bytes */
  170. int log_auditfail_exptime; /* time */
  171. int log_auditfail_exptimeunit; /* unit time */
  172. int log_auditfail_exptime_secs; /* time in secs */
  173. char *log_auditfail_file; /* auditfail log name */
  174. LOGFD log_auditfail_fdes; /* auditfail log fdes */
  175. unsigned int log_numof_auditfail_logs; /* number of logs */
  176. time_t log_auditfail_ctime; /* log creation time */
  177. LogFileInfo *log_auditfail_logchain; /* all the logs info */
  178. char *log_auditfailinfo_file; /* auditfail log rotation info file */
  179. Slapi_RWLock *log_auditfail_rwlock; /* lock on auditfail */
  180. int log_backend;
  181. };
  182. /* For log_state */
  183. #define LOGGING_ENABLED (int) 0x1 /* logging is enabled */
  184. #define LOGGING_NEED_TITLE 0x2 /* need to write title */
  185. #define LOG_ACCESS_LOCK_READ() PR_Lock(loginfo.log_access_buffer->lock)
  186. #define LOG_ACCESS_UNLOCK_READ() PR_Unlock(loginfo.log_access_buffer->lock)
  187. #define LOG_ACCESS_LOCK_WRITE() PR_Lock(loginfo.log_access_buffer->lock)
  188. #define LOG_ACCESS_UNLOCK_WRITE() PR_Unlock(loginfo.log_access_buffer->lock)
  189. #define LOG_ERROR_LOCK_READ() slapi_rwlock_rdlock(loginfo.log_error_rwlock)
  190. #define LOG_ERROR_UNLOCK_READ() slapi_rwlock_unlock(loginfo.log_error_rwlock)
  191. #define LOG_ERROR_LOCK_WRITE() slapi_rwlock_wrlock(loginfo.log_error_rwlock)
  192. #define LOG_ERROR_UNLOCK_WRITE() slapi_rwlock_unlock(loginfo.log_error_rwlock)
  193. #define LOG_AUDIT_LOCK_READ() slapi_rwlock_rdlock(loginfo.log_audit_rwlock)
  194. #define LOG_AUDIT_UNLOCK_READ() slapi_rwlock_unlock(loginfo.log_audit_rwlock)
  195. #define LOG_AUDIT_LOCK_WRITE() slapi_rwlock_wrlock(loginfo.log_audit_rwlock)
  196. #define LOG_AUDIT_UNLOCK_WRITE() slapi_rwlock_unlock(loginfo.log_audit_rwlock)
  197. #define LOG_AUDITFAIL_LOCK_READ() slapi_rwlock_rdlock(loginfo.log_auditfail_rwlock)
  198. #define LOG_AUDITFAIL_UNLOCK_READ() slapi_rwlock_unlock(loginfo.log_auditfail_rwlock)
  199. #define LOG_AUDITFAIL_LOCK_WRITE() slapi_rwlock_wrlock(loginfo.log_auditfail_rwlock)
  200. #define LOG_AUDITFAIL_UNLOCK_WRITE() slapi_rwlock_unlock(loginfo.log_auditfail_rwlock)