Skip to content

Commit c4e2985

Browse files
committed
Migrate resetprop to Rust
1 parent 42d9f87 commit c4e2985

21 files changed

Lines changed: 675 additions & 652 deletions

native/src/Android.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ LOCAL_SRC_FILES := \
2222
core/sqlite.cpp \
2323
core/thread.cpp \
2424
core/core-rs.cpp \
25-
core/resetprop/resetprop.cpp \
25+
core/resetprop/sys.cpp \
2626
core/su/su.cpp \
2727
core/zygisk/entry.cpp \
2828
core/zygisk/module.cpp \
@@ -123,7 +123,7 @@ LOCAL_STATIC_LIBRARIES := \
123123

124124
LOCAL_SRC_FILES := \
125125
core/applet_stub.cpp \
126-
core/resetprop/resetprop.cpp \
126+
core/resetprop/sys.cpp \
127127
core/core-rs.cpp
128128

129129
LOCAL_CFLAGS := -DAPPLET_STUB_MAIN=resetprop_main

native/src/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

native/src/core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ quick-protobuf = { workspace = true }
2525
bytemuck = { workspace = true, features = ["derive"] }
2626
thiserror = { workspace = true }
2727
bit-set = { workspace = true }
28+
argh = { workspace = true }

native/src/core/applet_stub.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
#include <sys/stat.h>
2-
3-
#include <consts.hpp>
4-
#include <base.hpp>
1+
#include <core.hpp>
52

