]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
stat: add wrappers to isolate OS_WIN32 specifics
authorVictor Julien <vjulien@oisf.net>
Wed, 9 Aug 2023 06:00:09 +0000 (08:00 +0200)
committerVictor Julien <vjulien@oisf.net>
Thu, 7 Sep 2023 19:15:38 +0000 (21:15 +0200)
(cherry picked from commit 269f751d36e8c485b6a31bd40242749056f49bc5)

src/detect-engine.c
src/runmode-unix-socket.c
src/source-pcap-file-directory-helper.c
src/suricata.c
src/util-conf.c
src/util-path.h

index b176fedc867ebeb435e0f66028bf82cca6e2384e..2a3c7f4c76484672aef4b4cdc57ab40d7f038b64 100644 (file)
@@ -75,6 +75,7 @@
 #include "util-spm.h"
 #include "util-device.h"
 #include "util-var-name.h"
+#include "util-path.h"
 #include "util-profiling.h"
 #include "util-validate.h"
 
@@ -3301,13 +3302,8 @@ static int DetectEngineMultiTenantLoadTenant(uint32_t tenant_id, const char *fil
 
     snprintf(prefix, sizeof(prefix), "multi-detect.%u", tenant_id);
 
-#ifdef OS_WIN32
-    struct _stat st;
-    if(_stat(filename, &st) != 0) {
-#else
-    struct stat st;
-    if(stat(filename, &st) != 0) {
-#endif /* OS_WIN32 */
+    SCStat st;
+    if (SCStatFn(filename, &st) != 0) {
         SCLogError(SC_ERR_FOPEN, "failed to stat file %s", filename);
         goto error;
     }
index 8dbe1de3e24b56da5253dec647f763c49606f754..66c02e49e593af2999328acdcd46453b779b4b8b 100644 (file)
@@ -27,6 +27,8 @@
 #include "util-time.h"
 #include "util-cpu.h"
 #include "util-affinity.h"
+#include "util-var-name.h"
+#include "util-path.h"
 #include "unix-manager.h"
 
 #include "detect-engine.h"
@@ -304,11 +306,7 @@ static TmEcode UnixSocketAddPcapFileImpl(json_t *cmd, json_t* answer, void *data
     bool should_delete = false;
     time_t delay = 30;
     time_t poll_interval = 5;
-#ifdef OS_WIN32
-    struct _stat st;
-#else
-    struct stat st;
-#endif /* OS_WIN32 */
+    SCStat st;
 
     json_t *jarg = json_object_get(cmd, "filename");
     if (!json_is_string(jarg)) {
@@ -318,11 +316,7 @@ static TmEcode UnixSocketAddPcapFileImpl(json_t *cmd, json_t* answer, void *data
         return TM_ECODE_FAILED;
     }
     filename = json_string_value(jarg);
-#ifdef OS_WIN32
-    if (_stat(filename, &st) != 0) {
-#else
-    if (stat(filename, &st) != 0) {
-#endif /* OS_WIN32 */
+    if (SCStatFn(filename, &st) != 0) {
         json_object_set_new(answer, "message",
                             json_string("filename does not exist"));
         return TM_ECODE_FAILED;
@@ -346,11 +340,7 @@ static TmEcode UnixSocketAddPcapFileImpl(json_t *cmd, json_t* answer, void *data
         return TM_ECODE_FAILED;
     }
 
-#ifdef OS_WIN32
-    if (_stat(output_dir, &st) != 0) {
-#else
-    if (stat(output_dir, &st) != 0) {
-#endif /* OS_WIN32 */
+    if (SCStatFn(output_dir, &st) != 0) {
         json_object_set_new(answer, "message",
                             json_string("output-dir does not exist"));
         return TM_ECODE_FAILED;
@@ -909,11 +899,7 @@ TmEcode UnixSocketUnregisterTenantHandler(json_t *cmd, json_t* answer, void *dat
 TmEcode UnixSocketRegisterTenant(json_t *cmd, json_t* answer, void *data)
 {
     const char *filename;
-#ifdef OS_WIN32
-    struct _stat st;
-#else
-    struct stat st;
-#endif /* OS_WIN32 */
+    SCStat st;
 
     if (!(DetectEngineMultiTenantEnabled())) {
         SCLogInfo("error: multi-tenant support not enabled");
@@ -936,11 +922,7 @@ TmEcode UnixSocketRegisterTenant(json_t *cmd, json_t* answer, void *data)
         return TM_ECODE_FAILED;
     }
     filename = json_string_value(jarg);
-#ifdef OS_WIN32
-    if (_stat(filename, &st) != 0) {
-#else
-    if (stat(filename, &st) != 0) {
-#endif /* OS_WIN32 */
+    if (SCStatFn(filename, &st) != 0) {
         json_object_set_new(answer, "message", json_string("file does not exist"));
         return TM_ECODE_FAILED;
     }
@@ -985,11 +967,7 @@ static int reload_cnt = 1;
 TmEcode UnixSocketReloadTenant(json_t *cmd, json_t* answer, void *data)
 {
     const char *filename;
-#ifdef OS_WIN32
-    struct _stat st;
-#else
-    struct stat st;
-#endif /* OS_WIN32 */
+    SCStat st;
 
     if (!(DetectEngineMultiTenantEnabled())) {
         SCLogInfo("error: multi-tenant support not enabled");
@@ -1012,11 +990,7 @@ TmEcode UnixSocketReloadTenant(json_t *cmd, json_t* answer, void *data)
         return TM_ECODE_FAILED;
     }
     filename = json_string_value(jarg);
-#ifdef OS_WIN32
-    if (_stat(filename, &st) != 0) {
-#else
-    if (stat(filename, &st) != 0) {
-#endif /* OS_WIN32 */
+    if (SCStatFn(filename, &st) != 0) {
         json_object_set_new(answer, "message", json_string("file does not exist"));
         return TM_ECODE_FAILED;
     }
index 7cdad36254700d839d51dda3cf1bbcad3f307a83..b0045a891b76b0a41ba2ad4080775e039c45eb00 100644 (file)
@@ -26,6 +26,8 @@
 #include "source-pcap-file-directory-helper.h"
 #include "runmode-unix-socket.h"
 #include "util-mem.h"
+#include "util-time.h"
+#include "util-path.h"
 #include "source-pcap-file.h"
 
 static void GetTime(struct timespec *tm);
@@ -227,23 +229,14 @@ TmEcode PcapDetermineDirectoryOrFile(char *filename, DIR **directory)
 
 int PcapDirectoryGetModifiedTime(char const *file, struct timespec *out)
 {
-#ifdef OS_WIN32
-    struct _stat buf;
-#else
-    struct stat buf;
-#endif /* OS_WIN32 */
+    SCStat buf;
     int ret;
 
     if (file == NULL)
         return -1;
 
-#ifdef OS_WIN32
-    if((ret = _stat(file, &buf)) != 0)
-        return ret;
-#else
-    if ((ret = stat(file, &buf)) != 0)
+    if ((ret = SCStatFn(file, &buf)) != 0)
         return ret;
-#endif
 
 #ifdef OS_DARWIN
     out->tv_sec = buf.st_mtimespec.tv_sec;
index 45d9d5d742b9ea18fc4e764f42631107fc650177..65fed9a07d7a108ba577bdf880d0802be7f94cbb 100644 (file)
 #include "util-proto-name.h"
 #include "util-running-modes.h"
 #include "util-signal.h"
-#include "util-time.h"
-#include "util-validate.h"
 #include "util-var-name.h"
 
 #include "util-lua.h"
@@ -518,30 +516,21 @@ static void SetBpfStringFromFile(char *filename)
     char *bpf_comment_tmp = NULL;
     char *bpf_comment_start =  NULL;
     uint32_t bpf_len = 0;
-#ifdef OS_WIN32
-    struct _stat st;
-#else
-    struct stat st;
-#endif /* OS_WIN32 */
+    SCStat st;
     FILE *fp = NULL;
     size_t nm = 0;
 
-#ifdef OS_WIN32
-    if(_stat(filename, &st) != 0) {
-#else
-    if(stat(filename, &st) != 0) {
-#endif /* OS_WIN32 */
-        SCLogError(SC_ERR_FOPEN, "Failed to stat file %s", filename);
+    fp = fopen(filename, "r");
+    if (fp == NULL) {
+        SCLogError(SC_ERR_FOPEN, "Failed to open file %s", filename);
         exit(EXIT_FAILURE);
     }
-    bpf_len = st.st_size + 1;
 
-    // coverity[toctou : FALSE]
-    fp = fopen(filename,"r");
-    if (fp == NULL) {
-        SCLogError(SC_ERR_FOPEN, "Failed to open file %s", filename);
+    if (SCFstatFn(fileno(fp), &st) != 0) {
+        SCLogError(SC_ERR_FOPEN, "Failed to stat file %s", filename);
         exit(EXIT_FAILURE);
     }
+    bpf_len = st.st_size + 1;
 
     bpf_filter = SCMalloc(bpf_len * sizeof(char));
     if (unlikely(bpf_filter == NULL)) {
@@ -1828,14 +1817,9 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri)
                 PrintUsage(argv[0]);
                 return TM_ECODE_FAILED;
             }
-#ifdef OS_WIN32
-            struct _stat buf;
-            if(_stat(optarg, &buf) != 0) {
-#else
-            struct stat buf;
-            if (stat(optarg, &buf) != 0) {
-#endif /* OS_WIN32 */
-                SCLogError(SC_ERR_INITIALIZATION, "ERROR: Pcap file does not exist\n");
+            SCStat buf;
+            if (SCStatFn(optarg, &buf) != 0) {
+                SCLogError(SC_ERR_INITIALIZATION, "pcap file '%s': %s", optarg, strerror(errno));
                 return TM_ECODE_FAILED;
             }
             if (ConfSetFinal("pcap-file.file", optarg) != 1) {
index da337c6b4bfb273021f085ec833dc8ec02add5a6..916a0a8ba71885201967c327f7c218781f5c6e42 100644 (file)
@@ -26,6 +26,8 @@
 #include "conf.h"
 #include "runmodes.h"
 #include "util-conf.h"
+#include "util-debug.h"
+#include "util-path.h"
 
 TmEcode ConfigSetLogDirectory(const char *name)
 {
@@ -53,13 +55,8 @@ const char *ConfigGetLogDirectory(void)
 TmEcode ConfigCheckLogDirectoryExists(const char *log_dir)
 {
     SCEnter();
-#ifdef OS_WIN32
-    struct _stat buf;
-    if (_stat(log_dir, &buf) != 0) {
-#else
-    struct stat buf;
-    if (stat(log_dir, &buf) != 0) {
-#endif /* OS_WIN32 */
+    SCStat buf;
+    if (SCStatFn(log_dir, &buf) != 0) {
         SCReturnInt(TM_ECODE_FAILED);
     }
     SCReturnInt(TM_ECODE_OK);
@@ -101,13 +98,8 @@ const char *ConfigGetDataDirectory(void)
 TmEcode ConfigCheckDataDirectory(const char *data_dir)
 {
     SCEnter();
-#ifdef OS_WIN32
-    struct _stat buf;
-    if (_stat(data_dir, &buf) != 0) {
-#else
-    struct stat buf;
-    if (stat(data_dir, &buf) != 0) {
-#endif /* OS_WIN32 */
+    SCStat buf;
+    if (SCStatFn(data_dir, &buf) != 0) {
         SCReturnInt(TM_ECODE_FAILED);
     }
     SCReturnInt(TM_ECODE_OK);
index 6f788a8f2513b7880b7deb91668791d339a30b46..b8a5dd25939dc188e8c4563e86a7b14b4cc1703b 100644 (file)
 #ifndef __UTIL_PATH_H__
 #define __UTIL_PATH_H__
 
+#ifdef OS_WIN32
+typedef struct _stat SCStat;
+#define SCFstatFn(fd, statbuf)      _fstat((fd), (statbuf))
+#define SCStatFn(pathname, statbuf) _stat((pathname), (statbuf))
+#else
+typedef struct stat SCStat;
+#define SCFstatFn(fd, statbuf)      fstat((fd), (statbuf))
+#define SCStatFn(pathname, statbuf) stat((pathname), (statbuf))
+#endif
+
 #ifndef HAVE_NON_POSIX_MKDIR
     #define SCMkDir(a, b) mkdir(a, b)
 #else