Skip to content
This repository has been archived by the owner on Apr 5, 2023. It is now read-only.

Commit

Permalink
Add Zygisk module files
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohnwu committed Oct 24, 2021
1 parent 667a1ac commit 82f2bc2
Show file tree
Hide file tree
Showing 7 changed files with 414 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "libcxx"]
path = module/jni/libcxx
url = https://github.com/topjohnwu/libcxx.git
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Developing Zygisk Modules

This repository hosts a template zygisk module for developers to start developing Zygisk modules. Before developing Zygisk modules, you should first check out the official documentation for [Magisk Modules](https://topjohnwu.github.io/Magisk/guides.html). Do not fork this repository for your new module; either manually clone this repository, or press the "Use this template" button in the GitHub UI.

This repository is archived because it is meant to be read-only; the project is not abandoned. For any issues, please report them to the main Magisk repository.

## API

- The canonical URL of the public Zygisk API is [module/jni/zygisk.hpp](https://github.com/topjohnwu/zygisk-module-sample/blob/master/module/jni/zygisk.hpp).
- The header file is self documented; directly refer to the header source code for all Zygisk API details.
- Magisk is committed to maintain backwards compatibility forever. That is, whenever there is an API update for Zygisk in a newer Magisk version, Magisk can always load Zygisk modules built for an older Zygisk API.

## Notes

- This repository can be opened with Android Studio.
- Developing Zygisk modules requires a modern C++ compiler. Please use NDK r21 or higher.
- All the C++ code is in the [module/jni](https://github.com/topjohnwu/zygisk-module-sample/tree/master/module/jni) folder.
- DO NOT modify the default configurations in `Application.mk` unless you know what you are doing.

## C++ STL

- The `APP_STL` variable in `Application.mk` is set to `none`. **DO NOT** use any C++ STL included in NDK.
- If you'd like to use C++ STL, you **have to** use the `libcxx` included as a git submodule in this repository. Zygisk modules' code are injected into Zygote, and the included `libc++` is setup to be lightweight and fully self contained that prevents conflicts with the hosting program.
- If you do not need STL, link to the system `libstdc++` so that you can at least call the `new` operator.
- Both configurations are demonstrated in the example `Android.mk`.

## Building

- In the `module` folder, call [`ndk-build`](https://developer.android.com/ndk/guides/ndk-build) to compile your modules.
- Your module libraries will be in `libs/<abi>/lib<module_name>.so`.
- Copy the libraries into your module's `zygisk` folder, with the ABI as it's file name:

```
module_id
├── module.prop
└── zygisk
   ├── arm64-v8a.so
   ├── armeabi-v7a.so
   ├── x86.so
   └── x86_64.so
```

## License

Although the main Magisk project is licensed under GPLv3, the Zygisk API and its headers are not. Every file in this repository is released to the public domain, so you don't have to worry about any licensing issues while developing Zygisk modules. You can create a closed source module, or publish your source code under any open source license you prefer; there is no restrictions at all.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.2"
classpath "com.android.tools.build:gradle:7.0.3"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
14 changes: 12 additions & 2 deletions module/jni/Android.mk
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := example
LOCAL_SRC_FILES := example.cpp
LOCAL_STATIC_LIBRARIES := libcxx
LOCAL_LDLIBS := -llog

include $(BUILD_SHARED_LIBRARY)

include jni/libcxx/Android.mk

# If you do not want to use libc++, link to system stdc++
# so that you can at least call the new operator in your code

# include $(CLEAR_VARS)
# LOCAL_MODULE := example
# LOCAL_SRC_FILES := example.cpp
# LOCAL_LDLIBS := -llog -lstdc++
# include $(BUILD_SHARED_LIBRARY)
58 changes: 58 additions & 0 deletions module/jni/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <cstdlib>
#include <unistd.h>
#include <fcntl.h>
#include <android/log.h>

#include "zygisk.hpp"

using zygisk::Api;
using zygisk::AppSpecializeArgs;
using zygisk::ServerSpecializeArgs;

#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, "Magisk", __VA_ARGS__)

class MyModule : public zygisk::ModuleBase {
public:
void onLoad(Api *api, JNIEnv *env) override {
this->api = api;
this->env = env;
}

void preAppSpecialize(AppSpecializeArgs *args) override {
// Use JNI to fetch our process name
const char *process = env->GetStringUTFChars(args->nice_name, nullptr);
preSpecialize(process);
env->ReleaseStringUTFChars(args->nice_name, process);
}

void preServerSpecialize(ServerSpecializeArgs *args) override {
preSpecialize("system_server");
}

private:
Api *api;
JNIEnv *env;

void preSpecialize(const char *process) {
// Demonstrate connecting to to companion process
// We ask the companion for a random number
int r = 0;
int fd = api->connectCompanion();
read(fd, &r, sizeof(r));
close(fd);
LOGD("example: process=[%s], r=[%u]\n", process, r);
}

};

static void companion_handler(int i) {
int fd = open("/dev/urandom", O_RDONLY);
int r;
read(fd, &r, sizeof(r));
close(fd);
LOGD("example: companion r=[%u]\n", r);
write(i, &r, sizeof(r));
}

REGISTER_ZYGISK_MODULE(MyModule)
REGISTER_ZYGISK_COMPANION(companion_handler)
1 change: 1 addition & 0 deletions module/jni/libcxx
Submodule libcxx added at b74fd5
Loading

0 comments on commit 82f2bc2

Please sign in to comment.