63
int main(int argc, char *argv[]) {
74
if (argc < 1)

native/src/core/applets.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#include <libgen.h>
2-
#include <sys/types.h>
32
#include <sys/stat.h>
43

5-
#include <consts.hpp>
6-
#include <base.hpp>
4+
#include <core.hpp>
75

86
using namespace std;
97

native/src/core/daemon.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ use crate::consts::{MAGISK_FULL_VER, MAGISK_PROC_CON, MAIN_CONFIG, ROOTMNT, ROOT
22
use crate::db::Sqlite3;
33
use crate::ffi::{
44
DbEntryKey, ModuleInfo, RequestCode, check_key_combo, exec_common_scripts, exec_module_scripts,
5-
get_magisk_tmp, get_prop, initialize_denylist, set_prop, setup_magisk_env,
5+
get_magisk_tmp, initialize_denylist, setup_magisk_env,
66
};
77
use crate::logging::{magisk_logging, setup_logfile, start_log_daemon};
88
use crate::module::disable_modules;
99
use crate::mount::{clean_mounts, setup_preinit_dir};
1010
use crate::package::ManagerInfo;
11+
use crate::resetprop::{get_prop, set_prop};
1112
use crate::selinux::restore_tmpcon;
1213
use crate::su::SuInfo;
1314
use crate::zygisk::ZygiskState;
@@ -121,8 +122,8 @@ impl MagiskD {
121122
.log()
122123
.ok();
123124
let safe_mode = boot_cnt >= 2
124-
|| get_prop(cstr!("persist.sys.safemode"), true) == "1"
125-
|| get_prop(cstr!("ro.sys.safemode"), false) == "1"
125+
|| get_prop(cstr!("persist.sys.safemode")) == "1"
126+
|| get_prop(cstr!("ro.sys.safemode")) == "1"
126127
|| check_key_combo();
127128

128129
if safe_mode {
@@ -232,9 +233,9 @@ pub fn daemon_entry() {
232233
magisk_logging();
233234
info!("Magisk {} daemon started", MAGISK_FULL_VER);
234235

235-
let is_emulator = get_prop(cstr!("ro.kernel.qemu"), false) == "1"
236-
|| get_prop(cstr!("ro.boot.qemu"), false) == "1"
237-
|| get_prop(cstr!("ro.product.device"), false).contains("vsoc");
236+
let is_emulator = get_prop(cstr!("ro.kernel.qemu")) == "1"
237+
|| get_prop(cstr!("ro.boot.qemu")) == "1"
238+
|| get_prop(cstr!("ro.product.device")).contains("vsoc");
238239

239240
// Load config status
240241
let magisk_tmp = get_magisk_tmp();
@@ -265,7 +266,7 @@ pub fn daemon_entry() {
265266
}
266267
if sdk_int < 0 {
267268
// In case some devices do not store this info in build.prop, fallback to getprop
268-
sdk_int = get_prop(cstr!("ro.build.version.sdk"), false)
269+
sdk_int = get_prop(cstr!("ro.build.version.sdk"))
269270
.parse::<i32>()
270271
.unwrap_or(-1);
271272
}
@@ -278,13 +279,13 @@ pub fn daemon_entry() {
278279
switch_cgroup("/acct", pid);
279280
switch_cgroup("/dev/cg2_bpf", pid);
280281
switch_cgroup("/sys/fs/cgroup", pid);
281-
if get_prop(cstr!("ro.config.per_app_memcg"), false) != "false" {
282+
if get_prop(cstr!("ro.config.per_app_memcg")) != "false" {
282283
switch_cgroup("/dev/memcg/apps", pid);
283284
}
284285

285286
// Samsung workaround #7887
286287
if cstr!("/system_ext/app/mediatek-res/mediatek-res.apk").exists() {
287-
set_prop(cstr!("ro.vendor.mtk_model"), cstr!("0"), false);
288+
set_prop(cstr!("ro.vendor.mtk_model"), cstr!("0"));
288289
}
289290

290291
// Cleanup pre-init mounts
@@ -346,14 +347,14 @@ fn check_data() -> bool {
346347
if !mnt {
347348
return false;
348349
}
349-
let crypto = get_prop(cstr!("ro.crypto.state"), false);
350+
let crypto = get_prop(cstr!("ro.crypto.state"));
350351
return if !crypto.is_empty() {
351352
if crypto != "encrypted" {
352353
// Unencrypted, we can directly access data
353354
true
354355
} else {
355356
// Encrypted, check whether vold is started
356-
!get_prop(cstr!("init.svc.vold"), false).is_empty()
357+
!get_prop(cstr!("init.svc.vold")).is_empty()
357358
}
358359
} else {
359360
// ro.crypto.state is not set, assume it's unencrypted

native/src/core/include/core.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
#define to_app_id(uid) (uid % AID_USER_OFFSET)
1818
#define to_user_id(uid) (uid / AID_USER_OFFSET)
1919

20+
// Multi-call entrypoints
21+
int magisk_main(int argc, char *argv[]);
22+
int su_client_main(int argc, char *argv[]);
23+
int zygisk_main(int argc, char *argv[]);
24+
2025
// Return codes for daemon
2126
enum class RespondCode : int {
2227
ERROR = -1,

native/src/core/include/resetprop.hpp

Lines changed: 0 additions & 30 deletions
This file was deleted.

native/src/core/lib.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use derive::Decodable;
1515
use logging::{android_logging, setup_logfile, zygisk_close_logd, zygisk_get_logd, zygisk_logging};
1616
use module::remove_modules;
1717
use mount::{find_preinit_device, revert_unmount};
18-
use resetprop::{persist_delete_prop, persist_get_prop, persist_get_props, persist_set_prop};
18+
use resetprop::{get_prop, resetprop_main};
1919
use selinux::{lgetfilecon, lsetfilecon, restorecon, setfilecon};
2020
use socket::{recv_fd, recv_fds, send_fd, send_fds};
2121
use std::fs::File;
@@ -164,19 +164,6 @@ pub mod ffi {
164164
fn get_text(self: &DbValues, index: i32) -> &str;
165165
fn bind_text(self: Pin<&mut DbStatement>, index: i32, val: &str) -> i32;
166166
fn bind_int64(self: Pin<&mut DbStatement>, index: i32, val: i64) -> i32;
167-
168-
include!("include/resetprop.hpp");
169-
170-
#[cxx_name = "prop_callback"]
171-
type PropCallback;
172-
fn exec(self: Pin<&mut PropCallback>, name: Utf8CStrRef, value: Utf8CStrRef);
173-
174-
#[cxx_name = "get_prop_rs"]
175-
fn get_prop(name: Utf8CStrRef, persist: bool) -> String;
176-
#[cxx_name = "set_prop_rs"]
177-
fn set_prop(name: Utf8CStrRef, value: Utf8CStrRef, skip_svc: bool) -> i32;
178-
#[cxx_name = "load_prop_file_rs"]
179-
fn load_prop_file(filename: Utf8CStrRef, skip_svc: bool);
180167
}
181168

182169
extern "Rust" {
@@ -189,10 +176,6 @@ pub mod ffi {
189176
fn revert_unmount(pid: i32);
190177
fn remove_modules();
191178
fn zygisk_should_load_module(flags: u32) -> bool;
192-
unsafe fn persist_get_prop(name: Utf8CStrRef, prop_cb: Pin<&mut PropCallback>);
193-
unsafe fn persist_get_props(prop_cb: Pin<&mut PropCallback>);
194-
unsafe fn persist_delete_prop(name: Utf8CStrRef) -> bool;
195-
unsafe fn persist_set_prop(name: Utf8CStrRef, value: Utf8CStrRef) -> bool;
196179
fn send_fd(socket: i32, fd: i32) -> bool;
197180
fn send_fds(socket: i32, fds: &[i32]) -> bool;
198181
fn recv_fd(socket: i32) -> i32;
@@ -206,6 +189,9 @@ pub mod ffi {
206189
fn setfilecon(path: Utf8CStrRef, con: Utf8CStrRef) -> bool;
207190
fn lsetfilecon(path: Utf8CStrRef, con: Utf8CStrRef) -> bool;
208191

192+
fn get_prop(name: Utf8CStrRef) -> String;
193+
unsafe fn resetprop_main(argc: i32, argv: *mut *mut c_char) -> i32;
194+
209195
#[namespace = "rust"]
210196
fn daemon_entry();
211197
}

native/src/core/module.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::consts::{MODULEMNT, MODULEROOT, MODULEUPGRADE, WORKERDIR};
22
use crate::daemon::MagiskD;
3-
use crate::ffi::{ModuleInfo, exec_module_scripts, exec_script, get_magisk_tmp, load_prop_file};
3+
use crate::ffi::{ModuleInfo, exec_module_scripts, exec_script, get_magisk_tmp};
44
use crate::mount::setup_module_mount;
5+
use crate::resetprop::load_prop_file;
56
use base::{
67
DirEntry, Directory, FsPathBuilder, LibcReturn, LoggedResult, OsResultStatic, ResultExt,
78
SilentLogExt, Utf8CStr, Utf8CStrBuf, Utf8CString, WalkResult, clone_attr, cstr, debug, error,
@@ -804,8 +805,7 @@ impl MagiskD {
804805
// Read props
805806
let prop = module_paths.append("system.prop");
806807
if prop.module().exists() {
807-
// Do NOT go through property service as it could cause boot lock
808-
load_prop_file(prop.module(), true);
808+
load_prop_file(prop.module());
809809
}
810810
}
811811
{

0 commit comments

Comments
 (0)