2017-04-04 19:44:13 +00:00
|
|
|
/* xwrap.c - wrappers around existing library functions.
|
|
|
|
*
|
|
|
|
* Functions with the x prefix are wrappers that either succeed or kill the
|
|
|
|
* program with an error message, but never return failure. They usually have
|
|
|
|
* the same arguments and return value as the function they wrap.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-04-05 22:12:29 +00:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <sched.h>
|
2017-04-04 19:44:13 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-04-05 22:12:29 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2017-04-06 22:21:20 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
2017-04-07 23:37:43 +00:00
|
|
|
#include <sys/socket.h>
|
2017-04-05 22:12:29 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2017-04-07 23:37:43 +00:00
|
|
|
#include <selinux/selinux.h>
|
2017-04-04 19:44:13 +00:00
|
|
|
|
2017-04-07 23:37:43 +00:00
|
|
|
#include "magisk.h"
|
2017-04-04 19:44:13 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
FILE *xfopen(const char *pathname, const char *mode) {
|
|
|
|
FILE *fp = fopen(pathname, mode);
|
|
|
|
if (fp == NULL) {
|
2017-04-07 23:37:43 +00:00
|
|
|
PLOGE("fopen: %s", pathname);
|
2017-04-04 19:44:13 +00:00
|
|
|
}
|
|
|
|
return fp;
|
2017-04-05 22:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int xopen(const char *pathname, int flags) {
|
|
|
|
int fd = open(pathname, flags);
|
|
|
|
if (fd < 0) {
|
2017-04-07 23:37:43 +00:00
|
|
|
PLOGE("open: %s", pathname);
|
2017-04-05 22:12:29 +00:00
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t xwrite(int fd, const void *buf, size_t count) {
|
2017-04-08 23:25:10 +00:00
|
|
|
int ret = write(fd, buf, count);
|
|
|
|
if (count != ret) {
|
2017-04-05 22:12:29 +00:00
|
|
|
PLOGE("write");
|
|
|
|
}
|
2017-04-08 23:25:10 +00:00
|
|
|
return ret;
|
2017-04-05 22:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read error other than EOF
|
|
|
|
ssize_t xread(int fd, void *buf, size_t count) {
|
|
|
|
int ret = read(fd, buf, count);
|
|
|
|
if (ret < 0) {
|
|
|
|
PLOGE("read");
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read exact same size as count
|
|
|
|
ssize_t xxread(int fd, void *buf, size_t count) {
|
2017-04-08 23:25:10 +00:00
|
|
|
int ret = read(fd, buf, count);
|
|
|
|
if (count != ret) {
|
2017-04-05 22:12:29 +00:00
|
|
|
PLOGE("read");
|
|
|
|
}
|
2017-04-08 23:25:10 +00:00
|
|
|
return ret;
|
2017-04-05 22:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int xpipe(int pipefd[2]) {
|
2017-04-08 23:25:10 +00:00
|
|
|
int ret = pipe(pipefd);
|
|
|
|
if (ret == -1) {
|
2017-04-05 22:12:29 +00:00
|
|
|
PLOGE("pipe");
|
|
|
|
}
|
2017-04-08 23:25:10 +00:00
|
|
|
return ret;
|
2017-04-05 22:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int xsetns(int fd, int nstype) {
|
2017-04-08 23:25:10 +00:00
|
|
|
int ret = setns(fd, nstype);
|
|
|
|
if (ret == -1) {
|
2017-04-05 22:12:29 +00:00
|
|
|
PLOGE("setns");
|
|
|
|
}
|
2017-04-08 23:25:10 +00:00
|
|
|
return ret;
|
2017-04-05 22:12:29 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 22:21:20 +00:00
|
|
|
DIR *xopendir(const char *name) {
|
|
|
|
DIR *d = opendir(name);
|
|
|
|
if (d == NULL) {
|
2017-04-07 23:37:43 +00:00
|
|
|
PLOGE("opendir: %s", name);
|
2017-04-06 22:21:20 +00:00
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dirent *xreaddir(DIR *dirp) {
|
|
|
|
errno = 0;
|
|
|
|
struct dirent *e = readdir(dirp);
|
|
|
|
if (errno && e == NULL) {
|
|
|
|
PLOGE("readdir");
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2017-04-07 23:37:43 +00:00
|
|
|
pid_t xsetsid() {
|
|
|
|
pid_t pid = setsid();
|
|
|
|
if (pid == -1) {
|
|
|
|
PLOGE("setsid");
|
|
|
|
}
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
int xsetcon(char *context) {
|
2017-04-08 23:25:10 +00:00
|
|
|
int ret = setcon(context);
|
|
|
|
if (ret == -1) {
|
2017-04-07 23:37:43 +00:00
|
|
|
PLOGE("setcon: %s", context);
|
|
|
|
}
|
2017-04-08 23:25:10 +00:00
|
|
|
return ret;
|
2017-04-07 23:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int xsocket(int domain, int type, int protocol) {
|
|
|
|
int fd = socket(domain, type, protocol);
|
|
|
|
if (fd == -1) {
|
|
|
|
PLOGE("socket");
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int xbind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
|
2017-04-08 23:25:10 +00:00
|
|
|
int ret = bind(sockfd, addr, addrlen);
|
|
|
|
if (ret == -1) {
|
2017-04-07 23:37:43 +00:00
|
|
|
PLOGE("bind");
|
|
|
|
}
|
2017-04-08 23:25:10 +00:00
|
|
|
return ret;
|
2017-04-07 23:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int xconnect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
|
2017-04-08 23:25:10 +00:00
|
|
|
int ret = connect(sockfd, addr, addrlen);
|
|
|
|
if (ret == -1) {
|
2017-04-07 23:37:43 +00:00
|
|
|
PLOGE("bind");
|
|
|
|
}
|
2017-04-08 23:25:10 +00:00
|
|
|
return ret;
|
2017-04-07 23:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int xlisten(int sockfd, int backlog) {
|
2017-04-08 23:25:10 +00:00
|
|
|
int ret = listen(sockfd, backlog);
|
|
|
|
if (ret == -1) {
|
2017-04-07 23:37:43 +00:00
|
|
|
PLOGE("listen");
|
|
|
|
}
|
2017-04-08 23:25:10 +00:00
|
|
|
return ret;
|
2017-04-07 23:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int xaccept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
|
|
|
|
int fd = accept(sockfd, addr, addrlen);
|
|
|
|
if (fd == -1) {
|
|
|
|
PLOGE("accept");
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *xmalloc(size_t size) {
|
|
|
|
void *p = malloc(size);
|
|
|
|
if (p == NULL) {
|
|
|
|
PLOGE("malloc");
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *xcalloc(size_t nmemb, size_t size) {
|
|
|
|
void *p = calloc(nmemb, size);
|
|
|
|
if (p == NULL) {
|
|
|
|
PLOGE("calloc");
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *xrealloc(void *ptr, size_t size) {
|
|
|
|
void *p = realloc(ptr, size);
|
|
|
|
if (p == NULL) {
|
|
|
|
PLOGE("realloc");
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t xsendmsg(int sockfd, const struct msghdr *msg, int flags) {
|
|
|
|
int sent = sendmsg(sockfd, msg, flags);
|
|
|
|
if (sent == -1) {
|
|
|
|
PLOGE("sendmsg");
|
|
|
|
}
|
|
|
|
return sent;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t xrecvmsg(int sockfd, struct msghdr *msg, int flags) {
|
|
|
|
int rec = recvmsg(sockfd, msg, flags);
|
|
|
|
if (rec == -1) {
|
|
|
|
PLOGE("recvmsg");
|
|
|
|
}
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
2017-04-08 23:25:10 +00:00
|
|
|
int xpthread_create(pthread_t *thread, const pthread_attr_t *attr,
|
|
|
|
void *(*start_routine) (void *), void *arg) {
|
|
|
|
errno = pthread_create(thread, attr, start_routine, arg);
|
|
|
|
if (errno) {
|
|
|
|
PLOGE("pthread_create");
|
|
|
|
}
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
int xsocketpair(int domain, int type, int protocol, int sv[2]) {
|
|
|
|
int ret = socketpair(domain, type, protocol, sv);
|
|
|
|
if (ret == -1) {
|
|
|
|
PLOGE("socketpair");
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-07 23:37:43 +00:00
|
|
|
|