ei: add support for libei
This commit is contained in:
parent
084fe50259
commit
40e87f8f91
69 changed files with 4340 additions and 72 deletions
|
|
@ -2,12 +2,16 @@ use {crate::io_uring::IoUringError, thiserror::Error};
|
|||
pub use {
|
||||
buf_in::BufFdIn,
|
||||
buf_out::{BufFdOut, OutBuffer, OutBufferSwapchain},
|
||||
ei_formatter::EiMsgFormatter,
|
||||
ei_parser::{EiMsgParser, EiMsgParserError},
|
||||
formatter::MsgFormatter,
|
||||
parser::{MsgParser, MsgParserError},
|
||||
};
|
||||
|
||||
mod buf_in;
|
||||
mod buf_out;
|
||||
mod ei_formatter;
|
||||
mod ei_parser;
|
||||
mod formatter;
|
||||
mod parser;
|
||||
|
||||
|
|
|
|||
106
src/utils/buffd/ei_formatter.rs
Normal file
106
src/utils/buffd/ei_formatter.rs
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
use {
|
||||
crate::{
|
||||
ei::ei_object::EiObjectId,
|
||||
utils::buffd::buf_out::{MsgFds, OutBuffer, OutBufferMeta, OUT_BUF_SIZE},
|
||||
},
|
||||
std::{mem, rc::Rc},
|
||||
uapi::OwnedFd,
|
||||
};
|
||||
|
||||
pub struct EiMsgFormatter<'a> {
|
||||
buf: &'a mut [u8],
|
||||
meta: &'a mut OutBufferMeta,
|
||||
pos: usize,
|
||||
fds: &'a mut Vec<Rc<OwnedFd>>,
|
||||
}
|
||||
|
||||
impl<'a> EiMsgFormatter<'a> {
|
||||
pub fn new(buf: &'a mut OutBuffer, fds: &'a mut Vec<Rc<OwnedFd>>) -> Self {
|
||||
Self {
|
||||
pos: buf.meta.write_pos,
|
||||
buf: &mut buf.buf[..],
|
||||
fds,
|
||||
meta: &mut buf.meta,
|
||||
}
|
||||
}
|
||||
|
||||
fn write(&mut self, bytes: &[u8]) {
|
||||
if bytes.len() > OUT_BUF_SIZE - self.meta.write_pos {
|
||||
panic!("Out buffer overflow");
|
||||
}
|
||||
self.buf[self.meta.write_pos..self.meta.write_pos + bytes.len()].copy_from_slice(bytes);
|
||||
self.meta.write_pos += bytes.len();
|
||||
}
|
||||
|
||||
pub fn int(&mut self, int: i32) -> &mut Self {
|
||||
self.write(uapi::as_bytes(&int));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn uint(&mut self, int: u32) -> &mut Self {
|
||||
self.write(uapi::as_bytes(&int));
|
||||
self
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn long(&mut self, int: i64) -> &mut Self {
|
||||
self.write(uapi::as_bytes(&int));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn ulong(&mut self, int: u64) -> &mut Self {
|
||||
self.write(uapi::as_bytes(&int));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn float(&mut self, f: f32) -> &mut Self {
|
||||
self.write(uapi::as_bytes(&f));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn optstr<S: AsRef<[u8]> + ?Sized>(&mut self, s: Option<&S>) -> &mut Self {
|
||||
match s {
|
||||
Some(s) => self.string(s),
|
||||
_ => self.uint(0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn string<S: AsRef<[u8]> + ?Sized>(&mut self, s: &S) -> &mut Self {
|
||||
let s = s.as_ref();
|
||||
let len = s.len() + 1;
|
||||
let cap = (len + 3) & !3;
|
||||
self.uint(len as u32);
|
||||
self.write(uapi::as_bytes(s));
|
||||
let none = [0; 4];
|
||||
self.write(&none[..cap - len + 1]);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn fd(&mut self, fd: Rc<OwnedFd>) -> &mut Self {
|
||||
self.fds.push(fd);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn object<T: Into<EiObjectId>>(&mut self, obj: T) -> &mut Self {
|
||||
self.ulong(obj.into().raw())
|
||||
}
|
||||
|
||||
pub fn header<T: Into<EiObjectId>>(&mut self, obj: T, event: u32) -> &mut Self {
|
||||
self.object(obj).uint(0).uint(event)
|
||||
}
|
||||
|
||||
pub fn write_len(self) {
|
||||
assert!(self.meta.write_pos - self.pos >= 16);
|
||||
assert_eq!(self.pos % 4, 0);
|
||||
unsafe {
|
||||
let second_ptr = self.buf.as_ptr().add(self.pos + 8) as *mut u32;
|
||||
*second_ptr = (self.meta.write_pos - self.pos) as u32;
|
||||
}
|
||||
if self.fds.len() > 0 {
|
||||
self.meta.fds.push_back(MsgFds {
|
||||
pos: self.pos,
|
||||
fds: mem::take(self.fds),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
112
src/utils/buffd/ei_parser.rs
Normal file
112
src/utils/buffd/ei_parser.rs
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
use {
|
||||
crate::{ei::ei_object::EiObjectId, utils::buffd::BufFdIn},
|
||||
std::{ptr, rc::Rc},
|
||||
thiserror::Error,
|
||||
uapi::OwnedFd,
|
||||
};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum EiMsgParserError {
|
||||
#[error("The message ended unexpectedly")]
|
||||
UnexpectedEof,
|
||||
#[error("The message contained a string of size 0")]
|
||||
EmptyString,
|
||||
#[error("Message is missing a required file descriptor")]
|
||||
MissingFd,
|
||||
#[error("There is trailing data after the message")]
|
||||
TrailingData,
|
||||
#[error("String is not UTF-8")]
|
||||
NonUtf8,
|
||||
}
|
||||
|
||||
pub struct EiMsgParser<'a, 'b> {
|
||||
buf: &'a mut BufFdIn,
|
||||
pos: usize,
|
||||
data: &'b [u8],
|
||||
}
|
||||
|
||||
impl<'a, 'b> EiMsgParser<'a, 'b> {
|
||||
pub fn new(buf: &'a mut BufFdIn, data: &'b [u32]) -> Self {
|
||||
Self {
|
||||
buf,
|
||||
pos: 0,
|
||||
data: uapi::as_bytes(data),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn int(&mut self) -> Result<i32, EiMsgParserError> {
|
||||
if self.data.len() - self.pos < 4 {
|
||||
return Err(EiMsgParserError::UnexpectedEof);
|
||||
}
|
||||
let res = unsafe { *(self.data.as_ptr().add(self.pos) as *const i32) };
|
||||
self.pos += 4;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub fn uint(&mut self) -> Result<u32, EiMsgParserError> {
|
||||
self.int().map(|i| i as u32)
|
||||
}
|
||||
|
||||
pub fn long(&mut self) -> Result<i64, EiMsgParserError> {
|
||||
if self.data.len() - self.pos < 8 {
|
||||
return Err(EiMsgParserError::UnexpectedEof);
|
||||
}
|
||||
let res = unsafe { ptr::read_unaligned(self.data.as_ptr().add(self.pos) as *const i64) };
|
||||
self.pos += 8;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub fn ulong(&mut self) -> Result<u64, EiMsgParserError> {
|
||||
self.long().map(|i| i as u64)
|
||||
}
|
||||
|
||||
pub fn object<T>(&mut self) -> Result<T, EiMsgParserError>
|
||||
where
|
||||
EiObjectId: Into<T>,
|
||||
{
|
||||
self.ulong().map(|i| EiObjectId::from_raw(i).into())
|
||||
}
|
||||
|
||||
pub fn float(&mut self) -> Result<f32, EiMsgParserError> {
|
||||
Ok(f32::from_bits(self.uint()?))
|
||||
}
|
||||
|
||||
pub fn optstr(&mut self) -> Result<Option<&'b str>, EiMsgParserError> {
|
||||
let len = self.uint()? as usize;
|
||||
if len == 0 {
|
||||
return Ok(None);
|
||||
}
|
||||
let cap = (len + 3) & !3;
|
||||
if cap > self.data.len() - self.pos {
|
||||
return Err(EiMsgParserError::UnexpectedEof);
|
||||
}
|
||||
let pos = self.pos;
|
||||
self.pos += cap;
|
||||
match std::str::from_utf8(&self.data[pos..pos + len - 1]) {
|
||||
Ok(s) => Ok(Some(s)),
|
||||
Err(_) => Err(EiMsgParserError::NonUtf8),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn str(&mut self) -> Result<&'b str, EiMsgParserError> {
|
||||
match self.optstr()? {
|
||||
Some(s) => Ok(s),
|
||||
_ => Err(EiMsgParserError::EmptyString),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fd(&mut self) -> Result<Rc<OwnedFd>, EiMsgParserError> {
|
||||
match self.buf.get_fd() {
|
||||
Ok(fd) => Ok(fd),
|
||||
_ => Err(EiMsgParserError::MissingFd),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eof(&self) -> Result<(), EiMsgParserError> {
|
||||
if self.pos == self.data.len() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(EiMsgParserError::TrailingData)
|
||||
}
|
||||
}
|
||||
}
|
||||
44
src/utils/pid_info.rs
Normal file
44
src/utils/pid_info.rs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
use {
|
||||
crate::utils::{errorfmt::ErrorFmt, oserror::OsError, trim::AsciiTrim},
|
||||
bstr::ByteSlice,
|
||||
uapi::{c, OwnedFd},
|
||||
};
|
||||
|
||||
pub struct PidInfo {
|
||||
pub _uid: c::uid_t,
|
||||
pub pid: c::pid_t,
|
||||
pub comm: String,
|
||||
}
|
||||
|
||||
pub fn get_pid_info(uid: c::uid_t, pid: c::pid_t) -> PidInfo {
|
||||
let comm = match std::fs::read(format!("/proc/{}/comm", pid)) {
|
||||
Ok(name) => name.trim().as_bstr().to_string(),
|
||||
Err(e) => {
|
||||
log::warn!("Could not read `comm` of pid {}: {}", pid, ErrorFmt(e));
|
||||
"Unknown".to_string()
|
||||
}
|
||||
};
|
||||
PidInfo {
|
||||
_uid: uid,
|
||||
pid,
|
||||
comm,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_socket_creds(socket: &OwnedFd) -> Option<(c::uid_t, c::pid_t)> {
|
||||
let mut cred = c::ucred {
|
||||
pid: 0,
|
||||
uid: 0,
|
||||
gid: 0,
|
||||
};
|
||||
match uapi::getsockopt(socket.raw(), c::SOL_SOCKET, c::SO_PEERCRED, &mut cred) {
|
||||
Ok(_) => Some((cred.uid, cred.pid)),
|
||||
Err(e) => {
|
||||
log::error!(
|
||||
"Cannot determine peer credentials of new connection: {:?}",
|
||||
OsError::from(e)
|
||||
);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -37,6 +37,10 @@ impl<T> SyncQueue<T> {
|
|||
unsafe { self.el.get().deref_mut().is_empty() }
|
||||
}
|
||||
|
||||
pub fn is_not_empty(&self) -> bool {
|
||||
!self.is_empty()
|
||||
}
|
||||
|
||||
pub fn swap(&self, queue: &mut VecDeque<T>) {
|
||||
unsafe {
|
||||
mem::swap(self.el.get().deref_mut(), queue);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue