Android permission denied while reading / proc / self / exe from main thread

I am trying to get the canonical path / proc / self / exe. When I do this in the main thread, it works, when I do it in another thread, it crashes using IOException: "Permission denied":

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) 

The 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 occurs when debuggable is set to false in the build.gradle file

Verification code: https://github.com/quanturium/TestBugProcSelfExe

Is this a mistake or an alleged behavior? What will be the workaround to get the path to the current executable?

+5
source share
2 answers

Is a block of code? If this is not the case, there should be no consequences of his work in the main thread. You can, however, do this from another thread using:

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

This is the cleanest work that I can think of without editing the permissions of your file (you cannot get the path without running your code in the main thread), because you have r / w privileges on / Proc / I / ex.

This is very strange, and I'm still exploring the resolution differences in different threads on Android.

If you can make it work in the main thread, my opinion would be to just do it in the main thread and not worry about optimization, since the performance is not different from different threads.

+1
source

What will be the workaround to get the path to the current executable?

Since every Android application is forked by Zygote, which is the first Java vm process when a virtual machine is created using /system/bin/app_process when the system boots.

If you try to read /proc/self/exe from your Android application, the actual executable will be /system/bin/app_process . Even if you read this outside the mainstream application stream, the result will be the same, and theoretically it will not have a resolution error.

The question you asked is kind of a weird problem, I tested it with the following code on Android 2.3.3 and worked fine.

 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(); 
0
source

Source: https://habr.com/ru/post/1213634/


All Articles