Skip to content

Commit

Permalink
fix: 正确处理 daemon 杀进程
Browse files Browse the repository at this point in the history
  • Loading branch information
Mufanc committed Dec 10, 2023
1 parent 8a9dc21 commit d8c5c79
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ path = "src/daemon.rs"
anyhow = "1.0.75"
ctor = "0.2.5"
goblin = "0.7.1"
nix = { version = "0.27.1", features = ["socket", "process"] }
nix = { version = "0.27.1", features = ["socket", "process", "signal"] }
once_cell = "1.19.0"
url = "2.5.0"
21 changes: 14 additions & 7 deletions daemon/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

use std::fs;
use std::fs::Metadata;
use std::io::Read;
use std::io::{Read, Write};
use std::os::fd::{AsRawFd, OwnedFd};
use std::os::unix::fs::MetadataExt;
use std::os::unix::net::UnixListener;
use std::os::unix::net::{UCred, UnixListener, UnixStream};
use std::process::{Command, exit};
use std::thread::sleep;
use std::time::Duration;

use anyhow::{bail, Result};
use nix::{libc, unistd};
use nix::errno::Errno;
use nix::sys::prctl;
use nix::sys::{prctl, signal};
use nix::sys::signal::Signal;
use nix::sys::socket;
use nix::sys::socket::{AddressFamily, MsgFlags, SockFlag, SockType, UnixAddr};
use nix::sys::socket::{AddressFamily, SockFlag, SockType, UnixAddr};
use nix::unistd::Pid;

mod configs;

Expand All @@ -29,15 +30,21 @@ fn bind_server(fd: &OwnedFd) -> Result<()> {
bail!(err);
}

eprintln!("address already in use, stop old daemon and retry...");
eprintln!("address already in use, kill old daemon and retry...");

let client = socket::socket(AddressFamily::Unix, SockType::Stream, SockFlag::empty(), None)?;
let client_raw = client.as_raw_fd();

socket::connect(client_raw, addr)?;
socket::send(client_raw, "@exit".as_bytes(), MsgFlags::empty())?;

drop(client); // close fd
let mut stream = UnixStream::from(client);

if let Ok(UCred { pid: Some(pid), .. }) = stream.peer_cred() {
signal::killpg(Pid::from_raw(pid), Signal::SIGKILL)?;
} else {
stream.write_all("@exit".as_bytes())?;
drop(stream);
}

sleep(Duration::from_secs(1));

Expand Down
3 changes: 0 additions & 3 deletions launcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ LITELOADER="$HOME/.config/QQ/LiteLoaderQQNT"

mkdir -p "$HOME/.config/QQ"

killall /opt/QQ/qq 2>/dev/null
killall /opt/QQ/main 2>/dev/null

args=""
args="$args --unshare-all --share-net" # 分离命名空间(主要是 /proc 隔离)
args="$args --dev-bind / /" # 挂载根目录
Expand Down

0 comments on commit d8c5c79

Please sign in to comment.