|
| 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 alloc::vec::Vec; |
| 6 | +use core::ptr; |
| 7 | + |
| 8 | +use crate::bindings; |
| 9 | +use crate::rcu; |
| 10 | + |
| 11 | +extern "C" { |
| 12 | + fn task_lock_helper(p: *mut bindings::task_struct); |
| 13 | + fn task_unlock_helper(p: *mut bindings::task_struct); |
| 14 | + fn next_task_helper(p: *mut bindings::task_struct) -> *mut bindings::task_struct; |
| 15 | +} |
| 16 | + |
| 17 | +/// Represents a `struct task_struct *`. |
| 18 | +pub struct TaskStruct<'a>(&'a mut bindings::task_struct); |
| 19 | + |
| 20 | +impl TaskStruct<'_> { |
| 21 | + /// Returns the command name / process title. This is a short name, |
| 22 | + /// typically the base name of the command, and does not have the |
| 23 | + /// full path or arguments. It's a fixed-sized set of bytes, but by |
| 24 | + /// convention it's interpreted as NUL-terminated. |
| 25 | + pub fn comm(&mut self) -> Vec<u8> { |
| 26 | + unsafe { |
| 27 | + task_lock_helper(self.0); |
| 28 | + // if only char were unsigned char |
| 29 | + let v = (&*(&(*self.0).comm[..] as *const _ as *const [u8])).to_vec(); |
| 30 | + task_unlock_helper(self.0); |
| 31 | + v |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/// Iterate over every process on the system. Returns only processes, |
| 37 | +/// i.e., thread group leaders. |
| 38 | +/// |
| 39 | +/// ``` |
| 40 | +/// for p in each_process() { |
| 41 | +/// println!("{}", p.comm()); |
| 42 | +/// } |
| 43 | +/// ``` |
| 44 | +pub struct EachProcess<'g> { |
| 45 | + p: *mut bindings::task_struct, |
| 46 | + _g: &'g rcu::RcuReadGuard, |
| 47 | +} |
| 48 | + |
| 49 | +pub fn each_process(g: &rcu::RcuReadGuard) -> EachProcess { |
| 50 | + // unsafe is bogus here because we don't read it |
| 51 | + // https://github.com/rust-lang/rust/issues/74843 |
| 52 | + EachProcess { |
| 53 | + p: unsafe { &mut bindings::init_task }, |
| 54 | + _g: g, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl<'g> Iterator for EachProcess<'g> { |
| 59 | + type Item = TaskStruct<'g>; |
| 60 | + |
| 61 | + fn next(&mut self) -> Option<TaskStruct<'g>> { |
| 62 | + // Safety: |
| 63 | + // - oldp is valid if not null, because it is either &init_task |
| 64 | + // (a static location) or updated by this function. |
| 65 | + // - next_task calls rcu_dereference internally, which is safe |
| 66 | + // because we hold self._g. |
| 67 | + // - Casting the returned pointer to &'g is safe because _g lives |
| 68 | + // for at least 'g. |
| 69 | + let oldp = self.p; |
| 70 | + if oldp.is_null() { |
| 71 | + return None; |
| 72 | + } |
| 73 | + self.p = unsafe { next_task_helper(self.p) }; |
| 74 | + if self.p == unsafe { &mut bindings::init_task } { |
| 75 | + self.p = ptr::null_mut(); |
| 76 | + } |
| 77 | + Some(TaskStruct(unsafe { &mut *oldp })) |
| 78 | + } |
| 79 | +} |
0 commit comments