You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
flowchart TD
A["Start"] --> B["Delete PID File"]
B --> C["Start Suricata"]
C --> D["Wait for PID File"]
D --> E["Check Socket via suricatasc"]
E --> F["Ready"]
Loading
File Walkthrough
Relevant files
Enhancement
argument.rs
Add socket path CLI argument
src/argument.rs
Added path_to_socket argument to Commands::Suricata
get_suricata_pid and wait_on_suricata_start each bound the wait to 10 attempts of 1 second. Suricata only opens its unix command socket after full initialization, which on large rule sets or slow disks can take well over 10 seconds. In that scenario the tool panics with 'Suricata could not start.' even though Suricata would have started successfully moments later, killing the run. Consider making the timeout configurable (e.g., reusing preconf_time) or retrying on the total startup budget rather than a fixed 10 seconds. Impact is limited to environments where startup exceeds ~10 seconds.
fnget_suricata_pid() -> Result<i32,String>{for _ in0..10{ifletOk(content) = fs::read_to_string(PIDFILE){ifletOk(pid) = content.trim().parse::<i32>(){ifPath::new(&format!("/proc/{}", pid)).exists(){returnOk(pid);}}}
thread::sleep(Duration::from_millis(1000));}Err("Suricata process not found.".into())}fnwait_on_suricata_start(socket:&PathBuf) -> Result<i32,String>{matchget_suricata_pid(){Ok(pid) => {let socket = socket.to_str().ok_or("Socket path is not valid UTF-8.")?;for _ in0..10{let output = Command::new("sudo").arg("-n").arg("suricatasc").arg("-c").arg("uptime").arg(socket).status().map_err(|e| format!("Unable to execute Suricata socket control tool: {e}"))?;if output.success(){returnOk(pid)}
thread::sleep(Duration::from_millis(1000));}Err("Suricata could not start.".into())}Err(e) => {Err(e)}}}
wait_on_suricata_start only checks that suricatasc -c uptime against the configured socket succeeds. If a leftover Suricata instance from a previous crashed run is still bound to that same socket, the check passes even though the newly spawned Suricata may have failed to bind the socket, and the tool proceeds with uptime/PID data from the old instance. The PID-file deletion in delete_pid_file removes the stale pid file but does not stop a stale process. Worth confirming whether killing the previous instance is expected before start.
fn wait_on_suricata_start(socket:&PathBuf) -> Result<i32,String>{matchget_suricata_pid(){Ok(pid) => {let socket = socket.to_str().ok_or("Socket path is not valid UTF-8.")?;for _ in0..10{let output = Command::new("sudo").arg("-n").arg("suricatasc").arg("-c").arg("uptime").arg(socket).status().map_err(|e| format!("Unable to execute Suricata socket control tool: {e}"))?;if output.success(){returnOk(pid)}
thread::sleep(Duration::from_millis(1000));}Err("Suricata could not start.".into())
suricatasc does not accept the socket path as a positional argument; it must be passed via the --socket flag. As written, the command fails every iteration (or binds to the default socket), so wait_on_suricata_start will report "Suricata could not start" even when Suricata is running. Pass the socket path using --socket.
let output = Command::new("sudo")
.arg("-n")
.arg("suricatasc")
.arg("-c")
.arg("uptime")
+ .arg("--socket")
.arg(socket)
.status()
.map_err(|e| format!("Unable to execute Suricata socket control tool: {e}"))?;
Suggestion importance[1-10]: 8
__
Why: suricatasc expects the socket path via the --socket flag, not as a positional argument, so the current command would fail (or use the default socket) and cause wait_on_suricata_start to incorrectly report that Suricata could not start. This is a critical correctness bug in the new readiness check logic.
Medium
Align Suricata's socket with the readiness check
The socket path used for the suricatasc readiness check is read from suriconf.socket, but Suricata itself is never told to use that socket path. If the configured socket differs from the one in Suricata's own configuration, the readiness check will always fail. Add --unix-socket with suriconf.socket to the Suricata command line so both sides agree.
Why: The readiness check uses suriconf.socket, but Suricata's socket path comes from its own configuration, so a mismatch would make the check always fail. Passing --unix-socket with the same path is a reasonable robustness improvement, though the setup assumes the operator keeps both configs in sync, so the impact is moderate rather than critical.
Low
General
Verify the PID belongs to Suricata
Verifying only that /proc/{pid} exists is not sufficient: the PID may belong to an unrelated process (e.g., a pre-existing system Suricata instance or a reused PID), and subsequent functions like get_cores_with_threads will then inspect the wrong process. Validate that the process command name is actually Suricata before returning the PID.
if let Ok(content) = fs::read_to_string(PIDFILE) {
if let Ok(pid) = content.trim().parse::<i32>() {
- if Path::new(&format!("/proc/{}", pid)).exists() {- return Ok(pid);+ if let Ok(comm) = fs::read_to_string(format!("/proc/{}/comm", pid)) {+ if comm.trim().contains("Suricata") {+ return Ok(pid);+ }
}
}
}
Suggestion importance[1-10]: 4
__
Why: Checking only that /proc/{pid} exists could match an unrelated process due to PID reuse, leading get_cores_with_threads to inspect the wrong process. Since the PR already deletes the stale PID file at startup, the risk is low, making this a minor defensive improvement.
Low
Signal worker threads before panic exit
Before panicking, the kill atomic flag shared with the already-spawned monitor threads is never set, so those threads (and the stderr reader) are left running with the child half-shutdown. Set kill_thread1/kill_thread2 (or the shared kill flag) to true before killing and panicking so all spawned threads terminate cleanly.
Why: A panic! on the main thread terminates the entire process, which also kills all spawned threads, so the claim that threads are "left running" is inaccurate and setting the kill flags has essentially no observable effect here. The change is harmless but addresses a non-issue.
Low
Author self-review: I have reviewed the PR code suggestions, and addressed the relevant ones.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Type
Enhancement, Bug fix
Description
Implement PID file and socket startup check
Add CLI argument for socket path configuration
Update Suriconf struct to store socket path
Remove unreliable process name scanning logic
Diagram Walkthrough
File Walkthrough
argument.rs
Add socket path CLI argumentsrc/argument.rs
path_to_socketargument toCommands::Suricatayaml.rs
Add socket field to configsrc/yaml.rs
socketfield toSuriconfstructfind_socketparsing helpersuricata.rs
Refactor startup detection logicsrc/suricata.rs
wait_on_suricata_startusing PID and socketdelete_pid_fileandset_pid_filehelperscheck_process_name_for_suricata_mainlogicStdio::nullduring executionsuriconf.yaml
Define default socket pathsrc/suriconf.yaml
socketpath configuration/var/run/suricata/suricata-command.socket