|
17 | 17 | //! This binary provides a command-line interface for managing the bssh SSH server. |
18 | 18 |
|
19 | 19 | use anyhow::{Context, Result}; |
20 | | -use bssh::server::config::{generate_config_template, load_config, ServerFileConfig}; |
21 | 20 | use bssh::server::BsshServer; |
| 21 | +use bssh::server::config::{ServerFileConfig, generate_config_template, load_config}; |
22 | 22 | use bssh::utils::logging; |
23 | 23 | use clap::{ArgAction, Parser, Subcommand}; |
24 | 24 | use std::fs; |
@@ -471,37 +471,36 @@ fn setup_signal_handlers() -> Result<impl std::future::Future<Output = ()>> { |
471 | 471 | /// Write the current process ID to a PID file |
472 | 472 | fn write_pid_file(path: &PathBuf) -> Result<()> { |
473 | 473 | // Check if PID file already exists and refers to a running process |
474 | | - if path.exists() { |
475 | | - if let Ok(existing_pid_str) = fs::read_to_string(path) { |
476 | | - if let Ok(existing_pid) = existing_pid_str.trim().parse::<i32>() { |
477 | | - // Check if process is still running |
478 | | - #[cfg(unix)] |
479 | | - { |
480 | | - use nix::sys::signal::kill; |
481 | | - use nix::unistd::Pid; |
482 | | - |
483 | | - let pid = Pid::from_raw(existing_pid); |
484 | | - // Use signal 0 (None) to check if process exists without sending actual signal |
485 | | - if kill(pid, None).is_ok() { |
486 | | - anyhow::bail!( |
487 | | - "Another instance is already running with PID {}. \ |
| 474 | + if path.exists() |
| 475 | + && let Ok(existing_pid_str) = fs::read_to_string(path) |
| 476 | + && let Ok(existing_pid) = existing_pid_str.trim().parse::<i32>() |
| 477 | + { |
| 478 | + // Check if process is still running |
| 479 | + #[cfg(unix)] |
| 480 | + { |
| 481 | + use nix::sys::signal::kill; |
| 482 | + use nix::unistd::Pid; |
| 483 | + |
| 484 | + let pid = Pid::from_raw(existing_pid); |
| 485 | + // Use signal 0 (None) to check if process exists without sending actual signal |
| 486 | + if kill(pid, None).is_ok() { |
| 487 | + anyhow::bail!( |
| 488 | + "Another instance is already running with PID {}. \ |
488 | 489 | If this is incorrect, remove {} and try again.", |
489 | | - existing_pid, |
490 | | - path.display() |
491 | | - ); |
492 | | - } |
493 | | - } |
494 | | - |
495 | | - #[cfg(not(unix))] |
496 | | - { |
497 | | - // On non-Unix systems, warn but allow overwrite |
498 | | - tracing::warn!( |
499 | | - "PID file exists with PID {}. Overwriting (process check not available on this platform).", |
500 | | - existing_pid |
501 | | - ); |
502 | | - } |
| 490 | + existing_pid, |
| 491 | + path.display() |
| 492 | + ); |
503 | 493 | } |
504 | 494 | } |
| 495 | + |
| 496 | + #[cfg(not(unix))] |
| 497 | + { |
| 498 | + // On non-Unix systems, warn but allow overwrite |
| 499 | + tracing::warn!( |
| 500 | + "PID file exists with PID {}. Overwriting (process check not available on this platform).", |
| 501 | + existing_pid |
| 502 | + ); |
| 503 | + } |
505 | 504 | } |
506 | 505 |
|
507 | 506 | let pid = std::process::id(); |
@@ -798,10 +797,12 @@ mod tests { |
798 | 797 |
|
799 | 798 | let result = gen_host_key("rsa", &key_path, 1024); |
800 | 799 | assert!(result.is_err()); |
801 | | - assert!(result |
802 | | - .unwrap_err() |
803 | | - .to_string() |
804 | | - .contains("RSA key size must be at least 2048")); |
| 800 | + assert!( |
| 801 | + result |
| 802 | + .unwrap_err() |
| 803 | + .to_string() |
| 804 | + .contains("RSA key size must be at least 2048") |
| 805 | + ); |
805 | 806 | } |
806 | 807 |
|
807 | 808 | #[test] |
|
0 commit comments