Converting pids into a new type ContextId

Keeping pid (and file descriptor, and scheme id, ...) as usize is a
footgun. Let's remove it.
This commit is contained in:
David Teller 2016-11-13 15:36:52 +01:00
parent d16515ea12
commit 37a34ab7f7
10 changed files with 52 additions and 44 deletions

View file

@ -25,7 +25,7 @@ pub fn file_op(a: usize, fd: usize, c: usize, d: usize) -> Result<usize> {
let mut packet = Packet {
id: 0,
pid: pid,
pid: pid.into(),
uid: uid,
gid: gid,
a: a,

View file

@ -14,6 +14,8 @@ use self::data::TimeSpec;
use self::error::{Error, Result, ENOSYS};
use self::number::*;
use context::ContextId;
/// Filesystem syscalls
pub mod fs;
@ -54,13 +56,13 @@ pub extern fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize
},
_ => match a {
SYS_EXIT => exit(b),
SYS_WAITPID => waitpid(b, c, d),
SYS_WAITPID => waitpid(ContextId::from(b), c, d).map(ContextId::into),
SYS_EXECVE => exec(validate_slice(b as *const u8, c)?, validate_slice(d as *const [usize; 2], e)?),
SYS_CHDIR => chdir(validate_slice(b as *const u8, c)?),
SYS_GETPID => getpid(),
SYS_GETPID => getpid().map(ContextId::into),
SYS_BRK => brk(b),
SYS_IOPL => iopl(b),
SYS_CLONE => clone(b, stack),
SYS_CLONE => clone(b, stack).map(ContextId::into),
SYS_YIELD => sched_yield(),
SYS_NANOSLEEP => nanosleep(validate_slice(b as *const TimeSpec, 1).map(|req| &req[0])?, validate_slice_mut(c as *mut TimeSpec, 1).ok().map(|rem| &mut rem[0])),
SYS_GETCWD => getcwd(validate_slice_mut(b as *mut u8, c)?),

View file

@ -13,6 +13,7 @@ use arch::paging::{ActivePageTable, InactivePageTable, Page, PhysicalAddress, Vi
use arch::paging::temporary_page::TemporaryPage;
use arch::start::usermode;
use context;
use context::ContextId;
use context::memory::Grant;
use elf::{self, program_header};
use scheme;
@ -54,7 +55,7 @@ pub fn brk(address: usize) -> Result<usize> {
}
}
pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
pub fn clone(flags: usize, stack_base: usize) -> Result<ContextId> {
let ppid;
let pid;
{
@ -665,10 +666,10 @@ pub fn exec(path: &[u8], arg_ptrs: &[[usize; 2]]) -> Result<usize> {
if let Some(context_lock) = contexts.get(ppid) {
let mut context = context_lock.write();
if ! context.unblock() {
println!("{} not blocked for exec vfork unblock", ppid);
println!("{:?} not blocked for exec vfork unblock", ppid);
}
} else {
println!("{} not found for exec vfork unblock", ppid);
println!("{:?} not found for exec vfork unblock", ppid);
}
}
},
@ -756,7 +757,7 @@ pub fn exit(status: usize) -> ! {
let mut parent = parent_lock.write();
if vfork {
if ! parent.unblock() {
println!("{} not blocked for exit vfork unblock", ppid);
println!("{:?} not blocked for exit vfork unblock", ppid);
}
}
parent.waitpid.clone()
@ -767,7 +768,7 @@ pub fn exit(status: usize) -> ! {
}
waitpid.send(pid, status);
} else {
println!("{} not found for exit vfork unblock", ppid);
println!("{:?} not found for exit vfork unblock", ppid);
}
}
}
@ -798,7 +799,7 @@ pub fn getgid() -> Result<usize> {
Ok(context.rgid as usize)
}
pub fn getpid() -> Result<usize> {
pub fn getpid() -> Result<ContextId> {
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
let context = context_lock.read();
@ -817,7 +818,7 @@ pub fn iopl(_level: usize) -> Result<usize> {
Ok(0)
}
pub fn kill(pid: usize, sig: usize) -> Result<usize> {
pub fn kill(pid: ContextId, sig: usize) -> Result<usize> {
use syscall::flag::*;
let _context_lock = {
@ -827,19 +828,19 @@ pub fn kill(pid: usize, sig: usize) -> Result<usize> {
};
let term = || {
println!("Terminate {}", pid);
println!("Terminate {:?}", pid);
};
let core = || {
println!("Core {}", pid);
println!("Core {:?}", pid);
};
let stop = || {
println!("Stop {}", pid);
println!("Stop {:?}", pid);
};
let cont = || {
println!("Continue {}", pid);
println!("Continue {:?}", pid);
};
match sig {
@ -1001,7 +1002,7 @@ pub fn virttophys(virtual_address: usize) -> Result<usize> {
}
}
fn reap(pid: usize) -> Result<usize> {
fn reap(pid: ContextId) -> Result<ContextId> {
// Spin until not running
let mut running = false;
while running {
@ -1019,7 +1020,7 @@ fn reap(pid: usize) -> Result<usize> {
contexts.remove(pid).ok_or(Error::new(ESRCH)).and(Ok(pid))
}
pub fn waitpid(pid: usize, status_ptr: usize, flags: usize) -> Result<usize> {
pub fn waitpid(pid: ContextId, status_ptr: usize, flags: usize) -> Result<ContextId> {
let waitpid = {
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
@ -1034,13 +1035,13 @@ pub fn waitpid(pid: usize, status_ptr: usize, flags: usize) -> Result<usize> {
&mut tmp
};
if pid == 0 {
if pid.into() == 0 {
if flags & WNOHANG == WNOHANG {
if let Some((w_pid, status)) = waitpid.receive_any_nonblock() {
status_slice[0] = status;
reap(w_pid)
} else {
Ok(0)
Ok(ContextId::from(0))
}
} else {
let (w_pid, status) = waitpid.receive_any();
@ -1053,7 +1054,7 @@ pub fn waitpid(pid: usize, status_ptr: usize, flags: usize) -> Result<usize> {
status_slice[0] = status;
reap(pid)
} else {
Ok(0)
Ok(ContextId::from(0))
}
} else {
let status = waitpid.receive(&pid);