suricata
util-logopenfile.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2026 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Mike Pomraning <mpomraning@qualys.com>
22  */
23 
24 #ifndef SURICATA_UTIL_LOGOPENFILE_H
25 #define SURICATA_UTIL_LOGOPENFILE_H
26 
27 #include "threads.h"
28 #include "conf.h" /* ConfNode */
29 #include "util-buffer.h"
30 #include "util-hash.h"
31 
32 #ifdef HAVE_LIBHIREDIS
33 #include "util-log-redis.h"
34 #endif /* HAVE_LIBHIREDIS */
35 
36 #include "output-eve.h"
37 
43  /** New style or modular filetypes. */
46 };
47 
48 typedef struct ThreadLogFileHashEntry {
49  struct LogFileCtx_ *ctx;
50 
51  uint64_t thread_id; /* OS thread identifier */
52  ThreadId internal_thread_id; /* Suri internal thread id; to assist output plugins correlating
53  usage */
54  uint16_t slot_number; /* Slot identifier - used when forming per-thread output names*/
55  bool isopen;
57 
58 struct LogFileCtx_;
59 typedef struct LogThreadedFileCtx_ {
62  char *append;
64 
65 typedef struct LogFileTypeCtx_ {
67  void *init_data;
68  void *thread_data;
70 
71 typedef struct LogFileEntry_ {
72  struct LogFileCtx_ *ctx;
75 
76 typedef enum LogFileRotateUnit_ {
78  LOGFILE_ROTATE_ON_FIXED_PERIOD, /* relative interval, e.g. 30s */
83 
84 /** Global structure for Output Context */
85 typedef struct LogFileCtx_ {
86  union {
87  FILE *fp;
88 #ifdef HAVE_LIBHIREDIS
89  void *redis;
90 #endif
91  };
93 
94  union {
95 #ifdef HAVE_LIBHIREDIS
96  RedisSetup redis_setup;
97 #endif
98  };
99 
100  int (*Write)(const char *buffer, int buffer_len, struct LogFileCtx_ *fp);
101  void (*Close)(struct LogFileCtx_ *fp);
102  void (*Flush)(struct LogFileCtx_ *fp);
103  void (*Rotate)(struct LogFileCtx_ *fp);
104 
106 
107  /** It will be locked if the log/alert
108  * record cannot be written to the file in one call */
110 
111  /** When threaded, track of the parent and thread id */
112  bool threaded;
115 
116  /** the type of file */
117  enum LogFileType type;
118 
119  /** The name of the file */
120  char *filename;
121 
122  /** File permissions */
123  uint32_t filemode;
124 
125  /** File buffering */
126  uint32_t buffer_size;
127 
128  /** Suricata sensor name */
129  char *sensor_name;
130 
131  /** Handle auto-connecting / reconnecting sockets */
132  int is_sock;
134  uint64_t reconn_timer;
135 
136  /** The next time to rotate log file, if rotate interval is
137  specified. For calendar units, the next time to check if the
138  local time period changed. */
139  time_t rotate_time;
140 
141  /** The interval to rotate the log file */
142  uint64_t rotate_interval;
143 
145  /** Local time period of the open file, for calendar units */
146  int64_t rotate_period;
147 
148  /**< Used by some alert loggers like the unified ones that append
149  * the date onto the end of files. */
150  char *prefix;
151  uint32_t prefix_len;
152 
153  /** Generic size_limit and size_current
154  * They must be common to the threads accessing the same file */
155  uint64_t size_limit; /**< file size limit */
156  uint64_t size_current; /**< file current size */
157 
158  /* flag to avoid multiple threads printing the same stats */
159  uint8_t flags;
160 
161  /* flags to set when sending over a socket */
162  uint8_t send_flags;
163 
164  /* Flag if file is a regular file or not. Only regular files
165  * allow for rotation. */
166  uint8_t is_regular;
167 
168  /* Compress ipv6 addresses? */
170 
171  /* Flag set when file rotation notification is received. */
173 
174  /* JSON flags */
175  size_t json_flags; /* passed to json_dump_callback() */
176 
177  /* if set to true EVE will add a pcap file record */
179 
180  /* Socket types may need to drop events to keep from blocking
181  * Suricata. */
182  uint64_t dropped;
183 
184  uint64_t output_errors;
185 
186  /* Track buffered content */
189 
190 /* Min time (msecs) before trying to reconnect a Unix domain socket */
191 #define LOGFILE_RECONN_MIN_TIME 500
192 
193 /* Default EVE output buffering size */
194 #define LOGFILE_EVE_BUFFER_SIZE 0
195 
198 int LogFileWrite(LogFileCtx *file_ctx, MemBuffer *buffer);
199 void LogFileFlush(LogFileCtx *file_ctx);
200 
202 int SCConfLogOpenGeneric(SCConfNode *conf, LogFileCtx *, const char *, int);
204 bool SCLogOpenThreadedFile(const char *log_path, const char *append, LogFileCtx *parent_ctx);
205 
206 /* Log file list management functions */
209 void LogFileFlushAll(void);
210 void LogFileRotateAll(void);
211 
212 void LogFileRegisterTests(void);
213 
214 #endif /* SURICATA_UTIL_LOGOPENFILE_H */
LogFileCtx_::rotation_flag
int rotation_flag
Definition: util-logopenfile.h:172
LogThreadedFileCtx_::append
char * append
Definition: util-logopenfile.h:62
LogFileCtx
struct LogFileCtx_ LogFileCtx
LogFileEntry_::ctx
struct LogFileCtx_ * ctx
Definition: util-logopenfile.h:72
LOGFILE_TYPE_REDIS
@ LOGFILE_TYPE_REDIS
Definition: util-logopenfile.h:42
LogFileCtx_::sensor_name
char * sensor_name
Definition: util-logopenfile.h:129
LogFileCtx_::reconn_timer
uint64_t reconn_timer
Definition: util-logopenfile.h:134
LogFileCtx_::fp_mutex
SCMutex fp_mutex
Definition: util-logopenfile.h:109
LOGFILE_ROTATE_ON_FIXED_PERIOD
@ LOGFILE_ROTATE_ON_FIXED_PERIOD
Definition: util-logopenfile.h:78
LogFileUnregister
void LogFileUnregister(LogFileCtx *ctx)
Unregister a LogFileCtx from maintenance operations.
Definition: util-logopenfile.c:1125
LogFileCtx_::rotate_period
int64_t rotate_period
Definition: util-logopenfile.h:146
LOGFILE_ROTATE_ON_NEW_DAY
@ LOGFILE_ROTATE_ON_NEW_DAY
Definition: util-logopenfile.h:81
threads.h
LogFileCtx_::json_flags
size_t json_flags
Definition: util-logopenfile.h:175
SCLogOpenThreadedFile
bool SCLogOpenThreadedFile(const char *log_path, const char *append, LogFileCtx *parent_ctx)
Definition: util-logopenfile.c:462
util-hash.h
LogFileRegister
void LogFileRegister(LogFileCtx *ctx)
Register a LogFileCtx for maintenance operations.
Definition: util-logopenfile.c:1099
ctx
struct Thresholds ctx
LogFileCtx_
Definition: util-logopenfile.h:85
LogThreadedFileCtx
struct LogThreadedFileCtx_ LogThreadedFileCtx
LogFileEntry
struct LogFileEntry_ LogFileEntry
LOGFILE_TYPE_NOTSET
@ LOGFILE_TYPE_NOTSET
Definition: util-logopenfile.h:45
HashTable_
Definition: util-hash.h:35
util-log-redis.h
LogFileTypeCtx
struct LogFileTypeCtx_ LogFileTypeCtx
LogFileCtx_::size_current
uint64_t size_current
Definition: util-logopenfile.h:156
LogFileCtx_::Write
int(* Write)(const char *buffer, int buffer_len, struct LogFileCtx_ *fp)
Definition: util-logopenfile.h:100
LogFileCtx_::filename
char * filename
Definition: util-logopenfile.h:120
LogFileCtx_::size_limit
uint64_t size_limit
Definition: util-logopenfile.h:155
ThreadLogFileHashEntry::ctx
struct LogFileCtx_ * ctx
Definition: util-logopenfile.h:49
LogFileCtx_::send_flags
uint8_t send_flags
Definition: util-logopenfile.h:162
LogFileFlush
void LogFileFlush(LogFileCtx *file_ctx)
Definition: util-logopenfile.c:1085
ThreadLogFileHashEntry::slot_number
uint16_t slot_number
Definition: util-logopenfile.h:54
ThreadLogFileHashEntry::isopen
bool isopen
Definition: util-logopenfile.h:55
LogFileCtx_::sock_type
int sock_type
Definition: util-logopenfile.h:133
LOGFILE_ROTATE_ON_NEW_HOUR
@ LOGFILE_ROTATE_ON_NEW_HOUR
Definition: util-logopenfile.h:80
LogFileCtx_::filetype
LogFileTypeCtx filetype
Definition: util-logopenfile.h:105
LogFileFlushAll
void LogFileFlushAll(void)
Flush all registered LogFileCtx instances.
Definition: util-logopenfile.c:1148
LogFileCtx_::rotate_unit
LogFileRotateUnit rotate_unit
Definition: util-logopenfile.h:144
ThreadLogFileHashEntry
struct ThreadLogFileHashEntry ThreadLogFileHashEntry
LogFileCtx_::rotate_interval
uint64_t rotate_interval
Definition: util-logopenfile.h:142
LogFileCtx_::is_sock
int is_sock
Definition: util-logopenfile.h:132
LogFileCtx_::prefix_len
uint32_t prefix_len
Definition: util-logopenfile.h:151
LogFileCtx_::dropped
uint64_t dropped
Definition: util-logopenfile.h:182
LogFileRotateUnit
enum LogFileRotateUnit_ LogFileRotateUnit
ThreadLogFileHashEntry::internal_thread_id
ThreadId internal_thread_id
Definition: util-logopenfile.h:52
LogFileTypeCtx_
Definition: util-logopenfile.h:65
LogFileCtx_::type
enum LogFileType type
Definition: util-logopenfile.h:117
LOGFILE_TYPE_FILE
@ LOGFILE_TYPE_FILE
Definition: util-logopenfile.h:39
LogFileCtx_::threads
LogThreadedFileCtx * threads
Definition: util-logopenfile.h:92
ThreadLogFileHashEntry::thread_id
uint64_t thread_id
Definition: util-logopenfile.h:51
conf.h
LOGFILE_TYPE_UNIX_DGRAM
@ LOGFILE_TYPE_UNIX_DGRAM
Definition: util-logopenfile.h:40
LogFileTypeCtx_::thread_data
void * thread_data
Definition: util-logopenfile.h:68
LogFileCtx_::compress_ipv6
bool compress_ipv6
Definition: util-logopenfile.h:169
LOGFILE_TYPE_UNIX_STREAM
@ LOGFILE_TYPE_UNIX_STREAM
Definition: util-logopenfile.h:41
MemBuffer_
Definition: util-buffer.h:27
LogFileCtx_::is_regular
uint8_t is_regular
Definition: util-logopenfile.h:166
LogFileCtx_::buffer_size
uint32_t buffer_size
Definition: util-logopenfile.h:126
LogFileWrite
int LogFileWrite(LogFileCtx *file_ctx, MemBuffer *buffer)
Definition: util-logopenfile.c:1178
LogFileCtx_::bytes_since_last_flush
uint64_t bytes_since_last_flush
Definition: util-logopenfile.h:187
LogFileCtx_::parent
struct LogFileCtx_ * parent
Definition: util-logopenfile.h:113
LogFileFreeCtx
int LogFileFreeCtx(LogFileCtx *)
LogFileFreeCtx() Destroy a LogFileCtx (Close the file and free memory)
Definition: util-logopenfile.c:1012
LogFileEntry_
Definition: util-logopenfile.h:71
LogThreadedFileCtx_
Definition: util-logopenfile.h:59
LogFileTypeCtx_::init_data
void * init_data
Definition: util-logopenfile.h:67
SCConfLogOpenGeneric
int SCConfLogOpenGeneric(SCConfNode *conf, LogFileCtx *, const char *, int)
open a generic output "log file", which may be a regular file or a socket
Definition: util-logopenfile.c:564
LogFileCtx_::output_errors
uint64_t output_errors
Definition: util-logopenfile.h:184
ThreadLogFileHashEntry
Definition: util-logopenfile.h:48
ThreadId
uint32_t ThreadId
Definition: output-eve-bindgen.h:31
LogFileCtx_::Flush
void(* Flush)(struct LogFileCtx_ *fp)
Definition: util-logopenfile.h:102
LogFileCtx_::entry
ThreadLogFileHashEntry * entry
Definition: util-logopenfile.h:114
LogFileCtx_::rotate_time
time_t rotate_time
Definition: util-logopenfile.h:139
LOGFILE_ROTATE_ON_NEW_MINUTE
@ LOGFILE_ROTATE_ON_NEW_MINUTE
Definition: util-logopenfile.h:79
LogFileRegisterTests
void LogFileRegisterTests(void)
Definition: util-logopenfile.c:1328
output-eve.h
EVE logging subsystem.
LOGFILE_ROTATE_NONE
@ LOGFILE_ROTATE_NONE
Definition: util-logopenfile.h:77
SCConfLogReopen
int SCConfLogReopen(LogFileCtx *)
Reopen a regular log file with the side-affect of truncating it.
Definition: util-logopenfile.c:764
util-buffer.h
LogThreadedFileCtx_::mutex
SCMutex mutex
Definition: util-logopenfile.h:60
LogFileCtx_::prefix
char * prefix
Definition: util-logopenfile.h:150
LogFileCtx_::is_pcap_offline
bool is_pcap_offline
Definition: util-logopenfile.h:178
LogFileType
LogFileType
Definition: util-logopenfile.h:38
LogFileEnsureExists
LogFileCtx * LogFileEnsureExists(ThreadId thread_id, LogFileCtx *lf_ctx)
LogFileEnsureExists() Ensure a log file context for the thread exists.
Definition: util-logopenfile.c:847
LogFileCtx_::flags
uint8_t flags
Definition: util-logopenfile.h:159
LogFileCtx_::Close
void(* Close)(struct LogFileCtx_ *fp)
Definition: util-logopenfile.h:101
LogFileTypeCtx_::filetype
SCEveFileType * filetype
Definition: util-logopenfile.h:66
LogThreadedFileCtx_::ht
HashTable * ht
Definition: util-logopenfile.h:61
SCConfNode_
Definition: conf.h:37
LogFileCtx_::Rotate
void(* Rotate)(struct LogFileCtx_ *fp)
Definition: util-logopenfile.h:103
LogFileCtx_::fp
FILE * fp
Definition: util-logopenfile.h:87
SCMutex
#define SCMutex
Definition: threads-debug.h:114
LOGFILE_TYPE_FILETYPE
@ LOGFILE_TYPE_FILETYPE
Definition: util-logopenfile.h:44
LogFileEntry_::TAILQ_ENTRY
TAILQ_ENTRY(LogFileEntry_) entries
LogFileCtx_::threaded
bool threaded
Definition: util-logopenfile.h:112
LogFileRotateUnit_
LogFileRotateUnit_
Definition: util-logopenfile.h:76
LogFileNewCtx
LogFileCtx * LogFileNewCtx(void)
LogFileNewCtx() Get a new LogFileCtx.
Definition: util-logopenfile.c:795
SCEveFileType_
Structure used to define an EVE output file type.
Definition: output-eve-bindgen.h:110
LogFileRotateAll
void LogFileRotateAll(void)
Check rotation for all registered LogFileCtx instances.
Definition: util-logopenfile.c:1166
LogFileCtx_::filemode
uint32_t filemode
Definition: util-logopenfile.h:123