简体   繁体   English

从非主线程读取/ proc / self / exe时,Android权限被拒绝

[英]Android permission denied when reading /proc/self/exe from non-main thread

I'm trying to get the canonical path of /proc/self/exe. 我正试图获得/ proc / self / exe的规范路径。 When I do this on the Main Thread it works, when I do this on an different thread it crashes with an IOException: "Permission denied": 当我在主线程上执行此操作时,它可以工作,当我在另一个线程上执行此操作时,它会因IOException崩溃:“权限被拒绝”:

                   DBG  E  Thread: main
                        E  Path: /system/bin/app_process32
                        E  Thread: Thread-21656
            System.err  W  java.io.IOException: Permission denied
                        W      at java.io.File.canonicalizePath(Native Method)
                        W      at java.io.File.getCanonicalPath(File.java:414)
                        W      at java.io.File.getCanonicalFile(File.java:428)
                        W      at com.quanturium.testbugprocselfexe.MyActivity.getPathOfExecutable(MyActivity.java:36)
                        W      at com.quanturium.testbugprocselfexe.MyActivity.access$000(MyActivity.java:12)
                        W      at com.quanturium.testbugprocselfexe.MyActivity$1.run(MyActivity.java:26)
                        W      at java.lang.Thread.run(Thread.java:818)

Code: 码:

@Override
protected void onCreate (Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getPathOfExecutable(); // Works as expected

    new Thread(new Runnable() {
        @Override
        public void run ()
        {
            getPathOfExecutable(); // Trigger the IOException: Permission denied
        }
    }).start();
}

private void getPathOfExecutable()
{
    try
    {
        Log.e("DBG", "Thread: " + Thread.currentThread().getName());
        Log.e("DBG", "Path: " + new File("/proc/self/exe").getCanonicalFile().getPath());
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

This error only happens when debuggable is configured to false in the build.gradle file 只有在build.gradle文件中将debuggable配置为false时才会发生此错误

Code to try it out: https://github.com/quanturium/TestBugProcSelfExe 试用它的代码: https//github.com/quanturium/TestBugProcSelfExe

Is this a bug or an intended behavior? 这是一个错误还是预期的行为? What would be a workaround to get the path of the current executable? 获取当前可执行文件路径的解决方法是什么?

Does the code block? 代码是否阻止? If it doesn't, there should be no ramifications of running it in the main thread. 如果没有,则应该没有在主线程中运行它的后果。 You can, however, do that from another thread, with: 但是,您可以从另一个线程执行此操作,其中:

Context.runOnUiThread(new Runnable() {
    getPathOfExecutable();
});

This is the cleanest work around I can think of, short of editing the permissions of your file (that you can't get the path of without running your code on the main thread anyways) because you have r/w privileges on /proc/self/exe. 这是我能想到的最干净的工作,没有编辑文件的权限(无论如何都无法在主线程上运行代码的路径),因为你在/ proc /上有r / w权限自我/ exe文件。

This is very weird, and I am still researching the permission differences in different threads on android. 这很奇怪,我仍在研究android上不同线程的权限差异。

If you can get it working in the main thread, my opinion would be to just do it in the main thread, and not worry much about optimization, as the performance is no different on different threads. 如果你可以在主线程中使用它,我的意见是在主线程中执行它,而不用担心优化,因为在不同的线程上性能没有区别。

What would be a workaround to get the path of the current executable? 获取当前可执行文件路径的解决方法是什么?

Since every Android app is forked from Zygote, which is the first Java vm process when the virtual machine created by /system/bin/app_process at system booting. 由于每个Android应用程序都是从Zygote分叉的,这是在系统启动时由/system/bin/app_process创建的虚拟机的第一个Java vm进程。

If you try to read the /proc/self/exe from your Android app, the actual executable will be /system/bin/app_process . 如果您尝试从Android应用程序中读取/proc/self/exe ,则实际的可执行文件将是/system/bin/app_process Even if you read this outside of your app's main thread, the result is the same and it wouldn't have the permission error in theory. 即使您在应用程序主线程之外阅读此内容,结果也是相同的,理论上它不会出现权限错误。

The question you asked is a kind of weird problem, I have tested with the following code on Android 2.3.3 and worked fine. 你问的问题是一个奇怪的问题,我已经在Android 2.3.3上使用以下代码进行了测试并且工作正常。

new Thread() {

    /* (non-Javadoc)
     * @see java.lang.Thread#run()
     */
    @Override
    public void run() {
        // TODO Auto-generated method stub
        super.run();
        try {
            Log.d(TAG, new File("/proc/self/exe").getCanonicalFile().getPath());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}.start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Applet中将字符串从非主线程动态更新为Main - Updating String from a Non-Main Thread to Main dynamically in Applet 从Java Applet中的非主线程调用repaint() - Calling repaint() from non-main thread in java applet 非主线程的图片不会移动 - pictures with non-main Thread does not move 是否可以使用RxJava在非主线程中调用“onSensorChanged”方法? - Is it possible to call “onSensorChanged” method in a non-main thread using RxJava? 是否可以在非主线程中创建openGL VBO /显示列表 - Is it possible to create a openGL VBO/Display List in a non-main thread 来自非主线程的Android networkonmainthreadexception - Android networkonmainthreadexception from non main thread 读取CPU使用率时出错:/ proc / stat(权限被拒绝) - Error reading CPU usage: /proc/stat (permission denied) 在非主要活动中运行 DatePickerDialog - Run DatePickerDialog in non-main activity java android Q 从热点 /proc/net/arp 读取 ip:打开失败:EACCES(权限被拒绝) - java android Q read ip from hotspot /proc/net/arp: open failed: EACCES (Permission denied) 在OSGi中,我的权限在主线程中被拒绝,但在EDT线程中被允许 - In OSGi, my Permission is denied in Main thread but allowed in EDT thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM