Skip to content

Commit

Permalink
Introduce post-mount stage scripts
Browse files Browse the repository at this point in the history
Same as post-fs-data mode, only difference is that scripts run
after magic mount completes. This provides a stage where scripts
can be instantly executed after module mount is ready to keep a
clean memory map/mount namespace for new exec.
  • Loading branch information
aviraxp committed Jan 26, 2025
1 parent 95d1e69 commit 4f26f47
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
7 changes: 7 additions & 0 deletions docs/details.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ SECURE_DIR=/data/adb
# Folder storing general post-fs-data scripts
$SECURE_DIR/post-fs-data.d
# Folder storing general post-mount scripts
$SECURE_DIR/post-mount.d
# Folder storing general late_start service scripts
$SECURE_DIR/service.d
Expand Down Expand Up @@ -89,6 +92,10 @@ DATABIN=$SECURE_DIR/magisk

This triggers on `post-fs-data` when `/data` is decrypted and mounted. The daemon `magiskd` will be launched, post-fs-data scripts are executed, and module files are magic mounted.

### post-mount

This triggers on `post-fs-data` but instantly after magic mount completes. In this mode, post-mount scripts are executed.

### late_start

Later in the booting process, the class `late_start` will be triggered, and Magisk "service" mode will be started. In this mode, service scripts are executed.
Expand Down
16 changes: 10 additions & 6 deletions docs/guides.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ A Magisk module is a folder placed in `/data/adb/modules` with the structure bel
│ │
│ │ *** Optional Files ***
│ │
│ ├── post-fs-data.sh <--- This script will be executed in post-fs-data
│ ├── post-fs-data.sh <--- This script will be executed in post-fs-data and before magic mount is done
│ ├── post-mount.sh <--- This script will be executed in post-fs-data and after magic mount is done
│ ├── service.sh <--- This script will be executed in late_start service
| ├── uninstall.sh <--- This script will be executed when Magisk removes your module
| ├── action.sh <--- This script will be executed when user click the action button in Magisk app
Expand Down Expand Up @@ -111,7 +112,7 @@ Update JSON format:

#### Shell scripts (`*.sh`)

