suricata
log-maintenance.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-maintenance.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 
34 {
35  intmax_t output_flush_interval = 0;
36  if (SCConfGetInt("heartbeat.output-flush-interval", &output_flush_interval) == 0) {
37  output_flush_interval = 0;
38  }
39  if (output_flush_interval < 0 || output_flush_interval > 60) {
40  SCLogConfig("flush_interval must be 0 or less than 60; using 0");
41  output_flush_interval = 0;
42  }
43 
44  return (int)output_flush_interval;
45 }
46 
47 static void *LogMaintenanceThread(void *arg)
48 {
49  int output_flush_interval = OutputFlushInterval();
50 
51  if (output_flush_interval > 0) {
52  SCLogConfig("Log maintenance thread started: rotation check every 1s, flush interval %ds",
53  output_flush_interval);
54  } else {
55  SCLogConfig("Log maintenance thread started: rotation check every 1s, flush disabled");
56  }
57 
58  /*
59  * Calculate the number of sleep intervals based on the output flush interval. This is necessary
60  * because this thread pauses a fixed amount of time to react to shutdown situations more
61  * quickly.
62  */
63  const int maintenance_sleep_time = 500; /* milliseconds */
64  const int rotation_wait_count = 1000 / maintenance_sleep_time; /* = 2, check every 1 second */
65  const int flush_wait_count =
66  output_flush_interval > 0 ? (1000 * output_flush_interval) / maintenance_sleep_time : 0;
67 
68  ThreadVars *tv_local = (ThreadVars *)arg;
69  SCSetThreadName(tv_local->name);
70 
71  if (tv_local->thread_setup_flags != 0)
72  TmThreadSetupOptions(tv_local);
73 
74  /* Set the threads capability */
75  tv_local->cap_flags = 0;
76  SCDropCaps(tv_local);
77 
79 
80  int rotation_counter = 0;
81  int flush_counter = 0;
82  uint64_t rotation_check_count = 0;
83  uint64_t worker_flush_count = 0;
84  bool run = TmThreadsWaitForUnpause(tv_local);
85  while (run) {
86  SleepMsec(maintenance_sleep_time);
87 
88  /* Check rotation every 1 second */
89  if (++rotation_counter >= rotation_wait_count) {
90  rotation_check_count++;
92  rotation_counter = 0;
93  }
94 
95  /* Flush at configured interval (if enabled) */
96  if (flush_wait_count > 0 && ++flush_counter >= flush_wait_count) {
97  worker_flush_count++;
99  flush_counter = 0;
100  }
101 
102  if (TmThreadsCheckFlag(tv_local, THV_KILL)) {
103  break;
104  }
105  }
106 
108  TmThreadWaitForFlag(tv_local, THV_DEINIT);
109  TmThreadsSetFlag(tv_local, THV_CLOSED);
110  SCLogInfo("%s: performed %" PRIu64 " rotation checks, %" PRIu64 " flushes", tv_local->name,
111  rotation_check_count, worker_flush_count);
112  return NULL;
113 }
114 
116 {
117  ThreadVars *tv_maintenance =
118  TmThreadCreateMgmtThread(thread_name_heartbeat, LogMaintenanceThread, 1);
119  if (!tv_maintenance || (TmThreadSpawn(tv_maintenance) != 0)) {
120  FatalError("Unable to create and start log maintenance thread");
121  }
122 }
tm-threads.h
TmThreadSpawn
TmEcode TmThreadSpawn(ThreadVars *tv)
Spawns a thread associated with the ThreadVars instance tv.
Definition: tm-threads.c:1702
TmThreadSetupOptions
TmEcode TmThreadSetupOptions(ThreadVars *tv)
Set the thread options (cpu affinitythread). Priority should be already set by pthread_create.
Definition: tm-threads.c:865
LogMaintenanceThreadSpawn
void LogMaintenanceThreadSpawn(void)
Definition: log-maintenance.c:115
LogFileRotateAll
void LogFileRotateAll(void)
Check rotation for all registered LogFileCtx instances.
Definition: util-logopenfile.c:1103
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:1820
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
OutputFlushInterval
int OutputFlushInterval(void)
Definition: log-maintenance.c:33
THV_RUNNING_DONE
#define THV_RUNNING_DONE
Definition: threadvars.h:46
log-maintenance.h
SCConfGetInt
int SCConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
Definition: conf.c:441
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
THV_KILL
#define THV_KILL
Definition: threadvars.h:40
LogFileFlushAll
void LogFileFlushAll(void)
Flush all registered LogFileCtx instances.
Definition: util-logopenfile.c:1085
conf-yaml-loader.h
conf.h
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:1095
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
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