|
5 | 5 | #![allow(non_snake_case)] |
6 | 6 | include!(concat!(env!("OUT_DIR"), "/bindings.rs")); |
7 | 7 |
|
8 | | -use byteorder::{NativeEndian, WriteBytesExt}; |
| 8 | +use std::ffi::CString; |
9 | 9 |
|
10 | | -#[derive(thiserror::Error, Debug)] |
| 10 | +use thiserror::Error; |
| 11 | + |
| 12 | +#[derive(Error, Debug)] |
11 | 13 | pub enum NewBpfstructError { |
12 | | - #[error("FFI nul error")] |
| 14 | + #[error(transparent)] |
13 | 15 | NulError(#[from] std::ffi::NulError), |
14 | | -} |
15 | | - |
16 | | -#[derive(thiserror::Error, Debug)] |
17 | | -pub enum MapOperationError { |
18 | | - #[error("could not convert the key to a byte array")] |
19 | | - ByteWriteError(#[from] std::io::Error), |
20 | | - |
21 | | - #[error("libbpf error")] |
22 | | - LibbpfError(#[from] libbpf_rs::Error), |
23 | | -} |
24 | 16 |
|
25 | | -/// Deletes an entry from the given map under the given key. |
26 | | -pub fn map_delete(map: &mut libbpf_rs::Map, key: u32) -> Result<(), MapOperationError> { |
27 | | - let mut key_b = vec![]; |
28 | | - key_b.write_u32::<NativeEndian>(key)?; |
29 | | - |
30 | | - map.delete(&key_b)?; |
31 | | - |
32 | | - Ok(()) |
| 17 | + #[error("could not convert Vec<u8> to CString")] |
| 18 | + VecU8CStringConv, |
33 | 19 | } |
34 | 20 |
|
35 | | -pub trait BpfStruct { |
36 | | - /// Updates the given map with an entry under the given key and a value |
37 | | - /// with a binary representation of the struct. |
38 | | - fn map_update(&self, map: &mut libbpf_rs::Map, key: u32) -> Result<(), MapOperationError> { |
39 | | - let mut key_b = vec![]; |
40 | | - key_b.write_u32::<NativeEndian>(key)?; |
41 | | - |
42 | | - let val_b = unsafe { plain::as_bytes(self) }; |
43 | | - |
44 | | - map.update(&key_b, val_b, libbpf_rs::MapFlags::empty())?; |
45 | | - |
46 | | - Ok(()) |
47 | | - } |
48 | | -} |
49 | | - |
50 | | -impl BpfStruct for container {} |
51 | | -impl BpfStruct for process {} |
52 | | -impl BpfStruct for accessed_path {} |
53 | | - |
54 | 21 | impl accessed_path { |
55 | 22 | /// Creates a new accessed_path instance and converts the given Rust string |
56 | 23 | /// into C fixed-size char array. |
57 | 24 | pub fn new(path: &str) -> Result<Self, NewBpfstructError> { |
58 | | - let mut path_b = std::ffi::CString::new(path)?.into_bytes_with_nul(); |
| 25 | + let mut path_b = CString::new(path)?.into_bytes_with_nul(); |
59 | 26 | path_b.resize(PATH_LEN as usize, 0); |
60 | 27 | Ok(accessed_path { |
61 | | - path: path_b.try_into().unwrap(), |
| 28 | + path: path_b |
| 29 | + .try_into() |
| 30 | + .map_err(|_| NewBpfstructError::VecU8CStringConv)?, |
62 | 31 | }) |
63 | 32 | } |
64 | 33 | } |
65 | 34 |
|
| 35 | +impl container_id { |
| 36 | + /// Creates a new container_id instance and converts the given Rust string |
| 37 | + /// into C fixed size char array. |
| 38 | + pub fn new(id: &str) -> Result<Self, NewBpfstructError> { |
| 39 | + let mut id_b = CString::new(id)?.into_bytes_with_nul(); |
| 40 | + id_b.resize(CONTAINER_ID_LIMIT as usize, 0); |
| 41 | + Ok(container_id { |
| 42 | + id: id_b.try_into().unwrap(), |
| 43 | + }) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +unsafe impl aya::Pod for accessed_path {} |
| 48 | +unsafe impl aya::Pod for container {} |
| 49 | +unsafe impl aya::Pod for container_id {} |
| 50 | +unsafe impl aya::Pod for process {} |
| 51 | + |
66 | 52 | #[cfg(test)] |
67 | 53 | mod tests { |
68 | 54 | use super::*; |
|
0 commit comments