fix close_on_spawn and file_actions

This commit is contained in:
WangRunji 2019-04-12 14:27:48 +08:00
parent 6d432b0a03
commit 80a73eaa0d
2 changed files with 16 additions and 24 deletions

@ -4,7 +4,7 @@ use std;
pub type FileDesc = u32; pub type FileDesc = u32;
#[derive(Debug, Default)] #[derive(Debug, Default, Clone)]
#[repr(C)] #[repr(C)]
pub struct FileTable { pub struct FileTable {
table: Vec<Option<FileTableEntry>>, table: Vec<Option<FileTableEntry>>,
@ -123,30 +123,19 @@ impl FileTable {
None => errno!(EBADF, "Invalid file descriptor"), None => errno!(EBADF, "Invalid file descriptor"),
} }
} }
}
impl Clone for FileTable { /// Remove file descriptors that are close-on-spawn
fn clone(&self) -> FileTable { pub fn close_on_spawn(&mut self) {
// Only clone file descriptors that are not close-on-spawn for entry in self.table.iter_mut() {
let mut num_cloned_fds = 0; let need_close = if let Some(entry) = entry {
let cloned_table = self entry.close_on_spawn
.table } else {
.iter() false
.map(|entry| match entry { };
Some(file_table_entry) => match file_table_entry.close_on_spawn { if need_close {
false => { *entry = None;
num_cloned_fds += 1; self.num_fds -= 1;
Some(file_table_entry.clone())
} }
true => None,
},
None => None,
})
.collect();
FileTable {
table: cloned_table,
num_fds: num_cloned_fds,
} }
} }
} }

@ -87,6 +87,7 @@ fn init_files(parent_ref: &ProcessRef, file_actions: &[FileAction]) -> Result<Fi
let parent = parent_ref.lock().unwrap(); let parent = parent_ref.lock().unwrap();
let should_inherit_file_table = parent.get_pid() > 0; let should_inherit_file_table = parent.get_pid() > 0;
if should_inherit_file_table { if should_inherit_file_table {
// Fork: clone file table
let mut cloned_file_table = parent.get_files().lock().unwrap().clone(); let mut cloned_file_table = parent.get_files().lock().unwrap().clone();
// Perform file actions to modify the cloned file table // Perform file actions to modify the cloned file table
for file_action in file_actions { for file_action in file_actions {
@ -102,6 +103,8 @@ fn init_files(parent_ref: &ProcessRef, file_actions: &[FileAction]) -> Result<Fi
} }
} }
} }
// Exec: close fd with close_on_spawn
cloned_file_table.close_on_spawn();
return Ok(cloned_file_table); return Ok(cloned_file_table);
} }
drop(parent); drop(parent);