Skip to content

Commit

Permalink
Sync with upstream syscall library
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpot51 committed Sep 12, 2017
1 parent eba374f commit e2820ea
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 12 deletions.
63 changes: 51 additions & 12 deletions src/libstd/sys/redox/syscall/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@
// except according to those terms.

use super::arch::*;
use super::data::{Stat, StatVfs, TimeSpec};
use super::data::{SigAction, Stat, StatVfs, TimeSpec};
use super::error::Result;
use super::number::*;

use core::mem;
use core::{mem, ptr};

// Signal restorer
extern "C" fn restorer() -> ! {
sigreturn().unwrap();
unreachable!();
}

/// Set the end of the process's heap
///
Expand Down Expand Up @@ -43,12 +49,12 @@ pub unsafe fn brk(addr: usize) -> Result<usize> {
/// * `EIO` - an I/O error occurred
/// * `ENOENT` - `path` does not exit
/// * `ENOTDIR` - `path` is not a directory
pub fn chdir(path: &str) -> Result<usize> {
unsafe { syscall2(SYS_CHDIR, path.as_ptr() as usize, path.len()) }
pub fn chdir<T: AsRef<[u8]>>(path: T) -> Result<usize> {
unsafe { syscall2(SYS_CHDIR, path.as_ref().as_ptr() as usize, path.as_ref().len()) }
}

pub fn chmod(path: &str, mode: usize) -> Result<usize> {
unsafe { syscall3(SYS_CHMOD, path.as_ptr() as usize, path.len(), mode) }
pub fn chmod<T: AsRef<[u8]>>(path: T, mode: usize) -> Result<usize> {
unsafe { syscall3(SYS_CHMOD, path.as_ref().as_ptr() as usize, path.as_ref().len(), mode) }
}

/// Produce a fork of the current process, or a new process thread
Expand Down Expand Up @@ -132,6 +138,11 @@ pub fn ftruncate(fd: usize, len: usize) -> Result<usize> {
unsafe { syscall2(SYS_FTRUNCATE, fd, len) }
}

// Change modify and/or access times
pub fn futimens(fd: usize, times: &[TimeSpec]) -> Result<usize> {
unsafe { syscall3(SYS_FUTIMENS, fd, times.as_ptr() as usize, times.len() * mem::size_of::<TimeSpec>()) }
}

/// Fast userspace mutex
pub unsafe fn futex(addr: *mut i32, op: usize, val: i32, val2: usize, addr2: *mut i32)
-> Result<usize> {
Expand Down Expand Up @@ -173,6 +184,16 @@ pub fn getpid() -> Result<usize> {
unsafe { syscall0(SYS_GETPID) }
}

/// Get the process group ID
pub fn getpgid(pid: usize) -> Result<usize> {
unsafe { syscall1(SYS_GETPGID, pid) }
}

/// Get the parent process ID
pub fn getppid() -> Result<usize> {
unsafe { syscall0(SYS_GETPPID) }
}

/// Get the current user ID
pub fn getuid() -> Result<usize> {
unsafe { syscall0(SYS_GETUID) }
Expand Down Expand Up @@ -210,8 +231,8 @@ pub fn nanosleep(req: &TimeSpec, rem: &mut TimeSpec) -> Result<usize> {
}

/// Open a file
pub fn open(path: &str, flags: usize) -> Result<usize> {
unsafe { syscall3(SYS_OPEN, path.as_ptr() as usize, path.len(), flags) }
pub fn open<T: AsRef<[u8]>>(path: T, flags: usize) -> Result<usize> {
unsafe { syscall3(SYS_OPEN, path.as_ref().as_ptr() as usize, path.as_ref().len(), flags) }
}

/// Allocate pages, linearly in physical memory
Expand Down Expand Up @@ -245,8 +266,13 @@ pub fn read(fd: usize, buf: &mut [u8]) -> Result<usize> {
}

/// Remove a directory
pub fn rmdir(path: &str) -> Result<usize> {
unsafe { syscall2(SYS_RMDIR, path.as_ptr() as usize, path.len()) }
pub fn rmdir<T: AsRef<[u8]>>(path: T) -> Result<usize> {
unsafe { syscall2(SYS_RMDIR, path.as_ref().as_ptr() as usize, path.as_ref().len()) }
}

/// Set the process group ID
pub fn setpgid(pid: usize, pgid: usize) -> Result<usize> {
unsafe { syscall2(SYS_SETPGID, pid, pgid) }
}

/// Set the current process group IDs
Expand All @@ -264,9 +290,22 @@ pub fn setreuid(ruid: usize, euid: usize) -> Result<usize> {
unsafe { syscall2(SYS_SETREUID, ruid, euid) }
}

/// Set up a signal handler
pub fn sigaction(sig: usize, act: Option<&SigAction>, oldact: Option<&mut SigAction>) -> Result<usize> {
unsafe { syscall4(SYS_SIGACTION, sig,
act.map(|x| x as *const _).unwrap_or_else(ptr::null) as usize,
oldact.map(|x| x as *mut _).unwrap_or_else(ptr::null_mut) as usize,
restorer as usize) }
}

// Return from signal handler
pub fn sigreturn() -> Result<usize> {
unsafe { syscall0(SYS_SIGRETURN) }
}

/// Remove a file
pub fn unlink(path: &str) -> Result<usize> {
unsafe { syscall2(SYS_UNLINK, path.as_ptr() as usize, path.len()) }
pub fn unlink<T: AsRef<[u8]>>(path: T) -> Result<usize> {
unsafe { syscall2(SYS_UNLINK, path.as_ref().as_ptr() as usize, path.as_ref().len()) }
}

/// Convert a virtual address to a physical one
Expand Down
72 changes: 72 additions & 0 deletions src/libstd/sys/redox/syscall/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,78 @@
use core::ops::{Deref, DerefMut};
use core::{mem, slice};

#[derive(Copy, Clone, Debug, Default)]
pub struct Event {
pub id: usize,
pub flags: usize,
pub data: usize
}

impl Deref for Event {
type Target = [u8];
fn deref(&self) -> &[u8] {
unsafe {
slice::from_raw_parts(self as *const Event as *const u8, mem::size_of::<Event>()) as &[u8]
}
}
}

impl DerefMut for Event {
fn deref_mut(&mut self) -> &mut [u8] {
unsafe {
slice::from_raw_parts_mut(self as *mut Event as *mut u8, mem::size_of::<Event>()) as &mut [u8]
}
}
}

#[derive(Copy, Clone, Debug, Default)]
#[repr(C)]
pub struct Packet {
pub id: u64,
pub pid: usize,
pub uid: u32,
pub gid: u32,
pub a: usize,
pub b: usize,
pub c: usize,
pub d: usize
}

impl Deref for Packet {
type Target = [u8];
fn deref(&self) -> &[u8] {
unsafe {
slice::from_raw_parts(self as *const Packet as *const u8, mem::size_of::<Packet>()) as &[u8]
}
}
}

impl DerefMut for Packet {
fn deref_mut(&mut self) -> &mut [u8] {
unsafe {
slice::from_raw_parts_mut(self as *mut Packet as *mut u8, mem::size_of::<Packet>()) as &mut [u8]
}
}
}

#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub struct SigAction {
pub sa_handler: extern "C" fn(usize),
pub sa_mask: [u64; 2],
pub sa_flags: usize,
}

impl Default for SigAction {
fn default() -> Self {
Self {
sa_handler: unsafe { mem::transmute(0usize) },
sa_mask: [0; 2],
sa_flags: 0,
}
}
}

#[derive(Copy, Clone, Debug, Default)]
#[repr(C)]
pub struct Stat {
Expand Down
17 changes: 17 additions & 0 deletions src/libstd/sys/redox/syscall/flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
pub const CLONE_VM: usize = 0x100;
pub const CLONE_FS: usize = 0x200;
pub const CLONE_FILES: usize = 0x400;
pub const CLONE_SIGHAND: usize = 0x800;
pub const CLONE_VFORK: usize = 0x4000;
pub const CLONE_THREAD: usize = 0x10000;

pub const CLOCK_REALTIME: usize = 1;
pub const CLOCK_MONOTONIC: usize = 4;
Expand All @@ -20,6 +22,7 @@ pub const EVENT_NONE: usize = 0;
pub const EVENT_READ: usize = 1;
pub const EVENT_WRITE: usize = 2;

pub const F_DUPFD: usize = 0;
pub const F_GETFD: usize = 1;
pub const F_SETFD: usize = 2;
pub const F_GETFL: usize = 3;
Expand All @@ -36,6 +39,8 @@ pub const MODE_TYPE: u16 = 0xF000;
pub const MODE_DIR: u16 = 0x4000;
pub const MODE_FILE: u16 = 0x8000;
pub const MODE_SYMLINK: u16 = 0xA000;
pub const MODE_FIFO: u16 = 0x1000;
pub const MODE_CHR: u16 = 0x2000;

pub const MODE_PERM: u16 = 0x0FFF;
pub const MODE_SETUID: u16 = 0o4000;
Expand Down Expand Up @@ -96,4 +101,16 @@ pub const SIGIO: usize = 29;
pub const SIGPWR: usize = 30;
pub const SIGSYS: usize = 31;

pub const SIG_DFL: usize = 0;
pub const SIG_IGN: usize = 1;

pub const SA_NOCLDSTOP: usize = 0x00000001;
pub const SA_NOCLDWAIT: usize = 0x00000002;
pub const SA_SIGINFO: usize = 0x00000004;
pub const SA_RESTORER: usize = 0x04000000;
pub const SA_ONSTACK: usize = 0x08000000;
pub const SA_RESTART: usize = 0x10000000;
pub const SA_NODEFER: usize = 0x40000000;
pub const SA_RESETHAND: usize = 0x80000000;

pub const WNOHANG: usize = 1;
6 changes: 6 additions & 0 deletions src/libstd/sys/redox/syscall/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub const SYS_FSTAT: usize = SYS_CLASS_FILE | SYS_ARG_MSLICE | 28;
pub const SYS_FSTATVFS: usize = SYS_CLASS_FILE | SYS_ARG_MSLICE | 100;
pub const SYS_FSYNC: usize = SYS_CLASS_FILE | 118;
pub const SYS_FTRUNCATE: usize =SYS_CLASS_FILE | 93;
pub const SYS_FUTIMENS: usize = SYS_CLASS_FILE | SYS_ARG_SLICE | 320;

pub const SYS_BRK: usize = 45;
pub const SYS_CHDIR: usize = 12;
Expand All @@ -56,6 +57,8 @@ pub const SYS_GETEUID: usize = 201;
pub const SYS_GETGID: usize = 200;
pub const SYS_GETNS: usize = 950;
pub const SYS_GETPID: usize = 20;
pub const SYS_GETPGID: usize = 132;
pub const SYS_GETPPID: usize = 64;
pub const SYS_GETUID: usize = 199;
pub const SYS_IOPL: usize = 110;
pub const SYS_KILL: usize = 37;
Expand All @@ -67,8 +70,11 @@ pub const SYS_PHYSMAP: usize = 947;
pub const SYS_PHYSUNMAP: usize =948;
pub const SYS_VIRTTOPHYS: usize=949;
pub const SYS_PIPE2: usize = 331;
pub const SYS_SETPGID: usize = 57;
pub const SYS_SETREGID: usize = 204;
pub const SYS_SETRENS: usize = 952;
pub const SYS_SETREUID: usize = 203;
pub const SYS_SIGACTION: usize =67;
pub const SYS_SIGRETURN: usize =119;
pub const SYS_WAITPID: usize = 7;
pub const SYS_YIELD: usize = 158;

0 comments on commit e2820ea

Please sign in to comment.