|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +//! Trait for defining `seq_file`s under `/proc`. |
| 4 | +//! |
| 5 | +//! This module allows Rust devices to implement [`struct seq_operations`] and |
| 6 | +//! and create a file under `/proc` based on that implementation. |
| 7 | +//! |
| 8 | +//! C header: [`include/linux/seq_file.h`](../../../include/linux/seq_file.h) |
| 9 | +//! C header: [`include/linux/proc_fs.h`](../../../include/linux/proc_fs.h) |
| 10 | +//! |
| 11 | +//! Reference: <https://www.kernel.org/doc/html/latest/filesystems/seq_file.html> |
| 12 | +
|
| 13 | +use alloc::{boxed::Box, string::ToString}; |
| 14 | +use core::{ |
| 15 | + fmt::Display, |
| 16 | + iter::{Iterator, Peekable}, |
| 17 | + marker::{PhantomData, PhantomPinned}, |
| 18 | + ops::Deref, |
| 19 | + pin::Pin, |
| 20 | + ptr, |
| 21 | +}; |
| 22 | + |
| 23 | +use crate::{bindings, c_types, CStr, KernelResult}; |
| 24 | + |
| 25 | +/// Rust equivalent of the [`seq_operations`] interface on the C side. |
| 26 | +/// |
| 27 | +/// # Example |
| 28 | +/// |
| 29 | +/// ``` |
| 30 | +/// struct Data { |
| 31 | +/// contents: Vec<u32>, |
| 32 | +/// } |
| 33 | +/// |
| 34 | +/// impl SeqOperations for Data { |
| 35 | +/// type Item = u32; |
| 36 | +/// type Iterator = alloc::vec::IntoIter<u32>; |
| 37 | +/// |
| 38 | +/// fn start(arg: &self) -> Option<Box<Peekable<Self::Iterator>>> { |
| 39 | +/// Box::try_new(arg.contents.clone().into_iter().peekable()).ok() |
| 40 | +/// } |
| 41 | +/// } |
| 42 | +/// ``` |
| 43 | +/// |
| 44 | +/// [`seq_operations`]: ../../../include/linux/seq_file.h |
| 45 | +pub trait SeqOperations { |
| 46 | + /// Type produced on each iteration. |
| 47 | + type Item: Display; |
| 48 | + |
| 49 | + /// Type created when the seq file is opened. |
| 50 | + type Iterator: Iterator<Item = Self::Item>; |
| 51 | + |
| 52 | + /// Called once each time the `seq_file` is opened. |
| 53 | + fn start(arg: &Self) -> Option<Box<Peekable<Self::Iterator>>>; |
| 54 | +} |
| 55 | + |
| 56 | +extern "C" fn stop_callback<T: SeqOperations>( |
| 57 | + _m: *mut bindings::seq_file, |
| 58 | + v: *mut c_types::c_void, |
| 59 | +) { |
| 60 | + if !v.is_null() { |
| 61 | + // SAFETY: `v` was created by a previous call to `next_callback` or |
| 62 | + // `start_callback` and both functions return either a null pointer |
| 63 | + // or pointer generated by `Box<Peekable<T::Iterator>>::into_raw`. |
| 64 | + let _iterator = unsafe { Box::from_raw(v as *mut Peekable<T::Iterator>) }; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +extern "C" fn next_callback<T: SeqOperations>( |
| 69 | + _m: *mut bindings::seq_file, |
| 70 | + v: *mut c_types::c_void, |
| 71 | + pos: *mut bindings::loff_t, |
| 72 | +) -> *mut c_types::c_void { |
| 73 | + if !v.is_null() { |
| 74 | + // SAFETY: `v` was created by a previous call to `next_callback` or |
| 75 | + // `start_callback` and both functions return either a null pointer |
| 76 | + // or pointer generated by `Box<Peekable<T::Iterator>>::into_raw`. |
| 77 | + // |
| 78 | + // The caller guarantees tha `pos` is a valid pointer to an |
| 79 | + // `loff_t` and expects this function to mutate the value. |
| 80 | + let mut iterator = unsafe { |
| 81 | + *pos += 1; |
| 82 | + Box::from_raw(v as *mut Peekable<T::Iterator>) |
| 83 | + }; |
| 84 | + match iterator.as_mut().next() { |
| 85 | + Some(_seen) => Box::into_raw(iterator) as *mut c_types::c_void, |
| 86 | + None => ptr::null_mut(), |
| 87 | + } |
| 88 | + } else { |
| 89 | + ptr::null_mut() |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +extern "C" fn show_callback<T: SeqOperations>( |
| 94 | + m: *mut bindings::seq_file, |
| 95 | + v: *mut c_types::c_void, |
| 96 | +) -> c_types::c_int { |
| 97 | + // SAFETY: `v` was created by a previous call to `next_callback` or |
| 98 | + // `start_callback` and both functions return either a null pointer |
| 99 | + // or pointer generated by `Box<Peekable<T::Iterator>>::into_raw`. |
| 100 | + let iterator = unsafe { (v as *mut Peekable<T::Iterator>).as_mut() }; |
| 101 | + if let Some(iterator) = iterator { |
| 102 | + let s = match iterator.peek() { |
| 103 | + // TODO: Replace with fallible `to_string` when available. |
| 104 | + Some(item) => item.to_string() + "\0", |
| 105 | + None => "\0".to_string(), |
| 106 | + }; |
| 107 | + // SAFETY: Calling a C function. `s` is guaranteed to be null terminated |
| 108 | + // because we explicitly constructed it just above. |
| 109 | + unsafe { |
| 110 | + bindings::seq_puts(m, s.as_ptr() as *const u8 as *const c_types::c_char); |
| 111 | + } |
| 112 | + } |
| 113 | + 0 |
| 114 | +} |
| 115 | + |
| 116 | +extern "C" fn start_callback<T: SeqOperations>( |
| 117 | + m: *mut bindings::seq_file, |
| 118 | + pos: *mut bindings::loff_t, |
| 119 | +) -> *mut c_types::c_void { |
| 120 | + // SAFETY: This function will be called by opening a proc file generated |
| 121 | + // by a call to `proc_create::<T>`, so `m` can be converted using |
| 122 | + // `SeqFile<T>`. |
| 123 | + let arg = unsafe { &*SeqFile::<T>::convert(m) }; |
| 124 | + // SAFETY: The caller guarantees that `pos` points to a valid `loff_t`. |
| 125 | + let pos = unsafe { *pos }; |
| 126 | + match T::start(arg) { |
| 127 | + Some(mut wrapper) => { |
| 128 | + for _ in 0..pos { |
| 129 | + wrapper.as_mut().next(); |
| 130 | + } |
| 131 | + Box::into_raw(wrapper) as *mut c_types::c_void |
| 132 | + } |
| 133 | + None => ptr::null_mut(), |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +struct SeqFileOperationsVTable<T>(PhantomData<T>); |
| 138 | + |
| 139 | +impl<T: SeqOperations> SeqFileOperationsVTable<T> { |
| 140 | + const VTABLE: bindings::seq_operations = bindings::seq_operations { |
| 141 | + start: Some(start_callback::<T>), |
| 142 | + stop: Some(stop_callback::<T>), |
| 143 | + next: Some(next_callback::<T>), |
| 144 | + show: Some(show_callback::<T>), |
| 145 | + }; |
| 146 | +} |
| 147 | + |
| 148 | +/// A `seq_file` referencing data of type `T`. |
| 149 | +pub struct SeqFile<T> { |
| 150 | + seq_ops: bindings::seq_operations, |
| 151 | + _pin: PhantomPinned, |
| 152 | + context: T, |
| 153 | +} |
| 154 | + |
| 155 | +impl<T> SeqFile<T> { |
| 156 | + fn new_pinned(seq_ops: bindings::seq_operations, context: T) -> KernelResult<Pin<Box<Self>>> { |
| 157 | + Ok(Pin::from(Box::try_new(SeqFile { |
| 158 | + seq_ops, |
| 159 | + _pin: PhantomPinned, |
| 160 | + context, |
| 161 | + })?)) |
| 162 | + } |
| 163 | + |
| 164 | + /// Retrieve the [`SeqFile::context`] associated with a [`bindings::seq_file`]. |
| 165 | + /// |
| 166 | + /// # Safety |
| 167 | + /// |
| 168 | + /// `m` must have been created from a proc file generated by calling |
| 169 | + /// `proc_create::<T>`. |
| 170 | + unsafe fn convert(m: *mut bindings::seq_file) -> *const T { |
| 171 | + let reg = crate::container_of!((*m).op, Self, seq_ops); |
| 172 | + &(*reg).context |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +/// Create a `/proc` file. |
| 177 | +/// |
| 178 | +/// The returned value must not be dropped until the module is unloaded. |
| 179 | +pub fn proc_create<T: SeqOperations + Sync>( |
| 180 | + name: CStr<'static>, |
| 181 | + context: T, |
| 182 | +) -> KernelResult<Pin<Box<SeqFile<T>>>> { |
| 183 | + let reg = SeqFile::new_pinned(SeqFileOperationsVTable::<T>::VTABLE, context)?; |
| 184 | + // SAFETY: Calling a C function. `name` is guaranteed to be null terminated |
| 185 | + // because it is of type `CStr`. |
| 186 | + unsafe { |
| 187 | + let _dir_entry = bindings::proc_create_seq_private( |
| 188 | + name.deref().as_ptr() as *const u8 as *const c_types::c_char, |
| 189 | + 0, |
| 190 | + ptr::null_mut(), |
| 191 | + ®.seq_ops, |
| 192 | + 0, |
| 193 | + ptr::null_mut(), |
| 194 | + ); |
| 195 | + Ok(reg) |
| 196 | + } |
| 197 | +} |
0 commit comments