|
| 1 | +//! APIs for interacting with the scheduler and with processes, |
| 2 | +//! corresponding to <linux/sched.h> and related header files. |
| 3 | +#![allow(improper_ctypes)] |
| 4 | + |
| 5 | +use core::ptr; |
| 6 | + |
| 7 | +use crate::bindings; |
| 8 | +use crate::rcu; |
| 9 | + |
| 10 | +extern "C" { |
| 11 | + fn task_lock_helper(p: *mut bindings::task_struct); |
| 12 | + fn task_unlock_helper(p: *mut bindings::task_struct); |
| 13 | + fn next_task_helper(p: *mut bindings::task_struct) -> *mut bindings::task_struct; |
| 14 | +} |
| 15 | + |
| 16 | +/// Represents a `struct task_struct *`. |
| 17 | +pub struct TaskStruct<'a>(&'a mut bindings::task_struct); |
| 18 | + |
| 19 | +impl TaskStruct<'_> { |
| 20 | + /// Returns the threadgroup ID (what userspace calls the process ID). |
| 21 | + pub fn tgid(&self) -> i32 { |
| 22 | + self.0.tgid |
| 23 | + } |
| 24 | + |
| 25 | + /// Returns the command name / process title. This is a short name, |
| 26 | + /// typically the base name of the command, and does not have the |
| 27 | + /// full path or arguments. It's a fixed-sized set of bytes, but by |
| 28 | + /// convention it's interpreted as NUL-terminated. |
| 29 | + pub fn comm(&mut self) -> [u8; bindings::TASK_COMM_LEN as usize] { |
| 30 | + let mut result = [0u8; bindings::TASK_COMM_LEN as usize]; |
| 31 | + unsafe { |
| 32 | + task_lock_helper(self.0); |
| 33 | + } |
| 34 | + // if only char were unsigned char |
| 35 | + let mut it = result.iter_mut(); |
| 36 | + for byte in &self.0.comm { |
| 37 | + if *byte == 0 { |
| 38 | + break; |
| 39 | + } |
| 40 | + match it.next() { |
| 41 | + Some(lvalue) => { |
| 42 | + *lvalue = *byte as _; |
| 43 | + } |
| 44 | + None => { |
| 45 | + break; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + unsafe { |
| 50 | + task_unlock_helper(self.0); |
| 51 | + } |
| 52 | + result |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/// Iterate over every process on the system. Returns only processes, |
| 57 | +/// i.e., thread group leaders. |
| 58 | +/// |
| 59 | +/// ``` |
| 60 | +/// let g = rcu::RcuReadGuard::new(); |
| 61 | +/// for p in each_process(&g) { |
| 62 | +/// println!("{:?}", p.comm()); |
| 63 | +/// } |
| 64 | +/// ``` |
| 65 | +pub struct EachProcess<'g> { |
| 66 | + p: *mut bindings::task_struct, |
| 67 | + _g: &'g rcu::RcuReadGuard, |
| 68 | +} |
| 69 | + |
| 70 | +pub fn each_process(g: &rcu::RcuReadGuard) -> EachProcess { |
| 71 | + // unsafe is bogus here because we don't read it |
| 72 | + // https://github.com/rust-lang/rust/issues/74843 |
| 73 | + EachProcess { |
| 74 | + p: unsafe { &mut bindings::init_task }, |
| 75 | + _g: g, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl<'g> Iterator for EachProcess<'g> { |
| 80 | + type Item = TaskStruct<'g>; |
| 81 | + |
| 82 | + fn next(&mut self) -> Option<TaskStruct<'g>> { |
| 83 | + // Safety: |
| 84 | + // - oldp is valid if not null, because it is either &init_task |
| 85 | + // (a static location) or updated by this function. |
| 86 | + // - next_task calls rcu_dereference internally, which is safe |
| 87 | + // because we hold self._g. |
| 88 | + // - The returned reference has lifetime 'g, which is valid |
| 89 | + // because self._g lives at least that long. |
| 90 | + let oldp = unsafe { self.p.as_mut()? }; |
| 91 | + self.p = unsafe { next_task_helper(self.p) }; |
| 92 | + if self.p == unsafe { &mut bindings::init_task } { |
| 93 | + self.p = ptr::null_mut(); |
| 94 | + } |
| 95 | + Some(TaskStruct(oldp)) |
| 96 | + } |
| 97 | +} |
0 commit comments