VirtualSystem

Struct VirtualSystem 

Source
pub struct VirtualSystem {
    pub state: Rc<RefCell<SystemState>>,
    pub process_id: Pid,
}
Expand description

Simulated system.

See the module-level documentation to grasp a basic understanding of VirtualSystem.

A VirtualSystem instance has two members: state and process_id. The former is a SystemState that effectively contains the state of the system. The state is contained in Rc so that processes can share the same state. The latter is a process ID that identifies a process calling the System interface.

When you clone a virtual system, the clone will have the same process_id as the original. To simulate the fork system call, you would probably want to assign a new process ID and add a new Process to the system state.

Fields§

§state: Rc<RefCell<SystemState>>

State of the system.

§process_id: Pid

Process ID of the process that is interacting with the system.

Implementations§

Source§

impl VirtualSystem

Source

pub fn new() -> VirtualSystem

Creates a virtual system with an almost empty state.

The process_id of the returned VirtualSystem will be 2. (Process ID 1 has special meaning in some system calls, so we don’t use it as a default value.)

The state of the returned VirtualSystem will have a Process with process ID 2 in the process set (SystemState::processes). The file system will contain files named /dev/stdin, /dev/stdout, and /dev/stderr that are opened in the process with file descriptor 0, 1, and 2, respectively. The file system also contains an empty directory /tmp.

Source

pub fn current_process(&self) -> Ref<'_, Process>

Finds the current process from the system state.

§Panics

This function will panic if it cannot find a process having self.process_id.

Source

pub fn current_process_mut(&self) -> RefMut<'_, Process>

Finds the current process from the system state.

§Panics

This function will panic if it cannot find a process having self.process_id.

Source

pub fn with_open_file_description<F, R>(&self, fd: Fd, f: F) -> Result<R>

Calls the given closure passing the open file description for the FD.

Returns Err(Errno::EBADF) if the FD is not open.

Source

pub fn with_open_file_description_mut<F, R>(&self, fd: Fd, f: F) -> Result<R>
where F: FnOnce(&mut OpenFileDescription) -> Result<R>,

Calls the given closure passing the open file description for the FD.

Returns Err(Errno::EBADF) if the FD is not open.

Trait Implementations§

Source§

impl CaughtSignals for VirtualSystem

Source§

fn caught_signals(&self) -> Vec<Number>

Returns signals this process has caught, if any. Read more
Source§

impl Chdir for VirtualSystem

Source§

fn chdir(&self, path: &CStr) -> Result<()>

Changes the working directory.
Source§

impl Clock for VirtualSystem

Source§

fn now(&self) -> Instant

Returns now in SystemState.

Panics if it is None.

Source§

impl Clone for VirtualSystem

Source§

fn clone(&self) -> VirtualSystem

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Close for VirtualSystem

Source§

fn close(&self, fd: Fd) -> Result<()>

Closes a file descriptor. Read more
Source§

impl Debug for VirtualSystem

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for VirtualSystem

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Dup for VirtualSystem

Source§

fn dup(&self, from: Fd, to_min: Fd, flags: EnumSet<FdFlag>) -> Result<Fd>

Duplicates a file descriptor. Read more
Source§

fn dup2(&self, from: Fd, to: Fd) -> Result<Fd>

Duplicates a file descriptor. Read more
Source§

impl Exec for VirtualSystem

Source§

fn execve( &self, path: &CStr, args: &[CString], envs: &[CString], ) -> FlexFuture<Result<Infallible>>

Stub for the execve system call.

The execve system call cannot be simulated in the userland. This function returns ENOSYS if the file at path is a native executable, ENOEXEC if a non-executable file, and ENOENT otherwise.

Source§

impl Exit for VirtualSystem

Source§

fn exit(&self, exit_status: ExitStatus) -> FlexFuture<Infallible>

Terminates the current process. Read more
Source§

impl Fcntl for VirtualSystem

Source§

fn ofd_access(&self, fd: Fd) -> Result<OfdAccess>

Returns the open file description access mode.
Source§

fn get_and_set_nonblocking(&self, fd: Fd, _nonblocking: bool) -> Result<bool>

Gets and sets the non-blocking mode for the open file description. Read more
Source§

fn fcntl_getfd(&self, fd: Fd) -> Result<EnumSet<FdFlag>>

Returns the attributes for the file descriptor.
Source§

fn fcntl_setfd(&self, fd: Fd, flags: EnumSet<FdFlag>) -> Result<()>

Sets attributes for the file descriptor.
Source§

impl Fork for VirtualSystem

Source§

fn new_child_process(&self) -> Result<ChildProcessStarter<Self>>

Creates a new child process.

This implementation does not create any real child process. Instead, it returns a child process starter that runs its task concurrently in the same process.

