Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/descriptor/err.rs → src/descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::error::Error;
use std::fmt;
use std::{
error::Error,
ffi::CStr,
fmt,
os::fd::{FromRawFd, OwnedFd},
};

/// The enum `DescriptorError` defines the possible errors
/// from constructor Descriptor.
Expand Down Expand Up @@ -33,3 +37,19 @@ impl Error for DescriptorError {
None
}
}

/// Open the given file as a file descriptor.
pub fn open(
path: &CStr,
flag: libc::c_int,
mode: Option<libc::c_int>,
) -> Result<OwnedFd, DescriptorError> {
// Safety: we've just ensured that path is non-null and the
// other params are valid by construction.
unsafe {
match libc::open(path.as_ptr().cast(), flag, mode.unwrap_or_default()) {
-1 => Err(DescriptorError::OpenFail),
fd => Ok(OwnedFd::from_raw_fd(fd)),
}
}
}
23 changes: 0 additions & 23 deletions src/descriptor/mod.rs

This file was deleted.

72 changes: 70 additions & 2 deletions src/fork/mod.rs → src/fork.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,79 @@
mod err;
mod pty;

pub use self::err::{ForkError, Result};
pub use self::pty::{Master, MasterError};
pub use self::pty::{Slave, SlaveError};
use crate::descriptor::DescriptorError;
use std::error::Error;
use std::ffi::CStr;
use std::ffi::CString;
use std::fmt;

/// The alias `Result` learns `ForkError` possibility.
pub type Result<T> = ::std::result::Result<T, ForkError>;

/// The enum `ForkError` defines the possible errors from constructor Fork.
#[derive(Clone, Copy, Debug)]
pub enum ForkError {
/// Can't creates the child.
Failure,
/// Can't set the id group.
SetsidFail,
/// Can't suspending the calling process.
WaitpidFail,
/// Is child and not parent.
IsChild,
/// Is parent and not child.
IsParent,
/// The Master occured a error.
BadMaster(MasterError),
/// The Slave occured a error.
BadSlave(SlaveError),
/// The Master's Descriptor occured a error.
BadDescriptorMaster(DescriptorError),
/// The Slave's Descriptor occured a error.
BadDescriptorSlave(DescriptorError),
}

impl fmt::Display for ForkError {
/// The function `fmt` formats the value using the given formatter.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ::errno::errno())
}
}

impl Error for ForkError {
/// The function `description` returns a short description of the error.
fn description(&self) -> &str {
match *self {
ForkError::Failure => {
"On failure, -1 is returned in the parent,no child process is created, and errno \
isset appropriately."
}
ForkError::SetsidFail => {
"fails if the calling process is alreadya process group leader."
}
ForkError::WaitpidFail => "Can't suspending the calling process.",
ForkError::IsChild => "is child and not parent",
ForkError::IsParent => "is parent and not child",
ForkError::BadMaster(_) => "the master as occured an error",
ForkError::BadSlave(_) => "the slave as occured an error",
ForkError::BadDescriptorMaster(_) => "the master's descriptor as occured an error",
ForkError::BadDescriptorSlave(_) => "the slave's descriptor as occured an error",
}
}

/// The function `cause` returns the lower-level cause of this error, if
/// any.
fn cause(&self) -> Option<&dyn Error> {
match *self {
ForkError::BadMaster(ref err) => Some(err),
ForkError::BadSlave(ref err) => Some(err),
ForkError::BadDescriptorMaster(ref err) => Some(err),
ForkError::BadDescriptorSlave(ref err) => Some(err),
_ => None,
}
}
}

const MAX_PTS_NAME: usize = 1024;

Expand Down
72 changes: 0 additions & 72 deletions src/fork/err.rs

This file was deleted.

File renamed without changes.
52 changes: 45 additions & 7 deletions src/fork/pty/master/mod.rs → src/fork/pty/master.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,58 @@
mod err;

#[cfg(target_os = "macos")]
mod ptsname_r_macos;

use crate::descriptor;

pub use self::err::{MasterError, Result};
use crate::descriptor::{self, DescriptorError};
use std::{
error::Error,
ffi::CStr,
io,
fmt, io,
os::{
fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd},
unix::io::RawFd,
},
sync::Arc,
};

/// The alias `Result` learns `MasterError` possibility.
pub type Result<T> = ::std::result::Result<T, MasterError>;

/// The enum `MasterError` defines the possible errors from constructor Master.
#[derive(Clone, Copy, Debug)]
pub enum MasterError {
BadDescriptor(DescriptorError),
GrantptError,
UnlockptError,
PtsnameError,
}

impl fmt::Display for MasterError {
/// The function `fmt` formats the value using the given formatter.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ::errno::errno())
}
}

impl Error for MasterError {
/// The function `description` returns a short description of the error.
fn description(&self) -> &str {
match *self {
MasterError::BadDescriptor(_) => "the descriptor as occured an error",
MasterError::GrantptError => "the `grantpt` has a error, errnois set appropriately.",
MasterError::UnlockptError => "the `grantpt` has a error, errnois set appropriately.",
MasterError::PtsnameError => "the `ptsname` has a error",
}
}

/// The function `cause` returns the lower-level cause of this error, if
/// any.
fn cause(&self) -> Option<&dyn Error> {
match *self {
MasterError::BadDescriptor(ref err) => Some(err),
_ => None,
}
}
}

#[derive(Debug, Clone)]
pub struct Master {
pty: Arc<OwnedFd>,
Expand Down Expand Up @@ -76,7 +113,8 @@ impl Master {
let result = libc::ptsname_r(self.raw_fd(), data as *mut libc::c_char, buf.len());

#[cfg(target_os = "macos")]
let result = ptsname_r_macos::ptsname_r(fd, data as *mut libc::c_char, buf.len());
let result =
ptsname_r_macos::ptsname_r(self.raw_fd(), data as *mut libc::c_char, buf.len());

match result {
0 => Ok(()),
Expand Down
43 changes: 0 additions & 43 deletions src/fork/pty/master/err.rs

This file was deleted.

Loading
Loading