diff --git a/app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt b/app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt index adc0d8c46..d2e09c51f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt @@ -12,7 +12,6 @@ import com.topjohnwu.superuser.Shell import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import timber.log.Timber -import java.io.Closeable import java.io.DataOutputStream import java.io.FileOutputStream import java.io.IOException @@ -21,11 +20,10 @@ import java.util.concurrent.TimeUnit class SuRequestHandler( val pm: PackageManager, private val policyDB: PolicyDao -) : Closeable { +) { private lateinit var output: DataOutputStream - lateinit var policy: SuPolicy - private set + private lateinit var policy: SuPolicy lateinit var pkgInfo: PackageInfo private set @@ -54,35 +52,36 @@ class SuRequestHandler( return true } - override fun close() { + private fun close() { if (::output.isInitialized) runCatching { output.close() } } - private class SuRequestError : IOException() - private suspend fun init(intent: Intent) = withContext(Dispatchers.IO) { try { - val fifo = intent.getStringExtra("fifo") ?: throw SuRequestError() - val uid = intent.getIntExtra("uid", -1).also { if (it < 0) throw SuRequestError() } - val pid = intent.getIntExtra("pid", -1) - pkgInfo = pm.getPackageInfo(uid, pid) ?: PackageInfo().apply { - val name = pm.getNameForUid(uid) ?: throw SuRequestError() - // We only fill in sharedUserId and leave other fields uninitialized - sharedUserId = name.split(":")[0] + val fifo = intent.getStringExtra("fifo") ?: throw IOException("fifo == null") + output = DataOutputStream(FileOutputStream(fifo)) + val uid = intent.getIntExtra("uid", -1) + if (uid <= 0) { + throw IOException("uid == $uid") } - output = DataOutputStream(FileOutputStream(fifo).buffered()) policy = SuPolicy(uid) - true - } catch (e: Exception) { - when (e) { - is IOException, is PackageManager.NameNotFoundException -> { - Timber.e(e) - close() - false + val pid = intent.getIntExtra("pid", -1) + try { + pkgInfo = pm.getPackageInfo(uid, pid) ?: PackageInfo().apply { + val name = pm.getNameForUid(uid) ?: throw PackageManager.NameNotFoundException() + // We only fill in sharedUserId and leave other fields uninitialized + sharedUserId = name.split(":")[0] } - else -> throw e // Unexpected error + return@withContext true + } catch (e: PackageManager.NameNotFoundException) { + respond(SuPolicy.DENY, -1) + return@withContext false } + } catch (e: IOException) { + Timber.e(e) + close() + return@withContext false } }