} SCPlugin;
typedef SCPlugin *(*SCPluginRegisterFunc)(void);
+typedef uint32_t ThreadId;
/**
* Structure used to define an Eve output file type plugin.
int (*Write)(const char *buffer, int buffer_len, void *init_data, void *thread_data);
/* Close - Called on final close */
void (*Deinit)(void *init_data);
- /* ThreadInit - Called for each thread using file object*/
- int (*ThreadInit)(void *init_data, int thread_id, void **thread_data);
+ /* ThreadInit - Called for each thread using file object; non-zero thread_ids correlate
+ * to Suricata's worker threads; 0 correlates to the Suricata main thread */
+ int (*ThreadInit)(void *init_data, ThreadId thread_id, void **thread_data);
/* ThreadDeinit - Called for each thread using file object */
int (*ThreadDeinit)(void *init_data, void *thread_data);
TAILQ_ENTRY(SCEveFileType_) entries;
ThreadLogFileHashEntry *entry);
// Threaded eve.json identifier
-static SC_ATOMIC_DECL_AND_INIT_WITH_VAL(uint32_t, eve_file_id, 1);
+static SC_ATOMIC_DECL_AND_INIT_WITH_VAL(uint16_t, eve_file_id, 1);
#ifdef BUILD_WITH_UNIXSOCKET
/** \brief connect to the indicated local stream socket, logging any errors
* Each thread -- identified by its operating system thread-id -- has its
* own file entry that includes a file pointer.
*/
-static ThreadLogFileHashEntry *LogFileThread2Slot(LogThreadedFileCtx *parent)
+static ThreadLogFileHashEntry *LogFileThread2Slot(LogThreadedFileCtx *parent, ThreadId thread_id)
{
ThreadLogFileHashEntry thread_hash_entry;
if (!ent) {
ent = SCCalloc(1, sizeof(*ent));
if (!ent) {
- FatalError("Unable to allocate thread/entry entry");
+ FatalError("Unable to allocate thread/hash-entry entry");
}
ent->thread_id = thread_hash_entry.thread_id;
- SCLogDebug("Trying to add thread %ld to entry %d", ent->thread_id, ent->slot_number);
+ ent->internal_thread_id = thread_id;
+ SCLogDebug(
+ "Trying to add thread %" PRIi64 " to entry %d", ent->thread_id, ent->slot_number);
if (0 != HashTableAdd(parent->ht, ent, 0)) {
- FatalError("Unable to add thread/entry mapping");
+ FatalError("Unable to add thread/hash-entry mapping");
}
}
return ent;
* \param parent_ctx
* \retval LogFileCtx * pointer if successful; NULL otherwise
*/
-LogFileCtx *LogFileEnsureExists(LogFileCtx *parent_ctx)
+LogFileCtx *LogFileEnsureExists(ThreadId thread_id, LogFileCtx *parent_ctx)
{
/* threaded output disabled */
if (!parent_ctx->threaded)
SCMutexLock(&parent_ctx->threads->mutex);
/* Find this thread's entry */
- ThreadLogFileHashEntry *entry = LogFileThread2Slot(parent_ctx->threads);
- SCLogDebug("Adding reference for thread %ld [slot %d] to file %s [ctx %p]", SCGetThreadIdLong(),
- entry->slot_number, parent_ctx->filename, parent_ctx);
+ ThreadLogFileHashEntry *entry = LogFileThread2Slot(parent_ctx->threads, thread_id);
+ SCLogDebug("%s: Adding reference for thread %" PRIi64
+ " (local thread id %d) to file %s [ctx %p]",
+ t_thread_name, SCGetThreadIdLong(), thread_id, parent_ctx->filename, parent_ctx);
bool new = entry->isopen;
/* has it been opened yet? */
if (!entry->isopen) {
- SCLogDebug("Opening new file for thread/slot %d to file %s [ctx %p]", entry->slot_number,
- parent_ctx->filename, parent_ctx);
+ SCLogDebug("%s: Opening new file for thread/id %d to file %s [ctx %p]", t_thread_name,
+ thread_id, parent_ctx->filename, parent_ctx);
if (LogFileNewThreadedCtx(
parent_ctx, parent_ctx->filename, parent_ctx->threads->append, entry)) {
entry->isopen = true;
*thread = *parent_ctx;
if (parent_ctx->type == LOGFILE_TYPE_FILE) {
char fname[LOGFILE_NAME_MAX];
- if (!LogFileThreadedName(log_path, fname, sizeof(fname), SC_ATOMIC_ADD(eve_file_id, 1))) {
+ entry->slot_number = SC_ATOMIC_ADD(eve_file_id, 1);
+ if (!LogFileThreadedName(log_path, fname, sizeof(fname), entry->slot_number)) {
SCLogError("Unable to create threaded filename for log");
goto error;
}
- SCLogDebug("Thread open -- using name %s [replaces %s]", fname, log_path);
+ SCLogDebug("%s: thread open -- using name %s [replaces %s] - thread %d [slot %d]",
+ t_thread_name, fname, log_path, entry->internal_thread_id, entry->slot_number);
thread->fp = SCLogOpenFileFp(fname, append, thread->filemode);
if (thread->fp == NULL) {
goto error;
OutputRegisterFileRotationFlag(&thread->rotation_flag);
} else if (parent_ctx->type == LOGFILE_TYPE_PLUGIN) {
entry->slot_number = SC_ATOMIC_ADD(eve_file_id, 1);
+ SCLogDebug("%s - thread %d [slot %d]", log_path, entry->internal_thread_id,
+ entry->slot_number);
thread->plugin.plugin->ThreadInit(
- thread->plugin.init_data, entry->slot_number, &thread->plugin.thread_data);
+ thread->plugin.init_data, entry->internal_thread_id, &thread->plugin.thread_data);
}
thread->threaded = false;
thread->parent = parent_ctx;