Magisk/jni/utils/xwrap.c

75 lines
1.4 KiB
C
Raw Normal View History

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>
#include <sys/types.h>
#include <sys/stat.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) {
PLOGE("fopen");
}
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) {
PLOGE("open");
}
return fd;
}
ssize_t xwrite(int fd, const void *buf, size_t count) {
if (count != write(fd, buf, count)) {
PLOGE("write");
}
return count;
}
// 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) {
if (count != read(fd, buf, count)) {
PLOGE("read");
}
return count;
}
int xpipe(int pipefd[2]) {
if (pipe(pipefd) == -1) {
PLOGE("pipe");
exit(1);
}
return 0;
}
int xsetns(int fd, int nstype) {
if (setns(fd, nstype) == -1) {
PLOGE("setns");
}
return 0;
}