diff --git a/example/.gitignore b/example/.gitignore index 796b96d..9ff2a37 100644 --- a/example/.gitignore +++ b/example/.gitignore @@ -1 +1,2 @@ /build +/.cxx diff --git a/example/build.gradle.kts b/example/build.gradle.kts index 1069984..ad3e1ec 100644 --- a/example/build.gradle.kts +++ b/example/build.gradle.kts @@ -23,6 +23,11 @@ android { ) } } + externalNativeBuild { + cmake { + path = file("src/main/cpp/CMakeLists.txt") + } + } } dependencies { diff --git a/example/src/main/aidl/com/topjohnwu/libsuexample/ITestService.aidl b/example/src/main/aidl/com/topjohnwu/libsuexample/ITestService.aidl index 072c9fd..039d7c9 100644 --- a/example/src/main/aidl/com/topjohnwu/libsuexample/ITestService.aidl +++ b/example/src/main/aidl/com/topjohnwu/libsuexample/ITestService.aidl @@ -6,4 +6,5 @@ package com.topjohnwu.libsuexample; interface ITestService { int getPid(); int getUid(); + String readPartitions(); } diff --git a/example/src/main/cpp/CMakeLists.txt b/example/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000..66bf8f6 --- /dev/null +++ b/example/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +# Sets the minimum version of CMake required to build your native library. +# This ensures that a certain set of CMake features is available to +# your build. + +cmake_minimum_required(VERSION 3.4.1) + +# Specifies a library name, specifies whether the library is STATIC or +# SHARED, and provides relative paths to the source code. You can +# define multiple libraries by adding multiple add_library() commands, +# and CMake builds them for you. When you build your app, Gradle +# automatically packages shared libraries with your APK. + +add_library(native-lib SHARED + test.cpp) diff --git a/example/src/main/cpp/test.cpp b/example/src/main/cpp/test.cpp new file mode 100644 index 0000000..391b5e0 --- /dev/null +++ b/example/src/main/cpp/test.cpp @@ -0,0 +1,26 @@ +// +// Created by John Wu on 7/26/20. +// +#include +#include +#include +#include +#include +#include + +extern "C" JNIEXPORT JNICALL +jint Java_com_topjohnwu_libsuexample_MainActivity_00024ExampleService_nativeGetUid( + JNIEnv *env, jobject instance) { + return getuid(); +} + +extern "C" JNIEXPORT JNICALL +jstring Java_com_topjohnwu_libsuexample_MainActivity_00024ExampleService_nativeReadFile( + JNIEnv *env, jobject instance, jstring name) { + const char *path = env->GetStringUTFChars(name, nullptr); + int fd = open(path, O_RDONLY); + env->ReleaseStringUTFChars(name, path); + char buf[4096]; + buf[read(fd, buf, sizeof(buf) - 1)] = 0; + return env->NewStringUTF(buf); +} diff --git a/example/src/main/java/com/topjohnwu/libsuexample/MainActivity.java b/example/src/main/java/com/topjohnwu/libsuexample/MainActivity.java index 24788a4..e58f9ea 100644 --- a/example/src/main/java/com/topjohnwu/libsuexample/MainActivity.java +++ b/example/src/main/java/com/topjohnwu/libsuexample/MainActivity.java @@ -71,6 +71,15 @@ public class MainActivity extends Activity { // Demonstrate RootService static class ExampleService extends RootService { + static { + // Only load in root process + if (Process.myUid() == 0) + System.loadLibrary("native-lib"); + } + + native int nativeGetUid(); + native String nativeReadFile(String file); + @Override public IBinder onBind(@NonNull Intent intent) { return new ITestService.Stub() { @@ -81,7 +90,12 @@ public class MainActivity extends Activity { @Override public int getUid() { - return Process.myUid(); + return nativeGetUid(); + } + + @Override + public String readPartitions() { + return nativeReadFile("/proc/partitions"); } }; } @@ -109,6 +123,7 @@ public class MainActivity extends Activity { try { consoleList.add("Remote PID: " + testIPC.getPid()); consoleList.add("Remote UID: " + testIPC.getUid()); + consoleList.add("/proc/partitions:\n" + testIPC.readPartitions()); } catch (RemoteException e) { Log.e(TAG, "Remote error", e); }