log.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. /***********************************************************************
  39. * log.h
  40. *
  41. * structures related to logging facility.
  42. *
  43. *************************************************************************/
  44. #include <stdio.h>
  45. #include <time.h>
  46. #include <stdarg.h>
  47. #include <sys/types.h>
  48. #include <sys/stat.h>
  49. #ifndef _WIN32
  50. #include <errno.h>
  51. #ifdef LINUX
  52. #include <sys/statfs.h>
  53. #else
  54. #include <sys/statvfs.h>
  55. #endif
  56. #endif
  57. #include <fcntl.h>
  58. #include "prio.h"
  59. #include "prprf.h"
  60. #include "slap.h"
  61. #include "slapi-plugin.h"
  62. #define LOG_MB_IN_BYTES (1024 * 1024)
  63. #define LOG_SUCCESS 0 /* fine & dandy */
  64. #define LOG_CONTINUE LOG_SUCCESS
  65. #define LOG_ERROR 1 /* default error case */
  66. #define LOG_EXCEEDED 2 /*err: > max logs allowed */
  67. #define LOG_ROTATE 3 /*ok; go to the next log */
  68. #define LOG_UNABLE_TO_OPENFILE 4
  69. #define LOG_UNIT_UNKNOWN 0
  70. #define LOG_UNIT_MONTHS 1
  71. #define LOG_UNIT_WEEKS 2
  72. #define LOG_UNIT_DAYS 3
  73. #define LOG_UNIT_HOURS 4
  74. #define LOG_UNIT_MINS 5
  75. #define LOGFILE_NEW 0
  76. #define LOGFILE_REOPENED 1
  77. #define LOG_UNIT_TYPE_UNKNOWN "unknown"
  78. #define LOG_UNIT_TYPE_MONTHS "month"
  79. #define LOG_UNIT_TYPE_WEEKS "week"
  80. #define LOG_UNIT_TYPE_DAYS "day"
  81. #define LOG_UNIT_TYPE_HOURS "hour"
  82. #define LOG_UNIT_TYPE_MINUTES "minute"
  83. #define LOG_BUFFER_MAXSIZE 512 * 1024
  84. /* see log.c for why this is done */
  85. #ifdef XP_WIN32
  86. typedef FILE *LOGFD;
  87. #else
  88. typedef PRFileDesc *LOGFD;
  89. #endif
  90. struct logfileinfo {
  91. int l_size; /* size is in KB */
  92. time_t l_ctime; /* log creation time*/
  93. struct logfileinfo *l_next; /* next log */
  94. };
  95. typedef struct logfileinfo LogFileInfo;
  96. struct logbufinfo {
  97. char *top; /* beginning of the buffer */
  98. char *current; /* current pointer into buffer */
  99. size_t maxsize; /* size of buffer */
  100. PRLock *lock; /* lock for access logging */
  101. PRInt32 refcount; /* Reference count for buffer copies */
  102. };
  103. typedef struct logbufinfo LogBufferInfo;
  104. struct logging_opts {
  105. /* These are access log specific */
  106. int log_access_state;
  107. int log_access_mode; /* access mode */
  108. int log_access_maxnumlogs; /* Number of logs */
  109. int log_access_maxlogsize; /* max log size in bytes*/
  110. int log_access_rotationtime; /* time in units. */
  111. int log_access_rotationunit; /* time in units. */
  112. int log_access_rotationtime_secs; /* time in seconds */
  113. int log_access_rotationsync_enabled;/* 0 or 1*/
  114. int log_access_rotationsynchour; /* 0-23 */
  115. int log_access_rotationsyncmin; /* 0-59 */
  116. time_t log_access_rotationsyncclock; /* clock in seconds */
  117. int log_access_maxdiskspace; /* space in bytes */
  118. int log_access_minfreespace; /* free space in bytes */
  119. int log_access_exptime; /* time */
  120. int log_access_exptimeunit; /* unit time */
  121. int log_access_exptime_secs; /* time in secs */
  122. int log_access_level; /* access log level */
  123. char *log_access_file; /* access log file path */
  124. LOGFD log_access_fdes; /* fp for the cur access log */
  125. unsigned int log_numof_access_logs; /* number of logs */
  126. time_t log_access_ctime; /* log creation time */
  127. LogFileInfo *log_access_logchain; /* all the logs info */
  128. char *log_accessinfo_file; /* access log rotation info file */
  129. LogBufferInfo *log_access_buffer; /* buffer for access log */
  130. /* These are error log specific */
  131. int log_error_state;
  132. int log_error_mode; /* access mode */
  133. int log_error_maxnumlogs; /* Number of logs */
  134. int log_error_maxlogsize; /* max log size in bytes*/
  135. int log_error_rotationtime; /* time in units. */
  136. int log_error_rotationunit; /* time in units. */
  137. int log_error_rotationtime_secs; /* time in seconds */
  138. int log_error_rotationsync_enabled;/* 0 or 1*/
  139. int log_error_rotationsynchour; /* 0-23 */
  140. int log_error_rotationsyncmin; /* 0-59 */
  141. time_t log_error_rotationsyncclock; /* clock in seconds */
  142. int log_error_maxdiskspace; /* space in bytes */
  143. int log_error_minfreespace; /* free space in bytes */
  144. int log_error_exptime; /* time */
  145. int log_error_exptimeunit; /* unit time */
  146. int log_error_exptime_secs; /* time in secs */
  147. char *log_error_file; /* error log file path */
  148. LOGFD log_error_fdes; /* fp for the cur error log */
  149. unsigned int log_numof_error_logs; /* number of logs */
  150. time_t log_error_ctime; /* log creation time */
  151. LogFileInfo *log_error_logchain; /* all the logs info */
  152. char *log_errorinfo_file; /* error log rotation info file */
  153. rwl *log_error_rwlock; /* lock on error*/
  154. /* These are audit log specific */
  155. int log_audit_state;
  156. int log_audit_mode; /* access mode */
  157. int log_audit_maxnumlogs; /* Number of logs */
  158. int log_audit_maxlogsize; /* max log size in bytes*/
  159. int log_audit_rotationtime; /* time in units. */
  160. int log_audit_rotationunit; /* time in units. */
  161. int log_audit_rotationtime_secs; /* time in seconds */
  162. int log_audit_rotationsync_enabled;/* 0 or 1*/
  163. int log_audit_rotationsynchour; /* 0-23 */
  164. int log_audit_rotationsyncmin; /* 0-59 */
  165. time_t log_audit_rotationsyncclock; /* clock in seconds */
  166. int log_audit_maxdiskspace; /* space in bytes */
  167. int log_audit_minfreespace; /* free space in bytes */
  168. int log_audit_exptime; /* time */
  169. int log_audit_exptimeunit; /* unit time */
  170. int log_audit_exptime_secs; /* time in secs */
  171. char *log_audit_file; /* aufit log name */
  172. LOGFD log_audit_fdes; /* audit log fdes */
  173. unsigned int log_numof_audit_logs; /* number of logs */
  174. time_t log_audit_ctime; /* log creation time */
  175. LogFileInfo *log_audit_logchain; /* all the logs info */
  176. char *log_auditinfo_file; /* audit log rotation info file */
  177. rwl *log_audit_rwlock; /* lock on audit*/
  178. };
  179. /* For log_state */
  180. #define LOGGING_ENABLED (int) 0x1 /* logging is enabled */
  181. #define LOGGING_NEED_TITLE 0x2 /* need to write title */
  182. #define LOG_ACCESS_LOCK_READ() PR_Lock(loginfo.log_access_buffer->lock)
  183. #define LOG_ACCESS_UNLOCK_READ() PR_Unlock(loginfo.log_access_buffer->lock)
  184. #define LOG_ACCESS_LOCK_WRITE() PR_Lock(loginfo.log_access_buffer->lock)
  185. #define LOG_ACCESS_UNLOCK_WRITE() PR_Unlock(loginfo.log_access_buffer->lock)
  186. #define LOG_ERROR_LOCK_READ() loginfo.log_error_rwlock->rwl_acquire_read_lock(loginfo.log_error_rwlock)
  187. #define LOG_ERROR_UNLOCK_READ() loginfo.log_error_rwlock->rwl_relinquish_read_lock(loginfo.log_error_rwlock)
  188. #define LOG_ERROR_LOCK_WRITE() loginfo.log_error_rwlock->rwl_acquire_write_lock(loginfo.log_error_rwlock)
  189. #define LOG_ERROR_UNLOCK_WRITE() loginfo.log_error_rwlock->rwl_relinquish_write_lock(loginfo.log_error_rwlock)
  190. #define LOG_AUDIT_LOCK_READ() loginfo.log_audit_rwlock->rwl_acquire_read_lock(loginfo.log_audit_rwlock)
  191. #define LOG_AUDIT_UNLOCK_READ() loginfo.log_audit_rwlock->rwl_relinquish_read_lock(loginfo.log_audit_rwlock)
  192. #define LOG_AUDIT_LOCK_WRITE() loginfo.log_audit_rwlock->rwl_acquire_write_lock(loginfo.log_audit_rwlock)
  193. #define LOG_AUDIT_UNLOCK_WRITE() loginfo.log_audit_rwlock->rwl_relinquish_write_lock(loginfo.log_audit_rwlock)