Repository navigation
Expand file tree
/
Copy pathlib.rs
More file actions
138 lines (113 loc) · 4.11 KB
/
Copy pathlib.rs
File metadata and controls
138 lines (113 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#![allow(dead_code, non_camel_case_types)]
use std::{error::Error, ffi::c_char, fmt::Display, hash::Hash, path::PathBuf};
use aya::{maps::lpm_trie, Pod};
use libc::memcpy;
use serde::{ser::SerializeStruct, Serialize};
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
#[derive(Debug)]
pub struct PathPrefixError {
prefix: String,
}
impl Error for PathPrefixError {}
impl Display for PathPrefixError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Invalid prefix: {}", self.prefix)
}
}
impl TryFrom<&PathBuf> for path_prefix_t {
type Error = PathPrefixError;
fn try_from(value: &PathBuf) -> Result<Self, PathPrefixError> {
let Some(filename) = value.to_str() else {
return Err(PathPrefixError {
prefix: value.display().to_string(),
});
};
// Take the start of the path until the first occurence of a wildcard
// character. This is used as a filter in the kernel in cases where
// the inode has failed to match. The full wildcard string is used
// for further processing in userspace.
//
// unwrap is safe here - if there are no matches, the full string is the
// only item in the iterator
let filename_prefix = filename.split(['*', '?', '[', '{']).next().unwrap();
let len = filename_prefix.len().min(LPM_SIZE_MAX as usize);
unsafe {
let mut cfg: path_prefix_t = std::mem::zeroed();
memcpy(
cfg.path.as_mut_ptr() as *mut _,
filename_prefix.as_ptr() as *const _,
len,
);
cfg.bit_len = (len * 8) as u32;
Ok(cfg)
}
}
}
impl From<path_prefix_t> for lpm_trie::Key<[c_char; LPM_SIZE_MAX as usize]> {
fn from(value: path_prefix_t) -> Self {
lpm_trie::Key::new(value.bit_len, value.path)
}
}
impl PartialEq for path_prefix_t {
fn eq(&self, other: &Self) -> bool {
self.bit_len == other.bit_len && self.path == other.path
}
}
unsafe impl Pod for path_prefix_t {}
impl inode_key_t {
pub fn empty(&self) -> bool {
self.inode == 0 && self.dev == 0
}
}
impl PartialEq for inode_key_t {
fn eq(&self, other: &Self) -> bool {
self.inode == other.inode && self.dev == other.dev
}
}
impl Eq for inode_key_t {}
impl Hash for inode_key_t {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.inode.hash(state);
self.dev.hash(state);
}
}
impl Serialize for inode_key_t {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut state = serializer.serialize_struct("inode_key_t", 2)?;
state.serialize_field("inode", &self.inode)?;
state.serialize_field("dev", &self.dev)?;
state.end()
}
}
unsafe impl Pod for inode_key_t {}
impl metrics_by_hook_t {
fn accumulate(&self, other: &metrics_by_hook_t) -> metrics_by_hook_t {
let mut m = metrics_by_hook_t { ..*self };
m.total += other.total;
m.added += other.added;
m.error += other.error;
m.ignored += other.ignored;
m.ringbuffer_full += other.ringbuffer_full;
m
}
}
impl metrics_t {
pub fn accumulate(&self, other: &metrics_t) -> metrics_t {
let mut m = metrics_t { ..*self };
m.file_open = m.file_open.accumulate(&other.file_open);
m.path_unlink = m.path_unlink.accumulate(&other.path_unlink);
m.path_chmod = m.path_chmod.accumulate(&other.path_chmod);
m.path_chown = m.path_chown.accumulate(&other.path_chown);
m.path_rename = m.path_rename.accumulate(&other.path_rename);
m.path_mkdir = m.path_mkdir.accumulate(&other.path_mkdir);
m.path_rmdir = m.path_rmdir.accumulate(&other.path_rmdir);
m.d_instantiate = m.d_instantiate.accumulate(&other.d_instantiate);
m
}
}
unsafe impl Pod for metrics_t {}
pub const EBPF_OBJ: &[u8] = aya::include_bytes_aligned!(concat!(env!("OUT_DIR"), "/main.o"));
pub const CHECKS_OBJ: &[u8] = aya::include_bytes_aligned!(concat!(env!("OUT_DIR"), "/checks.o"));