Skip to content

Commit

Permalink
Remove confs of modules when uninstalled
Browse files Browse the repository at this point in the history
"再改一下"
  • Loading branch information
LoveSy authored and kotori2 committed Dec 15, 2020
1 parent 57f15c1 commit f51e71d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.elderdrivers.riru.edxp.config.ConfigManager;
import com.elderdrivers.riru.edxp.deopt.PrebuiltMethodsDeopter;
import com.elderdrivers.riru.edxp.util.Utils;

import de.robv.android.xposed.SELinuxHelper;
import de.robv.android.xposed.XposedInit;
Expand All @@ -22,14 +23,6 @@ public void forkAndSpecializePre(int uid, int gid, int[] gids, int debugFlags,
}

public void forkAndSpecializePost(int pid, String appDataDir, String niceName) {
SELinuxHelper.initOnce();
mRouter.initResourcesHook();
// call this to ensure the flag is set to false ASAP
mRouter.prepare(false);
PrebuiltMethodsDeopter.deoptBootMethods(); // do it once for secondary zygote
// install bootstrap hooks for secondary zygote
mRouter.installBootstrapHooks(false);
// TODO consider processes without forkAndSpecializePost being called
forkPostCommon(pid, false, appDataDir, niceName);
}

Expand All @@ -38,27 +31,22 @@ public void forkSystemServerPre(int uid, int gid, int[] gids, int debugFlags, in
}

public void forkSystemServerPost(int pid) {
SELinuxHelper.initOnce();
mRouter.initResourcesHook();
// set startsSystemServer flag used when loadModules
mRouter.prepare(true);
PrebuiltMethodsDeopter.deoptBootMethods(); // do it once for main zygote
// install bootstrap hooks for main zygote as early as possible
// in case we miss some processes not forked via forkAndSpecialize
// for instance com.android.phone
mRouter.installBootstrapHooks(true);
// in system_server process
forkPostCommon(pid, true,
getDataPathPrefix() + "android", "system_server");
}


private void forkPostCommon(int pid, boolean isSystem, String appDataDir, String niceName) {
SELinuxHelper.initOnce();
mRouter.initResourcesHook();
mRouter.prepare(isSystem);
PrebuiltMethodsDeopter.deoptBootMethods(); // do it once for secondary zygote
mRouter.installBootstrapHooks(isSystem);
ConfigManager.appDataDir = appDataDir;
ConfigManager.niceName = niceName;
XposedInit.prefsBasePath = ConfigManager.getPrefsPath("");
mRouter.prepare(isSystem);
mRouter.onEnterChildProcess();
Utils.logI("Loading modules for " + niceName);
mRouter.loadModulesSafely(true);
}

