Custom *.rc (init.rc) scripts with Magisk. (Or running a script at network change.)

Search This thread

lockywolf

Member
Oct 2, 2015
49
4
Hello, everyone.

In order to avoid an XY problem, I would like to introduce the actual problem first.

I need to run a script each time network changes. Android automatically changes quite a few settings when network changes, and because I need to have some of them set to specific values, I need to tweak them each time something happens.

How would I like to proceed:

There is a sysprop setting that changes each time network changes: sys.radio.cellular.netId.

Naturally, I would like to hook my script to that property change.

Android init system seems to provide such an option: init.rc syntax allows to subscribe to a property change using the
Code:
on property:propname=*
syntax.

Seems easy:
Add a custom_network.rc
Bash:
on property:sys.radio.cellular.netId=*
   start custom_network

service custom_network /bin/custom_network.sh
    user root
    seclabel u:r:magisk:s0
    oneshot
Add a file /bin/custom_network.sh:
Bash:
#/system/bin/sh
echo "TODO"

The above is essentially following this guide: https://android.stackexchange.com/q...run-an-executable-on-boot-and-keep-it-running

So, I created a magisk module, added the files above to the $MODDIR/system/etc/init, and $MODDIR/system/bin directories.
Then I added the following lines to the customize.sh:

Bash:
set_perm  $MODPATH/system/bin/custom_network.sh       0 0 0755
set_perm  $MODPATH/bin/custom_network.sh       0 0 0755

chown 0.0 $MODPATH/system/etc/init/custom_network.rc
chmod 0644 $MODPATH/system/etc/init/custom_network.rc
chcon u:object_r:system_file:s0 $MODPATH/system/etc/init/custom_network.rc

However, this does not work. The service custom_network does not appear in the getprop | grep svc list, and cannot be started with setprop ctl.start "custom_network".

Is it true that in order for _any_ custom rc files, the system boot image must be patched?
If yes, is there a manual how to do so?
If no, then what am I doing wrong here?

Furthermore, if patching the boot image cannot be avoided, is there a manual on how to do this with minimal pain?

On the other hand, is there a way to avoid adding a custom init service entirely, and add a network listener by some other means?
 
Last edited:

Renate

Recognized Contributor / Inactive Recognized Dev
I don't believe init services get a stdout.
You need to write to either /dev/kmsg or logcat.

You can test your service with start custom_network.

You could also listen for uevents:
Code:
s=socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
Although I'm not sure what you're looking for is there.
 

csdvrx

New member
Nov 11, 2022
4
0
I was mostly looking for feedback by someone also wanting to patch the init.rc: I'm still trying to understand the cascade of events causing adbd to be started twice in boot, to find and modify the rc script responsible for the first time it's started and use instead a patched adbd
 

xristoskon

Member
Feb 4, 2018
6
0
Samsung Galaxy S22 Plus
I was mostly looking for feedback by someone also wanting to patch the init.rc: I'm still trying to understand the cascade of events causing adbd to be started twice in boot, to find and modify the rc script responsible for the first time it's started and use instead a patched adbd
have you figured it out ? i have the same issue , i am trying to create a file at /system/etc/init folder