mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-12-03 04:41:53 +00:00
Support compiling against lower SDK
Reduce even more size for static binaries
This commit is contained in:
@@ -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