问题
I need to access a UNIX domain socket from my Android app. Its file is located in /dev
, so I need root permissions to open it. My phone is rooted, so gaining this permissions is not a problem. However, I failed to find a way to access this socket from root and forward the communication to my app.
AFAIK, the only way Android apps can get root permissions is through the su
tool, so I need to find a native tool (already installed in the phone) that can make a bridge between STDIN/STDOUT and UNIX domain packets (so that my app can control the communication). The typical tools for that purpose are nc
and socat
. The former is available through busybox, but that version does not support UNIX domain sockets. The latter is not available at all. I'd rather avoid modifying the system partition to install a different tool.
My app could also launch a script that communicate with that socket from su
directly without the need to forward the communication to my app, but again, I found no way to write such a script without the previously mentioned tools.
Is it possible to do what I want without installing/developing a native binary tool?
Maybe it is possible to do something with iptables
/netfilter
, but that does not seem to support this kind of sockets.
EDIT: well, I ended up developing a native binary executable which opens the Unix domain socket as root, and forwards it to the Java part using another Unix domain socket (created by the Java part). There is a hidden constructor for LocalSocket which takes a file descriptor and uses it as the opened socket. The biggest challenge was to find out how to compile a native executable with Android studio (not a library), but this answer helped a lot.
来源:https://stackoverflow.com/questions/45269285/access-protected-unix-domain-sockets-from-android-app-using-root