mirror of
https://github.com/topjohnwu/libsu.git
synced 2024-11-26 21:40:42 +00:00
Add native JNI to root service to example
This commit is contained in:
parent
dd6decbeb0
commit
19461ab3b9
1
example/.gitignore
vendored
1
example/.gitignore
vendored
@ -1 +1,2 @@
|
||||
/build
|
||||
/.cxx
|
||||
|
@ -23,6 +23,11 @@ android {
|
||||
)
|
||||
}
|
||||
}
|
||||
externalNativeBuild {
|
||||
cmake {
|
||||
path = file("src/main/cpp/CMakeLists.txt")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
@ -6,4 +6,5 @@ package com.topjohnwu.libsuexample;
|
||||
interface ITestService {
|
||||
int getPid();
|
||||
int getUid();
|
||||
String readPartitions();
|
||||
}
|
||||
|
14
example/src/main/cpp/CMakeLists.txt
Normal file
14
example/src/main/cpp/CMakeLists.txt
Normal file
@ -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)
|
26
example/src/main/cpp/test.cpp
Normal file
26
example/src/main/cpp/test.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by John Wu on 7/26/20.
|
||||
//
|
||||
#include <jni.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string>
|
||||
|
||||
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);
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user