33#include < unistd.h>
44#include < jni.h>
55#include < sys/types.h>
6+ #include < riru.h>
7+ #include < malloc.h>
8+ #include < cstring>
69
710#include " log.h"
811
@@ -128,32 +131,26 @@ static void post(JNIEnv *env) {
128131 }
129132}
130133
131- // You can remove functions you don't need
132-
133- extern " C" {
134- #define EXPORT __attribute__ ((visibility(" default" ))) __attribute__((used))
135- EXPORT void nativeForkAndSpecializePre(
134+ static void forkAndSpecializePre (
136135 JNIEnv *env, jclass clazz, jint *_uid, jint *gid, jintArray *gids, jint *runtimeFlags,
137136 jobjectArray *rlimits, jint *mountExternal, jstring *seInfo, jstring *niceName,
138137 jintArray *fdsToClose, jintArray *fdsToIgnore, jboolean *is_child_zygote,
139138 jstring *instructionSet, jstring *appDataDir, jboolean *isTopApp, jobjectArray *pkgDataInfoList,
140139 jobjectArray *whitelistedDataInfoList, jboolean *bindMountAppDataDirs, jboolean *bindMountAppStorageDirs) {
141- // packageName, packagesForUID, sandboxId are added from Android Q beta 2, removed from beta 5
142140 pre (env, appDataDir, niceName);
143141}
144142
145- EXPORT int nativeForkAndSpecializePost (JNIEnv *env, jclass clazz, jint res) {
143+ static void forkAndSpecializePost (JNIEnv *env, jclass clazz, jint res) {
146144 if (res == 0 ) {
147145 // in app process
148146 post (env);
149147 } else {
150148 // in zygote process, res is child pid
151149 // don't print log here, see https://github.com/RikkaApps/Riru/blob/77adfd6a4a6a81bfd20569c910bc4854f2f84f5e/riru-core/jni/main/jni_native_method.cpp#L55-L66
152150 }
153- return 0 ;
154151}
155152
156- EXPORT __attribute__ ((visibility( " default " ))) void specializeAppProcessPre(
153+ static void specializeAppProcessPre (
157154 JNIEnv *env, jclass clazz, jint *_uid, jint *gid, jintArray *gids, jint *runtimeFlags,
158155 jobjectArray *rlimits, jint *mountExternal, jstring *seInfo, jstring *niceName,
159156 jboolean *startChildZygote, jstring *instructionSet, jstring *appDataDir,
@@ -163,36 +160,114 @@ EXPORT __attribute__((visibility("default"))) void specializeAppProcessPre(
163160 pre (env, appDataDir, niceName);
164161}
165162
166- EXPORT __attribute__ ((visibility( " default " ))) int specializeAppProcessPost(
163+ static void specializeAppProcessPost (
167164 JNIEnv *env, jclass clazz) {
168165 // added from Android 10, but disabled at least in Google Pixel devices
169166 post (env);
170- return 0 ;
171167}
172168
173- EXPORT void nativeForkSystemServerPre (
169+ static void forkSystemServerPre (
174170 JNIEnv *env, jclass clazz, uid_t *uid, gid_t *gid, jintArray *gids, jint *runtimeFlags,
175171 jobjectArray *rlimits, jlong *permittedCapabilities, jlong *effectiveCapabilities) {
176172
177173}
178174
179- EXPORT int nativeForkSystemServerPost (JNIEnv *env, jclass clazz, jint res) {
175+ static void forkSystemServerPost (JNIEnv *env, jclass clazz, jint res) {
180176 if (res == 0 ) {
181177 // in system server process
182178 } else {
183179 // in zygote process, res is child pid
184180 // don't print log here, see https://github.com/RikkaApps/Riru/blob/77adfd6a4a6a81bfd20569c910bc4854f2f84f5e/riru-core/jni/main/jni_native_method.cpp#L55-L66
185181 }
186- return 0 ;
187182}
188183
189- EXPORT int shouldSkipUid (int uid) {
184+ static int shouldSkipUid (int uid) {
190185 // by default, Riru only call module functions in "normal app processes" (10000 <= uid % 100000 <= 19999)
191186 // false = don't skip
192187 return false ;
193188}
194189
195- EXPORT void onModuleLoaded () {
190+ static void onModuleLoaded () {
196191 // called when the shared library of Riru core is loaded
197192}
193+
194+ extern " C" {
195+
196+ int riru_api_version;
197+ RiruApiV9 *riru_api_v9;
198+
199+ /*
200+ * Init will be called three times.
201+ *
202+ * The first time:
203+ * Returns the highest version number supported by both Riru and the module.
204+ *
205+ * arg: (int *) Riru's API version
206+ * returns: (int *) the highest possible API version
207+ *
208+ * The second time:
209+ * Returns the RiruModuleX struct created by the module.
210+ * (X is the return of the first call)
211+ *
212+ * arg: (RiruApiVX *) RiruApi strcut, this pointer can be saved for further use
213+ * returns: (RiruModuleX *) RiruModule strcut
214+ *
215+ * The second time:
216+ * Let the module to cleanup (such as RiruModuleX struct created before).
217+ *
218+ * arg: null
219+ * returns: (ignored)
220+ *
221+ */
222+ void *init (void *arg) {
223+ static int step = 0 ;
224+ step += 1 ;
225+
226+ static void *_module;
227+
228+ switch (step) {
229+ case 1 : {
230+ auto core_max_api_version = *(int *) arg;
231+ riru_api_version = core_max_api_version <= RIRU_MODULE_API_VERSION ? core_max_api_version : RIRU_MODULE_API_VERSION;
232+ return &riru_api_version;
233+ }
234+ case 2 : {
235+ switch (riru_api_version) {
236+ // RiruApiV10 and RiruModuleInfoV10 are equal to V9
237+ case 10 :
238+ case 9 : {
239+ riru_api_v9 = (RiruApiV9 *) arg;
240+
241+ auto module = (RiruModuleInfoV9 *) malloc (sizeof (RiruModuleInfoV9));
242+ memset (module , 0 , sizeof (RiruModuleInfoV9));
243+ _module = module ;
244+
245+ module ->supportHide = true ;
246+
247+ module ->version = RIRU_MODULE_VERSION;
248+ module ->versionName = RIRU_MODULE_VERSION_NAME;
249+ module ->onModuleLoaded = onModuleLoaded;
250+ module ->shouldSkipUid = shouldSkipUid;
251+ module ->forkAndSpecializePre = forkAndSpecializePre;
252+ module ->forkAndSpecializePost = forkAndSpecializePost;
253+ module ->specializeAppProcessPre = specializeAppProcessPre;
254+ module ->specializeAppProcessPost = specializeAppProcessPost;
255+ module ->forkSystemServerPre = forkSystemServerPre;
256+ module ->forkSystemServerPost = forkSystemServerPost;
257+ return module ;
258+ }
259+ default : {
260+ return nullptr ;
261+ }
262+ }
263+ }
264+ case 3 : {
265+ free (_module);
266+ return nullptr ;
267+ }
268+ default : {
269+ return nullptr ;
270+ }
271+ }
272+ }
198273}
0 commit comments