This commit is contained in:
Generated
-9
@@ -5,12 +5,3 @@ version = 4
|
||||
[[package]]
|
||||
name = "alkane-os"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.190"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce5d3ddc6d3fa000eb1536d85e147bfe31aacaba692ed6a876f95cb7c855be78"
|
||||
|
||||
@@ -8,9 +8,6 @@ description = "Portable mining and tuning core for Alkane control boards"
|
||||
[lib]
|
||||
name = "alkane_os"
|
||||
|
||||
[dependencies]
|
||||
libc = "0.2.169"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
codegen-units = 1
|
||||
|
||||
+70
-23
@@ -24,6 +24,53 @@ const SET_FAN_TARGET: u8 = 4;
|
||||
const READ_TEMPERATURE: u8 = 5;
|
||||
const SET_PSU_ENABLED: u8 = 6;
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
compile_error!("Alkane HAL sockets require Linux");
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
mod sys {
|
||||
pub use std::ffi::{c_char as CChar, c_int as CInt, c_void as CVoid};
|
||||
pub type SaFamily = u16;
|
||||
pub type SocklenT = u32;
|
||||
|
||||
pub const AF_UNIX: CInt = 1;
|
||||
pub const SOCK_STREAM: CInt = 1;
|
||||
pub const SOCK_NONBLOCK: CInt = 0x800;
|
||||
pub const SOCK_CLOEXEC: CInt = 0x80000;
|
||||
pub const SOL_SOCKET: CInt = 1;
|
||||
pub const SO_ERROR: CInt = 4;
|
||||
pub const POLLOUT: i16 = 4;
|
||||
pub const EAGAIN: CInt = 11;
|
||||
pub const EISCONN: CInt = 106;
|
||||
pub const EINPROGRESS: CInt = 115;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct SockaddrUn {
|
||||
pub sun_family: SaFamily,
|
||||
pub sun_path: [CChar; 108],
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct PollFd {
|
||||
pub fd: CInt,
|
||||
pub events: i16,
|
||||
pub revents: i16,
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
pub fn socket(domain: CInt, socket_type: CInt, protocol: CInt) -> CInt;
|
||||
pub fn connect(fd: CInt, address: *const CVoid, length: SocklenT) -> CInt;
|
||||
pub fn poll(fds: *mut PollFd, count: usize, timeout_ms: CInt) -> CInt;
|
||||
pub fn getsockopt(
|
||||
fd: CInt,
|
||||
level: CInt,
|
||||
name: CInt,
|
||||
value: *mut CVoid,
|
||||
length: *mut SocklenT,
|
||||
) -> CInt;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum HalError {
|
||||
Io(io::Error),
|
||||
@@ -192,8 +239,8 @@ fn read_exact_until(
|
||||
|
||||
fn connect_until(path: &Path, deadline: Instant) -> io::Result<UnixStream> {
|
||||
let bytes = path.as_os_str().as_bytes();
|
||||
let max_path = std::mem::size_of::<libc::sockaddr_un>()
|
||||
- std::mem::offset_of!(libc::sockaddr_un, sun_path);
|
||||
let max_path =
|
||||
std::mem::size_of::<sys::SockaddrUn>() - std::mem::offset_of!(sys::SockaddrUn, sun_path);
|
||||
if bytes.is_empty() || bytes.len() >= max_path || bytes.contains(&0) {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
@@ -201,9 +248,9 @@ fn connect_until(path: &Path, deadline: Instant) -> io::Result<UnixStream> {
|
||||
));
|
||||
}
|
||||
let raw = unsafe {
|
||||
libc::socket(
|
||||
libc::AF_UNIX,
|
||||
libc::SOCK_STREAM | libc::SOCK_NONBLOCK | libc::SOCK_CLOEXEC,
|
||||
sys::socket(
|
||||
sys::AF_UNIX,
|
||||
sys::SOCK_STREAM | sys::SOCK_NONBLOCK | sys::SOCK_CLOEXEC,
|
||||
0,
|
||||
)
|
||||
};
|
||||
@@ -211,18 +258,18 @@ fn connect_until(path: &Path, deadline: Instant) -> io::Result<UnixStream> {
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
let fd = unsafe { OwnedFd::from_raw_fd(raw) };
|
||||
let mut address: libc::sockaddr_un = unsafe { std::mem::zeroed() };
|
||||
address.sun_family = libc::AF_UNIX as libc::sa_family_t;
|
||||
let mut address: sys::SockaddrUn = unsafe { std::mem::zeroed() };
|
||||
address.sun_family = sys::AF_UNIX as sys::SaFamily;
|
||||
for (destination, source) in address.sun_path.iter_mut().zip(bytes) {
|
||||
*destination = *source as libc::c_char;
|
||||
*destination = *source as sys::CChar;
|
||||
}
|
||||
let length =
|
||||
(std::mem::offset_of!(libc::sockaddr_un, sun_path) + bytes.len() + 1) as libc::socklen_t;
|
||||
(std::mem::offset_of!(sys::SockaddrUn, sun_path) + bytes.len() + 1) as sys::SocklenT;
|
||||
loop {
|
||||
let result = unsafe {
|
||||
libc::connect(
|
||||
sys::connect(
|
||||
fd.as_raw_fd(),
|
||||
&address as *const _ as *const libc::sockaddr,
|
||||
&address as *const _ as *const sys::CVoid,
|
||||
length,
|
||||
)
|
||||
};
|
||||
@@ -230,22 +277,22 @@ fn connect_until(path: &Path, deadline: Instant) -> io::Result<UnixStream> {
|
||||
break;
|
||||
}
|
||||
let error = io::Error::last_os_error();
|
||||
if error.raw_os_error() == Some(libc::EISCONN) {
|
||||
if error.raw_os_error() == Some(sys::EISCONN) {
|
||||
break;
|
||||
}
|
||||
if !matches!(
|
||||
error.raw_os_error(),
|
||||
Some(libc::EINPROGRESS) | Some(libc::EAGAIN)
|
||||
Some(sys::EINPROGRESS) | Some(sys::EAGAIN)
|
||||
) {
|
||||
return Err(error);
|
||||
}
|
||||
let mut poll_fd = libc::pollfd {
|
||||
let mut poll_fd = sys::PollFd {
|
||||
fd: fd.as_raw_fd(),
|
||||
events: libc::POLLOUT,
|
||||
events: sys::POLLOUT,
|
||||
revents: 0,
|
||||
};
|
||||
let wait = remaining(deadline)?.as_millis().min(i32::MAX as u128) as i32;
|
||||
let polled = unsafe { libc::poll(&mut poll_fd, 1, wait.max(1)) };
|
||||
let polled = unsafe { sys::poll(&mut poll_fd, 1, wait.max(1)) };
|
||||
if polled < 0 {
|
||||
let error = io::Error::last_os_error();
|
||||
if error.kind() == io::ErrorKind::Interrupted {
|
||||
@@ -256,14 +303,14 @@ fn connect_until(path: &Path, deadline: Instant) -> io::Result<UnixStream> {
|
||||
if polled == 0 {
|
||||
return Err(timeout_error());
|
||||
}
|
||||
let mut socket_error: libc::c_int = 0;
|
||||
let mut socket_error_len = std::mem::size_of_val(&socket_error) as libc::socklen_t;
|
||||
let mut socket_error: sys::CInt = 0;
|
||||
let mut socket_error_len = std::mem::size_of_val(&socket_error) as sys::SocklenT;
|
||||
if unsafe {
|
||||
libc::getsockopt(
|
||||
sys::getsockopt(
|
||||
fd.as_raw_fd(),
|
||||
libc::SOL_SOCKET,
|
||||
libc::SO_ERROR,
|
||||
&mut socket_error as *mut _ as *mut libc::c_void,
|
||||
sys::SOL_SOCKET,
|
||||
sys::SO_ERROR,
|
||||
&mut socket_error as *mut _ as *mut sys::CVoid,
|
||||
&mut socket_error_len,
|
||||
)
|
||||
} < 0
|
||||
@@ -273,7 +320,7 @@ fn connect_until(path: &Path, deadline: Instant) -> io::Result<UnixStream> {
|
||||
if socket_error != 0 {
|
||||
return Err(io::Error::from_raw_os_error(socket_error));
|
||||
}
|
||||
if error.raw_os_error() == Some(libc::EINPROGRESS) {
|
||||
if error.raw_os_error() == Some(sys::EINPROGRESS) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
+26
-1
@@ -2,7 +2,7 @@
|
||||
use alkane_os::hal_wire::{HalClient, HalError};
|
||||
use alkane_os::HardwareBackend;
|
||||
use std::io::{Read, Write};
|
||||
use std::os::unix::net::UnixStream;
|
||||
use std::os::unix::net::{UnixListener, UnixStream};
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
@@ -142,3 +142,28 @@ fn slow_partial_response_hits_absolute_exchange_deadline() {
|
||||
drop(backend);
|
||||
worker.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn connects_to_a_local_backend_socket() {
|
||||
let path = std::env::temp_dir().join(format!(
|
||||
"alkane-hal-{}-{}.sock",
|
||||
std::process::id(),
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos()
|
||||
));
|
||||
let listener = UnixListener::bind(&path).unwrap();
|
||||
let worker = thread::spawn(move || {
|
||||
let (mut server, _) = listener.accept().unwrap();
|
||||
let mut header = [0_u8; 16];
|
||||
server.read_exact(&mut header).unwrap();
|
||||
let mut request = [0_u8; 1];
|
||||
server.read_exact(&mut request).unwrap();
|
||||
reply(&mut server, &header, &[0]);
|
||||
});
|
||||
let mut backend = HalClient::connect(&path).unwrap();
|
||||
backend.set_psu_enabled(true).unwrap();
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_file(path).unwrap();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user