To run the concurrent task, this function needs an executor that has been set in the SystemState. If the system state does not have an executor, this function fails with Errno::ENOSYS.

The process ID of the child will be the maximum of existing process IDs plus 1. If there are no other processes, it will be 2.

Source§

impl Fstat for VirtualSystem

Source§

fn fstat(&self, fd: Fd) -> Result<Stat>

Retrieves metadata of a file. Read more
Source§

fn fstatat( &self, dir_fd: Fd, path: &CStr, follow_symlinks: bool, ) -> Result<Stat>

Retrieves metadata of a file. Read more
Source§

fn is_directory(&self, path: &CStr) -> bool

Whether there is a directory at the specified path.
Source§

fn fd_is_pipe(&self, fd: Fd) -> bool

Tests if a file descriptor is a pipe.
Source§

impl GetCwd for VirtualSystem

Source§

fn getcwd(&self) -> Result<PathBuf>

Returns the current working directory path.
Source§

impl GetPid for VirtualSystem

Source§

fn getsid(&self, pid: Pid) -> Result<Pid>

Currently, this function always returns Pid(2) if the process exists.

Source§

fn getpid(&self) -> Pid

Returns the process ID of the current process. Read more
Source§

fn getppid(&self) -> Pid

Returns the process ID of the parent process. Read more
Source§

fn getpgrp(&self) -> Pid

Returns the process group ID of the current process. Read more
Source§

impl GetPw for VirtualSystem

Source§

fn getpwnam_dir(&self, name: &CStr) -> Result<Option<PathBuf>>

Returns the home directory path of the given user. Read more
Source§

impl GetRlimit for VirtualSystem

Source§

fn getrlimit(&self, resource: Resource) -> Result<LimitPair>

Returns the limits for the specified resource. Read more
Source§

impl GetUid for VirtualSystem

Source§

fn getuid(&self) -> Uid

Returns the real user ID of the current process.
Source§

fn geteuid(&self) -> Uid

Returns the effective user ID of the current process.
Source§

fn getgid(&self) -> Gid

Returns the real group ID of the current process.
Source§

fn getegid(&self) -> Gid

Returns the effective group ID of the current process.
Source§

impl IsExecutableFile for VirtualSystem

Source§

fn is_executable_file(&self, path: &CStr) -> bool

Tests whether the specified file is executable or not.

The current implementation only checks if the file has any executable bit in the permissions. The file owner and group are not considered.

Source§

impl Isatty for VirtualSystem

Source§

fn isatty(&self, fd: Fd) -> bool

Tests if a file descriptor is associated with a terminal device. Read more
Source§

impl Open for VirtualSystem

Source§

fn open( &self, path: &CStr, access: OfdAccess, flags: EnumSet<OpenFlag>, mode: Mode, ) -> Result<Fd>

Opens a file descriptor. Read more
Source§

fn open_tmpfile(&self, _parent_dir: &Path) -> Result<Fd>

Opens a file descriptor associated with an anonymous temporary file. Read more
Source§

fn fdopendir(&self, fd: Fd) -> Result<impl Dir + use<>>

Opens a directory for enumerating entries. Read more
Source§

fn opendir(&self, path: &CStr) -> Result<impl Dir + use<>>

Opens a directory for enumerating entries. Read more
Source§

impl Pipe for VirtualSystem

Source§

fn pipe(&self) -> Result<(Fd, Fd)>

Creates an unnamed pipe. Read more
Source§

impl Read for VirtualSystem

Source§

fn read(&self, fd: Fd, buffer: &mut [u8]) -> Result<usize>

Reads from the file descriptor. Read more
Source§

impl Seek for VirtualSystem

Source§

fn lseek(&self, fd: Fd, position: SeekFrom) -> Result<u64>

Moves the position of the open file description. Read more
Source§

impl Select for VirtualSystem

Source§

fn select( &self, readers: &mut Vec<Fd>, writers: &mut Vec<Fd>, timeout: Option<Duration>, signal_mask: Option<&[Number]>, ) -> Result<c_int>

Waits for a next event.

The VirtualSystem implementation for this method does not actually block the calling thread. The method returns immediately in any case.

The timeout is ignored if this function returns because of a ready FD or a caught signal. Otherwise, the timeout is added to SystemState::now, which must not be None then.

Source§

impl SendSignal for VirtualSystem

Source§

fn kill(&self, target: Pid, signal: Option<Number>) -> FlexFuture<Result<()>>

Sends a signal to the target process.

This function returns a future that enables the executor to block the calling thread until the current process is ready to proceed. If the signal is sent to the current process and it causes the process to stop, the future will be ready only when the process is resumed. Similarly, if the signal causes the current process to terminate, the future will never be ready.

Source§

fn raise(&self, signal: Number) -> FlexFuture<Result<()>>

