Rick Sanchez
Rick Sanchez

Reputation: 4756

AOSP - How to have an executable run with root access?

I am building an "embedded" system, based on Android, and at the moment I'm trying to implement updating the firmware of an audio module.
There is a binary which does that, but the catch is that it needs root access to do it ( it can't access GPIOs otherwise ).
So the problem now has shifted to giving the executable ( and just that executable, mustn't do full root) root access.
I've been at this for a few days, and have tried a bunch of things, with no success.
Things I've already tried :

I have run out of ideas or things to try. What else can I do ?

EDIT
I was not able to make the binary run as root, but was able to run a shell script after boot from init.rc , where there is root access, and export/change the permissions of the specific gpio I needed. This made it possible to access it without root access needed later on.

Upvotes: 4

Views: 3109

Answers (2)

satur9nine
satur9nine

Reputation: 15072

Alex Cohn's answer https://stackoverflow.com/a/44456378/215266 is correct but here's more details.

The init process runs as root and can spawn other root processes. You can use a oneshot service that is disabled by default and kicked off by setting a system property to some value such as 1.

For example in https://android.googlesource.com/device/google/marlin/+/refs/heads/android10-mainline-a-release/init.common.rc :

service vendor.power_sh /vendor/bin/init.power.sh
    class main
    user root
    group root system
    disabled
    oneshot

on property:sys.boot_completed=1
    start vendor.power_sh

Programs started by init are not Android services they are traditional command line applications. The above example is a shell script but you can use C/C++ or Java if you like. The am command for example is primarily written Java, if you do adb shell cat /system/bin/am you will see:

#!/system/bin/sh

if [ "$1" != "instrument" ] ; then
    cmd activity "$@"
else
    base=/system
    export CLASSPATH=$base/framework/am.jar
    exec app_process $base/bin com.android.commands.am.Am "$@"
fi

The source for the Am code is here and you can see it has a traditional main method: https://android.googlesource.com/platform/frameworks/base/+/742a67127366c376fdf188ff99ba30b27d3bf90c/cmds/am/src/com/android/commands/am/Am.java

Upvotes: 2

Alex Cohn
Alex Cohn

Reputation: 57203

You can start your app, or service, or executable from init.rc.

Upvotes: 1

Related Questions