From: Eric Leblond Date: Mon, 14 Dec 2015 13:02:20 +0000 (+0100) Subject: unix-manager: fix race condition X-Git-Tag: suricata-3.0RC3~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6b37985c666db978af42cc095b4719af46cf7a43;p=thirdparty%2Fsuricata.git unix-manager: fix race condition Under high load it is possible that the thread is not yet started and that we register a command at the same time. As a consequence, the commands list is not yet initialized and we have a segfault. This patch moves the initialization in the ThreadInit function to be sure the commands list is available when needed. --- diff --git a/src/unix-manager.c b/src/unix-manager.c index 9357f4c504..ca92677bab 100644 --- a/src/unix-manager.c +++ b/src/unix-manager.c @@ -851,25 +851,6 @@ static TmEcode UnixManagerThreadInit(ThreadVars *t, void *initdata, void **data) if (utd == NULL) return TM_ECODE_FAILED; - *data = utd; - return TM_ECODE_OK; -} - -static TmEcode UnixManagerThreadDeinit(ThreadVars *t, void *data) -{ - SCFree(data); - return TM_ECODE_OK; -} - -static TmEcode UnixManager(ThreadVars *th_v, void *thread_data) -{ - int ret; - - /* set the thread name */ - SCLogDebug("%s started...", th_v->name); - - StatsSetupPrivate(th_v); - if (UnixNew(&command) == 0) { int failure_fatal = 0; SCLogError(SC_ERR_INITIALIZATION, @@ -884,10 +865,6 @@ static TmEcode UnixManager(ThreadVars *th_v, void *thread_data) } } - /* Set the threads capability */ - th_v->cap_flags = 0; - SCDropCaps(th_v); - /* Init Unix socket */ UnixManagerRegisterCommand("shutdown", UnixManagerShutdownCommand, NULL, 0); UnixManagerRegisterCommand("command-list", UnixManagerListCommand, &command, 0); @@ -905,6 +882,28 @@ static TmEcode UnixManager(ThreadVars *th_v, void *thread_data) UnixManagerRegisterCommand("reload-tenant", UnixSocketReloadTenant, &command, UNIX_CMD_TAKE_ARGS); UnixManagerRegisterCommand("unregister-tenant", UnixSocketUnregisterTenant, &command, UNIX_CMD_TAKE_ARGS); + *data = utd; + return TM_ECODE_OK; +} + +static TmEcode UnixManagerThreadDeinit(ThreadVars *t, void *data) +{ + SCFree(data); + return TM_ECODE_OK; +} + +static TmEcode UnixManager(ThreadVars *th_v, void *thread_data) +{ + int ret; + + /* set the thread name */ + SCLogDebug("%s started...", th_v->name); + + StatsSetupPrivate(th_v); + + /* Set the threads capability */ + th_v->cap_flags = 0; + SCDropCaps(th_v); TmThreadsSetFlag(th_v, THV_INIT_DONE); while (1) {