Skip to content

Commit

Permalink
Use which crate to resolve binary paths on agent (#120)
Browse files Browse the repository at this point in the history
* Use which crate to resolve binary paths on agent

* Cargo fmt
  • Loading branch information
twizmwazin authored Mar 6, 2024
1 parent 58d2fc6 commit 993aa97
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
1 change: 1 addition & 0 deletions crates/bh_agent_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ env_logger = { version = "0.11.2", default-features = false, features = ["auto-c
bimap = "0.6.3"
argh = "0.1.12"
unicode_reader = "1.0.2"
which = "6.0.0"

[target.'cfg(target_family = "unix")'.dependencies]
daemonize = "0.5.0"
Expand Down
26 changes: 14 additions & 12 deletions crates/bh_agent_server/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use bimap::BiMap;
use log::{debug, trace};
use std::collections::HashMap;
use std::ffi::OsStr;
use std::ffi::OsString;
use std::fs::{File, OpenOptions};
use std::sync::{Arc, RwLock};
use std::thread::sleep;
use std::time::Duration;

use subprocess::{Popen, PopenConfig};
use which::which;

use bh_agent_common::AgentError::{
InvalidFileDescriptor, InvalidProcessId, IoError, ProcessStartFailure, Unknown,
Expand Down Expand Up @@ -127,7 +128,10 @@ impl BhAgentState {
Redirection::Save => subprocess::Redirection::Pipe,
},
detached: false,
executable: config.executable.map(|s| s.into()),
executable: config
.executable
.and_then(|s| which(&s).ok())
.map(|s| s.into()),
env: config.env.map(|v| {
v.iter()
.map(|t| (t.0.clone().into(), t.1.clone().into()))
Expand All @@ -143,16 +147,14 @@ impl BhAgentState {
popenconfig.setpgid = config.setpgid || popenconfig.setpgid;
}

let proc = Popen::create(
config
.argv
.iter()
.map(OsStr::new)
.collect::<Vec<_>>()
.as_slice(),
popenconfig,
)
.map_err(|e| ProcessStartFailure(e.to_string()))?;
let mut argv: Vec<OsString> = config.argv.iter().map(OsString::from).collect();
if !argv.is_empty() {
argv[0] = which(&argv[0])
.map_err(|e| ProcessStartFailure(e.to_string()))?
.into_os_string();
}
let proc =
Popen::create(&argv, popenconfig).map_err(|e| ProcessStartFailure(e.to_string()))?;

let proc_id = self.take_proc_id()?;

Expand Down
4 changes: 2 additions & 2 deletions python/binharness/bootstrap/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import os
import subprocess
import time
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -30,8 +31,7 @@ def __init__(
"""Create an AgentConnection."""
process = subprocess.Popen(
[str(agent_binary), address, str(port)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env={**os.environ, "RUST_LOG": "bh_agent_server::util::read_chars=trace"},
)
self._process = process
time.sleep(0.1)
Expand Down

0 comments on commit 993aa97

Please sign in to comment.