diff --git a/docs/guides.md b/docs/guides.md index d814f84b2..b13134a84 100644 --- a/docs/guides.md +++ b/docs/guides.md @@ -257,7 +257,7 @@ Since `/` is read-only on system-as-root devices, Magisk provides an overlay sys Overlay files shall be placed in the `overlay.d` folder in boot image ramdisk, and they follow these rules: -1. All `*.rc` files in `overlay.d` will be read and concatenated **AFTER** `init.rc` +1. Each `*.rc` file (except for `init.rc`) in `overlay.d` will be read and concatenated **AFTER** `init.rc` if it does not exist in the root directory, otherwise it will **REPLACE** the existing one. 2. Existing files can be replaced by files located at the same relative path 3. Files that correspond to a non-existing file will be ignored diff --git a/native/src/init/rootdir.cpp b/native/src/init/rootdir.cpp index 65e3e0201..b7c5fa42d 100644 --- a/native/src/init/rootdir.cpp +++ b/native/src/init/rootdir.cpp @@ -68,8 +68,18 @@ static void load_overlay_rc(const char *overlay) { int dfd = dirfd(dir.get()); // Do not allow overwrite init.rc unlinkat(dfd, "init.rc", 0); + + // '/' + name + '\0' + char buf[NAME_MAX + 2]; + buf[0] = '/'; for (dirent *entry; (entry = xreaddir(dir.get()));) { - if (str_ends(entry->d_name, ".rc")) { + if (!str_ends(entry->d_name, ".rc")) { + continue; + } + strscpy(buf + 1, entry->d_name, sizeof(buf) - 1); + if (access(buf, F_OK) == 0) { + LOGD("Replace rc script [%s]\n", entry->d_name); + } else { LOGD("Found rc script [%s]\n", entry->d_name); int rc = xopenat(dfd, entry->d_name, O_RDONLY | O_CLOEXEC); rc_list.push_back(full_read(rc));