-
Notifications
You must be signed in to change notification settings - Fork 109
feat: add generic ioctl support #1819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zyuiop
wants to merge
1
commit into
hermit-os:main
Choose a base branch
from
zyuiop:feat/ioctl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,33 @@ | ||
use crate::errno::Errno; | ||
use crate::executor::block_on; | ||
use crate::fd::ObjectInterface; | ||
use crate::{fd, io}; | ||
|
||
#[cfg(feature = "tcp")] | ||
pub(crate) mod tcp; | ||
#[cfg(feature = "udp")] | ||
pub(crate) mod udp; | ||
#[cfg(feature = "vsock")] | ||
pub(crate) mod vsock; | ||
|
||
/// Handles an ioctl (general function) | ||
fn socket_handle_ioctl( | ||
this: &dyn ObjectInterface, | ||
cmd: crate::fs::ioctl::IoCtlCall, | ||
argp: *mut core::ffi::c_void, | ||
) -> io::Result<()> { | ||
const FIONBIO: u32 = 0x8008_667eu32; | ||
|
||
if cmd.into_bits() == FIONBIO { | ||
let value = unsafe { *(argp as *const i32) }; | ||
let status_flags = if value != 0 { | ||
fd::StatusFlags::O_NONBLOCK | ||
} else { | ||
fd::StatusFlags::empty() | ||
}; | ||
|
||
block_on(this.set_status_flags(status_flags), None) | ||
} else { | ||
Err(Errno::Inval) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
//! A module for custom IOCTL objects | ||
|
||
use alloc::boxed::Box; | ||
use alloc::sync::Arc; | ||
use alloc::vec::Vec; | ||
use core::fmt::Debug; | ||
|
||
use bitfield_struct::bitfield; | ||
|
||
use crate::fd::{AccessPermission, ObjectInterface}; | ||
use crate::fs::{NodeKind, VfsNode}; | ||
use crate::io; | ||
|
||
/// Encoding for an IOCTL command, as done in the Linux Kernel. | ||
/// | ||
/// See [relevant kernel header](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/asm-generic/ioctl.h?h=v6.15) for reference. | ||
/// | ||
/// The goal of this interface is to easily support linux applications that communicate via IOCTL, | ||
/// so linux compatibility is an intended and explicit goal here. | ||
#[bitfield(u32)] | ||
pub struct IoCtlCall { | ||
call_nr: u8, | ||
|
||
call_type: u8, | ||
|
||
#[bits(2, from = IoCtlDirection::from_bits_truncate, default = IoCtlDirection::empty())] | ||
call_dir: IoCtlDirection, | ||
|
||
#[bits(14)] | ||
call_size: u16, | ||
} | ||
|
||
bitflags! { | ||
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)] | ||
pub struct IoCtlDirection: u8 { | ||
const IOC_WRITE = 1; | ||
const IOC_READ = 2; | ||
} | ||
} | ||
|
||
impl IoCtlDirection { | ||
// Required for IoCtlCall | ||
const fn into_bits(self) -> u8 { | ||
self.bits() | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct IoCtlNode(Arc<dyn ObjectInterface>); | ||
|
||
impl VfsNode for IoCtlNode { | ||
fn get_kind(&self) -> NodeKind { | ||
NodeKind::File | ||
} | ||
|
||
fn get_object(&self) -> io::Result<Arc<dyn ObjectInterface>> { | ||
Ok(self.0.clone()) | ||
} | ||
} | ||
|
||
/// Register a custom object to handle IOCTLS at a given path, | ||
/// | ||
/// This call mounts a trivial VfsNode that opens to the provided ioctl_object at the given path. | ||
#[allow(dead_code)] | ||
pub(crate) fn register_ioctl(path: &str, ioctl_object: Arc<dyn ObjectInterface>) { | ||
assert!(path.starts_with("/")); | ||
let mut path: Vec<&str> = path.split("/").skip(1).collect(); | ||
assert!(!path.is_empty()); | ||
|
||
let fs = super::FILESYSTEM | ||
.get() | ||
.expect("Failed to mount ioctl: filesystem is not yet initialized"); | ||
|
||
// Create parent directory | ||
let mut directory: Vec<&str> = path.clone(); | ||
directory.pop().unwrap(); // remove file name | ||
directory.reverse(); | ||
|
||
if !directory.is_empty() { | ||
let _ = fs | ||
.root | ||
.traverse_mkdir(&mut directory, AccessPermission::all()); // ignore possible errors at this step | ||
} | ||
|
||
// Mount the file | ||
path.reverse(); | ||
fs.root | ||
.traverse_mount(&mut path, Box::new(IoCtlNode(ioctl_object))) | ||
.expect("Failed to mount ioctl: filesystem error"); | ||
} | ||
|
||
#[cfg(all(test, not(target_os = "none")))] | ||
mod tests { | ||
use super::{IoCtlCall, IoCtlDirection}; | ||
|
||
#[test] | ||
fn ioctl_call_correctly_written() { | ||
let call_nr = 0x12u8; | ||
let call_type = 0x78u8; | ||
let call_dir = IoCtlDirection::IOC_WRITE; | ||
let call_size = 0x423u16; | ||
|
||
let ioctl_call_number = (u32::from(call_size) << 18) | ||
| (u32::from(call_dir.bits()) << 16) | ||
| (u32::from(call_type) << 8) | ||
| u32::from(call_nr); | ||
|
||
let call = IoCtlCall::new() | ||
.with_call_nr(call_nr) | ||
.with_call_type(call_type) | ||
.with_call_dir(call_dir) | ||
.with_call_size(call_size); | ||
|
||
assert_eq!(ioctl_call_number, call.into_bits()); | ||
} | ||
#[test] | ||
fn ioctl_call_correctly_parsed() { | ||
let call_nr = 0x12u8; | ||
let call_type = 0x78u8; | ||
let call_dir = IoCtlDirection::IOC_WRITE; | ||
let call_size = 0x423u16; | ||
|
||
let ioctl_call_number = (u32::from(call_size) << 18) | ||
| (u32::from(call_dir.bits()) << 16) | ||
| (u32::from(call_type) << 8) | ||
| u32::from(call_nr); | ||
|
||
let parsed = IoCtlCall::from_bits(ioctl_call_number); | ||
|
||
assert_eq!(call_nr, parsed.call_nr()); | ||
assert_eq!(call_type, parsed.call_type()); | ||
assert_eq!(call_dir, parsed.call_dir()); | ||
assert_eq!(call_size, parsed.call_size()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#[cfg(all(feature = "fuse", feature = "pci"))] | ||
pub(crate) mod fuse; | ||
pub mod ioctl; | ||
mod mem; | ||
mod uhyve; | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check if we can remove this change to preserve the ABI