More Rust

This commit is contained in:
topjohnwu 2022-08-15 11:53:51 -07:00
parent 40aab13601
commit 4d9b7e7114
4 changed files with 369 additions and 264 deletions

View File

@ -104,11 +104,7 @@ pub unsafe fn ptr_to_str<'a, T>(ptr: *const T) -> &'a str {
} }
pub fn errno() -> &'static mut i32 { pub fn errno() -> &'static mut i32 {
// On Android, errno is obtained through the __errno function for thread local storage unsafe { &mut *libc::__errno() }
extern "C" {
fn __errno() -> *mut i32;
}
unsafe { &mut *__errno() }
} }
pub fn error_str() -> &'static str { pub fn error_str() -> &'static str {

View File

@ -79,38 +79,6 @@ int xpipe2(int pipefd[2], int flags) {
return ret; return ret;
} }
int xsetns(int fd, int nstype) {
int ret = setns(fd, nstype);
if (ret < 0) {
PLOGE("setns");
}
return ret;
}
int xunshare(int flags) {
int ret = unshare(flags);
if (ret < 0) {
PLOGE("unshare");
}
return ret;
}
DIR *xopendir(const char *name) {
DIR *d = opendir(name);
if (d == nullptr) {
PLOGE("opendir: %s", name);
}
return d;
}
DIR *xfdopendir(int fd) {
DIR *d = fdopendir(fd);
if (d == nullptr) {
PLOGE("fdopendir");
}
return d;
}
struct dirent *xreaddir(DIR *dirp) { struct dirent *xreaddir(DIR *dirp) {
errno = 0; errno = 0;
for (dirent *e;;) { for (dirent *e;;) {
@ -127,62 +95,6 @@ struct dirent *xreaddir(DIR *dirp) {
} }
} }
pid_t xsetsid() {
pid_t pid = setsid();
if (pid < 0) {
PLOGE("setsid");
}
return pid;
}
int xsocket(int domain, int type, int protocol) {
int fd = socket(domain, type, protocol);
if (fd < 0) {
PLOGE("socket");
}
return fd;
}
int xbind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
int ret = bind(sockfd, addr, addrlen);
if (ret < 0) {
PLOGE("bind");
}
return ret;
}
int xlisten(int sockfd, int backlog) {
int ret = listen(sockfd, backlog);
if (ret < 0) {
PLOGE("listen");
}
return ret;
}
int xaccept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) {
int fd = accept4(sockfd, addr, addrlen, flags);
if (fd < 0) {
PLOGE("accept4");
}
return fd;
}
ssize_t xsendmsg(int sockfd, const struct msghdr *msg, int flags) {
int sent = sendmsg(sockfd, msg, flags);
if (sent < 0) {
PLOGE("sendmsg");
}
return sent;
}
ssize_t xrecvmsg(int sockfd, struct msghdr *msg, int flags) {
int rec = recvmsg(sockfd, msg, flags);
if (rec < 0) {
PLOGE("recvmsg");
}
return rec;
}
int xpthread_create(pthread_t *thread, const pthread_attr_t *attr, int xpthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg) { void *(*start_routine) (void *), void *arg) {
errno = pthread_create(thread, attr, start_routine, arg); errno = pthread_create(thread, attr, start_routine, arg);
@ -192,70 +104,6 @@ int xpthread_create(pthread_t *thread, const pthread_attr_t *attr,
return errno; return errno;
} }
int xaccess(const char *path, int mode) {
int ret = access(path, mode);
if (ret < 0) {
PLOGE("access %s", path);
}
return ret;
}
int xstat(const char *pathname, struct stat *buf) {
int ret = stat(pathname, buf);
if (ret < 0) {
PLOGE("stat %s", pathname);
}
return ret;
}
int xlstat(const char *pathname, struct stat *buf) {
int ret = lstat(pathname, buf);
if (ret < 0) {
PLOGE("lstat %s", pathname);
}
return ret;
}
int xfstat(int fd, struct stat *buf) {
int ret = fstat(fd, buf);
if (ret < 0) {
PLOGE("fstat %d", fd);
}
return ret;
}
int xfstatat(int dirfd, const char *pathname, struct stat *buf, int flags) {
int ret = fstatat(dirfd, pathname, buf, flags);
if (ret < 0) {
PLOGE("fstatat %s", pathname);
}
return ret;
}
int xdup(int fd) {
int ret = dup(fd);
if (ret < 0) {
PLOGE("dup");
}
return ret;
}
int xdup2(int oldfd, int newfd) {
int ret = dup2(oldfd, newfd);
if (ret < 0) {
PLOGE("dup2");
}
return ret;
}
int xdup3(int oldfd, int newfd, int flags) {
int ret = dup3(oldfd, newfd, flags);
if (ret < 0) {
PLOGE("dup3");
}
return ret;
}
ssize_t xreadlink(const char *pathname, char *buf, size_t bufsiz) { ssize_t xreadlink(const char *pathname, char *buf, size_t bufsiz) {
ssize_t ret = readlink(pathname, buf, bufsiz); ssize_t ret = readlink(pathname, buf, bufsiz);
if (ret < 0) { if (ret < 0) {
@ -301,72 +149,6 @@ int xfaccessat(int dirfd, const char *pathname) {
return ret; return ret;
} }
int xsymlink(const char *target, const char *linkpath) {
int ret = symlink(target, linkpath);
if (ret < 0) {
PLOGE("symlink %s->%s", target, linkpath);
}
return ret;
}
int xsymlinkat(const char *target, int newdirfd, const char *linkpath) {
int ret = symlinkat(target, newdirfd, linkpath);
if (ret < 0) {
PLOGE("symlinkat %s->%s", target, linkpath);
}
return ret;
}
int xlinkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags) {
int ret = linkat(olddirfd, oldpath, newdirfd, newpath, flags);
if (ret < 0) {
PLOGE("linkat %s->%s", oldpath, newpath);
}
return ret;
}
int xmount(const char *source, const char *target,
const char *filesystemtype, unsigned long mountflags,
const void *data) {
int ret = mount(source, target, filesystemtype, mountflags, data);
if (ret < 0) {
PLOGE("mount %s->%s", source, target);
}
return ret;
}
int xumount(const char *target) {
int ret = umount(target);
if (ret < 0) {
PLOGE("umount %s", target);
}
return ret;
}
int xumount2(const char *target, int flags) {
int ret = umount2(target, flags);
if (ret < 0) {
PLOGE("umount2 %s", target);
}
return ret;
}
int xrename(const char *oldpath, const char *newpath) {
int ret = rename(oldpath, newpath);
if (ret < 0) {
PLOGE("rename %s->%s", oldpath, newpath);
}
return ret;
}
int xmkdir(const char *pathname, mode_t mode) {
int ret = mkdir(pathname, mode);
if (ret < 0 && errno != EEXIST) {
PLOGE("mkdir %s %u", pathname, mode);
}
return ret;
}
int xmkdirs(const char *pathname, mode_t mode) { int xmkdirs(const char *pathname, mode_t mode) {
int ret = mkdirs(pathname, mode); int ret = mkdirs(pathname, mode);
if (ret < 0) { if (ret < 0) {
@ -375,14 +157,6 @@ int xmkdirs(const char *pathname, mode_t mode) {
return ret; return ret;
} }
int xmkdirat(int dirfd, const char *pathname, mode_t mode) {
int ret = mkdirat(dirfd, pathname, mode);
if (ret < 0 && errno != EEXIST) {
PLOGE("mkdirat %s %u", pathname, mode);
}
return ret;
}
void *xmmap(void *addr, size_t length, int prot, int flags, void *xmmap(void *addr, size_t length, int prot, int flags,
int fd, off_t offset) { int fd, off_t offset) {
void *ret = mmap(addr, length, prot, flags, fd, offset); void *ret = mmap(addr, length, prot, flags, fd, offset);
@ -401,14 +175,6 @@ ssize_t xsendfile(int out_fd, int in_fd, off_t *offset, size_t count) {
return ret; return ret;
} }
pid_t xfork() {
int ret = fork();
if (ret < 0) {
PLOGE("fork");
}
return ret;
}
int xpoll(struct pollfd *fds, nfds_t nfds, int timeout) { int xpoll(struct pollfd *fds, nfds_t nfds, int timeout) {
int ret = poll(fds, nfds, timeout); int ret = poll(fds, nfds, timeout);
if (ret < 0) { if (ret < 0) {
@ -417,14 +183,6 @@ int xpoll(struct pollfd *fds, nfds_t nfds, int timeout) {
return ret; return ret;
} }
int xinotify_init1(int flags) {
int ret = inotify_init1(flags);
if (ret < 0) {
PLOGE("inotify_init1");
}
return ret;
}
char *xrealpath(const char *path, char *resolved_path) { char *xrealpath(const char *path, char *resolved_path) {
char buf[PATH_MAX]; char buf[PATH_MAX];
char *ret = realpath(path, buf); char *ret = realpath(path, buf);
@ -435,11 +193,3 @@ char *xrealpath(const char *path, char *resolved_path) {
} }
return ret; return ret;
} }
int xmknod(const char *pathname, mode_t mode, dev_t dev) {
int ret = mknod(pathname, mode, dev);
if (ret < 0) {
PLOGE("mknod");
}
return ret;
}

View File

@ -31,6 +31,7 @@ ssize_t xrecvmsg(int sockfd, struct msghdr *msg, int flags);
int xpthread_create(pthread_t *thread, const pthread_attr_t *attr, int xpthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg); void *(*start_routine)(void *), void *arg);
int xaccess(const char *path, int mode); int xaccess(const char *path, int mode);
int xfaccessat(int dirfd, const char *pathname);
int xstat(const char *pathname, struct stat *buf); int xstat(const char *pathname, struct stat *buf);
int xlstat(const char *pathname, struct stat *buf); int xlstat(const char *pathname, struct stat *buf);
int xfstat(int fd, struct stat *buf); int xfstat(int fd, struct stat *buf);
@ -40,7 +41,6 @@ int xdup2(int oldfd, int newfd);
int xdup3(int oldfd, int newfd, int flags); int xdup3(int oldfd, int newfd, int flags);
ssize_t xreadlink(const char *pathname, char *buf, size_t bufsiz); ssize_t xreadlink(const char *pathname, char *buf, size_t bufsiz);
ssize_t xreadlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz); ssize_t xreadlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
int xfaccessat(int dirfd, const char *pathname);
int xsymlink(const char *target, const char *linkpath); int xsymlink(const char *target, const char *linkpath);
int xsymlinkat(const char *target, int newdirfd, const char *linkpath); int xsymlinkat(const char *target, int newdirfd, const char *linkpath);
int xlinkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags); int xlinkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags);
@ -57,7 +57,6 @@ void *xmmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset
ssize_t xsendfile(int out_fd, int in_fd, off_t *offset, size_t count); ssize_t xsendfile(int out_fd, int in_fd, off_t *offset, size_t count);
pid_t xfork(); pid_t xfork();
int xpoll(struct pollfd *fds, nfds_t nfds, int timeout); int xpoll(struct pollfd *fds, nfds_t nfds, int timeout);
int xinotify_init1(int flags);
char *xrealpath(const char *path, char *resolved_path); char *xrealpath(const char *path, char *resolved_path);
int xmknod(const char *pathname, mode_t mode, dev_t dev); int xmknod(const char *pathname, mode_t mode, dev_t dev);

View File

@ -1,4 +1,8 @@
use libc::{c_char, c_uint, mode_t}; use std::os::unix::io::RawFd;
use libc::{
c_char, c_uint, c_ulong, c_void, dev_t, mode_t, sockaddr, socklen_t, ssize_t, SYS_dup3,
};
use crate::{perror, ptr_to_str}; use crate::{perror, ptr_to_str};
@ -7,14 +11,14 @@ pub extern "C" fn xfopen(path: *const c_char, mode: *const c_char) -> *mut libc:
unsafe { unsafe {
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));
} }
return fp; return fp;
} }
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xfdopen(fd: i32, mode: *const c_char) -> *mut libc::FILE { pub extern "C" fn xfdopen(fd: RawFd, mode: *const c_char) -> *mut libc::FILE {
unsafe { unsafe {
let fp = libc::fdopen(fd, mode); let fp = libc::fdopen(fd, mode);
if fp.is_null() { if fp.is_null() {
@ -25,22 +29,22 @@ pub extern "C" fn xfdopen(fd: i32, mode: *const c_char) -> *mut libc::FILE {
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xopen(path: *const c_char, flags: i32, mode: mode_t) -> i32 { pub extern "C" fn xopen(path: *const c_char, flags: i32, mode: mode_t) -> RawFd {
unsafe { unsafe {
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));
} }
return r; return r;
} }
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn xopenat(dirfd: i32, path: *const c_char, flags: i32, mode: mode_t) -> i32 { pub extern "C" fn xopenat(dirfd: RawFd, path: *const c_char, flags: i32, mode: mode_t) -> RawFd {
unsafe { unsafe {
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));
} }
return r; return r;
} }
@ -55,3 +59,359 @@ macro_rules! xopen {
xopen($path, $flags, $mode) xopen($path, $flags, $mode)
}; };
} }
#[no_mangle]
pub extern "C" fn xsetns(fd: RawFd, nstype: i32) -> i32 {
unsafe {
let r = libc::setns(fd, nstype);
if r < 0 {
perror!("setns");
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xunshare(flags: i32) -> i32 {
unsafe {
let r = libc::unshare(flags);
if r < 0 {
perror!("unshare");
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xopendir(path: *const c_char) -> *mut libc::DIR {
unsafe {
let dp = libc::opendir(path);
if dp.is_null() {
perror!("opendir {}", ptr_to_str(path));
}
return dp;
}
}
#[no_mangle]
pub extern "C" fn xfdopendir(fd: RawFd) -> *mut libc::DIR {
unsafe {
let dp = libc::fdopendir(fd);
if dp.is_null() {
perror!("fdopendir");
}
return dp;
}
}
#[no_mangle]
pub extern "C" fn xsetsid() -> i32 {
unsafe {
let r = libc::setsid();
if r < 0 {
perror!("setsid");
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xsocket(domain: i32, ty: i32, protocol: i32) -> RawFd {
unsafe {
let fd = libc::socket(domain, ty, protocol);
if fd < 0 {
perror!("socket");
}
return fd;
}
}
#[no_mangle]
pub extern "C" fn xbind(socket: i32, address: *const sockaddr, len: socklen_t) -> i32 {
unsafe {
let r = libc::bind(socket, address, len);
if r < 0 {
perror!("bind");
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xlisten(socket: i32, backlog: i32) -> i32 {
unsafe {
let r = libc::listen(socket, backlog);
if r < 0 {
perror!("listen");
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xaccept4(
sockfd: RawFd,
addr: *mut sockaddr,
len: *mut socklen_t,
flg: i32,
) -> RawFd {
unsafe {
let fd = libc::accept4(sockfd, addr, len, flg);
if fd < 0 {
perror!("accept4");
}
return fd;
}
}
#[no_mangle]
pub extern "C" fn xsendmsg(fd: RawFd, msg: *const libc::msghdr, flags: i32) -> ssize_t {
unsafe {
let r = libc::sendmsg(fd, msg, flags);
if r < 0 {
perror!("sendmsg");
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xrecvmsg(fd: RawFd, msg: *mut libc::msghdr, flags: i32) -> ssize_t {
unsafe {
let r = libc::recvmsg(fd, msg, flags);
if r < 0 {
perror!("recvmsg");
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xaccess(path: *const c_char, mode: i32) -> i32 {
unsafe {
let r = libc::access(path, mode);
if r < 0 {
perror!("access {}", ptr_to_str(path));
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xstat(path: *const c_char, buf: *mut libc::stat) -> i32 {
unsafe {
let r = libc::stat(path, buf);
if r < 0 {
perror!("stat {}", ptr_to_str(path));
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xlstat(path: *const c_char, buf: *mut libc::stat) -> i32 {
unsafe {
let r = libc::lstat(path, buf);
if r < 0 {
perror!("lstat {}", ptr_to_str(path));
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xfstat(fd: RawFd, buf: *mut libc::stat) -> i32 {
unsafe {
let r = libc::fstat(fd, buf);
if r < 0 {
perror!("fstat");
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xfstatat(
dirfd: RawFd,
path: *const c_char,
buf: *mut libc::stat,
flags: i32,
) -> i32 {
unsafe {
let r = libc::fstatat(dirfd, path, buf, flags);
if r < 0 {
perror!("fstatat {}", ptr_to_str(path));
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xdup(oldfd: RawFd) -> RawFd {
unsafe {
let fd = libc::dup(oldfd);
if fd < 0 {
perror!("dup");
}
return fd;
}
}
#[no_mangle]
pub extern "C" fn xdup2(oldfd: RawFd, newfd: RawFd) -> RawFd {
unsafe {
let fd = libc::dup2(oldfd, newfd);
if fd < 0 {
perror!("dup2");
}
return fd;
}
}
#[no_mangle]
pub extern "C" fn xdup3(oldfd: RawFd, newfd: RawFd, flags: i32) -> RawFd {
unsafe {
let fd = libc::syscall(SYS_dup3, oldfd, newfd, flags) as RawFd;
if fd < 0 {
perror!("dup3");
}
return fd;
}
}
#[no_mangle]
pub extern "C" fn xsymlink(target: *const c_char, linkpath: *const c_char) -> i32 {
unsafe {
let r = libc::symlink(target, linkpath);
if r < 0 {
perror!("symlink {} -> {}", ptr_to_str(target), ptr_to_str(linkpath));
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xsymlinkat(target: *const c_char, dirfd: RawFd, linkpath: *const c_char) -> i32 {
unsafe {
let r = libc::symlinkat(target, dirfd, linkpath);
if r < 0 {
perror!(
"symlinkat {} -> {}",
ptr_to_str(target),
ptr_to_str(linkpath)
);
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xlinkat(
olddirfd: RawFd,
target: *const c_char,
newdirfd: RawFd,
linkpath: *const c_char,
flags: i32,
) -> i32 {
unsafe {
let r = libc::linkat(olddirfd, target, newdirfd, linkpath, flags);
if r < 0 {
perror!("linkat {} -> {}", ptr_to_str(target), ptr_to_str(linkpath));
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xmount(
src: *const c_char,
target: *const c_char,
fstype: *const c_char,
flags: c_ulong,
data: *const c_void,
) -> i32 {
unsafe {
let r = libc::mount(src, target, fstype, flags, data);
if r < 0 {
perror!("mount {} -> {}", ptr_to_str(src), ptr_to_str(target));
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xumount(target: *const c_char) -> i32 {
unsafe {
let r = libc::umount(target);
if r < 0 {
perror!("umount {}", ptr_to_str(target));
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xumount2(target: *const c_char, flags: i32) -> i32 {
unsafe {
let r = libc::umount2(target, flags);
if r < 0 {
perror!("umount2 {}", ptr_to_str(target));
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xrename(oldname: *const c_char, newname: *const c_char) -> i32 {
unsafe {
let r = libc::rename(oldname, newname);
if r < 0 {
perror!("rename {} -> {}", ptr_to_str(oldname), ptr_to_str(newname));
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xmkdir(path: *const c_char, mode: mode_t) -> i32 {
unsafe {
let r = libc::mkdir(path, mode);
if r < 0 {
perror!("mkdir {}", ptr_to_str(path));
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xmkdirat(dirfd: RawFd, path: *const c_char, mode: mode_t) -> i32 {
unsafe {
let r = libc::mkdirat(dirfd, path, mode);
if r < 0 {
perror!("mkdirat {}", ptr_to_str(path));
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xfork() -> i32 {
unsafe {
let r = libc::fork();
if r < 0 {
perror!("fork");
}
return r;
}
}
#[no_mangle]
pub extern "C" fn xmknod(pathname: *const c_char, mode: mode_t, dev: dev_t) -> i32 {
unsafe {
let r = libc::mknod(pathname, mode, dev);
if r < 0 {
perror!("mknod {}", ptr_to_str(pathname));
}
return r;
}
}