Skip to content

Commit

Permalink
refactor: fix ANR
Browse files Browse the repository at this point in the history
Mufanc committed Mar 18, 2024
1 parent 6ac0914 commit 078c94f
Showing 2 changed files with 29 additions and 17 deletions.
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
set -e

export SOURCE=$(curl -s https://im.qq.com/rainbow/linuxQQDownload/ | grep -Eo '"deb":"[^"]+"' | grep -Eo 'https://.*_amd64.*\.deb')
export PKGVER=$(echo "$SOURCE" | awk -F '_' '{print $2}')
export PKGROOT=$(realpath "$(dirname "$0")")
44 changes: 27 additions & 17 deletions daemon/src/inject.rs
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ use log::{debug, error, info};
use nix::libc::{c_int, RTLD_LAZY};
use nix::sys::socket;
use nix::sys::socket::{AddressFamily, SockFlag, SockType, UnixAddr};
use nix::unistd::getpid;
use nix::unistd::{fork, ForkResult, getpid};
use once_cell::sync::Lazy;
use tokio::io::AsyncWriteExt;
use tokio::net::UnixStream;
@@ -65,13 +65,13 @@ fn find_libc() -> Result<String> {
}


fn do_open(url: &str) -> Result<()> {
fn do_open(uri: &str) -> Result<()> {
let client = socket::socket(AddressFamily::Unix, SockType::Stream, SockFlag::empty(), None)?;
let client_raw = client.as_raw_fd();

socket::connect(client_raw, SERVER_ADDRESS.deref())?;

let url_owned = url.to_owned();
let url_owned = uri.to_owned();
let future = ASYNC_RUNTIME.spawn(async move {
let result: Result<()> = try {
let mut stream = UnixStream::from_std(SystemUnixStream::from(client))?;
@@ -87,6 +87,8 @@ fn do_open(url: &str) -> Result<()> {

ASYNC_RUNTIME.block_on(future)?;

debug!("do_open finished");

Ok(())
}

@@ -118,6 +120,8 @@ fn handle_open(args: &[&str]) {
if let Err(e) = do_open(&target) {
error!("failed to open [{}]: {}", args.join(", "), e);
}

debug!("handle_open finished");

exit(0);
}
@@ -146,23 +150,29 @@ pub fn execvp(file: *const c_char, argv: *const *const c_char) -> c_int {

if let Ok("xdg-open") = pathname {
info!("xdg-open detected, redirecting...");

let mut args: Vec<&str> = vec![];
let mut ptr: *const *const c_char = argv;
let mut index = 0;

unsafe {
while !(*ptr).is_null() {
let arg = CStr::from_ptr(*ptr).to_str().expect(&format!("failed to decode argv[{index}]"));
args.push(arg);
ptr = ptr.add(1);
index += 1;

if let Ok(ForkResult::Child) = unsafe { fork() } {
let mut args: Vec<&str> = vec![];
let mut ptr: *const *const c_char = argv;
let mut index = 0;

unsafe {
while !(*ptr).is_null() {
let arg = CStr::from_ptr(*ptr).to_str().expect(&format!("failed to decode argv[{index}]"));
args.push(arg);
ptr = ptr.add(1);
index += 1;
}
}
}

debug!("{}", args.join(" "));
debug!("{}", args.join(" "));

handle_open(&args);

handle_open(&args);
unreachable!("function `handle_open` returned unexpectedly");
} else {
exit(0)
}
}

real_execvp(file, argv)

0 comments on commit 078c94f

Please sign in to comment.