diff --git a/daemon/src/main/kotlin/org/matrix/vector/daemon/VectorService.kt b/daemon/src/main/kotlin/org/matrix/vector/daemon/VectorService.kt index aa22bc6a1..cd97f85db 100644 --- a/daemon/src/main/kotlin/org/matrix/vector/daemon/VectorService.kt +++ b/daemon/src/main/kotlin/org/matrix/vector/daemon/VectorService.kt @@ -532,17 +532,26 @@ object VectorService : IVectorDaemon.Stub() { val scopes = ModuleDatabase.getModuleScope(packageName) ?: mutableListOf() var added = false granted.forEach { scopePackageName -> + // Spelled the way the row will be stored: a module asks for the framework under the + // libxposed name `android`, and a row under that spelling used to be invisible to + // the system_server reader. [ModuleDatabase.setModuleScope] normalizes on the way + // in as well; this is spelled up front so the duplicate check below compares like + // with like. + val storedName = + if (scopePackageName == ModuleDatabase.SYSTEM_FRAMEWORK_ALIAS) + ModuleDatabase.SYSTEM_FRAMEWORK_PACKAGE + else scopePackageName // Compared against where the row will land, not against the user who asked: the // framework is stored under user 0 whoever requested it, so for "system" this test // never matched and every approval appended a duplicate and rewrote the whole // table. - val storedUserId = if (scopePackageName == "system") 0 else userId + val storedUserId = if (storedName == ModuleDatabase.SYSTEM_FRAMEWORK_PACKAGE) 0 else userId val present = - scopes.any { it.packageName == scopePackageName && it.userId == storedUserId } + scopes.any { it.packageName == storedName && it.userId == storedUserId } if (!present) { scopes.add( ScopeEntry().apply { - this.packageName = scopePackageName + this.packageName = storedName this.userId = storedUserId }) added = true diff --git a/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ConfigCache.kt b/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ConfigCache.kt index b79aa1181..6054225ef 100644 --- a/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ConfigCache.kt +++ b/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ConfigCache.kt @@ -341,14 +341,19 @@ object ConfigCache { } ModuleDatabase.enabledScopeRows().forEach { scopeRow -> - val appPkg = scopeRow.appPackage + val appPkg = + // The framework scope answers to two spellings in the wild (the manager's `system` and + // the libxposed spelling `android`); both name the same target here. + if (scopeRow.appPackage == ModuleDatabase.SYSTEM_FRAMEWORK_ALIAS) + ModuleDatabase.SYSTEM_FRAMEWORK_PACKAGE + else scopeRow.appPackage val modPkg = scopeRow.modulePackage val userId = scopeRow.userId val module = newModules[modPkg] ?: return@forEach val holders = moduleUsers[modPkg].orEmpty() - if (appPkg == "system") { + if (appPkg == ModuleDatabase.SYSTEM_FRAMEWORK_PACKAGE) { // system_server is one process for the whole device and belongs to no user, so any user // holding the module may hook it and the row is stored under user 0 whoever asked. It is // the one target the boundary below does not apply to, because there is no second copy @@ -502,7 +507,16 @@ object ConfigCache { val pkgName = row.packageName // A row with no recorded path has never been resolved; the rebuild will fill it in, // and injecting from a null path is not something to attempt in the meantime. - val apkPath = row.apkPath ?: return@forEach + val apkPath = row.apkPath + if (apkPath == null) { + // The silent version of this skip sent issue #1101's reporter looking at the dispatch + // chain, because nothing anywhere said the module was never served. + Log.w( + TAG, + "Module $pkgName is scoped to the system framework but its APK path is not " + + "resolved yet; it is not served to system_server this boot") + return@forEach + } val cached = currentState.modules[pkgName] if (cached != null) { diff --git a/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ModuleDatabase.kt b/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ModuleDatabase.kt index 733b2c3ee..aad048e0d 100644 --- a/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ModuleDatabase.kt +++ b/daemon/src/main/kotlin/org/matrix/vector/daemon/data/ModuleDatabase.kt @@ -36,6 +36,17 @@ private const val TAG = "VectorModuleDatabase" */ object ModuleDatabase { + /** + * The package name the system framework is stored under. The manager uses this spelling; the + * libxposed API, the modules asking for the scope through it, and the CLI (which forwards what + * the user typed) all use [SYSTEM_FRAMEWORK_ALIAS] for the same target — and a scope stored + * under that spelling used to be invisible to the one reader that serves system_server, leaving + * every android-scoped module unloaded without a word. Writes are normalized to this spelling, + * and the readers accept both. + */ + const val SYSTEM_FRAMEWORK_PACKAGE = "system" + const val SYSTEM_FRAMEWORK_ALIAS = "android" + /** The one database handle. [ConfigCache], [PreferenceStore] and the CLI all borrow it. */ val dbHelper = Database() @@ -55,7 +66,12 @@ object ModuleDatabase { while (cursor.moveToNext()) { result.add( ScopeEntry().apply { - this.packageName = cursor.getString(0) + this.packageName = + cursor.getString(0).let { + // One spelling out, so the manager sees its framework entry where a + // pre-normalization row spells it differently. + if (it == SYSTEM_FRAMEWORK_ALIAS) SYSTEM_FRAMEWORK_PACKAGE else it + } this.userId = cursor.getInt(1) }) } @@ -182,8 +198,8 @@ object ModuleDatabase { .query( "scope INNER JOIN modules ON scope.mid = modules.mid", arrayOf("module_pkg_name", "apk_path"), - "app_pkg_name=? AND enabled=1", - arrayOf("system"), + "app_pkg_name IN (?, ?) AND enabled=1", + arrayOf(SYSTEM_FRAMEWORK_PACKAGE, SYSTEM_FRAMEWORK_ALIAS), null, null, null) @@ -192,7 +208,9 @@ object ModuleDatabase { rows += EnabledModuleRow(cursor.getString(0), cursor.getString(1)) } } - return rows + // A module scoped under both spellings (a CLI row from before the writes were normalized, then + // the manager's) must be served once: twice in this list is twice loaded into system_server. + return rows.distinctBy { it.packageName } } /** @@ -319,8 +337,15 @@ object ModuleDatabase { // a backup written by an older manager, which recorded the framework under the module's // own user, silently lost the one target the module may have cared about — and said // nothing about it. - val userId = if (app.packageName == "system") 0 else app.userId - values.put("app_pkg_name", app.packageName) + // + // The framework target is normalized to one spelling here: the manager says `system`, the + // libxposed API, the modules and the CLI say `android`, and a row stored under the alias + // spelling used to match no reader at all. + val appPackage = + if (app.packageName == SYSTEM_FRAMEWORK_ALIAS) SYSTEM_FRAMEWORK_PACKAGE + else app.packageName + val userId = if (appPackage == SYSTEM_FRAMEWORK_PACKAGE) 0 else app.userId + values.put("app_pkg_name", appPackage) values.put("user_id", userId) db.insertWithOnConflict("scope", null, values, SQLiteDatabase.CONFLICT_IGNORE) } @@ -369,16 +394,32 @@ object ModuleDatabase { // matches no row at all - which meant a module outside user 0 could take system scope and then // never give it back. It reached that state through the very same call: removeScope returns // nothing, so the module was told its request had been honoured. - val storedUserId = if (scopePackageName == "system") 0 else userId + // + // Both spellings go, not just the caller's: a row written before the writes were normalized + // must not survive a removal the caller believes removed it. + val isFrameworkTarget = + scopePackageName == SYSTEM_FRAMEWORK_PACKAGE || scopePackageName == SYSTEM_FRAMEWORK_ALIAS + val storedUserId = if (isFrameworkTarget) 0 else userId val db = dbHelper.writableDatabase val mid = db.compileStatement("SELECT mid FROM modules WHERE module_pkg_name = ?") .apply { bindString(1, packageName) } .simpleQueryForLong() - db.delete( - "scope", - "mid = ? AND app_pkg_name = ? AND user_id = ?", - arrayOf(mid.toString(), scopePackageName, storedUserId.toString())) + if (isFrameworkTarget) { + db.delete( + "scope", + "mid = ? AND app_pkg_name IN (?, ?) AND user_id = ?", + arrayOf( + mid.toString(), + SYSTEM_FRAMEWORK_PACKAGE, + SYSTEM_FRAMEWORK_ALIAS, + storedUserId.toString())) + } else { + db.delete( + "scope", + "mid = ? AND app_pkg_name = ? AND user_id = ?", + arrayOf(mid.toString(), scopePackageName, storedUserId.toString())) + } ConfigCache.requestCacheUpdate() return true }