Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
Separate out write status to file
Browse files Browse the repository at this point in the history
  • Loading branch information
RikkaW committed Nov 24, 2020
1 parent f902cd4 commit 2c2e49b
Show file tree
Hide file tree
Showing 13 changed files with 753 additions and 195 deletions.
31 changes: 31 additions & 0 deletions flatbuffers/status.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Status;

table Core {
api:uint8;
version:uint32;
version_name:string (required);
hide:bool;
}

table Module {
name:string (required);
api:uint8;
version:uint32;
version_name:string (required);
hide:bool;
}

table JNIMethod {
name:string (required);
signature:string (required);
replaced:bool;
}

table FbStatus {
is_64bit:bool;
core:Core;
modules:[Module];
jni_methods:[JNIMethod];
}

root_type FbStatus;
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ org.gradle.jvmargs=-Xmx1536m
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
# https://github.com/google/prefab/issues/122
android.prefabVersion=1.1.2
7 changes: 4 additions & 3 deletions riru/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def minApiVersion = rootProject.ext.riruMinApiVersion
def moduleProp = [
id : "riru-core",
name : "Riru",
version : "v22.0",
versionCode: "41",
version : "v22.1",
versionCode: "42",
author : "Rikka",
description: "Inject into zygote process. Required by all \"Riru\" modules."
]
Expand Down Expand Up @@ -63,6 +63,7 @@ repositories {
dependencies {
implementation project(':riru-api')
implementation 'rikka.ndk.thirdparty:xhook:1.2.0'
implementation 'rikka.ndk.thirdparty:flatbuffers:1.12.0'

implementation 'androidx.annotation:annotation:1.1.0'
compileOnly project(':stub')
Expand Down Expand Up @@ -134,7 +135,7 @@ android.applicationVariants.all { variant ->

// copy dex
copy {
from zipTree(file(outputFile)).matching { include 'classes.dex' }.singleFile
from zipTree(file(outputFile)).matching { include 'classes*.dex' }.singleFile
into magiskDir
}

Expand Down
8 changes: 5 additions & 3 deletions riru/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${LINKER_FLAGS}")

find_package(xhook REQUIRED CONFIG)
find_package(riru-api REQUIRED CONFIG)
find_package(flatbuffers REQUIRED CONFIG)

include_directories(include)
include_directories(include_generated)

add_library(utils STATIC
util/misc.cpp
Expand All @@ -59,8 +61,8 @@ add_library(riru SHARED
native_method.cpp
hide/hide_utils.cpp
status.cpp
module.cpp)

module.cpp
daemon/status_writter.cpp)
target_include_directories(riru PRIVATE ${CMAKE_SOURCE_DIR})
target_link_libraries(riru log utils xhook::xhook flatbuffers::flatbuffers riru-api::riru-api)

Expand All @@ -70,7 +72,7 @@ target_link_libraries(riruhide log utils)

if ("${ANDROID_ABI}" STREQUAL "x86" OR "${ANDROID_ABI}" STREQUAL "x86_64")
add_definitions(-DHAS_NATIVE_BRIDGE)
endif()
endif ()

add_library(riruloader SHARED loader/loader.cpp)
target_link_libraries(riruloader log utils)
197 changes: 197 additions & 0 deletions riru/src/main/cpp/daemon/status_writter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <climits>
#include <cstdio>
#include <sys/system_properties.h>
#include <unistd.h>
#include <cstdarg>
#include <malloc.h>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include "status_writter.h"
#include "logging.h"
#include "misc.h"
#include "config.h"

#define TMP_DIR "/dev"
#define DEV_RANDOM64 CONFIG_DIR "/dev_random64"
#define DEV_RANDOM CONFIG_DIR "/dev_random"

static const char *getRandomName(bool is_64bit) {
static char *name = nullptr;
if (name != nullptr) return name;

size_t size = 7;
auto tmp = static_cast<char *>(malloc(size + 1));
const char *file = is_64bit ? DEV_RANDOM64 : DEV_RANDOM;
const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

int fd = open(file, O_RDONLY);
if (fd != -1) {
if (0 == read_full(fd, tmp, size)) {
tmp[size] = '\0';
close(fd);
name = tmp;
return name;
}
close(fd);

LOGE("bad %s", DEV_RANDOM);
}

srand(time(nullptr));
for (size_t n = 0; n < size; n++) {
auto key = rand() % (sizeof charset - 1);
tmp[n] = charset[key];
}
tmp[size] = '\0';

fd = open(file, O_CREAT | O_WRONLY | O_TRUNC, 0700);
if (fd == -1) {
PLOGE("open %s", file);
return nullptr;
}
write_full(fd, tmp, size);
close(fd);

name = tmp;
return name;
}

static int OpenFile(bool is_64bit, const char *name, ...) {
auto random_name = getRandomName(is_64bit);
if (random_name == nullptr) {
LOGE("unable to get random name");
return -1;
}
LOGD("random name is %s", random_name);

const char *filename, *next;
char dir[PATH_MAX];

strcpy(dir, TMP_DIR);
#ifdef __LP64__
strcat(dir, "/riru64_");
#else
strcat(dir, "/riru_");
#endif
strcat(dir, random_name);


va_list va;
va_start(va, name);
while (true) {
next = va_arg(va, const char *);
if (next == nullptr) {
filename = name;
break;
}
strcat(dir, "/");
strcat(dir, name);
name = next;
}
va_end(va);

LOGD("open %s/%s", dir, filename);

int dir_fd = open(dir, O_DIRECTORY);
if (dir_fd == -1 && mkdirs(dir, 0700) == 0) {
dir_fd = open(dir, O_DIRECTORY);
}
if (dir_fd == -1) {
PLOGE("cannot open dir %s", dir);
return -1;
}

int fd = openat(dir_fd, filename, O_CREAT | O_WRONLY | O_TRUNC, 0700);
if (fd < 0) {
PLOGE("unable to create/open %s", name);
}

close(dir_fd);
return fd;
}

#define OpenFile(...) OpenFile(status->is_64bit(), __VA_ARGS__, nullptr)

static void WriteCore(const Status::FbStatus *status) {
auto core = status->core();
if (!core) return;

char buf[1024];
int fd;

if ((fd = OpenFile("api")) != -1) {
write_full(fd, buf, sprintf(buf, "%d", core->api()));
close(fd);
}

if ((fd = OpenFile("version")) != -1) {
write_full(fd, buf, sprintf(buf, "%d", core->version()));
close(fd);
}

if ((fd = OpenFile("version_name")) != -1) {
write_full(fd, buf, sprintf(buf, "%s", core->version_name()->c_str()));
close(fd);
}

if ((fd = OpenFile("hide")) != -1) {
write_full(fd, buf, sprintf(buf, "%s", core->hide() ? "true" : "false"));
close(fd);
}
}

static void WriteModules(const Status::FbStatus *status) {
auto modules = status->modules();
if (!modules) return;

char buf[1024];
int fd;

for (auto module : *modules) {
auto name = module->name()->c_str();

if ((fd = OpenFile("modules", name, "hide")) != -1) {
write_full(fd, buf, sprintf(buf, "%s", module->hide() ? "true" : "false"));
close(fd);
}

if ((fd = OpenFile("modules", name, "api")) != -1) {
write_full(fd, buf, sprintf(buf, "%d", module->api()));
close(fd);
}

if ((fd = OpenFile("modules", name, "version")) != -1) {
write_full(fd, buf, sprintf(buf, "%d", module->version()));
close(fd);
}

if ((fd = OpenFile("modules", name, "version_name")) != -1) {
write_full(fd, buf, sprintf(buf, "%s", module->version_name()->c_str()));
close(fd);
}
}
}

static void WriteJNIMethods(const Status::FbStatus *status) {
auto methods = status->jni_methods();
if (!methods) return;

char buf[1024];
int fd;

for (auto method : *methods) {
if ((fd = OpenFile("methods", method->name()->c_str())) != -1) {
write_full(fd, buf, sprintf(buf, "%s\n%s", method->replaced() ? "true" : "false", method->signature()->c_str()));
close(fd);
}
}
}

void Status::WriteToFile(const FbStatus *status) {
WriteCore(status);
WriteModules(status);
WriteJNIMethods(status);
}
8 changes: 8 additions & 0 deletions riru/src/main/cpp/daemon/status_writter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <status_generated.h>

namespace Status {

void WriteToFile(const FbStatus *status);
}
Loading

0 comments on commit 2c2e49b

Please sign in to comment.