Expand Down
10 changes: 2 additions & 8 deletions edxp-core/src/main/cpp/main/src/config_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,8 @@ namespace edxp {
fs::permissions(conf_path, fs::perms::owner_all | fs::perms::group_all);
fs::permissions(log_path, fs::perms::owner_all | fs::perms::group_all);
if (pkg_name == "android") uid = -1;
if (const auto &[r_uid, r_gid] = path_own(conf_path);
(uid != -1 && r_uid != uid) || r_gid != 1000u) {
path_chown(conf_path, uid, 1000u, true);
}
if (const auto &[r_uid, r_gid] = path_own(log_path);
(uid != -1 && r_uid != uid) || r_gid != 1000u) {
path_chown(log_path, uid, 1000u, true);
}
path_chown(conf_path, uid, 1000u, true);
path_chown(log_path, uid, 1000u, true);

if (pkg_name == kPrimaryInstallerPkgName) {
auto installer_pkg_name_path = GetConfigPath("installer");
Expand Down
2 changes: 1 addition & 1 deletion edxp-core/template_override/post-fs-data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ grep_prop() {
MODDIR=${0%/*}

RIRU_PATH="/data/adb/riru"
RIRU_PROP="/data/adb/modules/riru-core/module.prop"
RIRU_PROP="$(magisk --path)/.magisk/modules/riru-core/module.prop"
TARGET="${RIRU_PATH}/modules"
[[ "$(getenforce)" == "Enforcing" ]] && ENFORCE=true || ENFORCE=false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,40 +81,57 @@ private Map<String, String> loadEnabledModules(int uid) {
File enabledModules = new File(CONFIG_PATH, uid + "/" + ENABLED_MODULES_LIST_FILENAME);
if (!enabledModules.exists()) return result;
Scanner scanner = new Scanner(enabledModules);
if (scanner.hasNextLine()) {
while (scanner.hasNextLine()) {
String packageName = scanner.nextLine();
PackageInfo info = getPackageInfo(packageName);
if (info != null && isXposedModule(info.applicationInfo)) {
if (info != null && isXposedModule(info.applicationInfo))
result.put(packageName, info.applicationInfo.sourceDir);
} else {
Utils.logW(String.format("remove obsolete package %s", packageName));
}
else if (info == null)
result.put(packageName, null);
}
} catch (Throwable e) {
Utils.logE("Unable to read enabled modules", e);
}
return result;
}

private void updateModuleList(int uid, String packageName) {
private boolean updateModuleList(int uid, String packageName) {
Map<String, String> enabledModules = loadEnabledModules(uid);

if(packageName != null && !enabledModules.containsKey(packageName)) return;
if (!enabledModules.containsKey(packageName)) return false;

try {
File moduleListFile = new File(CONFIG_PATH, uid + "/" + MODULES_LIST_FILENAME);
moduleListFile.createNewFile();
File enabledModuleListFile = new File(CONFIG_PATH, uid + "/" + ENABLED_MODULES_LIST_FILENAME);
if (moduleListFile.exists() && !moduleListFile.canWrite()) {
moduleListFile.delete();
moduleListFile.createNewFile();
}
if (enabledModuleListFile.exists() && !enabledModuleListFile.canWrite()) {
enabledModuleListFile.delete();
enabledModuleListFile.createNewFile();
}
PrintWriter modulesList = new PrintWriter(moduleListFile);
PrintWriter enabledModulesList = new PrintWriter(new File(CONFIG_PATH, uid + "/" + ENABLED_MODULES_LIST_FILENAME));
PrintWriter enabledModulesList = new PrintWriter(enabledModuleListFile);
for (Map.Entry<String, String> module : enabledModules.entrySet()) {
modulesList.println(module.getValue());
enabledModulesList.println(module.getKey());
String apkPath = module.getValue();
if (apkPath != null) {
modulesList.println(module.getValue());
enabledModulesList.println(module.getKey());
} else {
Utils.logI(String.format("remove obsolete package %s", packageName));
File prefsDir = new File(CONFIG_PATH, uid + "/prefs/" + packageName);
for (File childFile : prefsDir.listFiles()) {
childFile.delete();
}
}
}
modulesList.close();
enabledModulesList.close();
} catch (Throwable e) {
Utils.logE("Fail to update module list", e);
}
return true;
}

@Override
Expand Down Expand Up @@ -152,12 +169,14 @@ public void onReceive(Context context, Intent intent) {
@SuppressLint("DiscouragedPrivateApi")
Method m = UserManager.class.getDeclaredMethod("getUsers");
m.setAccessible(true);
boolean res = false;
for (Object uh : (List<Object>) m.invoke(um)) {
int uid = (int) uh.getClass().getDeclaredField("id").get(uh);
Utils.logI("updating uid: " + uid);
updateModuleList(uid, pkgInfo == null ? null : packageName);
res = res || updateModuleList(uid, packageName);
}
Toast.makeText(context, "EdXposed: Updated " + packageName, Toast.LENGTH_SHORT).show();
if (res)
Toast.makeText(context, "EdXposed: Updated " + packageName, Toast.LENGTH_SHORT).show();
} catch (Throwable e) {
Utils.logW("update failed", e);
}
Expand Down

0 comments on commit f51e71d

Please sign in to comment.