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.

This is very weird, and I am still researching the permission differences in different threads on 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.

Answer from MeetTitan on Stack Overflow
Top answer
1 of 2
1

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.

This is very weird, and I am still researching the permission differences in different threads on 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.

2 of 2
0

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.

If you try to read the /proc/self/exe from your Android app, the actual executable will be /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.

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();
🌐
GitHub
github.com › moby › moby › issues › 18883
readlink /proc/self/exe permission denied inside created thread of container entry point · Issue #18883 · moby/moby
December 23, 2015 - The results I received: Calls to readlink() on anything that's a link inside of /proc/self return permission denied, for my entrypoint process in a docker container (using the ubuntu container as a base), and if I'm calling it from a thread other than the main thread. The results I expected: Calls to readlink() on valid links inside /proc/self work correctly from any thread in my process, even if my process is a docker entrypoint. Additional info I think is important: Here's a simple test program that uses /proc/self/exe:
Author   moby
Discussions

panic: readlink /proc/self/exe: permission denied
Issue Details With AdGuard Home v0.107.6 I get the following error: panic: readlink /proc/self/exe: permission denied goroutine 1 [running]: github.com/AdguardTeam/AdGuardHome/internal/home.initWor... More on github.com
🌐 github.com
13
April 20, 2022
AppImage does not launch without sudo
Overview When I run AppImage executable, I get following error: Cannot open /proc/self/exe: Permission denied Failed to get fs offset for /proc/self/exe However it is working with sudo. Steps to Re... More on github.com
🌐 github.com
1
July 20, 2020
How do I get past the Cura terminal saying Cannot open, Permission denied?
What linux distro are you running? Cura is probably going to assume a debian-based environment, and if you're running RedHat, CentOS, OpenSUSE - then you're probably going to have problems. More on reddit.com
🌐 r/3Dprinting
22
1
June 24, 2022
go - fork/exec /proc/self/exe: operation not permitted - Stack Overflow
I am following : Containers From Scratch • Liz Rice • GOTO 2018 and test the source code lizrice/containers-from-scratch locally to learn containers. But with code below , I am not able to fork a... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Plan
saxonica.plan.io › issues › 2176
Support #2176: Unable to read link /proc/self/exe : Permission denied (errno 13) - SaxonC - Saxonica Developer Community - SaxonC - Saxonica Developer Community - SaxonC - Saxonica Developer Community
Probably, this special user (but not the user that runs the application) has not enough rights for accessing "/proc/self/exe". Also, please ask the user if SELinux is installed on the target system. SELinux provides extended permissions for system files and utilities, so it could be the root cause of "Permission denied" errors.
🌐
GitHub
github.com › AdguardTeam › AdGuardHome › issues › 4509
panic: readlink /proc/self/exe: permission denied · Issue #4509 · AdguardTeam/AdGuardHome
April 20, 2022 - Issue Details With AdGuard Home v0.107.6 I get the following error: panic: readlink /proc/self/exe: permission denied goroutine 1 [running]: github.com/AdguardTeam/AdGuardHome/internal/home.initWorkingDir({0x0, {0x0, 0x0}, {0x0, 0x0}, {0...
Author   AdguardTeam
🌐
GitHub
github.com › keepassxreboot › keepassxc › issues › 5138
AppImage does not launch without sudo · Issue #5138 · keepassxreboot/keepassxc
July 20, 2020 - Overview When I run AppImage executable, I get following error: Cannot open /proc/self/exe: Permission denied Failed to get fs offset for /proc/self/exe However it is working with sudo. Steps to Reproduce ./KeePassXC-2.6.0-x86_64.AppImag...
Author   keepassxreboot
🌐
GitHub
github.com › rootless-containers › rootlesskit › issues › 68
failed to start the child: fork/exec /proc/self/exe: permission denied · Issue #68 · rootless-containers/rootlesskit
June 12, 2019 - $ rootlesskit sh [rootlesskit:parent] error: failed to start the child: fork/exec /proc/self/exe: permission denied $ rootlesskit -v rootlesskit version 0.4.1
Author   rootless-containers
🌐
Stack Overflow
stackoverflow.com › questions › 72543182 › fork-exec-proc-self-exe-operation-not-permitted
go - fork/exec /proc/self/exe: operation not permitted - Stack Overflow
Not sure why this doesn't work (it does on my machine), but you can try 1) use go build instead of go run; 2) use os.Executable instead of /proc/self/exec.
Find elsewhere
🌐
Nabble
nongnu.13855.n7.nabble.com › Linux-Desktop-4-2-2-Qt5-12-5-Cannot-open-proc-self-exe-Permission-denied-td241446.html
linphone-users - (Linux) Desktop 4.2.2 - Qt5.12.5 - Cannot open /proc/self/exe: Permission denied
nongnu › nongnu - linphone › linphone-users · (Linux) Desktop 4.2.2 - Qt5.12.5 - Cannot open /proc/self/exe: Permission denied · ‹ Previous Topic Next Topic › · Locked 2 messages · David Pirotte · Reply | Threaded · Open this post in threaded view
🌐
GitHub
github.com › Revolutionary-Games › Thrive-Launcher › issues › 151
Linux Launcher fails to run (chrome sandbox) · Issue #151 · Revolutionary-Games/Thrive-Launcher
March 21, 2022 - I downloaded the Linux Launcher file (I'm running Arch Linux), but couldn't get it to run. I've marked the file as an executable, but I still get the following error message: Cannot open /proc/self/exe: Permission denied Failed to get fs...
Author   Revolutionary-Games
🌐
Linux Containers Forum
discuss.linuxcontainers.org › incus
Rootless docker on new Ubuntu kernels does not work - Incus - Linux Containers Forum
January 15, 2024 - I just found that rootless docker is unable to work on ZFS devices which are VFS idmap shifted. The log says: [rootlesskit:parent] error: failed to start the child: fork/exec /proc/self/exe: permission denied The comma…
🌐
Google Groups
groups.google.com › a › lbl.gov › g › singularity › c › mQDBffIYFhQ
with singularity version 3.0.0 : Cant' open /proc/self/auxv: Permission denied
November 14, 2018 - Hi, The installation is incorrect though. The issue here boils down to the starter-suid process. It was setuid to the sw user. So, when it was ran, the EUID was that of the sw user. The `sw` user doesn't have access to the /proc/self/auxv file for a process that your user started.
🌐
Docker Community
forums.docker.com › docker engine › general
Docker run error: /proc/self/fd/6: permission denied - General - Docker Community Forums
January 18, 2026 - Hi, I’m pretty new to docker and was trying to install it and run the hello-world container on a new Ubuntu 25.10 machine. I followed the steps listed here: Ubuntu | Docker Docs and used the “apt” install method (I am not using docker desktop). Here is the result of docker --version and ...
🌐
Stack Overflow
stackoverflow.com › questions › 41675809 › permission-denied-while-accessing-proc-pid-exe
linux - Permission denied while accessing /proc/<pid>/exe - Stack Overflow
I did some research and found that kernel enforces some restrictions while accessing certain files in/proc ,but i have 20 process all accessing the same /proc/<pid>/exe ,but only one facing this problem .. CHAR fn[100]; 159 CHAR args[500]; 160 CHAR ProgName[50]; 161 CHAR *arr[6]; 162 CHAR *buf; 163 CHAR ProcessId[10]; 164 static int count_try = 0; 165 166 167 memset(fn,0,100); 168 memset(ProcessId,0,10); 169 sprintf (ProcessId,"%d",Pid); 170 strcpy(fn, "/proc/"); 171 strcat(fn, ProcessId); 172 //strcat(fn, "/elf_prpsinfo"); 173 strcat(fn, "/exe"); 174 175 if ((psp = open(fn, O_RDONLY)) == -1) 176 { 177 perror("GetProgName:ps open::"); 178 exit(ERROR); 179 }
🌐
GitHub
github.com › rootless-containers › rootlesskit › issues › 172
[rootlesskit:parent] error: failed to start the child: fork/exec /proc/self/exe: operation not permitted · Issue #172 · rootless-containers/rootlesskit
September 9, 2020 - [rootlesskit:parent] error: failed to start the child: fork/exec /proc/self/exe: operation not permitted#172
Author   rootless-containers
🌐
AWS re:Post
repost.aws › questions › QULenmRaemROyavu0DZgX70g › aws-nitro-enclaves-proc-self-exe-operation-not-permitted
[AWS Nitro Enclaves] proc/self/exe operation not permitted | AWS re:Post
September 3, 2024 - This makes me believe that the operations that gvisor might be trying to do would be outside the scope of what is available via Nitro Enclaves. The CPU and memory of an Enclave is completely Isolated and hence the resources that Gofer might be requesting access to would keep getting denied by the enclave.
🌐
GitHub
github.com › rootless-containers › rootlesskit › issues › 425
Encountered error `[rootlesskit:parent] error: failed to start the child: fork/exec /proc/self/exe: operation not permitted` when build the docker image · Issue #425 · rootless-containers/rootlesskit
March 12, 2024 - Encountered error [rootlesskit:parent] error: failed to start the child: fork/exec /proc/self/exe: operation not permitted when build the docker image#425
Author   rootless-containers