Server Daemon Core
Overview
The server module builds the Shizuku daemon library (server/build.gradle) with an empty Android manifest (server/…/AndroidManifest.xml) because the daemon is started as a privileged Java process rather than a normal app component. Its main entry point is server/…/ShizukuService.java: main() names the process shizuku_server, sets the Rish native library path from shizuku.library.path, prepares the main looper, constructs ShizukuService, then enters Looper.loop().
ShizukuService is the server-side binder implementation for the Shizuku API. During construction it installs the main handler into shared handler utilities, waits for core framework services (package, activity, user, and appops), resolves the manager application (moe.shizuku.privileged.api from server/…/ServerConstants.java), initializes the config and client managers, starts an APK delete observer for the manager APK, registers process/UID observers, then posts initial binder delivery to all eligible clients and manager installs. If the manager package disappears from user 0, the daemon exits with MANAGER_APP_NOT_FOUND.
Client attachment and permission state are handled in ShizukuService.attachApplication(). The method verifies that the requested package belongs to the calling UID before adding a ClientRecord through server/…/ShizukuClientManager.java (a thin specialization of the shared ClientManager). It replies with server UID, API/server version, patch version, SELinux context, and the per-client allowed flag. API version -1 is intentionally answered as server version 12 to preserve compatibility with older API 12.2.0 clients that use a newer binder wrapper. Manager callers are identified by app-id match with the resolved manager UID and are also granted WRITE_SECURE_SETTINGS for their user.
Permission confirmation is manager-mediated. ShizukuService.showPermissionConfirmation() starts the manager's REQUEST_PERMISSION_ACTION activity with the requesting UID/PID, request code, and ApplicationInfo; for work-profile users it starts in user 0 when the manager is not installed inside the profile. dispatchPermissionConfirmationResult() accepts results only from the manager app-id, updates in-memory client records, optionally persists non-one-time decisions, and grants or revokes moe.shizuku.manager.permission.API_V23 for packages under the requested UID that declare it. updateFlagsForUid() is the manager-side administration API: it updates live ClientRecord.allowed, force-stops packages and removes their user services when permission is revoked, mirrors runtime permission grant/revoke, then persists flags.
Persistent permission state lives in server/…/ShizukuConfigManager.java and server/…/ShizukuConfig.java. The JSON file is data/…/shizuku.json, written through AtomicFile after a 10-second delay on the worker handler from server/…/Handler.kt. On startup, ShizukuConfigManager loads existing JSON, normalizes missing packages lists, removes entries for UIDs whose package set no longer matches, deduplicates recorded package names, and imports current runtime permission grants for installed packages that request the Shizuku permission. Each config entry stores a UID, bit flags, and package names; find(), update(), and remove() synchronize on the manager instance.
Binder delivery to apps is content-provider based. ShizukuService.sendBinderToUserApp() obtains <package>.shizuku through ActivityManagerApis.getContentProviderExternal(), wraps the daemon binder in BinderContainer, and calls provider method sendBinder using server/…/IContentProviderUtils.java, which selects the correct IContentProvider.call() signature for Android versions from pre-Q through S+. Before contacting the provider, the server temporarily whitelists the package from power saving for 30 seconds. If the provider binder is dead, it force-stops the package and retries once. Initial delivery scans all users for packages declaring moe.shizuku.manager.permission.API_V23 and sends separately to the manager package in every user.
server/…/BinderSender.java keeps binder delivery fresh after startup by registering process and UID observers with ActivityManager. Foreground/process-state callbacks and UID active/cached/idle callbacks deduplicate seen PIDs or UIDs, resolve packages for the UID, then send the daemon binder either to the manager provider when the UID has moe.shizuku.manager.permission.MANAGER granted or to a normal client provider when a package declares moe.shizuku.manager.permission.API_V23. UID gone callbacks remove the UID from the dedupe list so later starts can receive the binder again.
User-service support is specialized in server/…/ShizukuUserServiceManager.java. It builds launch commands with /system/bin/app_process or /system/bin/app_process32 and ServiceStarter.commandForUserService(), using the current manager APK source path as the classpath. For each user-service record, it registers an APK-change listener on the service package's APK path. If that package is later removed from all users, the record removes itself; if the APK path changes during upgrade, the listener is rebound to the new source path so the service can survive package upgrades.
APK change observation is centralized in server/…/ApkChangedObservers.kt. Because inotify watches inodes and DELETE_SELF can be missed while processes still hold the APK, observers watch the parent directory for DELETE and notify listeners only when the deleted entry is base.apk. Observers are shared by parent path and stopped when their listener set becomes empty.
ShizukuService.onTransact() also exposes server-specific transaction ServerConstants.BINDER_TRANSACTION_getApplications (10001). It returns a ParcelableListSlice<PackageInfo> for one user or all users, including packages that are already configured or that declare both the V3 support metadata flag and the Shizuku runtime permission, while excluding the manager package itself.
Coupling
- If server/…/ServerConstants.java changes
MANAGER_APPLICATION_ID,PERMISSION, orREQUEST_PERMISSION_ACTION, verify all manager/client provider and permission paths in server/…/ShizukuService.java and server/…/BinderSender.java; these files use literal permission strings and package names to decide who receives the daemon binder and who may administer permissions. - If the client content-provider authority, method name, or binder extra changes, update
ShizukuService.sendBinderToUserApp()and the client/provider contract together. The server currently assumes authority<package>.shizuku, methodsendBinder, and extra keymoe.shizuku.privileged.api.intent.extra.BINDERwithout schema negotiation. - If Android hidden
IContentProvider.call()signatures or attribution requirements change, update server/…/IContentProviderUtils.java before touching binder delivery.ShizukuService.sendBinderToUserApp()routes every provider call through this compatibility shim. - If config flags or JSON shape change, update both server/…/ShizukuConfig.java and server/…/ShizukuConfigManager.java. The manager writes with a Gson version of
ShizukuConfig.LATEST_VERSION, but startup cleanup and permission import logic assume each entry hasuid,flags, and a non-null package-name list. - If APK layout changes away from
base.apk, update server/…/ApkChangedObservers.kt and the user-service listener flow in server/…/ShizukuUserServiceManager.java; current observers ignore every deleted filename exceptbase.apk. - If user-service startup options change in the shared API, verify
ShizukuUserServiceManager.getUserServiceStartCmd(). It chooses the app_process binary locally but delegates the actual command layout and token/class/process arguments toServiceStarter.commandForUserService().
Risks
- Binder delivery is fragile around external content providers in server/…/ShizukuService.java and server/…/IContentProviderUtils.java: if
getContentProviderExternal()returns a dead provider or Android changesIContentProvider.call(), clients never receive the daemon binder. The history shows repeated fixes in this area, includingRetry getContentProviderExternal,Update IContentProviderUtils, and an Android 12 beta signature fix. - APK-change handling in server/…/ApkChangedObservers.kt and server/…/ShizukuUserServiceManager.java is sensitive to package-manager filesystem behavior. If an upgrade/delete event is missed or the deleted filename is not
base.apk, stale user-service records can remain or the manager-uninstall exit path may not fire; commit history contains multipleFix ApkChangedObserverschanges and a laterKeep existing user service if package upgradesadjustment. - Permission revocation spans in-memory clients, runtime permissions, force-stopping apps, and user-service cleanup in server/…/ShizukuService.java. If one leg is changed without the others, revoked apps may keep running user services or retain binder access until restart; history includes fixes for
updateFlagsForUidand killing user services after revocation. - Multi-user behavior is easy to regress in server/…/ShizukuService.java because binder delivery, permission confirmation, manager lookup, and package scans all take user IDs from different sources. Prior fixes mention secondary-user permission requests and multi-user user-service removal, so changes here should be tested across primary, secondary, and managed-profile users.