You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
3.6 KiB
Rust
166 lines
3.6 KiB
Rust
3 months ago
|
#![allow(dead_code)]
|
||
|
#![allow(unused_variables)]
|
||
3 months ago
|
use std::{marker::PhantomData, time::{Duration, Instant}};
|
||
3 months ago
|
use sysinfo::System;
|
||
|
|
||
3 months ago
|
|
||
3 months ago
|
#[derive(Debug)]
|
||
|
enum ProcState {
|
||
3 months ago
|
NeverSeen,
|
||
3 months ago
|
Seen,
|
||
|
NotSeen,
|
||
|
}
|
||
|
|
||
|
#[derive(Debug)]
|
||
3 months ago
|
struct Process {
|
||
3 months ago
|
first_seen: Option<Instant>,
|
||
|
last_seen: Option<Instant>,
|
||
|
state: ProcState,
|
||
|
prev_state: Option<ProcState>,
|
||
|
pid: Option<usize>,
|
||
|
}
|
||
|
|
||
3 months ago
|
|
||
3 months ago
|
impl Process {
|
||
3 months ago
|
fn new() -> Self {
|
||
|
Self {
|
||
|
first_seen: None,
|
||
|
last_seen: None,
|
||
|
state: ProcState::NeverSeen,
|
||
|
prev_state: None,
|
||
|
pid: None,
|
||
|
}
|
||
|
}
|
||
|
|
||
3 months ago
|
fn refresh(&mut self, sysinfo: &System, last_refresh: Instant) {
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
3 months ago
|
impl Matcher<Seen> for Process {
|
||
|
type Condition = LifetimeCond<Seen>;
|
||
|
|
||
|
fn matches(&self, c: Self::Condition) -> bool {
|
||
|
if !matches!(self.state, ProcState::Seen) { return false };
|
||
|
if let Some(first_seen) = self.first_seen {
|
||
|
first_seen.elapsed() > c.span
|
||
|
} else { false }
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Matcher<NotSeen> for Process {
|
||
|
type Condition = LifetimeCond<NotSeen>;
|
||
3 months ago
|
|
||
|
fn matches(&self, c: Self::Condition) -> bool {
|
||
3 months ago
|
if !matches!(self.state, ProcState::NotSeen | ProcState::NeverSeen) { return false };
|
||
3 months ago
|
if let Some(last_seen) = self.last_seen {
|
||
|
last_seen.elapsed() > c.span
|
||
|
} else { false }
|
||
|
}
|
||
|
}
|
||
|
|
||
3 months ago
|
impl<T> ProcessMatcher<T> for Process where Process: Matcher<T> {
|
||
|
fn matches_exe(&self, process: &sysinfo::Process) -> bool {
|
||
|
// if let Some(exe) = process.exe().and_then(|c| c.to_str()) {
|
||
|
// exe.contains(&self.conf.pattern)
|
||
|
// } else {
|
||
|
// false
|
||
|
// }
|
||
|
todo!()
|
||
|
}
|
||
3 months ago
|
|
||
3 months ago
|
fn matches_cmdline(&self, process: &sysinfo::Process) -> bool {
|
||
|
todo!()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
struct Seen {}
|
||
|
impl Seen {
|
||
3 months ago
|
fn from_duration(d: Duration) -> LifetimeCond<Self> {
|
||
|
LifetimeCond{span: d, ty: PhantomData{}}
|
||
|
}
|
||
|
}
|
||
|
|
||
3 months ago
|
struct NotSeen {}
|
||
|
impl NotSeen {
|
||
3 months ago
|
fn from_duration(d: Duration) -> LifetimeCond<Self> {
|
||
|
LifetimeCond{span: d, ty: PhantomData{}}
|
||
|
}
|
||
|
}
|
||
|
|
||
3 months ago
|
|
||
|
enum ProcLifetime {
|
||
|
Seen,
|
||
|
NotSeen
|
||
|
}
|
||
|
|
||
|
impl Matcher<ProcLifetime> for Process {
|
||
|
type Condition = ProcLifetime;
|
||
|
|
||
|
fn matches(&self, c: Self::Condition) -> bool {
|
||
|
match c {
|
||
|
ProcLifetime::Seen => todo!(),
|
||
|
ProcLifetime::NotSeen => todo!(),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
3 months ago
|
trait Condition<Type> {}
|
||
|
impl<T> Condition<T> for LifetimeCond<T>{}
|
||
3 months ago
|
impl<T> Condition<T> for ProcLifetime{}
|
||
3 months ago
|
|
||
|
struct LifetimeCond<CondType> {
|
||
|
span: Duration,
|
||
|
ty: PhantomData<CondType>
|
||
|
}
|
||
|
|
||
|
impl<T> LifetimeCond<T> {
|
||
|
fn new(span: Duration) -> Self {
|
||
|
Self{ span, ty: PhantomData{} }
|
||
|
}
|
||
3 months ago
|
}
|
||
|
|
||
3 months ago
|
|
||
|
|
||
3 months ago
|
trait Matcher<T> {
|
||
|
type Condition: Condition<T>;
|
||
3 months ago
|
|
||
|
fn matches(&self, c: Self::Condition) -> bool;
|
||
3 months ago
|
}
|
||
|
|
||
3 months ago
|
trait ProcessMatcher<T>: Matcher<T> {
|
||
|
fn matches_exe(&self, process: &sysinfo::Process) -> bool;
|
||
|
fn matches_cmdline(&self, process: &sysinfo::Process) -> bool;
|
||
|
}
|
||
|
|
||
3 months ago
|
|
||
|
fn main() {
|
||
|
unimplemented!()
|
||
3 months ago
|
}
|
||
|
|
||
3 months ago
|
#[cfg(test)]
|
||
|
mod test {
|
||
|
use sysinfo::{ProcessRefreshKind, RefreshKind, UpdateKind};
|
||
|
|
||
|
use super::*;
|
||
|
|
||
|
#[test]
|
||
3 months ago
|
fn process_seen() {
|
||
|
let cond = Seen::from_duration(Duration::from_secs(5));
|
||
3 months ago
|
|
||
|
let process_refresh_kind = ProcessRefreshKind::new()
|
||
|
.with_cmd(UpdateKind::Always)
|
||
|
.with_cwd(UpdateKind::Always)
|
||
|
.with_exe(UpdateKind::Always);
|
||
|
let process_refresh = RefreshKind::new().with_processes(process_refresh_kind);
|
||
|
let mut sys_info = System::new();
|
||
|
sys_info.refresh_specifics(process_refresh);
|
||
|
|
||
|
|
||
|
let mut p = Process::new();
|
||
|
p.refresh(&sys_info, Instant::now());
|
||
3 months ago
|
assert!(!Matcher::<Seen>::matches(&p, cond));
|
||
3 months ago
|
}
|
||
|
}
|