Sends a signal to the current process. Read more
Source§

impl SetPgid for VirtualSystem

Source§

fn setpgid(&self, pid: Pid, pgid: Pid) -> Result<()>

Modifies the process group ID of a process.

The current implementation does not yet support the concept of sessions.

Source§

impl SetRlimit for VirtualSystem

Source§

fn setrlimit(&self, resource: Resource, limits: LimitPair) -> Result<()>

Sets the limits for the specified resource. Read more
Source§

impl ShellPath for VirtualSystem

Source§

fn shell_path(&self) -> CString

Returns the path to the shell.

The current implementation returns “/bin/sh”.

Source§

impl Sigaction for VirtualSystem

Source§

fn get_sigaction(&self, signal: Number) -> Result<Disposition>

Gets the disposition for a signal. Read more
Source§

fn sigaction( &self, signal: Number, disposition: Disposition, ) -> Result<Disposition>

Gets and sets the disposition for a signal. Read more
Source§

impl Sigmask for VirtualSystem

Source§

fn sigmask( &self, op: Option<(SigmaskOp, &[Number])>, old_mask: Option<&mut Vec<Number>>, ) -> Result<()>

Gets and/or sets the signal blocking mask. Read more
Source§

impl Signals for VirtualSystem

Source§

fn validate_signal(&self, number: RawNumber) -> Option<(Name, Number)>

Tests if a signal number is valid. Read more
Source§

fn signal_number_from_name(&self, name: Name) -> Option<Number>

Gets the signal number from the signal name. Read more
Source§

fn signal_name_from_number(&self, number: Number) -> Name

Returns the signal name for the signal number. Read more
Source§

impl Sysconf for VirtualSystem

Source§

fn confstr_path(&self) -> Result<UnixString>

Returns the standard path for the system.

This function returns the value of SystemState::path. If it is empty, it returns the ENOSYS error.

Source§

impl TcGetPgrp for VirtualSystem

Source§

fn tcgetpgrp(&self, fd: Fd) -> Result<Pid>

Returns the current foreground process group ID.

The current implementation does not yet support the concept of controlling terminals and sessions. It accepts any open file descriptor.

Source§

impl TcSetPgrp for VirtualSystem

Source§

fn tcsetpgrp(&self, fd: Fd, pgid: Pid) -> FlexFuture<Result<()>>

Switches the foreground process.

The current implementation does not yet support the concept of controlling terminals and sessions. It accepts any open file descriptor.

Source§

impl Times for VirtualSystem

Source§

fn times(&self) -> Result<CpuTimes>

Returns times in SystemState.

Source§

impl Umask for VirtualSystem

Source§

fn umask(&self, new_mask: Mode) -> Mode

Gets and sets the file creation mode mask. Read more
Source§

impl Wait for VirtualSystem

Source§

fn wait(&self, target: Pid) -> Result<Option<(Pid, ProcessState)>>

Waits for a child.

TODO: Currently, this function only supports target == -1 || target > 0.

Source§

impl Write for VirtualSystem

Source§

fn write(&self, fd: Fd, buffer: &[u8]) -> Result<usize>

Writes to the file descriptor. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> SystemEx for T
where T: System + ?Sized,

Source§

fn move_fd_internal(&mut self, from: Fd) -> Result<Fd>

👎Deprecated since 0.11.0: use yash_env::io::move_fd_internal instead
Moves a file descriptor to MIN_INTERNAL_FD or larger. Read more
Source§

fn fd_is_pipe(&self, fd: Fd) -> bool

👎Deprecated since 0.11.0: use yash_env::system::Fstat::fd_is_pipe instead
Tests if a file descriptor is a pipe.
Source§

fn tcsetpgrp_with_block( &mut self, fd: Fd, pgid: Pid, ) -> impl Future<Output = Result<()>>

👎Deprecated since 0.11.0: use yash_env::job::tcsetpgrp_with_block instead
Switches the foreground process group with SIGTTOU blocked. Read more
Source§

fn tcsetpgrp_without_block( &mut self, fd: Fd, pgid: Pid, ) -> impl Future<Output = Result<()>>

👎Deprecated since 0.11.0: use yash_env::job::tcsetpgrp_without_block instead
Switches the foreground process group with the default SIGTTOU settings. Read more
Source§

fn signal_name_from_number(&self, number: Number) -> Name

👎Deprecated since 0.11.0: use yash_env::system::Signals::signal_name_from_number instead
Returns the signal name for the signal number. Read more
Source§

fn exit_or_raise( &mut self, exit_status: ExitStatus, ) -> impl Future<Output = Infallible>

👎Deprecated since 0.11.0: use yash_env::semantics::exit_or_raise instead
Terminates the current process with the given exit status, possibly sending a signal to kill the process. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> System for T