suricata
util-logopenfile.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2021 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 SyslogSetup_ {
51 
52 typedef struct ThreadLogFileHashEntry {
53  struct LogFileCtx_ *ctx;
54 
55  uint64_t thread_id; /* OS thread identifier */
56  ThreadId internal_thread_id; /* Suri internal thread id; to assist output plugins correlating
57  usage */
58  uint16_t slot_number; /* Slot identifier - used when forming per-thread output names*/
59  bool isopen;
61 
62 struct LogFileCtx_;
63 typedef struct LogThreadedFileCtx_ {
66  char *append;
68 
69 typedef struct LogFileTypeCtx_ {
71  void *init_data;
72  void *thread_data;
74 
75 /** Global structure for Output Context */
76 typedef struct LogFileCtx_ {
77  union {
78  FILE *fp;
79 #ifdef HAVE_LIBHIREDIS
80  void *redis;
81 #endif
82  };
84 
85  union {
86 #ifdef HAVE_LIBHIREDIS
87  RedisSetup redis_setup;
88 #endif
89  };
90 
91  int (*Write)(const char *buffer, int buffer_len, struct LogFileCtx_ *fp);
92  void (*Close)(struct LogFileCtx_ *fp);
93 
95 
96  /** It will be locked if the log/alert
97  * record cannot be written to the file in one call */
99 
100  /** When threaded, track of the parent and thread id */
101  bool threaded;
104 
105  /** the type of file */
106  enum LogFileType type;
107 
108  /** The name of the file */
109  char *filename;
110 
111  /** File permissions */
112  uint32_t filemode;
113 
114  /** Suricata sensor name */
115  char *sensor_name;
116 
117  /** Handle auto-connecting / reconnecting sockets */
118  int is_sock;
120  uint64_t reconn_timer;
121 
122  /** The next time to rotate log file, if rotate interval is
123  specified. */
124  time_t rotate_time;
125 
126  /** The interval to rotate the log file */
127  uint64_t rotate_interval;
128 
129  /**< Used by some alert loggers like the unified ones that append
130  * the date onto the end of files. */
131  char *prefix;
132  size_t prefix_len;
133 
134  /** Generic size_limit and size_current
135  * They must be common to the threads accessing the same file */
136  uint64_t size_limit; /**< file size limit */
137  uint64_t size_current; /**< file current size */
138 
139  /* flag to avoid multiple threads printing the same stats */
140  uint8_t flags;
141 
142  /* flags to set when sending over a socket */
143  uint8_t send_flags;
144 
145  /* Flag if file is a regular file or not. Only regular files
146  * allow for rotation. */
147  uint8_t is_regular;
148 
149  /* JSON flags */
150  size_t json_flags; /* passed to json_dump_callback() */
151 
152  /* Flag set when file rotation notification is received. */
154 
155  /* if set to true EVE will add a pcap file record */
157 
158  /* Socket types may need to drop events to keep from blocking
159  * Suricata. */
160  uint64_t dropped;
161 
162  uint64_t output_errors;
164 
165 /* Min time (msecs) before trying to reconnect a Unix domain socket */
166 #define LOGFILE_RECONN_MIN_TIME 500
167 
168 /* flags for LogFileCtx */
169 #define LOGFILE_ROTATE_INTERVAL 0x04
170 
173 int LogFileWrite(LogFileCtx *file_ctx, MemBuffer *buffer);
174 
176 int SCConfLogOpenGeneric(ConfNode *conf, LogFileCtx *, const char *, int);
178 bool SCLogOpenThreadedFile(const char *log_path, const char *append, LogFileCtx *parent_ctx);
179 
180 #endif /* SURICATA_UTIL_LOGOPENFILE_H */
LogFileCtx_::rotation_flag
int rotation_flag
Definition: util-logopenfile.h:153
LogFileCtx_::prefix_len
size_t prefix_len
Definition: util-logopenfile.h:132
SyslogSetup_::alert_syslog_level
int alert_syslog_level
Definition: util-logopenfile.h:49
LogThreadedFileCtx_::append
char * append
Definition: util-logopenfile.h:66
LogFileCtx
struct LogFileCtx_ LogFileCtx
LOGFILE_TYPE_REDIS
@ LOGFILE_TYPE_REDIS
Definition: util-logopenfile.h:42
LogFileCtx_::sensor_name
char * sensor_name
Definition: util-logopenfile.h:115
LogFileCtx_::reconn_timer
uint64_t reconn_timer
Definition: util-logopenfile.h:120
LogFileCtx_::fp_mutex
SCMutex fp_mutex
Definition: util-logopenfile.h:98
threads.h
LogFileCtx_::json_flags
size_t json_flags
Definition: util-logopenfile.h:150
SCLogOpenThreadedFile
bool SCLogOpenThreadedFile(const char *log_path, const char *append, LogFileCtx *parent_ctx)
Definition: util-logopenfile.c:359
SyslogSetup_
Definition: util-logopenfile.h:48
util-hash.h
LogFileCtx_
Definition: util-logopenfile.h:76
LogThreadedFileCtx
struct LogThreadedFileCtx_ LogThreadedFileCtx
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:137
LogFileCtx_::Write
int(* Write)(const char *buffer, int buffer_len, struct LogFileCtx_ *fp)
Definition: util-logopenfile.h:91
LogFileCtx_::filename
char * filename
Definition: util-logopenfile.h:109
LogFileCtx_::size_limit
uint64_t size_limit
Definition: util-logopenfile.h:136
ThreadLogFileHashEntry::ctx
struct LogFileCtx_ * ctx
Definition: util-logopenfile.h:53
SyslogSetup
struct SyslogSetup_ SyslogSetup
LogFileCtx_::send_flags
uint8_t send_flags
Definition: util-logopenfile.h:143
ThreadLogFileHashEntry::slot_number
uint16_t slot_number
Definition: util-logopenfile.h:58
ThreadLogFileHashEntry::isopen
bool isopen
Definition: util-logopenfile.h:59
ThreadId
uint32_t ThreadId
Definition: output-eve.h:36
LogFileCtx_::sock_type
int sock_type
Definition: util-logopenfile.h:119
LogFileCtx_::filetype
LogFileTypeCtx filetype
Definition: util-logopenfile.h:94
ThreadLogFileHashEntry
struct ThreadLogFileHashEntry ThreadLogFileHashEntry
LogFileCtx_::rotate_interval
uint64_t rotate_interval
Definition: util-logopenfile.h:127
LogFileCtx_::is_sock
int is_sock
Definition: util-logopenfile.h:118
LogFileCtx_::dropped
uint64_t dropped
Definition: util-logopenfile.h:160
ThreadLogFileHashEntry::internal_thread_id
ThreadId internal_thread_id
Definition: util-logopenfile.h:56
LogFileTypeCtx_
Definition: util-logopenfile.h:69
LogFileCtx_::type
enum LogFileType type
Definition: util-logopenfile.h:106
LOGFILE_TYPE_FILE
@ LOGFILE_TYPE_FILE
Definition: util-logopenfile.h:39
LogFileCtx_::threads
LogThreadedFileCtx * threads
Definition: util-logopenfile.h:83
ThreadLogFileHashEntry::thread_id
uint64_t thread_id
Definition: util-logopenfile.h:55
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:72
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:147
LogFileWrite
int LogFileWrite(LogFileCtx *file_ctx, MemBuffer *buffer)
Definition: util-logopenfile.c:925
LogFileCtx_::parent
struct LogFileCtx_ * parent
Definition: util-logopenfile.h:102
LogFileFreeCtx
int LogFileFreeCtx(LogFileCtx *)
LogFileFreeCtx() Destroy a LogFileCtx (Close the file and free memory)
Definition: util-logopenfile.c:868
SCConfLogOpenGeneric
int SCConfLogOpenGeneric(ConfNode *conf, LogFileCtx *, const char *, int)
open a generic output "log file", which may be a regular file or a socket
Definition: util-logopenfile.c:452
LogThreadedFileCtx_
Definition: util-logopenfile.h:63
LogFileTypeCtx_::init_data
void * init_data
Definition: util-logopenfile.h:71
LogFileCtx_::output_errors
uint64_t output_errors
Definition: util-logopenfile.h:162
ThreadLogFileHashEntry
Definition: util-logopenfile.h:52
LogFileCtx_::entry
ThreadLogFileHashEntry * entry
Definition: util-logopenfile.h:103
LogFileCtx_::rotate_time
time_t rotate_time
Definition: util-logopenfile.h:124
output-eve.h
EVE logging subsystem.
ConfNode_
Definition: conf.h:32
SCConfLogReopen
int SCConfLogReopen(LogFileCtx *)
Reopen a regular log file with the side-affect of truncating it.
Definition: util-logopenfile.c:629
util-buffer.h
LogThreadedFileCtx_::mutex
SCMutex mutex
Definition: util-logopenfile.h:64
LogFileCtx_::prefix
char * prefix
Definition: util-logopenfile.h:131
LogFileCtx_::is_pcap_offline
bool is_pcap_offline
Definition: util-logopenfile.h:156
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:709
LogFileCtx_::flags
uint8_t flags
Definition: util-logopenfile.h:140
LogFileCtx_::Close
void(* Close)(struct LogFileCtx_ *fp)
Definition: util-logopenfile.h:92
LogFileTypeCtx_::filetype
SCEveFileType * filetype
Definition: util-logopenfile.h:70
LogThreadedFileCtx_::ht
HashTable * ht
Definition: util-logopenfile.h:65
LogFileCtx_::fp
FILE * fp
Definition: util-logopenfile.h:78
SCMutex
#define SCMutex
Definition: threads-debug.h:114
LOGFILE_TYPE_FILETYPE
@ LOGFILE_TYPE_FILETYPE
Definition: util-logopenfile.h:44
LogFileCtx_::threaded
bool threaded
Definition: util-logopenfile.h:101
LogFileNewCtx
LogFileCtx * LogFileNewCtx(void)
LogFileNewCtx() Get a new LogFileCtx.
Definition: util-logopenfile.c:659
SCEveFileType_
Structure used to define an EVE output file type plugin.
Definition: output-eve.h:73
LogFileCtx_::filemode
uint32_t filemode
Definition: util-logopenfile.h:112