Please read the [Boot Scripts](#boot-scripts) section to understand the difference between `post-fs-data.sh` and `service.sh`. For most module developers, `service.sh` should be good enough if you just need to run a boot script. If you need to wait for boot completed, you can use `resetprop -w sys.boot_completed 0`.
Please read the [Boot Scripts](#boot-scripts) section to understand the difference between `post-fs-data.sh`, `post-mount.sh` and `service.sh`. For most module developers, `service.sh` should be good enough if you just need to run a boot script. If you need to wait for boot completed, you can use `resetprop -w sys.boot_completed 0`.

In all scripts of your module, please use `MODDIR=${0%/*}` to get your module's base directory path; do **NOT** hardcode your module path in scripts.
If Zygisk is enabled, the environment variable `ZYGISK_ENABLED` will be set to `1`.
Expand Down Expand Up @@ -242,29 +243,32 @@ The list above will result in the following dummy devices being created: `$MODPA

## Boot Scripts

In Magisk, you can run boot scripts in 2 different modes: **post-fs-data** and **late_start service** mode.
In Magisk, you can run boot scripts in 3 different modes: **post-fs-data**, **post-mount** and **late_start service** mode.

- post-fs-data mode
- This stage is BLOCKING. The boot process is paused before execution is done, or 40 seconds have passed.
- Scripts run before any modules are mounted. This allows a module developer to dynamically adjust their modules before it gets mounted.
- This stage happens before Zygote is started, which pretty much means everything in Android
- **WARNING:** using `setprop` will deadlock the boot process! Please use `resetprop -n <prop_name> <prop_value>` instead.
- **Only run scripts in this mode if necessary.**
- post-mount mode
- Same as post-fs-data mode, only difference is that scripts run after every module is mounted.
- **This is the recommended stage to run app_process.**
- late_start service mode
- This stage is NON-BLOCKING. Your script runs in parallel with the rest of the booting process.
- **This is the recommended stage to run most scripts.**

In Magisk, there are also 2 kinds of scripts: **general scripts** and **module scripts**.

- General Scripts
- Placed in `/data/adb/post-fs-data.d` or `/data/adb/service.d`
- Placed in `/data/adb/post-fs-data.d`, `/data/adb/post-mount.d` or `/data/adb/service.d`
- Only executed if the script is set as executable (`chmod +x script.sh`)
- Scripts in `post-fs-data.d` runs in post-fs-data mode, and scripts in `service.d` runs in late_start service mode.
- Scripts in `post-fs-data.d` runs in post-fs-data mode, scripts in `/data/adb/post-mount.d` runs in post-mount mode and scripts in `service.d` runs in late_start service mode.
- Modules should **NOT** add general scripts during installation
- Module Scripts
- Placed in the module's own folder
- Only executed if the module is enabled
- `post-fs-data.sh` runs in post-fs-data mode, and `service.sh` runs in late_start service mode.
- `post-fs-data.sh` and `post-mount.sh` runs in post-fs-data mode, and `service.sh` runs in late_start service mode.

All boot scripts will run in Magisk's BusyBox `ash` shell with "Standalone Mode" enabled.

Expand Down
5 changes: 5 additions & 0 deletions native/src/core/bootstages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ static bool magisk_env() {
xmkdir(DATABIN, 0755);
xmkdir(MODULEROOT, 0755);
xmkdir(SECURE_DIR "/post-fs-data.d", 0755);
xmkdir(SECURE_DIR "/post-mount.d", 0755);
xmkdir(SECURE_DIR "/service.d", 0755);
restorecon();

Expand Down Expand Up @@ -180,6 +181,10 @@ bool MagiskD::post_fs_data() const noexcept {
setup_mounts();
handle_modules();
load_modules();

LOGI("** post-mount mode running\n");
exec_common_scripts("post-mount");
exec_module_scripts("post-mount");
return false;
}

Expand Down
5 changes: 3 additions & 2 deletions native/src/core/scripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ if (pfs) { \
if (timer_pid < 0) \
continue; \
if (int pid = waitpid(-1, nullptr, 0); pid == timer_pid) { \
LOGW("* post-fs-data scripts blocking phase timeout\n"); \
LOGW("* post-fs-data or post-mount scripts blocking phase timeout\n"); \
timer_pid = -1; \
} \
}
Expand All @@ -82,6 +82,7 @@ void exec_common_scripts(const char *stage) {
auto dir = xopen_dir(path);
if (!dir) return;

// No need to check post-mount becasue it is not a separate stage
bool pfs = stage == "post-fs-data"sv;
int timer_pid = -1;
if (pfs) {
Expand Down Expand Up @@ -122,7 +123,7 @@ void exec_module_scripts(const char *stage, const vector<string_view> &modules)
if (modules.empty())
return;

bool pfs = stage == "post-fs-data"sv;
bool pfs = stage == "post-fs-data"sv || stage == "post-mount"sv;
if (pfs) {
timespec now{};
clock_gettime(CLOCK_MONOTONIC, &now);
Expand Down
1 change: 1 addition & 0 deletions scripts/avd_magisk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ mkdir -p $MAGISKBIN 2>/dev/null
unzip -oj magisk.apk 'assets/*.sh' -d $MAGISKBIN
mkdir /data/adb/modules 2>/dev/null
mkdir /data/adb/post-fs-data.d 2>/dev/null
mkdir /data/adb/post-mount.d 2>/dev/null
mkdir /data/adb/service.d 2>/dev/null

for file in magisk magisk32 magiskpolicy stub.apk; do
Expand Down
2 changes: 1 addition & 1 deletion scripts/uninstaller.sh
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ ui_print "- Removing Magisk files"
rm -rf \
/cache/*magisk* /cache/unblock /data/*magisk* /data/cache/*magisk* /data/property/*magisk* \
/data/Magisk.apk /data/busybox /data/custom_ramdisk_patch.sh /data/adb/*magisk* \
/data/adb/post-fs-data.d /data/adb/service.d /data/adb/modules* \
/data/adb/post-fs-data.d /data/adb/post-mount.d /data/adb/service.d /data/adb/modules* \
/data/unencrypted/magisk /metadata/magisk /metadata/watchdog/magisk /persist/magisk /mnt/vendor/persist/magisk

ADDOND=/system/addon.d/99-magisk.sh
Expand Down

0 comments on commit 4f26f47

Please sign in to comment.