Fix SU log and notification missing for first-time/denied requests

The native daemon skips app_log/app_notify callbacks when the stored
RootSettings defaults log=false and notify=false (first-time requests).
Handle logging and notification directly from SuRequestHandler.respond()
on the Java side so both grants and denials are always recorded.

Made-with: Cursor
This commit is contained in:
LoveSy
2026-03-16 02:56:02 -07:00
committed by topjohnwu
parent 73c3cfc7f3
commit 6e9e12e4b2
2 changed files with 44 additions and 1 deletions
@@ -3,6 +3,7 @@ package com.topjohnwu.magisk.core.su
import android.content.Context
import android.os.Bundle
import android.widget.Toast
import com.topjohnwu.magisk.core.AppContext
import com.topjohnwu.magisk.core.BuildConfig
import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.R
@@ -93,6 +94,18 @@ object SuCallbackHandler {
SuEvents.notifyPolicyChanged()
}
fun notify(granted: Boolean, appName: String) {
when (Config.suNotification) {
Config.Value.NOTIFICATION_TOAST -> {
val resId = if (granted) R.string.su_allow_toast else R.string.su_deny_toast
AppContext.toast(AppContext.getString(resId, appName), Toast.LENGTH_SHORT)
}
Config.Value.NOTIFICATION_STATUS_BAR -> {
Notifications.suNotification(granted, appName)
}
}
}
private fun notify(context: Context, granted: Boolean, appName: String) {
when (Config.suNotification) {
Config.Value.NOTIFICATION_TOAST -> {
@@ -6,8 +6,12 @@ import android.content.pm.PackageManager
import com.topjohnwu.magisk.core.BuildConfig
import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.data.magiskdb.PolicyDao
import com.topjohnwu.magisk.core.di.ServiceLocator
import com.topjohnwu.magisk.core.ktx.getLabel
import com.topjohnwu.magisk.core.ktx.getPackageInfo
import com.topjohnwu.magisk.core.model.su.SuLog
import com.topjohnwu.magisk.core.model.su.SuPolicy
import com.topjohnwu.magisk.view.Notifications
import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@@ -25,6 +29,7 @@ class SuRequestHandler(
private lateinit var output: File
private lateinit var policy: SuPolicy
private var pid: Int = -1
lateinit var pkgInfo: PackageInfo
private set
@@ -55,7 +60,7 @@ class SuRequestHandler(
private suspend fun init(intent: Intent): Boolean {
val uid = intent.getIntExtra("uid", -1)
val pid = intent.getIntExtra("pid", -1)
pid = intent.getIntExtra("pid", -1)
val fifo = intent.getStringExtra("fifo")
if (uid <= 0 || pid <= 0 || fifo == null) {
Timber.e("Unexpected extras: uid=[${uid}], pid=[${pid}], fifo=[${fifo}]")
@@ -104,7 +109,32 @@ class SuRequestHandler(
}
if (time >= 0) {
policyDB.update(policy)
val appInfo = pkgInfo.applicationInfo
val appName = appInfo?.getLabel(pm)
?: pkgInfo.sharedUserId ?: "[UID] ${policy.uid}"
val packageName = appInfo?.let { pm.getNameForUid(it.uid) }
?: pkgInfo.sharedUserId ?: "[UID] ${policy.uid}"
val log = SuLog(
fromUid = policy.uid,
toUid = 0,
fromPid = pid,
packageName = packageName,
appName = appName,
command = "",
action = policy.policy,
target = -1,
context = "",
gids = "",
)
ServiceLocator.logRepo.insert(log)
val granted = policy.policy >= SuPolicy.ALLOW
SuCallbackHandler.notify(granted, appName)
SuEvents.notifyPolicyChanged()
SuEvents.notifyLogUpdated()
}
}
}