diff options
author | Carl Lerche <me@carllerche.com> | 2015-04-03 16:54:12 -0700 |
---|---|---|
committer | Carl Lerche <me@carllerche.com> | 2015-04-06 17:33:17 -0700 |
commit | 9e935330dd252d4ec640197a6040df2aaeb583c9 (patch) | |
tree | 51b528bf8e36972c69ae004ea04ef8bc65456534 | |
parent | e5ae756203a036aafed472a3e83af6941f546887 (diff) | |
download | nix-9e935330dd252d4ec640197a6040df2aaeb583c9.zip |
Get compiling on Rust 1.0 beta
Initially support this by assuming the lowest common denominator. The long
term solution is to improve the build system to allow pulling in more specific
features that are available on the target system.
-rw-r--r-- | nix-test/Cargo.toml | 3 | ||||
-rw-r--r-- | nix-test/src/lib.rs | 2 | ||||
-rw-r--r-- | src/errno.rs | 265 | ||||
-rw-r--r-- | src/lib.rs | 15 | ||||
-rw-r--r-- | src/sys/eventfd.rs | 2 | ||||
-rw-r--r-- | src/sys/mod.rs | 5 | ||||
-rw-r--r-- | src/sys/signal.rs | 2 | ||||
-rw-r--r-- | src/sys/socket/mod.rs | 31 | ||||
-rw-r--r-- | src/unistd.rs | 61 |
9 files changed, 278 insertions, 108 deletions
diff --git a/nix-test/Cargo.toml b/nix-test/Cargo.toml index 6ccd381d..0082e0e4 100644 --- a/nix-test/Cargo.toml +++ b/nix-test/Cargo.toml @@ -7,3 +7,6 @@ authors = ["Carl Lerche <me@carllerche.com>"] homepage = "https://github.com/carllerche/nix-rust" build = "build.rs" license = "MIT" + +[dependencies] +libc = "*" diff --git a/nix-test/src/lib.rs b/nix-test/src/lib.rs index 97d6825c..66a66413 100644 --- a/nix-test/src/lib.rs +++ b/nix-test/src/lib.rs @@ -1,5 +1,3 @@ -#![feature(libc)] - extern crate libc; use std::ffi::CString; diff --git a/src/errno.rs b/src/errno.rs index 68915193..ceb035d6 100644 --- a/src/errno.rs +++ b/src/errno.rs @@ -1,5 +1,4 @@ use libc::c_int; -use std::num::from_i32; pub use self::consts::*; pub use self::consts::Errno::*; @@ -57,12 +56,16 @@ macro_rules! impl_errno { pub fn desc(self) -> &'static str { super::desc(self) } + + pub fn from_i32(err: i32) -> Errno { + from_i32(err) + } } } } fn last() -> Errno { - from_i32(errno()).unwrap_or(UnknownErrno) + Errno::from_i32(errno()) } fn desc(errno: Errno) -> &'static str { @@ -582,6 +585,149 @@ mod consts { pub const EWOULDBLOCK: Errno = Errno::EAGAIN; pub const EDEADLOCK: Errno = Errno::EDEADLK; + + pub fn from_i32(e: i32) -> Errno { + use self::Errno::*; + + match e { + 0 => UnknownErrno, + 1 => EPERM, + 2 => ENOENT, + 3 => ESRCH, + 4 => EINTR, + 5 => EIO, + 6 => ENXIO, + 7 => E2BIG, + 8 => ENOEXEC, + 9 => EBADF, + 10 => ECHILD, + 11 => EAGAIN, + 12 => ENOMEM, + 13 => EACCES, + 14 => EFAULT, + 15 => ENOTBLK, + 16 => EBUSY, + 17 => EEXIST, + 18 => EXDEV, + 19 => ENODEV, + 20 => ENOTDIR, + 21 => EISDIR, + 22 => EINVAL, + 23 => ENFILE, + 24 => EMFILE, + 25 => ENOTTY, + 26 => ETXTBSY, + 27 => EFBIG, + 28 => ENOSPC, + 29 => ESPIPE, + 30 => EROFS, + 31 => EMLINK, + 32 => EPIPE, + 33 => EDOM, + 34 => ERANGE, + 35 => EDEADLK, + 36 => ENAMETOOLONG, + 37 => ENOLCK, + 38 => ENOSYS, + 39 => ENOTEMPTY, + 40 => ELOOP, + 42 => ENOMSG, + 43 => EIDRM, + 44 => ECHRNG, + 45 => EL2NSYNC, + 46 => EL3HLT, + 47 => EL3RST, + 48 => ELNRNG, + 49 => EUNATCH, + 50 => ENOCSI, + 51 => EL2HLT, + 52 => EBADE, + 53 => EBADR, + 54 => EXFULL, + 55 => ENOANO, + 56 => EBADRQC, + 57 => EBADSLT, + 59 => EBFONT, + 60 => ENOSTR, + 61 => ENODATA, + 62 => ETIME, + 63 => ENOSR, + 64 => ENONET, + 65 => ENOPKG, + 66 => EREMOTE, + 67 => ENOLINK, + 68 => EADV, + 69 => ESRMNT, + 70 => ECOMM, + 71 => EPROTO, + 72 => EMULTIHOP, + 73 => EDOTDOT, + 74 => EBADMSG, + 75 => EOVERFLOW, + 76 => ENOTUNIQ, + 77 => EBADFD, + 78 => EREMCHG, + 79 => ELIBACC, + 80 => ELIBBAD, + 81 => ELIBSCN, + 82 => ELIBMAX, + 83 => ELIBEXEC, + 84 => EILSEQ, + 85 => ERESTART, + 86 => ESTRPIPE, + 87 => EUSERS, + 88 => ENOTSOCK, + 89 => EDESTADDRREQ, + 90 => EMSGSIZE, + 91 => EPROTOTYPE, + 92 => ENOPROTOOPT, + 93 => EPROTONOSUPPORT, + 94 => ESOCKTNOSUPPORT, + 95 => EOPNOTSUPP, + 96 => EPFNOSUPPORT, + 97 => EAFNOSUPPORT, + 98 => EADDRINUSE, + 99 => EADDRNOTAVAIL, + 100 => ENETDOWN, + 101 => ENETUNREACH, + 102 => ENETRESET, + 103 => ECONNABORTED, + 104 => ECONNRESET, + 105 => ENOBUFS, + 106 => EISCONN, + 107 => ENOTCONN, + 108 => ESHUTDOWN, + 109 => ETOOMANYREFS, + 110 => ETIMEDOUT, + 111 => ECONNREFUSED, + 112 => EHOSTDOWN, + 113 => EHOSTUNREACH, + 114 => EALREADY, + 115 => EINPROGRESS, + 116 => ESTALE, + 117 => EUCLEAN, + 118 => ENOTNAM, + 119 => ENAVAIL, + 120 => EISNAM, + 121 => EREMOTEIO, + 122 => EDQUOT, + 123 => ENOMEDIUM, + 124 => EMEDIUMTYPE, + 125 => ECANCELED, + 126 => ENOKEY, + 127 => EKEYEXPIRED, + 128 => EKEYREVOKED, + 129 => EKEYREJECTED, + 130 => EOWNERDEAD, + 131 => ENOTRECOVERABLE, + + #[cfg(not(target_os = "android"))] + 132 => ERFKILL, + #[cfg(not(target_os = "android"))] + 133 => EHWPOISON, + _ => UnknownErrno, + } + } } #[cfg(any(target_os = "macos", target_os = "ios"))] @@ -704,6 +850,121 @@ mod consts { pub const EDEADLOCK: Errno = Errno::EDEADLK; pub const EL2NSYNC: Errno = Errno::UnknownErrno; + + pub fn from_i32(e: i32) -> Errno { + use self::Errno::*; + + match e { + 0 => UnknownErrno, + 1 => EPERM, + 2 => ENOENT, + 3 => ESRCH, + 4 => EINTR, + 5 => EIO, + 6 => ENXIO, + 7 => E2BIG, + 8 => ENOEXEC, + 9 => EBADF, + 10 => ECHILD, + 11 => EDEADLK, + 12 => ENOMEM, + 13 => EACCES, + 14 => EFAULT, + 15 => ENOTBLK, + 16 => EBUSY, + 17 => EEXIST, + 18 => EXDEV, + 19 => ENODEV, + 20 => ENOTDIR, + 21 => EISDIR, + 22 => EINVAL, + 23 => ENFILE, + 24 => EMFILE, + 25 => ENOTTY, + 26 => ETXTBSY, + 27 => EFBIG, + 28 => ENOSPC, + 29 => ESPIPE, + 30 => EROFS, + 31 => EMLINK, + 32 => EPIPE, + 33 => EDOM, + 34 => ERANGE, + 35 => EAGAIN, + 36 => EINPROGRESS, + 37 => EALREADY, + 38 => ENOTSOCK, + 39 => EDESTADDRREQ, + 40 => EMSGSIZE, + 41 => EPROTOTYPE, + 42 => ENOPROTOOPT, + 43 => EPROTONOSUPPORT, + 44 => ESOCKTNOSUPPORT, + 45 => ENOTSUP, + 46 => EPFNOSUPPORT, + 47 => EAFNOSUPPORT, + 48 => EADDRINUSE, + 49 => EADDRNOTAVAIL, + 50 => ENETDOWN, + 51 => ENETUNREACH, + 52 => ENETRESET, + 53 => ECONNABORTED, + 54 => ECONNRESET, + 55 => ENOBUFS, + 56 => EISCONN, + 57 => ENOTCONN, + 58 => ESHUTDOWN, + 59 => ETOOMANYREFS, + 60 => ETIMEDOUT, + 61 => ECONNREFUSED, + 62 => ELOOP, + 63 => ENAMETOOLONG, + 64 => EHOSTDOWN, + 65 => EHOSTUNREACH, + 66 => ENOTEMPTY, + 67 => EPROCLIM, + 68 => EUSERS, + 69 => EDQUOT, + 70 => ESTALE, + 71 => EREMOTE, + 72 => EBADRPC, + 73 => ERPCMISMATCH, + 74 => EPROGUNAVAIL, + 75 => EPROGMISMATCH, + 76 => EPROCUNAVAIL, + 77 => ENOLCK, + 78 => ENOSYS, + 79 => EFTYPE, + 80 => EAUTH, + 81 => ENEEDAUTH, + 82 => EPWROFF, + 83 => EDEVERR, + 84 => EOVERFLOW, + 85 => EBADEXEC, + 86 => EBADARCH, + 87 => ESHLIBVERS, + 88 => EBADMACHO, + 89 => ECANCELED, + 90 => EIDRM, + 91 => ENOMSG, + 92 => EILSEQ, + 93 => ENOATTR, + 94 => EBADMSG, + 95 => EMULTIHOP, + 96 => ENODATA, + 97 => ENOLINK, + 98 => ENOSR, + 99 => ENOSTR, + 100 => EPROTO, + 101 => ETIME, + 102 => EOPNOTSUPP, + 103 => ENOPOLICY, + 104 => ENOTRECOVERABLE, + 105 => EOWNERDEAD, + 106 => EQFULL, + _ => UnknownErrno, + } + } } #[cfg(test)] @@ -3,15 +3,12 @@ //! Modules are structured according to the C header file that they would be //! defined in. #![crate_name = "nix"] - -#![feature(collections, core, linkage, std_misc)] #![allow(non_camel_case_types)] #[macro_use] extern crate bitflags; extern crate libc; -extern crate core; #[cfg(test)] extern crate nix_test as nixtest; @@ -46,10 +43,8 @@ pub mod unistd; * */ -use std::result; -use std::ffi::AsOsStr; +use std::{ptr, result}; use std::path::{Path, PathBuf}; -use std::slice::bytes; pub type Result<T> = result::Result<T, Error>; @@ -91,10 +86,14 @@ impl NixPath for [u8] { return Err(Error::InvalidPath); } - match self.position_elem(&0) { + match self.iter().position(|b| *b == 0) { Some(_) => Err(Error::InvalidPath), None => { - bytes::copy_memory(self, &mut buf); + unsafe { + // TODO: Replace with bytes::copy_memory. rust-lang/rust#24028 + ptr::copy_nonoverlapping(self.as_ptr(), buf.as_mut_ptr(), self.len()); + } + Ok(f(<OsStr as OsStrExt>::from_bytes(&buf[..self.len()]))) } } diff --git a/src/sys/eventfd.rs b/src/sys/eventfd.rs index f072405f..866c95a3 100644 --- a/src/sys/eventfd.rs +++ b/src/sys/eventfd.rs @@ -16,7 +16,7 @@ pub fn eventfd(initval: usize, flags: EventFdFlag) -> Result<Fd> { type F = unsafe extern "C" fn(initval: c_uint, flags: c_int) -> c_int; extern { - #[linkage = "extern_weak"] + // #[linkage = "extern_weak"] static eventfd: *const (); } diff --git a/src/sys/mod.rs b/src/sys/mod.rs index c14b02f2..fbd75d23 100644 --- a/src/sys/mod.rs +++ b/src/sys/mod.rs @@ -5,8 +5,9 @@ pub mod epoll; #[cfg(any(target_os = "macos", target_os = "ios"))] pub mod event; -#[cfg(any(target_os = "linux", target_os = "android"))] -pub mod eventfd; +// Dont' support eventfd for now +// #[cfg(any(target_os = "linux", target_os = "android"))] +// pub mod eventfd; #[cfg(not(target_os = "ios"))] pub mod ioctl; diff --git a/src/sys/signal.rs b/src/sys/signal.rs index afeb9c6c..1cc3f393 100644 --- a/src/sys/signal.rs +++ b/src/sys/signal.rs @@ -3,7 +3,7 @@ use libc; use errno::Errno; -use core::mem; +use std::mem; use {Error, Result}; pub use libc::consts::os::posix88::{ diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index ea5c9916..dce80cee 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -142,37 +142,6 @@ pub fn accept(sockfd: RawFd) -> Result<RawFd> { /// Accept a connection on a socket /// /// [Further reading](http://man7.org/linux/man-pages/man2/accept.2.html) -#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))] -pub fn accept4(sockfd: RawFd, flags: SockFlag) -> Result<RawFd> { - use libc::sockaddr; - - type F = unsafe extern "C" fn(c_int, *mut sockaddr, *mut socklen_t, c_int) -> c_int; - - extern { - #[linkage = "extern_weak"] - static accept4: *const (); - } - - if !accept4.is_null() { - let res = unsafe { - mem::transmute::<*const (), F>(accept4)( - sockfd, ptr::null_mut(), ptr::null_mut(), flags.bits) - }; - - if res < 0 { - return Err(Error::Sys(Errno::last())); - } - - Ok(res) - } else { - accept4_polyfill(sockfd, flags) - } -} - -/// Accept a connection on a socket -/// -/// [Further reading](http://man7.org/linux/man-pages/man2/accept.2.html) -#[cfg(any(target_os = "macos", target_os = "ios", target_os = "android"))] pub fn accept4(sockfd: RawFd, flags: SockFlag) -> Result<RawFd> { accept4_polyfill(sockfd, flags) } diff --git a/src/unistd.rs b/src/unistd.rs index 64203f02..6d7e2982 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -102,32 +102,6 @@ pub fn dup2(oldfd: Fd, newfd: Fd) -> Result<Fd> { Ok(res) } -#[cfg(not(any(target_os = "macos", target_os = "ios")))] -pub fn dup3(oldfd: Fd, newfd: Fd, flags: OFlag) -> Result<Fd> { - type F = unsafe extern "C" fn(c_int, c_int, c_int) -> c_int; - - extern { - #[linkage = "extern_weak"] - static dup3: *const (); - } - - if !dup3.is_null() { - let res = unsafe { - mem::transmute::<*const (), F>(dup3)( - oldfd, newfd, flags.bits()) - }; - - if res < 0 { - return Err(Error::Sys(Errno::last())); - } - - Ok(res) - } else { - dup3_polyfill(oldfd, newfd, flags) - } -} - -#[cfg(any(target_os = "macos", target_os = "ios"))] pub fn dup3(oldfd: Fd, newfd: Fd, flags: OFlag) -> Result<Fd> { dup3_polyfill(oldfd, newfd, flags) } @@ -245,41 +219,6 @@ pub fn pipe() -> Result<(Fd, Fd)> { } } -#[cfg(any(target_os = "linux", target_os = "android"))] -pub fn pipe2(flags: OFlag) -> Result<(Fd, Fd)> { - type F = unsafe extern "C" fn(fds: *mut c_int, flags: c_int) -> c_int; - - extern { - #[linkage = "extern_weak"] - static pipe2: *const (); - } - - let feat_atomic = !pipe2.is_null(); - - unsafe { - let mut res; - let mut fds: [c_int; 2] = mem::uninitialized(); - - if feat_atomic { - res = mem::transmute::<*const (), F>(pipe2)( - fds.as_mut_ptr(), flags.bits()); - } else { - res = ffi::pipe(fds.as_mut_ptr()); - } - - if res < 0 { - return Err(Error::Sys(Errno::last())); - } - - if !feat_atomic { - try!(pipe2_setflags(fds[0], fds[1], flags)); - } - - Ok((fds[0], fds[1])) - } -} - -#[cfg(any(target_os = "macos", target_os = "ios"))] pub fn pipe2(flags: OFlag) -> Result<(Fd, Fd)> { unsafe { let mut res; |