ANDROID: ashmem_rust: add show_fdinfo and GET_FILE_ID

These utilities let you get debug information about an ashmem file.

Bug: 370906207
Change-Id: I5b9d00650272b3b814eb9e37a3b38123331f7624
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
This commit is contained in:
Alice Ryhl
2024-09-13 13:43:36 +00:00
committed by Treehugger Robot
parent 00c67f121e
commit 9271869729
2 changed files with 32 additions and 0 deletions
+27
View File
@@ -20,6 +20,7 @@ use kernel::{
mm::virt::{flags as vma_flags, VmAreaNew},
page::page_align,
prelude::*,
seq_file::{seq_print, SeqFile},
sync::{new_mutex, Mutex},
task::Task,
uaccess::{UserSlice, UserSliceReader, UserSliceWriter},
@@ -208,6 +209,7 @@ impl MiscDevice for Ashmem {
bindings::ASHMEM_GET_SIZE => me.get_size(),
bindings::ASHMEM_SET_PROT_MASK => me.set_prot_mask(arg),
bindings::ASHMEM_GET_PROT_MASK => me.get_prot_mask(),
bindings::ASHMEM_GET_FILE_ID => me.get_file_id(UserSlice::new(arg, size).writer()),
_ => Err(EINVAL),
}
}
@@ -221,6 +223,19 @@ impl MiscDevice for Ashmem {
};
Self::ioctl(me, file, cmd, arg)
}
fn show_fdinfo(me: Pin<&Ashmem>, m: &SeqFile, _file: &File) {
let asma = me.inner.lock();
if let Some(file) = asma.file.as_ref() {
seq_print!(m, "inode:\t{}\n", file.inode_ino());
}
if let Some(name) = asma.name.as_ref() {
let name = core::str::from_utf8(name).unwrap_or("<invalid utf-8>");
seq_print!(m, "name:\t{}\n", name);
}
seq_print!(m, "size\t{}\n", asma.size);
}
}
impl Ashmem {
@@ -298,6 +313,18 @@ impl Ashmem {
fn get_prot_mask(&self) -> Result<isize> {
Ok(self.inner.lock().prot_mask as isize)
}
fn get_file_id(&self, mut writer: UserSliceWriter) -> Result<isize> {
let ino = {
let asma = self.inner.lock();
let Some(file) = asma.file.as_ref() else {
return Err(EINVAL);
};
file.inode_ino()
};
writer.write(&ino)?;
Ok(0)
}
}
impl AshmemInner {
+5
View File
@@ -107,6 +107,11 @@ impl ShmemFile {
Ok(ret as loff_t)
}
}
pub(crate) fn inode_ino(&self) -> usize {
// SAFETY: Accessing the ino is always okay.
unsafe { (*(*self.inner.as_ptr()).f_inode).i_ino as usize }
}
}
/// Fix the lockdep class of the shmem inode.