log.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /***********************************************************************
  42. * log.h
  43. *
  44. * structures related to logging facility.
  45. *
  46. *************************************************************************/
  47. #include <stdio.h>
  48. #ifdef LINUX
  49. #ifndef _XOPEN_SOURCE
  50. #define _XOPEN_SOURCE /* glibc2 needs this */
  51. #endif
  52. #ifndef __USE_XOPEN
  53. #define __USE_XOPEN
  54. #endif
  55. #endif
  56. #include <time.h>
  57. #include <stdarg.h>
  58. #include <sys/types.h>
  59. #include <sys/stat.h>
  60. #ifndef _WIN32
  61. #include <errno.h>
  62. #ifdef LINUX
  63. #include <sys/statfs.h>
  64. #else
  65. #include <sys/statvfs.h>
  66. #endif
  67. #endif
  68. #include <fcntl.h>
  69. #include "prio.h"
  70. #include "prprf.h"
  71. #include "slap.h"
  72. #include "slapi-plugin.h"
  73. #define LOG_MB_IN_BYTES (1024 * 1024)
  74. #define LOG_SUCCESS 0 /* fine & dandy */
  75. #define LOG_CONTINUE LOG_SUCCESS
  76. #define LOG_ERROR 1 /* default error case */
  77. #define LOG_EXCEEDED 2 /*err: > max logs allowed */
  78. #define LOG_ROTATE 3 /*ok; go to the next log */
  79. #define LOG_UNABLE_TO_OPENFILE 4
  80. #define LOG_DONE 5
  81. #define LOG_UNIT_UNKNOWN 0
  82. #define LOG_UNIT_MONTHS 1
  83. #define LOG_UNIT_WEEKS 2
  84. #define LOG_UNIT_DAYS 3
  85. #define LOG_UNIT_HOURS 4
  86. #define LOG_UNIT_MINS 5
  87. #define LOGFILE_NEW 0
  88. #define LOGFILE_REOPENED 1
  89. #define LOG_UNIT_TYPE_UNKNOWN "unknown"
  90. #define LOG_UNIT_TYPE_MONTHS "month"
  91. #define LOG_UNIT_TYPE_WEEKS "week"
  92. #define LOG_UNIT_TYPE_DAYS "day"
  93. #define LOG_UNIT_TYPE_HOURS "hour"
  94. #define LOG_UNIT_TYPE_MINUTES "minute"
  95. #define LOG_BUFFER_MAXSIZE 512 * 1024
  96. #define PREVLOGFILE "Previous Log File:"
  97. /* see log.c for why this is done */
  98. #ifdef XP_WIN32
  99. typedef FILE *LOGFD;
  100. #else
  101. typedef PRFileDesc *LOGFD;
  102. #endif
  103. struct logfileinfo {
  104. PRInt64 l_size; /* size is in bytes */
  105. time_t l_ctime; /* log creation time*/
  106. struct logfileinfo *l_next; /* next log */
  107. };
  108. typedef struct logfileinfo LogFileInfo;
  109. struct logbufinfo {
  110. char *top; /* beginning of the buffer */
  111. char *current; /* current pointer into buffer */
  112. size_t maxsize; /* size of buffer */
  113. PRLock *lock; /* lock for access logging */
  114. PRInt32 refcount; /* Reference count for buffer copies */
  115. };
  116. typedef struct logbufinfo LogBufferInfo;
  117. struct logging_opts {
  118. /* These are access log specific */
  119. int log_access_state;
  120. int log_access_mode; /* access mode */
  121. int log_access_maxnumlogs; /* Number of logs */
  122. PRInt64 log_access_maxlogsize; /* max log size in bytes*/
  123. int log_access_rotationtime; /* time in units. */
  124. int log_access_rotationunit; /* time in units. */
  125. int log_access_rotationtime_secs; /* time in seconds */
  126. int log_access_rotationsync_enabled;/* 0 or 1*/
  127. int log_access_rotationsynchour; /* 0-23 */
  128. int log_access_rotationsyncmin; /* 0-59 */
  129. time_t log_access_rotationsyncclock; /* clock in seconds */
  130. PRInt64 log_access_maxdiskspace; /* space in bytes */
  131. PRInt64 log_access_minfreespace; /* free space in bytes */
  132. int log_access_exptime; /* time */
  133. int log_access_exptimeunit; /* unit time */
  134. int log_access_exptime_secs; /* time in secs */
  135. int log_access_level; /* access log level */
  136. char *log_access_file; /* access log file path */
  137. LOGFD log_access_fdes; /* fp for the cur access log */
  138. unsigned int log_numof_access_logs; /* number of logs */
  139. time_t log_access_ctime; /* log creation time */
  140. LogFileInfo *log_access_logchain; /* all the logs info */
  141. char *log_accessinfo_file; /* access log rotation info file */
  142. LogBufferInfo *log_access_buffer; /* buffer for access log */
  143. /* These are error log specific */
  144. int log_error_state;
  145. int log_error_mode; /* access mode */
  146. int log_error_maxnumlogs; /* Number of logs */
  147. PRInt64 log_error_maxlogsize; /* max log size in bytes*/
  148. int log_error_rotationtime; /* time in units. */
  149. int log_error_rotationunit; /* time in units. */
  150. int log_error_rotationtime_secs; /* time in seconds */
  151. int log_error_rotationsync_enabled;/* 0 or 1*/
  152. int log_error_rotationsynchour; /* 0-23 */
  153. int log_error_rotationsyncmin; /* 0-59 */
  154. time_t log_error_rotationsyncclock; /* clock in seconds */
  155. PRInt64 log_error_maxdiskspace; /* space in bytes */
  156. PRInt64 log_error_minfreespace; /* free space in bytes */
  157. int log_error_exptime; /* time */
  158. int log_error_exptimeunit; /* unit time */
  159. int log_error_exptime_secs; /* time in secs */
  160. char *log_error_file; /* error log file path */
  161. LOGFD log_error_fdes; /* fp for the cur error log */
  162. unsigned int log_numof_error_logs; /* number of logs */
  163. time_t log_error_ctime; /* log creation time */
  164. LogFileInfo *log_error_logchain; /* all the logs info */
  165. char *log_errorinfo_file; /* error log rotation info file */
  166. rwl *log_error_rwlock; /* lock on error*/
  167. /* These are audit log specific */
  168. int log_audit_state;
  169. int log_audit_mode; /* access mode */
  170. int log_audit_maxnumlogs; /* Number of logs */
  171. PRInt64 log_audit_maxlogsize; /* max log size in bytes*/
  172. int log_audit_rotationtime; /* time in units. */
  173. int log_audit_rotationunit; /* time in units. */
  174. int log_audit_rotationtime_secs; /* time in seconds */
  175. int log_audit_rotationsync_enabled;/* 0 or 1*/
  176. int log_audit_rotationsynchour; /* 0-23 */
  177. int log_audit_rotationsyncmin; /* 0-59 */
  178. time_t log_audit_rotationsyncclock; /* clock in seconds */
  179. PRInt64 log_audit_maxdiskspace; /* space in bytes */
  180. PRInt64 log_audit_minfreespace; /* free space in bytes */
  181. int log_audit_exptime; /* time */
  182. int log_audit_exptimeunit; /* unit time */
  183. int log_audit_exptime_secs; /* time in secs */
  184. char *log_audit_file; /* aufit log name */
  185. LOGFD log_audit_fdes; /* audit log fdes */
  186. unsigned int log_numof_audit_logs; /* number of logs */
  187. time_t log_audit_ctime; /* log creation time */
  188. LogFileInfo *log_audit_logchain; /* all the logs info */
  189. char *log_auditinfo_file; /* audit log rotation info file */
  190. rwl *log_audit_rwlock; /* lock on audit*/
  191. };
  192. /* For log_state */
  193. #define LOGGING_ENABLED (int) 0x1 /* logging is enabled */
  194. #define LOGGING_NEED_TITLE 0x2 /* need to write title */
  195. #define LOG_ACCESS_LOCK_READ() PR_Lock(loginfo.log_access_buffer->lock)
  196. #define LOG_ACCESS_UNLOCK_READ() PR_Unlock(loginfo.log_access_buffer->lock)
  197. #define LOG_ACCESS_LOCK_WRITE() PR_Lock(loginfo.log_access_buffer->lock)
  198. #define LOG_ACCESS_UNLOCK_WRITE() PR_Unlock(loginfo.log_access_buffer->lock)
  199. #define LOG_ERROR_LOCK_READ() loginfo.log_error_rwlock->rwl_acquire_read_lock(loginfo.log_error_rwlock)
  200. #define LOG_ERROR_UNLOCK_READ() loginfo.log_error_rwlock->rwl_relinquish_read_lock(loginfo.log_error_rwlock)
  201. #define LOG_ERROR_LOCK_WRITE() loginfo.log_error_rwlock->rwl_acquire_write_lock(loginfo.log_error_rwlock)
  202. #define LOG_ERROR_UNLOCK_WRITE() loginfo.log_error_rwlock->rwl_relinquish_write_lock(loginfo.log_error_rwlock)
  203. #define LOG_AUDIT_LOCK_READ() loginfo.log_audit_rwlock->rwl_acquire_read_lock(loginfo.log_audit_rwlock)
  204. #define LOG_AUDIT_UNLOCK_READ() loginfo.log_audit_rwlock->rwl_relinquish_read_lock(loginfo.log_audit_rwlock)
  205. #define LOG_AUDIT_LOCK_WRITE() loginfo.log_audit_rwlock->rwl_acquire_write_lock(loginfo.log_audit_rwlock)
  206. #define LOG_AUDIT_UNLOCK_WRITE() loginfo.log_audit_rwlock->rwl_relinquish_write_lock(loginfo.log_audit_rwlock)