|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +//! Trait for defining `seq_file`s. |
| 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 | +//! |
| 10 | +//! Reference: <https://www.kernel.org/doc/html/latest/filesystems/seq_file.html> |
| 11 | +
|
| 12 | +// Currently this module is only usable through proc_fs. |
| 13 | +#![cfg_attr(not(CONFIG_PROC_FS), allow(dead_code))] |
| 14 | + |
| 15 | +use core::{ |
| 16 | + iter::{Iterator, Peekable}, |
| 17 | + marker::PhantomData, |
| 18 | + mem, |
| 19 | + ops::{Deref, DerefMut}, |
| 20 | + ptr, |
| 21 | +}; |
| 22 | + |
| 23 | +use crate::{bindings, c_types, cstr, types::PointerWrapper, CStr}; |
| 24 | + |
| 25 | +#[cfg(CONFIG_PROC_FS)] |
| 26 | +pub use proc::proc_create_seq; |
| 27 | + |
| 28 | +// TODOABK |
| 29 | +/// Rust equivalent of the [`seq_operations`] interface on the C side. |
| 30 | +/// |
| 31 | +/// # Example |
| 32 | +/// |
| 33 | +/// ``` |
| 34 | +/// struct Data(&'static [String]); |
| 35 | +/// |
| 36 | +/// impl seq_file::SeqOperations for Data { |
| 37 | +/// type Item = &'static String; |
| 38 | +/// type Iterator = core::slice::Iter<'static, String>; |
| 39 | +/// type DataWrapper = Box<Self>; |
| 40 | +/// |
| 41 | +/// fn start(arg: &Data) -> Option<Box<Peekable<Self::Iterator>>> { |
| 42 | +/// let iter = arg.0.iter(); |
| 43 | +/// Box::try_new(iter.peekable()).ok() |
| 44 | +/// } |
| 45 | +/// |
| 46 | +/// fn display(item: &Self::Item) -> &str { |
| 47 | +/// &item[..] |
| 48 | +/// } |
| 49 | +/// } |
| 50 | +/// ``` |
| 51 | +/// |
| 52 | +/// [`seq_operations`]: ../../../include/linux/seq_file.h |
| 53 | +pub trait SeqOperations { |
| 54 | + /// Type produced on each iteration. |
| 55 | + type Item; |
| 56 | + |
| 57 | + /// Type created when the seq file is opened. |
| 58 | + type Iterator: Iterator<Item = Self::Item>; |
| 59 | + |
| 60 | + /// Wrapper used to store a pointer to `T` on the C side. |
| 61 | + type DataWrapper: PointerWrapper + Deref<Target = Self>; |
| 62 | + |
| 63 | + /// Wrapper used to store a pointer to the iterator on the C side. |
| 64 | + type IteratorWrapper: PointerWrapper + DerefMut<Target = Peekable<Self::Iterator>>; |
| 65 | + |
| 66 | + /// Called once each time the `seq_file` is opened. |
| 67 | + fn start(arg: &Self) -> Option<Self::IteratorWrapper>; |
| 68 | + |
| 69 | + /// How the item will be displayed to the reader. |
| 70 | + fn display(item: &Self::Item) -> &str; |
| 71 | +} |
| 72 | + |
| 73 | +extern "C" fn stop_callback<T: SeqOperations>( |
| 74 | + _m: *mut bindings::seq_file, |
| 75 | + v: *mut c_types::c_void, |
| 76 | +) { |
| 77 | + if !v.is_null() { |
| 78 | + // TODOABK |
| 79 | + // SAFETY: `v` was created by a previous call to `next_callback` or |
| 80 | + // `start_callback` and both functions return either a null pointer |
| 81 | + // or pointer generated by `Box<Peekable<T::Iterator>>::into_raw`. |
| 82 | + drop(unsafe { T::IteratorWrapper::from_pointer(v) }) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +extern "C" fn next_callback<T: SeqOperations>( |
| 87 | + _m: *mut bindings::seq_file, |
| 88 | + v: *mut c_types::c_void, |
| 89 | + pos: *mut bindings::loff_t, |
| 90 | +) -> *mut c_types::c_void { |
| 91 | + if v.is_null() { |
| 92 | + return ptr::null_mut(); |
| 93 | + } |
| 94 | + |
| 95 | + // TODOABK |
| 96 | + // SAFETY: `v` was created by a previous call to `next_callback` or |
| 97 | + // `start_callback` and both functions return either a null pointer |
| 98 | + // or pointer generated by `Box<Peekable<T::Iterator>>::into_raw`. |
| 99 | + // We already checked for he null pointer case above. |
| 100 | + let mut iterator = unsafe { T::IteratorWrapper::from_pointer(v) }; |
| 101 | + |
| 102 | + // SAFETY: The caller guarantees tha `pos` is a valid pointer to an |
| 103 | + // `loff_t` and expects this function to mutate the value. |
| 104 | + unsafe { |
| 105 | + *pos += 1; |
| 106 | + } |
| 107 | + |
| 108 | + if iterator.next().is_none() { |
| 109 | + return ptr::null_mut(); |
| 110 | + } |
| 111 | + |
| 112 | + match iterator.peek() { |
| 113 | + Some(_next) => T::IteratorWrapper::into_pointer(iterator) as *mut _, |
| 114 | + None => ptr::null_mut(), |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +extern "C" fn show_callback<T: SeqOperations>( |
| 119 | + m: *mut bindings::seq_file, |
| 120 | + v: *mut c_types::c_void, |
| 121 | +) -> c_types::c_int { |
| 122 | + const FORMAT: CStr<'static> = cstr!("%.*s"); |
| 123 | + if v.is_null() { |
| 124 | + return 0; |
| 125 | + } |
| 126 | + // TODOABK |
| 127 | + // SAFETY: `v` was created by a previous call to `next_callback` or |
| 128 | + // `start_callback` and both functions return either a null pointer |
| 129 | + // or pointer generated by `Box<Peekable<T::Iterator>>::into_raw`. |
| 130 | + let mut iterator = unsafe { T::IteratorWrapper::from_pointer(v) }; |
| 131 | + if let Some(item) = iterator.peek() { |
| 132 | + let s = T::display(item); |
| 133 | + // SAFETY: Calling a C function. `FORMAT` is null terminated because |
| 134 | + // it comes from a `CStr`. `s` does not need to be null terminated |
| 135 | + // because we are only printing the first `s.len()` bytes. |
| 136 | + unsafe { |
| 137 | + bindings::seq_printf( |
| 138 | + m, |
| 139 | + FORMAT.as_ptr() as *const c_types::c_char, |
| 140 | + s.len(), |
| 141 | + s.as_ptr() as *const u8 as *const c_types::c_char, |
| 142 | + ); |
| 143 | + } |
| 144 | + } |
| 145 | + mem::forget(iterator); |
| 146 | + 0 |
| 147 | +} |
| 148 | + |
| 149 | +extern "C" fn start_callback<T: SeqOperations>( |
| 150 | + m: *mut bindings::seq_file, |
| 151 | + pos: *mut bindings::loff_t, |
| 152 | +) -> *mut c_types::c_void { |
| 153 | + // SAFETY: This function will be called by opening a proc file generated |
| 154 | + // from `proc_create_seq_private` on the C side with data created via |
| 155 | + // `T::DataWrapper::into_pointer`. We don't move the data in the wrapper |
| 156 | + // so the pointer will remain valid for later calls. |
| 157 | + let data_wrapper = |
| 158 | + unsafe { T::DataWrapper::from_pointer(bindings::PDE_DATA((*(*m).file).f_inode)) }; |
| 159 | + let iterator = T::start(&data_wrapper); |
| 160 | + // Data is still used in the `proc_dir_entry`. |
| 161 | + mem::forget(data_wrapper); |
| 162 | + // SAFETY: The caller guarantees that `pos` points to a valid `loff_t`. |
| 163 | + let pos = unsafe { *pos }; |
| 164 | + match iterator { |
| 165 | + Some(mut wrapper) => { |
| 166 | + for _ in 0..pos { |
| 167 | + wrapper.next(); |
| 168 | + } |
| 169 | + match wrapper.peek() { |
| 170 | + Some(_next) => T::IteratorWrapper::into_pointer(wrapper) as *mut _, |
| 171 | + None => ptr::null_mut(), |
| 172 | + } |
| 173 | + } |
| 174 | + None => ptr::null_mut(), |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +struct SeqFileOperationsVTable<T>(PhantomData<T>); |
| 179 | + |
| 180 | +impl<T: SeqOperations> SeqFileOperationsVTable<T> { |
| 181 | + const VTABLE: bindings::seq_operations = bindings::seq_operations { |
| 182 | + start: Some(start_callback::<T>), |
| 183 | + stop: Some(stop_callback::<T>), |
| 184 | + next: Some(next_callback::<T>), |
| 185 | + show: Some(show_callback::<T>), |
| 186 | + }; |
| 187 | + |
| 188 | + const fn build() -> &'static bindings::seq_operations { |
| 189 | + &Self::VTABLE |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +#[cfg(CONFIG_PROC_FS)] |
| 194 | +mod proc { |
| 195 | + use super::*; |
| 196 | + use crate::{ |
| 197 | + proc_fs::{self, ProcDirEntry}, |
| 198 | + Result, |
| 199 | + }; |
| 200 | + |
| 201 | + /// Create an entry in `/proc` for a `seq_file`. |
| 202 | + pub fn proc_create_seq<T: SeqOperations>( |
| 203 | + name: CStr<'static>, |
| 204 | + data: T::DataWrapper, |
| 205 | + ) -> Result<ProcDirEntry<T::DataWrapper>> { |
| 206 | + // SAFETY: The vtable for `T` expects a `T::DataWrapper` pointer in |
| 207 | + // the data field of the associated `proc_dir_entry`. |
| 208 | + unsafe { |
| 209 | + proc_fs::proc_create_seq_private(name, SeqFileOperationsVTable::<T>::build(), data) |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments