diff options
author | Carl Lerche <me@carllerche.com> | 2014-08-16 18:45:34 -0700 |
---|---|---|
committer | Carl Lerche <me@carllerche.com> | 2014-08-16 18:45:34 -0700 |
commit | 2442e299e80724d813fffa9a11a6fe86227f488b (patch) | |
tree | 8e4c167dc6ea15a1845d77fc06830722ab65997d /src/sys | |
parent | 78cd78bbdac732bf2114bdfadb564cd4f7232365 (diff) | |
download | nix-2442e299e80724d813fffa9a11a6fe86227f488b.zip |
Epoll, rename to nix, misc cleanup
Diffstat (limited to 'src/sys')
-rw-r--r-- | src/sys/epoll.rs | 77 | ||||
-rw-r--r-- | src/sys/mod.rs | 2 | ||||
-rw-r--r-- | src/sys/stat.rs | 51 |
3 files changed, 130 insertions, 0 deletions
diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs new file mode 100644 index 00000000..1a828af7 --- /dev/null +++ b/src/sys/epoll.rs @@ -0,0 +1,77 @@ +#![cfg(target_os = "linux")] + +use libc::c_int; +use fcntl::Fd; +use errno::{SysResult, SysError, from_ffi}; + +mod ffi { + use libc::{c_int}; + use super::EpollEvent; + + extern { + pub fn epoll_create(size: c_int) -> c_int; + pub fn epoll_ctl(epfd: c_int, op: c_int, fd: c_int, event: *const EpollEvent) -> c_int; + pub fn epoll_wait(epfd: c_int, events: *mut EpollEvent, max_events: c_int, timeout: c_int) -> c_int; + } +} + +bitflags!( + flags EpollEventKind: u32 { + static EPOLLIN = 0x001, + static EPOLLPRI = 0x002, + static EPOLLOUT = 0x004, + static EPOLLRDNORM = 0x040, + static EPOLLRDBAND = 0x080, + static EPOLLWRNORM = 0x100, + static EPOLLWRBAND = 0x200, + static EPOLLMSG = 0x400, + static EPOLLERR = 0x008, + static EPOLLHUP = 0x010, + static EPOLLRDHUP = 0x2000, + static EPOLLWAKEUP = 1 << 29, + static EPOLLONESHOT = 1 << 30, + static EPOLLET = 1 << 31 + } +) + +#[repr(C)] +pub enum EpollOp { + EpollCtlAdd = 1, + EpollCtlDel = 2, + EpollCtlMod = 3 +} + +pub struct EpollEvent { + pub events: EpollEventKind, + pub data: u64 +} + +#[inline] +pub fn epoll_create() -> SysResult<Fd> { + let res = unsafe { ffi::epoll_create(1024) }; + + if res < 0 { + return Err(SysError::last()); + } + + Ok(res) +} + +#[inline] +pub fn epoll_ctl(epfd: Fd, op: EpollOp, fd: Fd, event: &EpollEvent) -> SysResult<()> { + let res = unsafe { ffi::epoll_ctl(epfd, op as c_int, fd, event as *const EpollEvent) }; + from_ffi(res) +} + +#[inline] +pub fn epoll_wait(epfd: Fd, events: &mut [EpollEvent], timeout_ms: uint) -> SysResult<uint> { + let res = unsafe { + ffi::epoll_wait(epfd, events.as_mut_ptr(), events.len() as c_int, timeout_ms as c_int) + }; + + if res < 0 { + return Err(SysError::last()); + } + + Ok(res as uint) +} diff --git a/src/sys/mod.rs b/src/sys/mod.rs new file mode 100644 index 00000000..0a741ecf --- /dev/null +++ b/src/sys/mod.rs @@ -0,0 +1,2 @@ +pub mod epoll; +pub mod stat; diff --git a/src/sys/stat.rs b/src/sys/stat.rs new file mode 100644 index 00000000..fc90ca67 --- /dev/null +++ b/src/sys/stat.rs @@ -0,0 +1,51 @@ +#![cfg(target_os = "linux")] + +pub use libc::dev_t; + +use std::fmt; +use std::io::FilePermission; +use std::path::Path; +use libc::mode_t; +use errno::{SysResult, from_ffi}; + +mod ffi { + use libc::{c_char, c_int, mode_t, dev_t}; + + extern { + pub fn mknod(pathname: *const c_char, mode: mode_t, dev: dev_t) -> c_int; + pub fn umask(mask: mode_t) -> mode_t; + } +} + +bitflags!( + flags SFlag: mode_t { + static S_IFREG = 0o100000, + static S_IFCHR = 0o020000, + static S_IFBLK = 0o060000, + static S_IFIFO = 0o010000, + static S_IFSOCK = 0o140000 + } +) + +impl fmt::Show for SFlag { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!(fmt, "SFlag {{ bits: {} }}", self.bits()) + } +} + +pub fn mknod(path: &Path, kind: SFlag, perm: FilePermission, dev: dev_t) -> SysResult<()> { + let res = unsafe { ffi::mknod(path.to_c_str().as_ptr(), kind.bits | perm.bits(), dev) }; + from_ffi(res) +} + +static MINORBITS: uint = 20; +// static MINORMASK: dev_t = ((1 << MINORBITS) - 1); + +pub fn mkdev(major: u64, minor: u64) -> dev_t { + (major << MINORBITS) | minor +} + +pub fn umask(mode: FilePermission) -> FilePermission { + let prev = unsafe { ffi::umask(mode.bits()) }; + FilePermission::from_bits(prev).expect("[BUG] umask returned invalid FilePermission") +} |