From 5eae2993eef320d2bd13ce9d6a47f9959d46ba47 Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Fri, 27 Feb 2026 13:31:13 -0600 Subject: [PATCH] examples/lib/cplusplus: simplfy, make more like the simple example To prep for the removal of the lib runmode, simplify this C++ example to match our simple example. We don't yet have the C++ compatible headers to allow for a C++ app to register its own custom runmode. (cherry picked from commit 7dd23392cc87182e9d83d69b7b3b4e1a6ae1f02d) --- examples/lib/cplusplus/main.cpp | 61 +++++++++++++++------------------ 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/examples/lib/cplusplus/main.cpp b/examples/lib/cplusplus/main.cpp index 8606740a2d..f8bead08c9 100644 --- a/examples/lib/cplusplus/main.cpp +++ b/examples/lib/cplusplus/main.cpp @@ -1,7 +1,21 @@ -#include "suricata-common.h" +/* Copyright (C) 2024 Open Information Security Foundation + * + * You can copy, redistribute or modify this Program under the terms of + * the GNU General Public License version 2 as published by the Free + * Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * version 2 along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + #include "suricata.h" -#include "conf.h" -#include "util-device.h" int main(int argc, char **argv) { @@ -11,24 +25,6 @@ int main(int argc, char **argv) * directly configure Suricata through the Conf API. */ SCParseCommandLine(argc, argv); - /* Find our list of pcap files, after the "--". */ - while (argc) { - bool end = strncmp(argv[0], "--", 2) == 0; - argv++; - argc--; - if (end) { - break; - } - } - if (argc == 0) { - fprintf(stderr, "ERROR: No PCAP files provided\n"); - return 1; - } - - /* Set the runmode to library mode. Perhaps in the future this - * should be done in some library bootstrap function. */ - SCRunmodeSet(RUNMODE_LIB); - /* Validate/finalize the runmode. */ if (SCFinalizeRunMode() != TM_ECODE_OK) { exit(EXIT_FAILURE); @@ -51,20 +47,19 @@ int main(int argc, char **argv) exit(EXIT_FAILURE); } - /* Set "offline" runmode to replay a pcap in library mode. */ - if (!SCConfSetFromString("runmode=offline", 1)) { - exit(EXIT_FAILURE); - } + /* Enable default signal handlers just like Suricata. */ + SCEnableDefaultSignalHandlers(); - /* Force logging to the current directory. */ - SCConfSetFromString("default-log-dir=.", 1); + SuricataInit(); + SuricataPostInit(); - if (LiveRegisterDevice("lib0") < 0) { - fprintf(stderr, "LiveRegisterDevice failed"); - exit(1); - } + /* Suricata is now running, but we enter a loop to keep it running + * until it shouldn't be running anymore. */ + SuricataMainLoop(); - SuricataInit(); + /* Shutdown engine. */ + SuricataShutdown(); + GlobalsDestroy(); - return 0; + return EXIT_SUCCESS; }