]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust: add C callback for FatalErrorOnInit
authorShivani Bhardwaj <shivani@oisf.net>
Thu, 9 Jan 2025 06:47:13 +0000 (12:17 +0530)
committerVictor Julien <victor@inliniac.net>
Sat, 15 Feb 2025 10:11:39 +0000 (11:11 +0100)
rust/src/debug.rs
src/util-debug.c
src/util-debug.h

index 16b128390f4ca6341079222af02e527e56f39d74..72f7b737c310159fb0a9b48e9825171ed2f108f5 100644 (file)
 use std::{ffi::CString, path::Path};
 
 use crate::core::SC;
+use std::os::raw::c_char;
 
 /// cbindgen:ignore
-extern {
+extern "C" {
     pub fn SCLogGetLogLevel() -> i32;
 }
 
+// Defined in util-debug.c
+/// cbindgen:ignore
+extern "C" {
+    pub fn SCFatalErrorOnInitStatic(arg: *const c_char);
+}
+
 #[derive(Debug)]
 #[repr(C)]
 pub enum Level {
@@ -62,6 +69,12 @@ fn basename(filename: &str) -> &str {
     return filename;
 }
 
+pub fn fatalerror(message: &str) {
+    unsafe {
+        SCFatalErrorOnInitStatic(to_safe_cstring(message).as_ptr());
+    }
+}
+
 pub fn sclog(level: Level, file: &str, line: u32, function: &str, message: &str) {
     let filename = basename(file);
     let noext = &filename[0..filename.len() - 3];
@@ -203,13 +216,19 @@ macro_rules! SCLogDebug {
     ($($arg:tt)*) => {};
 }
 
+#[macro_export]
+macro_rules!SCFatalErrorOnInit {
+    ($($arg:tt)*) => {
+        $crate::debug::fatalerror(&format!($($arg)*));
+    }
+}
+
 #[cfg(not(feature = "debug-validate"))]
 #[macro_export]
 macro_rules! debug_validate_bug_on (
   ($item:expr) => {};
 );
 
-
 #[cfg(feature = "debug-validate")]
 #[macro_export]
 macro_rules! debug_validate_bug_on (
index 56258a034f94605f0f971083f86ce8dcc7d0045c..f315d1289048084be1e17b26612e54e6060b6127 100644 (file)
@@ -1614,6 +1614,11 @@ void SCLogDeInitLogModule(void)
 #endif /* OS_WIN32 */
 }
 
+void SCFatalErrorOnInitStatic(const char *arg)
+{
+    FatalErrorOnInit("%s", arg);
+}
+
 //------------------------------------Unit_Tests--------------------------------
 
 /* The logging engine should be tested to the maximum extent possible, since
index 29beb912c927d08024770f0d36793fc239d54226..6ed0eccf5b195806ef7e23866792fa1db864f77e 100644 (file)
@@ -530,6 +530,8 @@ SCLogInitData *SCLogAllocLogInitData(void);
 
 void SCLogAppendOPIfaceCtx(SCLogOPIfaceCtx *, SCLogInitData *);
 
+void SCFatalErrorOnInitStatic(const char *);
+
 void SCLogInitLogModule(SCLogInitData *);
 
 void SCLogDeInitLogModule(void);