Skip to content

Commit

Permalink
Returns the total number of fds (nervosnetwork#118)
Browse files Browse the repository at this point in the history
* Returns the total number of fds

* Use ///

* Add example
  • Loading branch information
mohanson authored Oct 11, 2024
1 parent 548a42b commit d74821c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/high_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,3 +713,16 @@ pub fn spawn_cell(
syscalls::spawn(index, Source::CellDep, 0, 0, &mut spgs)?;
Ok(process_id)
}

/// Get inherited file descriptors.
///
/// # Example
///
/// ```
/// let fds = inherited_fds();
/// ```
pub fn inherited_fds() -> Vec<u64> {
let mut fds = [0u64; 64];
let l = syscalls::inherited_fds(&mut fds);
fds[..l as usize].to_vec()
}
3 changes: 2 additions & 1 deletion src/syscalls/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ pub fn write(fd: u64, buffer: &[u8]) -> Result<usize, SysError> {
/// This syscall retrieves the file descriptors available to the current process, which are passed in from the parent
/// process. These results are copied from the inherited_fds parameter of the Spawn syscall.
/// Note: available after ckb2023.
pub fn inherited_fds(fds: &mut [u64]) {
pub fn inherited_fds(fds: &mut [u64]) -> u64 {
let mut l: u64 = fds.len() as u64;
unsafe {
syscall(
Expand All @@ -734,6 +734,7 @@ pub fn inherited_fds(fds: &mut [u64]) {
SYS_INHERITED_FDS,
)
};
l
}

/// This syscall manually closes a file descriptor. After calling this, any attempt to read/write the file descriptor
Expand Down
2 changes: 1 addition & 1 deletion src/syscalls/simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ pub fn write(_fd: u64, _buffer: &[u8]) -> Result<usize, SysError> {
panic!("This is not supported in the native-simulator!");
}

pub fn inherited_fds(_fds: &mut [u64]) {
pub fn inherited_fds(_fds: &mut [u64]) -> u64 {
panic!("This is not supported in the native-simulator!");
}

Expand Down

0 comments on commit d74821c

Please sign in to comment.