从Android应用程序中的本机库访问根文件(/system,/dev) [英] Accessing root files (/system, /dev) from a native library in Android application

查看:454
本文介绍了从Android应用程序中的本机库访问根文件(/system,/dev)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地库,它将尝试在android设备的/system,/dev文件夹中创建文件(使用open(),fopen()等).

I have an NATIVE LIBRARY which will try to create files in the /system, /dev folders in an android device (using open(), fopen() etc).

现在,我已使用JNI&将集成库与android应用程序集成在一起NDK.但是在根文件夹中创建文件失败. 我试图从本机库中的sdcard中创建一个文件,效果很好.

Now i have integrated the library with an android application using JNI & NDK. But the creation of the files in the root folders are failing. I have tried to create a file in the sdcard from the native library and this works fine.

我既不想将文件打开代码移至Android代码(Java代码),也不想在sdcard中创建文件.我对在根文件夹本身中创建文件有明确的要求.

Neither I want to move the file opening code to Android code (Java code) nor I want to create the files in the sdcard. I have clear requirements to create the files in root folder itself.

推荐答案

在最新的android版本中,rootfs和系统在init设置目录和文件后以只读方式挂载.

In recent android versions, rootfs and system are mounted read-only after init has set up the directories and files.

为了在系统分区中创建文件,必须使用写访问权重新挂载它们.因此,您将必须以root用户身份调用/system/bin/mount.

In order to create a file in the system partition you must remount them with write access. So you will have to call on /system/bin/mount as root user.

根据是否/system/bin/mount玩具箱或工具箱符号链接,挂载系统rw的命令会有所不同

The command for mounting system rw is different depending on if /system/bin/mount a toybox or toolbox symlink

如果您无法通过Runtime.getRuntime().exec("su")来获取su,您是否在userdebug版本中使用了aosp生成的su二进制文件?如果是这样,我相信您必须是shell用户才能使用它.也许切换到更常用的su二进制文件或更新aosp二进制文件.

If you're failing to get su in with Runtime.getRuntime().exec("su"), are you using the su binary produced by aosp in userdebug builds? If so I believe you would have to be shell user in order to use it. Maybe switch to a more commonly available su binary or update the aosp one.

对于安装系统rw,您首先需要确定/system/bin/mount是玩具箱还是工具箱的符号链接,因为它们用于安装系统rw的命令将有所不同

for mounting system rw, you first need to determine if /system/bin/mount is a symlink to toybox or to toolbox, because the command they use for mounting system rw will be different

ls -l或readlink应该能够轻松回答该问题.

ls -l, or readlink should be able to easily answer that.

对于工具箱,

(作为uid(0)运行)

(running as uid(0))

mount -o remount,rw/system

mount -o remount,rw /system

对于玩具箱,

(作为uid(0)运行)

(running as uid(0))

mount -o rw,remount -t auto/system

mount -o rw,remount -t auto /system

在Java中,子进程必须首先请求su,因为只有root用户才能执行mount命令

In Java the subprocess must request su first as only root user can execute the mount command

这篇关于从Android应用程序中的本机库访问根文件(/system,/dev)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