suricata
log-flush.c
Go to the documentation of this file.
1 /* Copyright (C) 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 Jeff Lucovsky <jlucovsky@oisf.net>
22  */
23 
24 #include "suricata-common.h"
25 #include "suricata.h"
26 #include "log-flush.h"
27 #include "util-logopenfile.h"
28 #include "tm-threads.h"
29 #include "conf.h"
30 #include "conf-yaml-loader.h"
31 #include "util-privs.h"
32 #include "util-logopenfile.h"
33 
35 {
36  intmax_t output_flush_interval = 0;
37  if (SCConfGetInt("heartbeat.output-flush-interval", &output_flush_interval) == 0) {
38  output_flush_interval = 0;
39  }
40  if (output_flush_interval < 0 || output_flush_interval > 60) {
41  SCLogConfig("flush_interval must be 0 or less than 60; using 0");
42  output_flush_interval = 0;
43  }
44 
45  return (int)output_flush_interval;
46 }
47 
48 static void *LogFlusherWakeupThread(void *arg)
49 {
50  int output_flush_interval = OutputFlushInterval();
51  /* This was checked by the logic creating this thread */
52  BUG_ON(output_flush_interval == 0);
53 
54  SCLogConfig("Using output-flush-interval of %d seconds", output_flush_interval);
55  /*
56  * Calculate the number of sleep intervals based on the output flush interval. This is necessary
57  * because this thread pauses a fixed amount of time to react to shutdown situations more
58  * quickly.
59  */
60  const int log_flush_sleep_time = 500; /* milliseconds */
61  const int flush_wait_count = (1000 * output_flush_interval) / log_flush_sleep_time;
62 
63  ThreadVars *tv_local = (ThreadVars *)arg;
64  SCSetThreadName(tv_local->name);
65 
66  if (tv_local->thread_setup_flags != 0)
67  TmThreadSetupOptions(tv_local);
68 
69  /* Set the threads capability */
70  tv_local->cap_flags = 0;
71  SCDropCaps(tv_local);
72 
74 
75  int wait_count = 0;
76  uint64_t worker_flush_count = 0;
77  bool run = TmThreadsWaitForUnpause(tv_local);
78  while (run) {
79  SleepMsec(log_flush_sleep_time);
80 
81  if (++wait_count == flush_wait_count) {
82  worker_flush_count++;
84  wait_count = 0;
85  }
86 
87  if (TmThreadsCheckFlag(tv_local, THV_KILL)) {
88  break;
89  }
90  }
91 
94  TmThreadsSetFlag(tv_local, THV_CLOSED);
95  SCLogInfo("%s: initiated %" PRIu64 " flushes", tv_local->name, worker_flush_count);
96  return NULL;
97 }
98 
99 void LogFlushThreads(void)
100 {
101  if (0 == OutputFlushInterval()) {
102  SCLogConfig("log flusher thread not used with heartbeat.output-flush-interval of 0");
103  return;
104  }
105 
106  ThreadVars *tv_log_flush =
107  TmThreadCreateMgmtThread(thread_name_heartbeat, LogFlusherWakeupThread, 1);
108  if (!tv_log_flush || (TmThreadSpawn(tv_log_flush) != 0)) {
109  FatalError("Unable to create and start log flush thread");
110  }
111 }
tm-threads.h
log-flush.h
TmThreadSpawn
TmEcode TmThreadSpawn(ThreadVars *tv)
Spawns a thread associated with the ThreadVars instance tv.
Definition: tm-threads.c:1706
TmThreadSetupOptions
TmEcode TmThreadSetupOptions(ThreadVars *tv)
Set the thread options (cpu affinitythread). Priority should be already set by pthread_create.
Definition: tm-threads.c:865
ThreadVars_::name
char name[16]
Definition: threadvars.h:65
TmThreadsSetFlag
void TmThreadsSetFlag(ThreadVars *tv, uint32_t flag)
Set a thread flag.
Definition: tm-threads.c:103
TmThreadWaitForFlag
void TmThreadWaitForFlag(ThreadVars *tv, uint32_t flags)
Waits till the specified flag(s) is(are) set. We don't bother if the kill flag has been set or not on...
Definition: tm-threads.c:1824
THV_DEINIT
#define THV_DEINIT
Definition: threadvars.h:45
SCSetThreadName
#define SCSetThreadName(n)
Definition: threads.h:305
THV_RUNNING
#define THV_RUNNING
Definition: threadvars.h:55
util-privs.h
SCDropCaps
#define SCDropCaps(...)
Definition: util-privs.h:89
ThreadVars_::cap_flags
uint8_t cap_flags
Definition: threadvars.h:80
THV_RUNNING_DONE
#define THV_RUNNING_DONE
Definition: threadvars.h:46
SCConfGetInt
int SCConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
Definition: conf.c:415
OutputFlushInterval
int OutputFlushInterval(void)
Definition: log-flush.c:34
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
THV_KILL
#define THV_KILL
Definition: threadvars.h:40
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:317
LogFileFlushAll
void LogFileFlushAll(void)
Flush all registered LogFileCtx instances.
Definition: util-logopenfile.c:1086
conf-yaml-loader.h
conf.h
ThreadVars_::thread_setup_flags
uint8_t thread_setup_flags
Definition: threadvars.h:68
TmThreadCreateMgmtThread
ThreadVars * TmThreadCreateMgmtThread(const char *name, void *(fn_p)(void *), int mucond)
Creates and returns the TV instance for a Management thread(MGMT). This function supports only custom...
Definition: tm-threads.c:1099
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:232
THV_INIT_DONE
#define THV_INIT_DONE
Definition: threadvars.h:37
SleepMsec
#define SleepMsec(msec)
Definition: tm-threads.h:45
LogFlushThreads
void LogFlushThreads(void)
Definition: log-flush.c:99
suricata-common.h
thread_name_heartbeat
const char * thread_name_heartbeat
Definition: runmodes.c:77
TmThreadsWaitForUnpause
bool TmThreadsWaitForUnpause(ThreadVars *tv)
Wait for a thread to become unpaused.
Definition: tm-threads.c:365
FatalError
#define FatalError(...)
Definition: util-debug.h:517
SCLogConfig
struct SCLogConfig_ SCLogConfig
Holds the config state used by the logging api.
util-logopenfile.h
suricata.h
TmThreadsCheckFlag
int TmThreadsCheckFlag(ThreadVars *tv, uint32_t flag)
Check if a thread flag is set.
Definition: tm-threads.c:95
THV_CLOSED
#define THV_CLOSED
Definition: threadvars.h:42