Skip to content

Commit

Permalink
update sys_pipe to sys_pipe2 (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
wfly1998 committed Aug 19, 2020
1 parent bdd4c31 commit 550771f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
4 changes: 3 additions & 1 deletion linux-object/src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct File {
/// file INode
inode: Arc<dyn INode>,
/// file open options
options: OpenOptions,
pub options: OpenOptions,
/// file path
pub path: String,
/// file inner mut data
Expand All @@ -44,6 +44,8 @@ pub struct OpenOptions {
pub append: bool,
/// non block open
pub nonblock: bool,
/// close on exec
pub fd_cloexec: bool,
}

/// file seek type
Expand Down
25 changes: 25 additions & 0 deletions linux-object/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ impl LinuxProcess {
write: false,
append: false,
nonblock: false,
fd_cloexec: false,
},
String::from("/dev/stdin"),
) as Arc<dyn FileLike>;
Expand All @@ -215,6 +216,7 @@ impl LinuxProcess {
write: true,
append: false,
nonblock: false,
fd_cloexec: false,
},
String::from("/dev/stdout"),
) as Arc<dyn FileLike>;
Expand Down Expand Up @@ -365,6 +367,29 @@ impl LinuxProcess {
self.inner.lock().signal_actions.table[signal as u8 as usize] = action;
}

/// Close file that FD_CLOEXEC is set
pub fn remove_cloexec_files(&self) {
let mut inner = self.inner.lock();
let close_fds = inner
.files
.iter()
.filter_map(|(fd, file_like)| {
if let Ok(file) = file_like.clone().downcast_arc::<File>() {
if file.options.fd_cloexec {
Some(*fd)
} else {
None
}
} else {
None
}
})
.collect::<Vec<_>>();
for fd in close_fds {
inner.files.remove(&fd).map(|_| ()).unwrap();
}
}

/// Insert a `SemArray` and return its ID
pub fn semaphores_add(&self, array: Arc<SemArray>) -> usize {
self.inner.lock().semaphores.add(array)
Expand Down
21 changes: 17 additions & 4 deletions linux-syscall/src/file/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,13 @@ impl Syscall<'_> {
}

/// Creates a pipe, a unidirectional data channel that can be used for interprocess communication.
pub fn sys_pipe(&self, mut fds: UserOutPtr<[i32; 2]>) -> SysResult {
info!("pipe: fds={:?}", fds);
pub fn sys_pipe(&self, fds: UserOutPtr<[i32; 2]>) -> SysResult {
self.sys_pipe2(fds, 0)
}

/// Creates a pipe, a unidirectional data channel that can be used for interprocess communication.
pub fn sys_pipe2(&self, mut fds: UserOutPtr<[i32; 2]>, flags: usize) -> SysResult {
info!("pipe2: fds={:?}, flags: {:#x}", fds, flags);

let proc = self.linux_process();
let (read, write) = Pipe::create_pair();
Expand All @@ -97,7 +102,8 @@ impl Syscall<'_> {
read: true,
write: false,
append: false,
nonblock: false,
nonblock: (flags & O_NONBLOCK) != 0,
fd_cloexec: (flags & O_CLOEXEC) != 0,
},
String::from("pipe_r:[]"),
))?;
Expand All @@ -109,13 +115,14 @@ impl Syscall<'_> {
write: true,
append: false,
nonblock: false,
fd_cloexec: (flags & O_CLOEXEC) != 0,
},
String::from("pipe_w:[]"),
))?;
fds.write([read_fd.into(), write_fd.into()])?;

info!(
"pipe: created rfd={:?} wfd={:?} fds={:?}",
"pipe2: created rfd={:?} wfd={:?} fds={:?}",
read_fd, write_fd, fds
);

Expand Down Expand Up @@ -158,6 +165,8 @@ bitflags! {
const TRUNCATE = 1 << 9;
/// append on each write
const APPEND = 1 << 10;
/// close on exec
const CLOEXEC = 1 << 19;
}
}

Expand All @@ -179,6 +188,10 @@ impl OpenFlags {
write: self.writable(),
append: self.contains(Self::APPEND),
nonblock: false,
fd_cloexec: self.contains(Self::CLOEXEC),
}
}
}

const O_NONBLOCK: usize = 0o4000;
const O_CLOEXEC: usize = 0o2000000; /* set close_on_exec */
2 changes: 1 addition & 1 deletion linux-syscall/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl Syscall<'_> {
Sys::FACCESSAT => self.sys_faccessat(a0.into(), a1.into(), a2, a3),
Sys::DUP => self.sys_dup(a0.into()),
Sys::DUP3 => self.sys_dup2(a0.into(), a1.into()), // TODO: handle `flags`
Sys::PIPE2 => self.sys_pipe(a0.into()), // TODO: handle `flags`
Sys::PIPE2 => self.sys_pipe2(a0.into(), a1), // TODO: handle `flags`
Sys::UTIMENSAT => self.sys_utimensat(a0.into(), a1.into(), a2.into(), a3),
Sys::COPY_FILE_RANGE => {
self.sys_copy_file_range(a0.into(), a1.into(), a2.into(), a3.into(), a4, a5)
Expand Down
2 changes: 2 additions & 0 deletions linux-syscall/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ impl Syscall<'_> {
let inode = proc.lookup_inode(&path)?;
let data = inode.read_as_vec()?;

proc.remove_cloexec_files();

let vmar = self.zircon_process().vmar();
vmar.clear()?;
let loader = LinuxElfLoader {
Expand Down

0 comments on commit 550771f

Please sign in to comment.