mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-08-25 15:37:46 +00:00
Support compiling against lower SDK
Reduce even more size for static binaries
This commit is contained in:
@@ -4,7 +4,8 @@ APP_CFLAGS := -std=gnu99 ${MAGISK_DEBUG} \
|
||||
APP_CPPFLAGS := -std=c++11
|
||||
APP_SHORT_COMMANDS := true
|
||||
ifdef OLD_PLAT
|
||||
APP_PLATFORM := android-9
|
||||
APP_PLATFORM := android-16
|
||||
APP_CFLAGS += -Wno-implicit-function-declaration
|
||||
else
|
||||
APP_PLATFORM := android-21
|
||||
endif
|
||||
|
@@ -579,7 +579,7 @@ void startup() {
|
||||
while((entry = xreaddir(dir))) {
|
||||
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
|
||||
snprintf(buf, PATH_MAX, "/root/%s", entry->d_name);
|
||||
symlinkat(buf, sbin, entry->d_name);
|
||||
xsymlinkat(buf, sbin, entry->d_name);
|
||||
}
|
||||
|
||||
close(sbin);
|
||||
|
@@ -382,9 +382,9 @@ int main(int argc, char *argv[]) {
|
||||
mknod("/null", S_IFCHR | 0666, makedev(1, 3));
|
||||
int null = open("/null", O_RDWR | O_CLOEXEC);
|
||||
unlink("/null");
|
||||
dup3(null, STDIN_FILENO, O_CLOEXEC);
|
||||
dup3(null, STDOUT_FILENO, O_CLOEXEC);
|
||||
dup3(null, STDERR_FILENO, O_CLOEXEC);
|
||||
xdup3(null, STDIN_FILENO, O_CLOEXEC);
|
||||
xdup3(null, STDOUT_FILENO, O_CLOEXEC);
|
||||
xdup3(null, STDERR_FILENO, O_CLOEXEC);
|
||||
if (null > STDERR_FILENO)
|
||||
close(null);
|
||||
|
||||
|
@@ -19,6 +19,13 @@
|
||||
|
||||
// xwrap.c
|
||||
|
||||
#ifndef SOCK_CLOEXEC
|
||||
#define SOCK_CLOEXEC O_CLOEXEC
|
||||
#endif
|
||||
#ifndef SOCK_NONBLOCK
|
||||
#define SOCK_NONBLOCK O_NONBLOCK
|
||||
#endif
|
||||
|
||||
FILE *xfopen(const char *pathname, const char *mode);
|
||||
FILE *xfdopen(int fd, const char *mode);
|
||||
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
|
||||
@@ -51,9 +58,12 @@ int xsocketpair(int domain, int type, int protocol, int sv[2]);
|
||||
int xstat(const char *pathname, struct stat *buf);
|
||||
int xlstat(const char *pathname, struct stat *buf);
|
||||
int xdup2(int oldfd, int newfd);
|
||||
int xdup3(int oldfd, int newfd, int flags);
|
||||
ssize_t xreadlink(const char *pathname, char *buf, size_t bufsiz);
|
||||
ssize_t xreadlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
|
||||
int xsymlink(const char *target, 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 xmount(const char *source, const char *target,
|
||||
const char *filesystemtype, unsigned long mountflags,
|
||||
const void *data);
|
||||
@@ -105,7 +115,6 @@ struct file_attr {
|
||||
char con[128];
|
||||
};
|
||||
|
||||
int fd_getpath(int fd, char *path, size_t size);
|
||||
int mkdirs(const char *pathname, mode_t mode);
|
||||
void in_order_walk(int dirfd, void (*callback)(int, struct dirent*));
|
||||
void rm_rf(const char *path);
|
||||
|
@@ -28,11 +28,16 @@ static int is_excl(const char *name) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fd_getpath(int fd, char *path, size_t size) {
|
||||
static int fd_getpath(int fd, char *path, size_t size) {
|
||||
snprintf(path, size, "/proc/self/fd/%d", fd);
|
||||
if (xreadlink(path, path, size) == -1)
|
||||
return -1;
|
||||
return 0;
|
||||
return xreadlink(path, path, size) == -1;
|
||||
}
|
||||
|
||||
static int fd_getpathat(int dirfd, const char *name, char *path, size_t size) {
|
||||
if (fd_getpath(dirfd, path, size))
|
||||
return 1;
|
||||
snprintf(path, size, "%s/%s", path, name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mkdirs(const char *pathname, mode_t mode) {
|
||||
@@ -220,7 +225,7 @@ void clone_dir(int src, int dest) {
|
||||
break;
|
||||
case DT_LNK:
|
||||
xreadlinkat(src, entry->d_name, buf, sizeof(buf));
|
||||
symlinkat(buf, dest, entry->d_name);
|
||||
xsymlinkat(buf, dest, entry->d_name);
|
||||
setattrat(dest, entry->d_name, &a);
|
||||
break;
|
||||
}
|
||||
@@ -249,7 +254,7 @@ void link_dir(int src, int dest) {
|
||||
close(newsrc);
|
||||
close(newdest);
|
||||
} else {
|
||||
linkat(src, entry->d_name, dest, entry->d_name, 0);
|
||||
xlinkat(src, entry->d_name, dest, entry->d_name, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -270,12 +275,9 @@ int getattr(const char *path, struct file_attr *a) {
|
||||
}
|
||||
|
||||
int getattrat(int dirfd, const char *pathname, struct file_attr *a) {
|
||||
int fd = xopenat(dirfd, pathname, O_PATH | O_NOFOLLOW | O_CLOEXEC);
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
int ret = fgetattr(fd, a);
|
||||
close(fd);
|
||||
return ret;
|
||||
char path[PATH_MAX];
|
||||
fd_getpathat(dirfd, pathname, path, sizeof(path));
|
||||
return getattr(path, a);
|
||||
}
|
||||
|
||||
int fgetattr(int fd, struct file_attr *a) {
|
||||
@@ -304,12 +306,9 @@ int setattr(const char *path, struct file_attr *a) {
|
||||
}
|
||||
|
||||
int setattrat(int dirfd, const char *pathname, struct file_attr *a) {
|
||||
int fd = xopenat(dirfd, pathname, O_PATH | O_NOFOLLOW | O_CLOEXEC);
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
int ret = fsetattr(fd, a);
|
||||
close(fd);
|
||||
return ret;
|
||||
char path[PATH_MAX];
|
||||
fd_getpathat(dirfd, pathname, path, sizeof(path));
|
||||
return setattr(path, a);
|
||||
}
|
||||
|
||||
int fsetattr(int fd, struct file_attr *a) {
|
||||
|
@@ -8,7 +8,7 @@
|
||||
#include <sys/wait.h>
|
||||
#include <sys/mount.h>
|
||||
#include <sys/sendfile.h>
|
||||
#include <sys/statvfs.h>
|
||||
#include <sys/statfs.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include <linux/loop.h>
|
||||
|
||||
@@ -70,12 +70,12 @@ static char *loopsetup(const char *img) {
|
||||
|
||||
static void check_filesystem(struct fs_info *info, const char *img, const char *mount) {
|
||||
struct stat st;
|
||||
struct statvfs vfs;
|
||||
struct statfs fs;
|
||||
stat(img, &st);
|
||||
statvfs(mount, &vfs);
|
||||
statfs(mount, &fs);
|
||||
info->size = st.st_size / 1048576;
|
||||
info->free = vfs.f_bfree * vfs.f_frsize / 1048576;
|
||||
info->used = (vfs.f_blocks - vfs.f_bfree) * vfs.f_frsize / 1048576;
|
||||
info->free = fs.f_bfree * (uint64_t)fs.f_frsize / 1048576;
|
||||
info->used = (fs.f_blocks - fs.f_bfree) * (uint64_t)fs.f_frsize / 1048576;
|
||||
}
|
||||
|
||||
static void usage() {
|
||||
|
@@ -21,6 +21,7 @@
|
||||
#include <sys/mount.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/sendfile.h>
|
||||
#include <sys/syscall.h>
|
||||
|
||||
#include "logging.h"
|
||||
#include "utils.h"
|
||||
@@ -100,7 +101,7 @@ int xpipe2(int pipefd[2], int flags) {
|
||||
}
|
||||
|
||||
int xsetns(int fd, int nstype) {
|
||||
int ret = setns(fd, nstype);
|
||||
int ret = (int) syscall(__NR_setns, fd, nstype);
|
||||
if (ret == -1) {
|
||||
PLOGE("setns");
|
||||
}
|
||||
@@ -165,9 +166,14 @@ int xlisten(int sockfd, int backlog) {
|
||||
}
|
||||
|
||||
int xaccept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) {
|
||||
int fd = accept4(sockfd, addr, addrlen, flags);
|
||||
#ifndef __NR_accept4
|
||||
#ifdef __i386__
|
||||
#define __NR_accept4 364
|
||||
#endif
|
||||
#endif
|
||||
int fd = (int) syscall(__NR_accept4, sockfd, addr, addrlen, flags);
|
||||
if (fd == -1) {
|
||||
PLOGE("accept");
|
||||
PLOGE("accept4");
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
@@ -212,8 +218,8 @@ ssize_t xrecvmsg(int sockfd, struct msghdr *msg, int flags) {
|
||||
return rec;
|
||||
}
|
||||
|
||||
int xpthread_create(pthread_t *thread, const pthread_attr_t *attr,
|
||||
void *(*start_routine) (void *), void *arg) {
|
||||
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");
|
||||
@@ -245,6 +251,14 @@ int xdup2(int oldfd, int newfd) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
int xdup3(int oldfd, int newfd, int flags) {
|
||||
int ret = (int) syscall(__NR_dup3, oldfd, newfd, flags);
|
||||
if (ret == -1) {
|
||||
PLOGE("dup3");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t xreadlink(const char *pathname, char *buf, size_t bufsiz) {
|
||||
ssize_t ret = readlink(pathname, buf, bufsiz);
|
||||
if (ret == -1) {
|
||||
@@ -256,7 +270,7 @@ 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 ret = readlinkat(dirfd, pathname, buf, bufsiz);
|
||||
ssize_t ret = syscall(__NR_readlinkat, dirfd, pathname, buf, bufsiz);
|
||||
if (ret == -1) {
|
||||
PLOGE("readlinkat %s", pathname);
|
||||
} else {
|
||||
@@ -273,10 +287,26 @@ int xsymlink(const char *target, const char *linkpath) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
int xsymlinkat(const char *target, int newdirfd, const char *linkpath) {
|
||||
int ret = (int) syscall(__NR_symlinkat, target, newdirfd, linkpath);
|
||||
if (ret == -1) {
|
||||
PLOGE("symlinkat %s->%s", target, linkpath);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int xlinkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags) {
|
||||
int ret = (int) syscall(__NR_linkat, olddirfd, oldpath, newdirfd, newpath, flags);
|
||||
if (ret == -1) {
|
||||
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, MS_SILENT | mountflags, data);
|
||||
int ret = mount(source, target, filesystemtype, mountflags, data);
|
||||
if (ret == -1) {
|
||||
PLOGE("mount %s->%s", source, target);
|
||||
}
|
||||
|
Reference in New Issue
Block a user