Properly handle visibility

This commit is contained in:
topjohnwu 2023-07-06 04:54:33 -07:00
parent af2207433d
commit e8e8afa6c2
5 changed files with 137 additions and 164 deletions

View File

@ -1,4 +1,4 @@
// Functions listed here are just to export to C++ // Functions in this file are only for exporting to C++, DO NOT USE IN RUST
use std::fmt::Write; use std::fmt::Write;
use std::io; use std::io;
@ -7,6 +7,7 @@ use std::os::fd::{BorrowedFd, OwnedFd, RawFd};
use cxx::private::c_char; use cxx::private::c_char;
use libc::mode_t; use libc::mode_t;
pub(crate) use crate::xwrap::*;
use crate::{ use crate::{
fd_path, map_fd, map_file, mkdirs, realpath, rm_rf, slice_from_ptr_mut, Directory, ResultExt, fd_path, map_fd, map_file, mkdirs, realpath, rm_rf, slice_from_ptr_mut, Directory, ResultExt,
Utf8CStr, Utf8CStr,

View File

@ -31,7 +31,7 @@ macro_rules! open_fd {
}; };
} }
pub unsafe fn readlink_unsafe(path: *const c_char, buf: *mut u8, bufsz: usize) -> isize { pub(crate) unsafe fn readlink_unsafe(path: *const c_char, buf: *mut u8, bufsz: usize) -> isize {
let r = libc::readlink(path, buf.cast(), bufsz - 1); let r = libc::readlink(path, buf.cast(), bufsz - 1);
if r >= 0 { if r >= 0 {
*buf.offset(r) = b'\0'; *buf.offset(r) = b'\0';

View File

@ -8,7 +8,6 @@ use cxx_extern::*;
pub use files::*; pub use files::*;
pub use logging::*; pub use logging::*;
pub use misc::*; pub use misc::*;
pub use xwrap::*;
mod cxx_extern; mod cxx_extern;
mod files; mod files;

View File

@ -256,22 +256,10 @@ impl PartialEq<Utf8CStr> for str {
} }
} }
pub fn ptr_to_str<'a, T>(ptr: *const T) -> &'a str {
if ptr.is_null() {
"(null)"
} else {
unsafe { CStr::from_ptr(ptr.cast()) }.to_str().unwrap_or("")
}
}
pub fn errno() -> &'static mut i32 { pub fn errno() -> &'static mut i32 {
unsafe { &mut *libc::__errno() } unsafe { &mut *libc::__errno() }
} }
pub fn error_str() -> &'static str {
unsafe { ptr_to_str(libc::strerror(*errno())) }
}
// When len is 0, don't care whether buf is null or not // When len is 0, don't care whether buf is null or not
#[inline] #[inline]
pub unsafe fn slice_from_ptr<'a, T>(buf: *const T, len: usize) -> &'a [T] { pub unsafe fn slice_from_ptr<'a, T>(buf: *const T, len: usize) -> &'a [T] {

View File

@ -1,13 +1,32 @@
// Functions in this file are only for exporting to C++, DO NOT USE IN RUST
use std::ffi::CStr;
use std::fmt::Write; use std::fmt::Write;
use std::os::unix::io::RawFd; use std::os::unix::io::RawFd;
use std::ptr; use std::ptr;
use cfg_if::cfg_if;
use libc::{ use libc::{
c_char, c_uint, c_ulong, c_void, dev_t, mode_t, nfds_t, off_t, pollfd, sockaddr, socklen_t, c_char, c_uint, c_ulong, c_void, dev_t, mode_t, nfds_t, off_t, pollfd, sockaddr, socklen_t,
ssize_t, SYS_dup3, ssize_t, SYS_dup3,
}; };
use crate::{cstr, errno, mkdirs, ptr_to_str, raw_cstr, ResultExt, Utf8CStr}; use crate::{
cstr, errno, mkdirs, raw_cstr, readlink_unsafe, realpath, slice_from_ptr_mut, ResultExt,
Utf8CStr,
};
fn ptr_to_str<'a, T>(ptr: *const T) -> &'a str {
if ptr.is_null() {
"(null)"
} else {
unsafe { CStr::from_ptr(ptr.cast()) }.to_str().unwrap_or("")
}
}
fn error_str() -> &'static str {
unsafe { ptr_to_str(libc::strerror(*errno())) }
}
macro_rules! error_cxx { macro_rules! error_cxx {
($($args:tt)+) => { ($($args:tt)+) => {
@ -19,70 +38,59 @@ macro_rules! perror {
($fmt:expr) => { ($fmt:expr) => {
$crate::log_with_formatter($crate::ffi::LogLevel::ErrorCxx, |w| { $crate::log_with_formatter($crate::ffi::LogLevel::ErrorCxx, |w| {
w.write_str($fmt)?; w.write_str($fmt)?;
w.write_fmt(format_args!(" failed with {}: {}", $crate::errno(), $crate::error_str())) w.write_fmt(format_args!(" failed with {}: {}", $crate::errno(), error_str()))
}) })
}; };
($fmt:expr, $($args:tt)*) => { ($fmt:expr, $($args:tt)*) => {
$crate::log_with_formatter($crate::ffi::LogLevel::ErrorCxx, |w| { $crate::log_with_formatter($crate::ffi::LogLevel::ErrorCxx, |w| {
w.write_fmt(format_args!($fmt, $($args)*))?; w.write_fmt(format_args!($fmt, $($args)*))?;
w.write_fmt(format_args!(" failed with {}: {}", $crate::errno(), $crate::error_str())) w.write_fmt(format_args!(" failed with {}: {}", $crate::errno(), error_str()))
}) })
}; };
} }
mod unsafe_impl { mod c_export {
use std::fmt::Write;
use std::os::unix::io::RawFd; use std::os::unix::io::RawFd;
use cfg_if::cfg_if; use crate::{slice_from_ptr, slice_from_ptr_mut};
use libc::{c_char, nfds_t, off_t, pollfd};
use crate::{
ptr_to_str, readlink_unsafe, realpath, slice_from_ptr, slice_from_ptr_mut, ResultExt,
Utf8CStr,
};
#[no_mangle] #[no_mangle]
unsafe extern "C" fn xwrite(fd: RawFd, buf: *const u8, bufsz: usize) -> isize { unsafe extern "C" fn xwrite(fd: RawFd, buf: *const u8, bufsz: usize) -> isize {
super::xwrite(fd, slice_from_ptr(buf, bufsz)) super::xwrite(fd, slice_from_ptr(buf, bufsz))
} }
#[no_mangle]
unsafe extern "C" fn xread(fd: RawFd, buf: *mut u8, bufsz: usize) -> isize {
super::xread(fd, slice_from_ptr_mut(buf, bufsz))
}
#[no_mangle] #[no_mangle]
unsafe extern "C" fn xxread(fd: RawFd, buf: *mut u8, bufsz: usize) -> isize { unsafe extern "C" fn xxread(fd: RawFd, buf: *mut u8, bufsz: usize) -> isize {
super::xxread(fd, slice_from_ptr_mut(buf, bufsz)) super::xxread(fd, slice_from_ptr_mut(buf, bufsz))
} }
}
#[no_mangle] #[no_mangle]
unsafe extern "C" fn xrealpath(path: *const c_char, buf: *mut u8, bufsz: usize) -> isize { unsafe extern "C" fn xrealpath(path: *const c_char, buf: *mut u8, bufsz: usize) -> isize {
match Utf8CStr::from_ptr(path) { match Utf8CStr::from_ptr(path) {
Ok(p) => realpath(p, slice_from_ptr_mut(buf, bufsz)) Ok(p) => realpath(p, slice_from_ptr_mut(buf, bufsz))
.log_cxx_with_msg(|w| w.write_fmt(format_args!("realpath {} failed", p))) .log_cxx_with_msg(|w| w.write_fmt(format_args!("realpath {} failed", p)))
.map_or(-1, |v| v as isize), .map_or(-1, |v| v as isize),
Err(_) => -1, Err(_) => -1,
} }
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xreadlink(path: *const c_char, buf: *mut u8, bufsz: usize) -> isize { unsafe extern "C" fn xreadlink(path: *const c_char, buf: *mut u8, bufsz: usize) -> isize {
let r = readlink_unsafe(path, buf, bufsz); let r = readlink_unsafe(path, buf, bufsz);
if r < 0 { if r < 0 {
perror!("readlink"); perror!("readlink");
} }
r r
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xreadlinkat( unsafe extern "C" fn xreadlinkat(
dirfd: RawFd, dirfd: RawFd,
path: *const c_char, path: *const c_char,
buf: *mut u8, buf: *mut u8,
bufsz: usize, bufsz: usize,
) -> isize { ) -> isize {
// readlinkat() may fail on x86 platform, returning random value // readlinkat() may fail on x86 platform, returning random value
// instead of number of bytes placed in buf (length of link) // instead of number of bytes placed in buf (length of link)
cfg_if! { cfg_if! {
@ -102,34 +110,10 @@ mod unsafe_impl {
} }
} }
r r
}
#[no_mangle]
pub unsafe extern "C" fn xpoll(fds: *mut pollfd, nfds: nfds_t, timeout: i32) -> i32 {
let r = libc::poll(fds, nfds, timeout);
if r < 0 {
perror!("poll");
}
r
}
#[no_mangle]
pub unsafe extern "C" fn xsendfile(
out_fd: RawFd,
in_fd: RawFd,
offset: *mut off_t,
count: usize,
) -> isize {
let r = libc::sendfile(out_fd, in_fd, offset, count);
if r < 0 {
perror!("sendfile");
}
r
}
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xfopen(path: *const c_char, mode: *const c_char) -> *mut libc::FILE { unsafe extern "C" fn xfopen(path: *const c_char, mode: *const c_char) -> *mut libc::FILE {
let fp = libc::fopen(path, mode); let fp = libc::fopen(path, mode);
if fp.is_null() { if fp.is_null() {
perror!("fopen {}", ptr_to_str(path)); perror!("fopen {}", ptr_to_str(path));
@ -138,7 +122,7 @@ pub unsafe extern "C" fn xfopen(path: *const c_char, mode: *const c_char) -> *mu
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xfdopen(fd: RawFd, mode: *const c_char) -> *mut libc::FILE { unsafe extern "C" fn xfdopen(fd: RawFd, mode: *const c_char) -> *mut libc::FILE {
let fp = libc::fdopen(fd, mode); let fp = libc::fdopen(fd, mode);
if fp.is_null() { if fp.is_null() {
perror!("fdopen"); perror!("fdopen");
@ -147,7 +131,7 @@ pub unsafe extern "C" fn xfdopen(fd: RawFd, mode: *const c_char) -> *mut libc::F
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xopen(path: *const c_char, flags: i32, mode: mode_t) -> RawFd { unsafe extern "C" fn xopen(path: *const c_char, flags: i32, mode: mode_t) -> RawFd {
let r = libc::open(path, flags, mode as c_uint); let r = libc::open(path, flags, mode as c_uint);
if r < 0 { if r < 0 {
perror!("open {}", ptr_to_str(path)); perror!("open {}", ptr_to_str(path));
@ -156,12 +140,7 @@ pub unsafe extern "C" fn xopen(path: *const c_char, flags: i32, mode: mode_t) ->
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xopenat( unsafe extern "C" fn xopenat(dirfd: RawFd, path: *const c_char, flags: i32, mode: mode_t) -> RawFd {
dirfd: RawFd,
path: *const c_char,
flags: i32,
mode: mode_t,
) -> RawFd {
let r = libc::openat(dirfd, path, flags, mode as c_uint); let r = libc::openat(dirfd, path, flags, mode as c_uint);
if r < 0 { if r < 0 {
perror!("openat {}", ptr_to_str(path)); perror!("openat {}", ptr_to_str(path));
@ -170,7 +149,7 @@ pub unsafe extern "C" fn xopenat(
} }
// Fully write data slice // Fully write data slice
pub fn xwrite(fd: RawFd, data: &[u8]) -> isize { fn xwrite(fd: RawFd, data: &[u8]) -> isize {
unsafe { unsafe {
let mut write_sz: usize = 0; let mut write_sz: usize = 0;
let mut r: ssize_t; let mut r: ssize_t;
@ -198,9 +177,10 @@ pub fn xwrite(fd: RawFd, data: &[u8]) -> isize {
} }
} }
pub fn xread(fd: RawFd, data: &mut [u8]) -> isize { #[no_mangle]
unsafe extern "C" fn xread(fd: RawFd, buf: *mut c_void, bufsz: usize) -> isize {
unsafe { unsafe {
let r = libc::read(fd, data.as_mut_ptr().cast(), data.len()); let r = libc::read(fd, buf, bufsz);
if r < 0 { if r < 0 {
perror!("read"); perror!("read");
} }
@ -209,7 +189,7 @@ pub fn xread(fd: RawFd, data: &mut [u8]) -> isize {
} }
// Fully read size of data slice // Fully read size of data slice
pub fn xxread(fd: RawFd, data: &mut [u8]) -> isize { fn xxread(fd: RawFd, data: &mut [u8]) -> isize {
unsafe { unsafe {
let mut read_sz: usize = 0; let mut read_sz: usize = 0;
let mut r: ssize_t; let mut r: ssize_t;
@ -238,7 +218,7 @@ pub fn xxread(fd: RawFd, data: &mut [u8]) -> isize {
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xlseek64(fd: RawFd, offset: i64, whence: i32) -> i64 { extern "C" fn xlseek64(fd: RawFd, offset: i64, whence: i32) -> i64 {
unsafe { unsafe {
let r = libc::lseek64(fd, offset, whence); let r = libc::lseek64(fd, offset, whence);
if r < 0 { if r < 0 {
@ -248,7 +228,7 @@ pub extern "C" fn xlseek64(fd: RawFd, offset: i64, whence: i32) -> i64 {
} }
} }
pub fn xpipe2(fds: &mut [i32; 2], flags: i32) -> i32 { pub(crate) fn xpipe2(fds: &mut [i32; 2], flags: i32) -> i32 {
unsafe { unsafe {
let r = libc::pipe2(fds.as_mut_ptr(), flags); let r = libc::pipe2(fds.as_mut_ptr(), flags);
if r < 0 { if r < 0 {
@ -259,7 +239,7 @@ pub fn xpipe2(fds: &mut [i32; 2], flags: i32) -> i32 {
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xsetns(fd: RawFd, nstype: i32) -> i32 { extern "C" fn xsetns(fd: RawFd, nstype: i32) -> i32 {
unsafe { unsafe {
let r = libc::setns(fd, nstype); let r = libc::setns(fd, nstype);
if r < 0 { if r < 0 {
@ -270,7 +250,7 @@ pub extern "C" fn xsetns(fd: RawFd, nstype: i32) -> i32 {
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xunshare(flags: i32) -> i32 { extern "C" fn xunshare(flags: i32) -> i32 {
unsafe { unsafe {
let r = libc::unshare(flags); let r = libc::unshare(flags);
if r < 0 { if r < 0 {
@ -281,7 +261,7 @@ pub extern "C" fn xunshare(flags: i32) -> i32 {
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xopendir(path: *const c_char) -> *mut libc::DIR { unsafe extern "C" fn xopendir(path: *const c_char) -> *mut libc::DIR {
let dp = libc::opendir(path); let dp = libc::opendir(path);
if dp.is_null() { if dp.is_null() {
perror!("opendir {}", ptr_to_str(path)); perror!("opendir {}", ptr_to_str(path));
@ -290,7 +270,7 @@ pub unsafe extern "C" fn xopendir(path: *const c_char) -> *mut libc::DIR {
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xfdopendir(fd: RawFd) -> *mut libc::DIR { extern "C" fn xfdopendir(fd: RawFd) -> *mut libc::DIR {
unsafe { unsafe {
let dp = libc::fdopendir(fd); let dp = libc::fdopendir(fd);
if dp.is_null() { if dp.is_null() {
@ -301,7 +281,7 @@ pub extern "C" fn xfdopendir(fd: RawFd) -> *mut libc::DIR {
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xreaddir(dirp: *mut libc::DIR) -> *mut libc::dirent { unsafe extern "C" fn xreaddir(dirp: *mut libc::DIR) -> *mut libc::dirent {
*errno() = 0; *errno() = 0;
loop { loop {
let e = libc::readdir(dirp); let e = libc::readdir(dirp);
@ -321,7 +301,7 @@ pub unsafe extern "C" fn xreaddir(dirp: *mut libc::DIR) -> *mut libc::dirent {
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xsetsid() -> i32 { extern "C" fn xsetsid() -> i32 {
unsafe { unsafe {
let r = libc::setsid(); let r = libc::setsid();
if r < 0 { if r < 0 {
@ -332,7 +312,7 @@ pub extern "C" fn xsetsid() -> i32 {
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xsocket(domain: i32, ty: i32, protocol: i32) -> RawFd { extern "C" fn xsocket(domain: i32, ty: i32, protocol: i32) -> RawFd {
unsafe { unsafe {
let fd = libc::socket(domain, ty, protocol); let fd = libc::socket(domain, ty, protocol);
if fd < 0 { if fd < 0 {
@ -343,7 +323,7 @@ pub extern "C" fn xsocket(domain: i32, ty: i32, protocol: i32) -> RawFd {
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xbind(socket: i32, address: *const sockaddr, len: socklen_t) -> i32 { unsafe extern "C" fn xbind(socket: i32, address: *const sockaddr, len: socklen_t) -> i32 {
let r = libc::bind(socket, address, len); let r = libc::bind(socket, address, len);
if r < 0 { if r < 0 {
perror!("bind"); perror!("bind");
@ -352,7 +332,7 @@ pub unsafe extern "C" fn xbind(socket: i32, address: *const sockaddr, len: sockl
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xlisten(socket: i32, backlog: i32) -> i32 { extern "C" fn xlisten(socket: i32, backlog: i32) -> i32 {
unsafe { unsafe {
let r = libc::listen(socket, backlog); let r = libc::listen(socket, backlog);
if r < 0 { if r < 0 {
@ -363,7 +343,7 @@ pub extern "C" fn xlisten(socket: i32, backlog: i32) -> i32 {
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xaccept4( unsafe extern "C" fn xaccept4(
sockfd: RawFd, sockfd: RawFd,
addr: *mut sockaddr, addr: *mut sockaddr,
len: *mut socklen_t, len: *mut socklen_t,
@ -377,7 +357,7 @@ pub unsafe extern "C" fn xaccept4(
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xsendmsg(fd: RawFd, msg: *const libc::msghdr, flags: i32) -> ssize_t { unsafe extern "C" fn xsendmsg(fd: RawFd, msg: *const libc::msghdr, flags: i32) -> ssize_t {
let r = libc::sendmsg(fd, msg, flags); let r = libc::sendmsg(fd, msg, flags);
if r < 0 { if r < 0 {
perror!("sendmsg"); perror!("sendmsg");
@ -386,7 +366,7 @@ pub unsafe extern "C" fn xsendmsg(fd: RawFd, msg: *const libc::msghdr, flags: i3
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xrecvmsg(fd: RawFd, msg: *mut libc::msghdr, flags: i32) -> ssize_t { unsafe extern "C" fn xrecvmsg(fd: RawFd, msg: *mut libc::msghdr, flags: i32) -> ssize_t {
let r = libc::recvmsg(fd, msg, flags); let r = libc::recvmsg(fd, msg, flags);
if r < 0 { if r < 0 {
perror!("recvmsg"); perror!("recvmsg");
@ -395,7 +375,7 @@ pub unsafe extern "C" fn xrecvmsg(fd: RawFd, msg: *mut libc::msghdr, flags: i32)
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xaccess(path: *const c_char, mode: i32) -> i32 { unsafe extern "C" fn xaccess(path: *const c_char, mode: i32) -> i32 {
let r = libc::access(path, mode); let r = libc::access(path, mode);
if r < 0 { if r < 0 {
perror!("access {}", ptr_to_str(path)); perror!("access {}", ptr_to_str(path));
@ -404,12 +384,7 @@ pub unsafe extern "C" fn xaccess(path: *const c_char, mode: i32) -> i32 {
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xfaccessat( unsafe extern "C" fn xfaccessat(dirfd: RawFd, path: *const c_char, mode: i32, flags: i32) -> i32 {
dirfd: RawFd,
path: *const c_char,
mode: i32,
flags: i32,
) -> i32 {
#[allow(unused_mut)] #[allow(unused_mut)]
let mut r = libc::faccessat(dirfd, path, mode, flags); let mut r = libc::faccessat(dirfd, path, mode, flags);
if r < 0 { if r < 0 {
@ -423,7 +398,7 @@ pub unsafe extern "C" fn xfaccessat(
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xstat(path: *const c_char, buf: *mut libc::stat) -> i32 { unsafe extern "C" fn xstat(path: *const c_char, buf: *mut libc::stat) -> i32 {
let r = libc::stat(path, buf); let r = libc::stat(path, buf);
if r < 0 { if r < 0 {
perror!("stat {}", ptr_to_str(path)); perror!("stat {}", ptr_to_str(path));
@ -432,7 +407,7 @@ pub unsafe extern "C" fn xstat(path: *const c_char, buf: *mut libc::stat) -> i32
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xlstat(path: *const c_char, buf: *mut libc::stat) -> i32 { unsafe extern "C" fn xlstat(path: *const c_char, buf: *mut libc::stat) -> i32 {
let r = libc::lstat(path, buf); let r = libc::lstat(path, buf);
if r < 0 { if r < 0 {
perror!("lstat {}", ptr_to_str(path)); perror!("lstat {}", ptr_to_str(path));
@ -441,7 +416,7 @@ pub unsafe extern "C" fn xlstat(path: *const c_char, buf: *mut libc::stat) -> i3
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xfstat(fd: RawFd, buf: *mut libc::stat) -> i32 { unsafe extern "C" fn xfstat(fd: RawFd, buf: *mut libc::stat) -> i32 {
let r = libc::fstat(fd, buf); let r = libc::fstat(fd, buf);
if r < 0 { if r < 0 {
perror!("fstat"); perror!("fstat");
@ -450,7 +425,7 @@ pub unsafe extern "C" fn xfstat(fd: RawFd, buf: *mut libc::stat) -> i32 {
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xfstatat( unsafe extern "C" fn xfstatat(
dirfd: RawFd, dirfd: RawFd,
path: *const c_char, path: *const c_char,
buf: *mut libc::stat, buf: *mut libc::stat,
@ -464,7 +439,7 @@ pub unsafe extern "C" fn xfstatat(
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xdup(oldfd: RawFd) -> RawFd { extern "C" fn xdup(oldfd: RawFd) -> RawFd {
unsafe { unsafe {
let fd = libc::dup(oldfd); let fd = libc::dup(oldfd);
if fd < 0 { if fd < 0 {
@ -475,7 +450,7 @@ pub extern "C" fn xdup(oldfd: RawFd) -> RawFd {
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xdup2(oldfd: RawFd, newfd: RawFd) -> RawFd { extern "C" fn xdup2(oldfd: RawFd, newfd: RawFd) -> RawFd {
unsafe { unsafe {
let fd = libc::dup2(oldfd, newfd); let fd = libc::dup2(oldfd, newfd);
if fd < 0 { if fd < 0 {
@ -486,7 +461,7 @@ pub extern "C" fn xdup2(oldfd: RawFd, newfd: RawFd) -> RawFd {
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xdup3(oldfd: RawFd, newfd: RawFd, flags: i32) -> RawFd { extern "C" fn xdup3(oldfd: RawFd, newfd: RawFd, flags: i32) -> RawFd {
unsafe { unsafe {
let fd = libc::syscall(SYS_dup3, oldfd, newfd, flags) as RawFd; let fd = libc::syscall(SYS_dup3, oldfd, newfd, flags) as RawFd;
if fd < 0 { if fd < 0 {
@ -497,7 +472,7 @@ pub extern "C" fn xdup3(oldfd: RawFd, newfd: RawFd, flags: i32) -> RawFd {
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xsymlink(target: *const c_char, linkpath: *const c_char) -> i32 { unsafe extern "C" fn xsymlink(target: *const c_char, linkpath: *const c_char) -> i32 {
let r = libc::symlink(target, linkpath); let r = libc::symlink(target, linkpath);
if r < 0 { if r < 0 {
perror!("symlink {} -> {}", ptr_to_str(target), ptr_to_str(linkpath)); perror!("symlink {} -> {}", ptr_to_str(target), ptr_to_str(linkpath));
@ -506,7 +481,7 @@ pub unsafe extern "C" fn xsymlink(target: *const c_char, linkpath: *const c_char
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xsymlinkat( unsafe extern "C" fn xsymlinkat(
target: *const c_char, target: *const c_char,
dirfd: RawFd, dirfd: RawFd,
linkpath: *const c_char, linkpath: *const c_char,
@ -523,7 +498,7 @@ pub unsafe extern "C" fn xsymlinkat(
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xlinkat( unsafe extern "C" fn xlinkat(
olddirfd: RawFd, olddirfd: RawFd,
target: *const c_char, target: *const c_char,
newdirfd: RawFd, newdirfd: RawFd,
@ -538,7 +513,7 @@ pub unsafe extern "C" fn xlinkat(
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xmount( unsafe extern "C" fn xmount(
src: *const c_char, src: *const c_char,
target: *const c_char, target: *const c_char,
fstype: *const c_char, fstype: *const c_char,
@ -553,7 +528,7 @@ pub unsafe extern "C" fn xmount(
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xumount(target: *const c_char) -> i32 { unsafe extern "C" fn xumount(target: *const c_char) -> i32 {
let r = libc::umount(target); let r = libc::umount(target);
if r < 0 { if r < 0 {
perror!("umount {}", ptr_to_str(target)); perror!("umount {}", ptr_to_str(target));
@ -562,7 +537,7 @@ pub unsafe extern "C" fn xumount(target: *const c_char) -> i32 {
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xumount2(target: *const c_char, flags: i32) -> i32 { unsafe extern "C" fn xumount2(target: *const c_char, flags: i32) -> i32 {
let r = libc::umount2(target, flags); let r = libc::umount2(target, flags);
if r < 0 { if r < 0 {
perror!("umount2 {}", ptr_to_str(target)); perror!("umount2 {}", ptr_to_str(target));
@ -571,7 +546,7 @@ pub unsafe extern "C" fn xumount2(target: *const c_char, flags: i32) -> i32 {
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xrename(oldname: *const c_char, newname: *const c_char) -> i32 { unsafe extern "C" fn xrename(oldname: *const c_char, newname: *const c_char) -> i32 {
let r = libc::rename(oldname, newname); let r = libc::rename(oldname, newname);
if r < 0 { if r < 0 {
perror!("rename {} -> {}", ptr_to_str(oldname), ptr_to_str(newname)); perror!("rename {} -> {}", ptr_to_str(oldname), ptr_to_str(newname));
@ -580,7 +555,7 @@ pub unsafe extern "C" fn xrename(oldname: *const c_char, newname: *const c_char)
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xmkdir(path: *const c_char, mode: mode_t) -> i32 { unsafe extern "C" fn xmkdir(path: *const c_char, mode: mode_t) -> i32 {
let r = libc::mkdir(path, mode); let r = libc::mkdir(path, mode);
if r < 0 && *errno() != libc::EEXIST { if r < 0 && *errno() != libc::EEXIST {
perror!("mkdir {}", ptr_to_str(path)); perror!("mkdir {}", ptr_to_str(path));
@ -589,7 +564,7 @@ pub unsafe extern "C" fn xmkdir(path: *const c_char, mode: mode_t) -> i32 {
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xmkdirs(path: *const c_char, mode: mode_t) -> i32 { unsafe extern "C" fn xmkdirs(path: *const c_char, mode: mode_t) -> i32 {
match Utf8CStr::from_ptr(path) { match Utf8CStr::from_ptr(path) {
Ok(p) => mkdirs(p, mode) Ok(p) => mkdirs(p, mode)
.log_cxx_with_msg(|w| w.write_fmt(format_args!("mkdirs {} failed", p))) .log_cxx_with_msg(|w| w.write_fmt(format_args!("mkdirs {} failed", p)))
@ -599,7 +574,7 @@ pub unsafe extern "C" fn xmkdirs(path: *const c_char, mode: mode_t) -> i32 {
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xmkdirat(dirfd: RawFd, path: *const c_char, mode: mode_t) -> i32 { unsafe extern "C" fn xmkdirat(dirfd: RawFd, path: *const c_char, mode: mode_t) -> i32 {
let r = libc::mkdirat(dirfd, path, mode); let r = libc::mkdirat(dirfd, path, mode);
if r < 0 && *errno() != libc::EEXIST { if r < 0 && *errno() != libc::EEXIST {
perror!("mkdirat {}", ptr_to_str(path)); perror!("mkdirat {}", ptr_to_str(path));
@ -607,16 +582,22 @@ pub unsafe extern "C" fn xmkdirat(dirfd: RawFd, path: *const c_char, mode: mode_
r r
} }
#[inline] #[no_mangle]
pub fn xsendfile(out_fd: RawFd, in_fd: RawFd, offset: Option<&mut off_t>, count: usize) -> isize { unsafe extern "C" fn xsendfile(
unsafe { out_fd: RawFd,
let p = offset.map_or(ptr::null_mut(), |it| it); in_fd: RawFd,
unsafe_impl::xsendfile(out_fd, in_fd, p, count) offset: *mut off_t,
count: usize,
) -> isize {
let r = libc::sendfile(out_fd, in_fd, offset, count);
if r < 0 {
perror!("sendfile");
} }
r
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xmmap( unsafe extern "C" fn xmmap(
addr: *mut c_void, addr: *mut c_void,
len: usize, len: usize,
prot: i32, prot: i32,
@ -633,7 +614,7 @@ pub unsafe extern "C" fn xmmap(
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xfork() -> i32 { extern "C" fn xfork() -> i32 {
unsafe { unsafe {
let r = libc::fork(); let r = libc::fork();
if r < 0 { if r < 0 {
@ -643,13 +624,17 @@ pub extern "C" fn xfork() -> i32 {
} }
} }
#[inline] #[no_mangle]
pub fn xpoll(fds: &mut [pollfd], timeout: i32) -> i32 { unsafe extern "C" fn xpoll(fds: *mut pollfd, nfds: nfds_t, timeout: i32) -> i32 {
unsafe { unsafe_impl::xpoll(fds.as_mut_ptr(), fds.len() as nfds_t, timeout) } let r = libc::poll(fds, nfds, timeout);
if r < 0 {
perror!("poll");
}
r
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn xmknod(pathname: *const c_char, mode: mode_t, dev: dev_t) -> i32 { unsafe extern "C" fn xmknod(pathname: *const c_char, mode: mode_t, dev: dev_t) -> i32 {
let r = libc::mknod(pathname, mode, dev); let r = libc::mknod(pathname, mode, dev);
if r < 0 { if r < 0 {
perror!("mknod {}", ptr_to_str(pathname)); perror!("mknod {}", ptr_to_str(pathname));