$ show topjohnwu/Magisk
Magisk is a suite of open-source tools for customizing Android devices, providing systemless root access (MagiskSU), a module framework, boot image patching (MagiskBoot), init process hijacking (MagiskInit), SELinux policy modification (MagiskPolicy), and Zygisk (code injection into the Zygote process). The system is composed of a native daemon (magiskd) written in Rust and C++ that communicates with an Android management app (Magisk Manager) via Unix sockets. The app is built with Kotlin and Jetpack Compose (modern UI) alongside a legacy XML/DataBinding variant, and includes a stub APK mechanism for hiding the app from detection. Key entry points include the native binaries (magisk, magiskinit, magiskboot, magiskpolicy, resetprop, su) and the Android app's MainActivity and SuRequestActivity. The build system is Gradle with custom plugins, orchestrating compilation of Rust, C++, and Kotlin components. Data flows involve the app fetching module updates from GitHub repositories, downloading and installing modules, managing superuser policies stored in a SQLite database (magisk.db), and logging su requests. The daemon handles root requests, module mounting, denylist enforcement, and Zygisk module loading. External dependencies include the Android SDK, NDK, Rust toolchain, and libraries such as Retrofit, OkHttp, Room, Moshi, Wire, and libsu. The codebase demonstrates high craftsmanship with elegant architecture, reusable components (e.g., a standalone CLI argument parsing proc-macro, a full terminal emulator, a download engine), and non-obvious engineering solutions for low-level system manipulation. However, it carries maintenance risks due to its multi-language complexity, reliance on Android internals, and dual UI codebase.
The LLM identified 5 files worth highlighting for reuse:
A standalone proc-macro crate for deriving `FromArgs` on structs, providing declarative CLI argument parsing with subcommands, options, and positional arguments. It is a modified fork of the `argh` crate, demonstrating deep Rust macro expertise. Fully self-contained and could be extracted into any Rust project needing CLI parsing.
A recursive descent parser for SELinux policy statements (allow, deny, type_transition, etc.). It handles complex grammar with braces, commas, wildcards, and extended permissions. Well-structured with clear error handling; reusable in any project that needs to parse or generate SELinux policy rules.
A full VT100/xterm terminal emulator implemented in Kotlin, handling all major ANSI escape sequences, cursor movement, scrolling, and color/attribute changes. Uses a state machine pattern and is self-contained with its own buffer and style representation. Could be extracted into any Android app needing an embedded terminal.
A download engine that manages concurrent downloads with a single session, supporting foreground service (API 23-33) and user-initiated jobs (API 34+). Handles progress notifications, error recovery, and module zip repackaging. The pattern is reusable for any Android app needing robust download management.
Provides `Utf8CStr`, `Utf8CString`, and `Utf8CStrBuf` types for safe UTF-8 C string handling in Rust FFI. Ensures null-termination and proper encoding, reducing common FFI errors. Self-contained and reusable in any Rust project interfacing with C code.