Add magisk daemon

This commit is contained in:
topjohnwu
2017-04-08 07:37:43 +08:00
parent a223f6056e
commit 054a1e5ea4
10 changed files with 394 additions and 18 deletions

View File

@ -14,15 +14,18 @@
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <selinux/selinux.h>
#include "magisk.h"
#include "utils.h"
FILE *xfopen(const char *pathname, const char *mode) {
FILE *fp = fopen(pathname, mode);
if (fp == NULL) {
PLOGE("fopen");
PLOGE("fopen: %s", pathname);
}
return fp;
}
@ -30,7 +33,7 @@ FILE *xfopen(const char *pathname, const char *mode) {
int xopen(const char *pathname, int flags) {
int fd = open(pathname, flags);
if (fd < 0) {
PLOGE("open");
PLOGE("open: %s", pathname);
}
return fd;
}
@ -76,7 +79,7 @@ int xsetns(int fd, int nstype) {
DIR *xopendir(const char *name) {
DIR *d = opendir(name);
if (d == NULL) {
PLOGE("opendir");
PLOGE("opendir: %s", name);
}
return d;
}
@ -90,3 +93,96 @@ struct dirent *xreaddir(DIR *dirp) {
return e;
}
pid_t xsetsid() {
pid_t pid = setsid();
if (pid == -1) {
PLOGE("setsid");
}
return pid;
}
int xsetcon(char *context) {
if (setcon(context) == -1) {
PLOGE("setcon: %s", context);
}
return 0;
}
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) {
if (bind(sockfd, addr, addrlen) == -1) {
PLOGE("bind");
}
return 0;
}
int xconnect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
if (connect(sockfd, addr, addrlen) == -1) {
PLOGE("bind");
}
return 0;
}
int xlisten(int sockfd, int backlog) {
if (listen(sockfd, backlog) == -1) {
PLOGE("listen");
}
return 0;
}
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;
}