use std::io;
use std::process::Command;
pub fn send_lock_signal() -> io::Result<()> {
let current_pid = std::process::id();
let output = Command::new("pgrep").arg("-x").arg("term39").output();
match output {
Ok(result) => {
let pids_str = String::from_utf8_lossy(&result.stdout);
let mut found = false;
for line in pids_str.lines() {
if let Ok(pid) = line.trim().parse::<u32>() {
if pid != current_pid {
unsafe {
if libc::kill(pid as i32, libc::SIGUSR1) == 0 {
println!("Sent lock signal to term39 (PID: {})", pid);
found = true;
}
}
}
}
}
if !found {
eprintln!("No running term39 instance found to lock.");
std::process::exit(1);
}
}
Err(_) => {
if let Ok(entries) = std::fs::read_dir("/proc") {
let mut found = false;
for entry in entries.flatten() {
let path = entry.path();
if let Some(name) = path.file_name() {
if let Ok(pid) = name.to_string_lossy().parse::<u32>() {
if pid != current_pid {
let comm_path = path.join("comm");
if let Ok(comm) = std::fs::read_to_string(&comm_path) {
if comm.trim() == "term39" {
unsafe {
if libc::kill(pid as i32, libc::SIGUSR1) == 0 {
println!(
"Sent lock signal to term39 (PID: {})",
pid
);
found = true;
}
}
}
}
}
}
}
}
if !found {
eprintln!("No running term39 instance found to lock.");
std::process::exit(1);
}
} else {
eprintln!("Could not find running term39 instances.");
std::process::exit(1);
}
}
}
Ok(())
}