Skip to content

Commit 2442e29

Browse files
committed
Epoll, rename to nix, misc cleanup
1 parent 78cd78b commit 2442e29

File tree

11 files changed

+149
-61
lines changed

11 files changed

+149
-61
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
22

3-
name = "linux"
3+
name = "nix"
44
version = "0.0.1"
55
authors = ["Carl Lerche <[email protected]>"]

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
# Rust bindings to Linux APIs
1+
# Rust bindings to *nix APIs
22

3-
The goal is to provide rust friendly bindings to Linux APIs. This is
4-
very much a work and progress and I am treating it as where I put
5-
bindings that I need vs. spreading them around the various libs that I
6-
work on.
3+
Rust friendly bindings to various *nix platform APIs (Linux, Darwin,
4+
...). The goal is to not provide a 100% unified interface, but try as
5+
possible as well as providing platform specific APIs. It is up to the
6+
consumer of this library to decide how portable they want to be.
7+
8+
This is very much a work in progress and I'm mostly just adding bindings
9+
as I need them.
710

811
Of course, PRs welcome :)

src/fcntl.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use std::c_str::CString;
1+
#![cfg(target_os = "linux")]
2+
23
use std::path::Path;
34
use std::io::FilePermission;
4-
use libc::{mode_t, c_int};
5+
use libc::c_int;
56
use errno::{SysResult, SysError, from_ffi};
67

78
pub type Fd = c_int;

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![crate_name = "linux"]
1+
#![crate_name = "nix"]
22
#![feature(globs)]
33

44
extern crate libc;

src/mount.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::c_str::CString;
1+
#![cfg(target_os = "linux")]
2+
23
use std::ptr;
34
use std::path::Path;
45
use libc::{c_ulong, c_int, c_void};

src/sys.rs

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/sys/epoll.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#![cfg(target_os = "linux")]
2+
3+
use libc::c_int;
4+
use fcntl::Fd;
5+
use errno::{SysResult, SysError, from_ffi};
6+
7+
mod ffi {
8+
use libc::{c_int};
9+
use super::EpollEvent;
10+
11+
extern {
12+
pub fn epoll_create(size: c_int) -> c_int;
13+
pub fn epoll_ctl(epfd: c_int, op: c_int, fd: c_int, event: *const EpollEvent) -> c_int;
14+
pub fn epoll_wait(epfd: c_int, events: *mut EpollEvent, max_events: c_int, timeout: c_int) -> c_int;
15+
}
16+
}
17+
18+
bitflags!(
19+
flags EpollEventKind: u32 {
20+
static EPOLLIN = 0x001,
21+
static EPOLLPRI = 0x002,
22+
static EPOLLOUT = 0x004,
23+
static EPOLLRDNORM = 0x040,
24+
static EPOLLRDBAND = 0x080,
25+
static EPOLLWRNORM = 0x100,
26+
static EPOLLWRBAND = 0x200,
27+
static EPOLLMSG = 0x400,
28+
static EPOLLERR = 0x008,
29+
static EPOLLHUP = 0x010,
30+
static EPOLLRDHUP = 0x2000,
31+
static EPOLLWAKEUP = 1 << 29,
32+
static EPOLLONESHOT = 1 << 30,
33+
static EPOLLET = 1 << 31
34+
}
35+
)
36+
37+
#[repr(C)]
38+
pub enum EpollOp {
39+
EpollCtlAdd = 1,
40+
EpollCtlDel = 2,
41+
EpollCtlMod = 3
42+
}
43+
44+
pub struct EpollEvent {
45+
pub events: EpollEventKind,
46+
pub data: u64
47+
}
48+
49+
#[inline]
50+
pub fn epoll_create() -> SysResult<Fd> {
51+
let res = unsafe { ffi::epoll_create(1024) };
52+
53+
if res < 0 {
54+
return Err(SysError::last());
55+
}
56+
57+
Ok(res)
58+
}
59+
60+
#[inline]
61+
pub fn epoll_ctl(epfd: Fd, op: EpollOp, fd: Fd, event: &EpollEvent) -> SysResult<()> {
62+
let res = unsafe { ffi::epoll_ctl(epfd, op as c_int, fd, event as *const EpollEvent) };
63+
from_ffi(res)
64+
}
65+
66+
#[inline]
67+
pub fn epoll_wait(epfd: Fd, events: &mut [EpollEvent], timeout_ms: uint) -> SysResult<uint> {
68+
let res = unsafe {
69+
ffi::epoll_wait(epfd, events.as_mut_ptr(), events.len() as c_int, timeout_ms as c_int)
70+
};
71+
72+
if res < 0 {
73+
return Err(SysError::last());
74+
}
75+
76+
Ok(res as uint)
77+
}

src/sys/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod epoll;
2+
pub mod stat;

src/sys/stat.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#![cfg(target_os = "linux")]
2+
3+
pub use libc::dev_t;
4+
5+
use std::fmt;
6+
use std::io::FilePermission;
7+
use std::path::Path;
8+
use libc::mode_t;
9+
use errno::{SysResult, from_ffi};
10+
11+
mod ffi {
12+
use libc::{c_char, c_int, mode_t, dev_t};
13+
14+
extern {
15+
pub fn mknod(pathname: *const c_char, mode: mode_t, dev: dev_t) -> c_int;
16+
pub fn umask(mask: mode_t) -> mode_t;
17+
}
18+
}
19+
20+
bitflags!(
21+
flags SFlag: mode_t {
22+
static S_IFREG = 0o100000,
23+
static S_IFCHR = 0o020000,
24+
static S_IFBLK = 0o060000,
25+
static S_IFIFO = 0o010000,
26+
static S_IFSOCK = 0o140000
27+
}
28+
)
29+
30+
impl fmt::Show for SFlag {
31+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
32+
write!(fmt, "SFlag {{ bits: {} }}", self.bits())
33+
}
34+
}
35+
36+
pub fn mknod(path: &Path, kind: SFlag, perm: FilePermission, dev: dev_t) -> SysResult<()> {
37+
let res = unsafe { ffi::mknod(path.to_c_str().as_ptr(), kind.bits | perm.bits(), dev) };
38+
from_ffi(res)
39+
}
40+
41+
static MINORBITS: uint = 20;
42+
// static MINORMASK: dev_t = ((1 << MINORBITS) - 1);
43+
44+
pub fn mkdev(major: u64, minor: u64) -> dev_t {
45+
(major << MINORBITS) | minor
46+
}
47+
48+
pub fn umask(mode: FilePermission) -> FilePermission {
49+
let prev = unsafe { ffi::umask(mode.bits()) };
50+
FilePermission::from_bits(prev).expect("[BUG] umask returned invalid FilePermission")
51+
}

src/syscall.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg(target_os = "linux")]
2+
13
use libc::c_int;
24

35
pub use self::arch::*;

src/unistd.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg(target_os = "linux")]
2+
13
use std::ptr;
24
use std::c_str::{CString, ToCStr};
35
use std::path::Path;

0 commit comments

Comments
 (0)