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:
		
							parent
							
								
									d16515ea12
								
							
						
					
					
						commit
						37a34ab7f7
					
				
					 10 changed files with 52 additions and 44 deletions
				
			
		| 
						 | 
				
			
			@ -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);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue