Skip to content

Commit 42d9f87

Browse files
committed
Cleanup resetprop code
1 parent 2e4fa68 commit 42d9f87

7 files changed

Lines changed: 71 additions & 83 deletions

File tree

native/src/core/daemon.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <sys/mount.h>
55
#include <sys/sysmacros.h>
66
#include <linux/input.h>
7+
#include <map>
78

89
#include <consts.hpp>
910
#include <base.hpp>

native/src/core/deny/logcat.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <android/log.h>
33
#include <sys/syscall.h>
44
#include <string>
5+
#include <map>
56

67
#include <core.hpp>
78

native/src/core/deny/utils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <fcntl.h>
66
#include <dirent.h>
77
#include <set>
8+
#include <map>
89

910
#include <consts.hpp>
1011
#include <sqlite.hpp>
Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
#pragma once
22

33
#include <string>
4-
#include <map>
54
#include <cxx.h>
65

7-
struct prop_cb {
8-
virtual void exec(const char *name, const char *value, uint32_t serial) = 0;
9-
};
10-
11-
using prop_list = std::map<std::string, std::string>;
6+
struct prop_info;
127

13-
struct prop_collector : prop_cb {
14-
explicit prop_collector(prop_list &list) : list(list) {}
15-
void exec(const char *name, const char *value, uint32_t) override {
16-
list.insert({name, value});
8+
struct prop_callback {
9+
virtual ~prop_callback() = default;
10+
virtual void exec(const char *name, const char *value, uint32_t serial) = 0;
11+
void exec(Utf8CStr name, Utf8CStr value) {
12+
exec(name.data(), value.data(), INT_MAX);
1713
}
18-
private:
19-
prop_list &list;
14+
void read(const prop_info *pi);
2015
};
2116

2217
// System properties
@@ -32,7 +27,4 @@ static inline int set_prop_rs(Utf8CStr name, Utf8CStr value, bool skip_svc) {
3227
}
3328
static inline void load_prop_file_rs(Utf8CStr filename, bool skip_svc) {
3429
load_prop_file(filename.data(), skip_svc);
35-
}
36-
static inline void prop_cb_exec(prop_cb &cb, Utf8CStr name, Utf8CStr value, uint32_t serial) {
37-
cb.exec(name.data(), value.data(), serial);
3830
}

native/src/core/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,16 @@ pub mod ffi {
167167

168168
include!("include/resetprop.hpp");
169169

170-
#[cxx_name = "prop_cb"]
171-
type PropCb;
170+
#[cxx_name = "prop_callback"]
171+
type PropCallback;
172+
fn exec(self: Pin<&mut PropCallback>, name: Utf8CStrRef, value: Utf8CStrRef);
173+
172174
#[cxx_name = "get_prop_rs"]
173175
fn get_prop(name: Utf8CStrRef, persist: bool) -> String;
174176
#[cxx_name = "set_prop_rs"]
175177
fn set_prop(name: Utf8CStrRef, value: Utf8CStrRef, skip_svc: bool) -> i32;
176178
#[cxx_name = "load_prop_file_rs"]
177179
fn load_prop_file(filename: Utf8CStrRef, skip_svc: bool);
178-
fn prop_cb_exec(cb: Pin<&mut PropCb>, name: Utf8CStrRef, value: Utf8CStrRef, serial: u32);
179180
}
180181

181182
extern "Rust" {
@@ -188,8 +189,8 @@ pub mod ffi {
188189
fn revert_unmount(pid: i32);
189190
fn remove_modules();
190191
fn zygisk_should_load_module(flags: u32) -> bool;
191-
unsafe fn persist_get_prop(name: Utf8CStrRef, prop_cb: Pin<&mut PropCb>);
192-
unsafe fn persist_get_props(prop_cb: Pin<&mut PropCb>);
192+
unsafe fn persist_get_prop(name: Utf8CStrRef, prop_cb: Pin<&mut PropCallback>);
193+
unsafe fn persist_get_props(prop_cb: Pin<&mut PropCallback>);
193194
unsafe fn persist_delete_prop(name: Utf8CStrRef) -> bool;
194195
unsafe fn persist_set_prop(name: Utf8CStrRef, value: Utf8CStrRef) -> bool;
195196
fn send_fd(socket: i32, fd: i32) -> bool;

native/src/core/resetprop/persist.rs

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ use std::io::Read;
22
use std::{
33
fs::File,
44
io::{BufWriter, Write},
5-
ops::{Deref, DerefMut},
65
os::fd::FromRawFd,
76
pin::Pin,
87
};
98

109
use quick_protobuf::{BytesReader, MessageRead, MessageWrite, Writer};
1110

12-
use crate::ffi::{PropCb, prop_cb_exec};
11+
use crate::ffi::PropCallback;
1312
use crate::resetprop::proto::persistent_properties::{
1413
PersistentProperties, mod_PersistentProperties::PersistentPropertyRecord,
1514
};
@@ -23,43 +22,20 @@ use base::{
2322
const PERSIST_PROP_DIR: &str = "/data/property";
2423
const PERSIST_PROP: &str = concatcp!(PERSIST_PROP_DIR, "/persistent_properties");
2524

26-
trait PropCbExec {
27-
fn exec(&mut self, name: &Utf8CStr, value: &Utf8CStr);
28-
}
29-
30-
impl PropCbExec for Pin<&mut PropCb> {
31-
fn exec(&mut self, name: &Utf8CStr, value: &Utf8CStr) {
32-
prop_cb_exec(self.as_mut(), name, value, u32::MAX)
33-
}
34-
}
35-
36-
impl Deref for PersistentProperties {
37-
type Target = Vec<PersistentPropertyRecord>;
38-
39-
fn deref(&self) -> &Self::Target {
40-
&self.properties
41-
}
42-
}
43-
44-
impl DerefMut for PersistentProperties {
45-
fn deref_mut(&mut self) -> &mut Self::Target {
46-
&mut self.properties
47-
}
48-
}
49-
5025
trait PropExt {
5126
fn find_index(&self, name: &Utf8CStr) -> Result<usize, usize>;
5227
fn find(&mut self, name: &Utf8CStr) -> LoggedResult<&mut PersistentPropertyRecord>;
5328
}
5429

5530
impl PropExt for PersistentProperties {
5631
fn find_index(&self, name: &Utf8CStr) -> Result<usize, usize> {
57-
self.binary_search_by(|p| p.name.as_deref().cmp(&Some(name.deref())))
32+
self.properties
33+
.binary_search_by(|p| p.name.as_deref().cmp(&Some(name.as_str())))
5834
}
5935

6036
fn find(&mut self, name: &Utf8CStr) -> LoggedResult<&mut PersistentPropertyRecord> {
6137
let idx = self.find_index(name).silent()?;
62-
Ok(&mut self[idx])
38+
Ok(&mut self.properties[idx])
6339
}
6440
}
6541

@@ -110,7 +86,9 @@ fn proto_read_props() -> LoggedResult<PersistentProperties> {
11086
let mut r = BytesReader::from_bytes(m);
11187
let mut props = PersistentProperties::from_reader(&mut r, m)?;
11288
// Keep the list sorted for binary search
113-
props.sort_unstable_by(|a, b| a.name.cmp(&b.name));
89+
props
90+
.properties
91+
.sort_unstable_by(|a, b| a.name.cmp(&b.name));
11492
Ok(props)
11593
}
11694

@@ -130,7 +108,7 @@ fn proto_write_props(props: &PersistentProperties) -> LoggedResult<()> {
130108
Ok(())
131109
}
132110

133-
pub fn persist_get_prop(name: &Utf8CStr, mut prop_cb: Pin<&mut PropCb>) {
111+
pub fn persist_get_prop(name: &Utf8CStr, prop_cb: Pin<&mut PropCallback>) {
134112
let res: LoggedResult<()> = try {
135113
if check_proto() {
136114
let mut props = proto_read_props()?;
@@ -151,17 +129,19 @@ pub fn persist_get_prop(name: &Utf8CStr, mut prop_cb: Pin<&mut PropCb>) {
151129
res.ok();
152130
}
153131

154-
pub fn persist_get_props(mut prop_cb: Pin<&mut PropCb>) {
132+
pub fn persist_get_props(mut prop_cb: Pin<&mut PropCallback>) {
155133
let res: LoggedResult<()> = try {
156134
if check_proto() {
157135
let mut props = proto_read_props()?;
158-
props.iter_mut().for_each(|prop| {
136+
props.properties.iter_mut().for_each(|prop| {
159137
if let PersistentPropertyRecord {
160138
name: Some(n),
161139
value: Some(v),
162140
} = prop
163141
{
164-
prop_cb.exec(Utf8CStr::from_string(n), Utf8CStr::from_string(v));
142+
prop_cb
143+
.as_mut()
144+
.exec(Utf8CStr::from_string(n), Utf8CStr::from_string(v));
165145
}
166146
});
167147
} else {
@@ -170,7 +150,9 @@ pub fn persist_get_props(mut prop_cb: Pin<&mut PropCb>) {
170150
if e.is_file()
171151
&& let Ok(mut value) = file_get_prop(e.name())
172152
{
173-
prop_cb.exec(e.name(), Utf8CStr::from_string(&mut value));
153+
prop_cb
154+
.as_mut()
155+
.exec(e.name(), Utf8CStr::from_string(&mut value));
174156
}
175157
// Do not traverse recursively
176158
Ok(WalkResult::Skip)
@@ -185,7 +167,7 @@ pub fn persist_delete_prop(name: &Utf8CStr) -> bool {
185167
if check_proto() {
186168
let mut props = proto_read_props()?;
187169
let idx = props.find_index(name).silent()?;
188-
props.remove(idx);
170+
props.properties.remove(idx);
189171
proto_write_props(&props)?;
190172
} else {
191173
file_set_prop(name, None)?;
@@ -199,8 +181,8 @@ pub fn persist_set_prop(name: &Utf8CStr, value: &Utf8CStr) -> bool {
199181
if check_proto() {
200182
let mut props = proto_read_props()?;
201183
match props.find_index(name) {
202-
Ok(idx) => props[idx].value = Some(value.to_string()),
203-
Err(idx) => props.insert(
184+
Ok(idx) => props.properties[idx].value = Some(value.to_string()),
185+
Err(idx) => props.properties.insert(
204186
idx,
205187
PersistentPropertyRecord {
206188
name: Some(name.to_string()),

native/src/core/resetprop/resetprop.cpp

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ using namespace std;
1414

1515
#ifdef APPLET_STUB_MAIN
1616
#define system_property_set __system_property_set
17-
#define system_property_read(...)
1817
#define system_property_find __system_property_find
1918
#define system_property_read_callback __system_property_read_callback
2019
#define system_property_foreach __system_property_foreach
@@ -111,30 +110,21 @@ static bool check_legal_property_name(const char *name) {
111110
return false;
112111
}
113112

114-
static void read_prop_with_cb(const prop_info *pi, void *cb) {
115-
if (system_property_read_callback) {
116-
auto callback = [](void *cb, const char *name, const char *value, uint32_t serial) {
117-
static_cast<prop_cb*>(cb)->exec(name, value, serial);
118-
};
119-
system_property_read_callback(pi, callback, cb);
120-
} else {
121-
char name[PROP_NAME_MAX];
122-
char value[PROP_VALUE_MAX];
123-
name[0] = '\0';
124-
value[0] = '\0';
125-
system_property_read(pi, name, value);
126-
static_cast<prop_cb*>(cb)->exec(name, value, pi->serial);
127-
}
113+
void prop_callback::read(const prop_info *pi) {
114+
auto fn = [](void *cb, const char *name, const char *value, uint32_t serial) {
115+
static_cast<prop_callback*>(cb)->exec(name, value, serial);
116+
};
117+
system_property_read_callback(pi, fn, this);
128118
}
129119

130120
template<class StringType>
131-
struct prop_to_string : prop_cb {
121+
struct prop_to_string : prop_callback {
132122
void exec(const char *, const char *value, uint32_t s) override {
133123
val = value;
134124
serial = s;
135125
}
136126
StringType val;
137-
uint32_t serial;
127+
uint32_t serial = 0;
138128
};
139129

140130
template<> void prop_to_string<rust::String>::exec(const char *, const char *value, uint32_t s) {
@@ -212,7 +202,7 @@ static StringType get_prop(const char *name, PropFlags flags) {
212202

213203
if (!flags.isPersistOnly()) {
214204
if (auto pi = system_property_find(name)) {
215-
read_prop_with_cb(pi, &cb);
205+
cb.read(pi);
216206
LOGD("resetprop: get prop [%s]: [%s]\n", name, cb.val.c_str());
217207
}
218208
}
@@ -238,28 +228,37 @@ static StringType wait_prop(const char *name, const char *old_value) {
238228
}
239229

240230
prop_to_string<StringType> cb;
241-
read_prop_with_cb(pi, &cb);
231+
cb.read(pi);
242232

243233
while (old_value == nullptr || cb.val == old_value) {
244234
LOGD("resetprop: waiting for prop [%s]\n", name);
245235
uint32_t new_serial;
246236
system_property_wait(pi, cb.serial, &new_serial, nullptr);
247-
read_prop_with_cb(pi, &cb);
237+
cb.read(pi);
248238
if (old_value == nullptr) break;
249239
}
250240

251241
LOGD("resetprop: get prop [%s]: [%s]\n", name, cb.val.c_str());
252242
return cb.val;
253243
}
254244

245+
struct prop_collector : prop_callback {
246+
void exec(const char *name, const char *value, uint32_t) override {
247+
list.insert({name, value});
248+
}
249+
map<string, string> list;
250+
};
251+
255252
static void print_props(PropFlags flags) {
256-
prop_list list;
257-
prop_collector collector(list);
258-
if (!flags.isPersistOnly())
259-
system_property_foreach(read_prop_with_cb, &collector);
253+
prop_collector collector;
254+
if (!flags.isPersistOnly()) {
255+
system_property_foreach([](const prop_info *pi, void *cb) {
256+
static_cast<prop_callback*>(cb)->read(pi);
257+
}, &collector);
258+
}
260259
if (flags.isPersist())
261260
persist_get_props(collector);
262-
for (auto &[key, val] : list) {
261+
for (auto &[key, val] : collector.list) {
263262
const char *v = flags.isContext() ?
264263
(__system_property_get_context(key.data()) ?: "") :
265264
val.data();
@@ -305,6 +304,17 @@ struct Initialize {
305304
// The platform API only exist on API 26+
306305
system_property_wait = __system_property_wait;
307306
}
307+
if (system_property_read_callback == nullptr) {
308+
// The platform API only exist on API 26+, create a polyfill
309+
system_property_read_callback = [](const prop_info *pi, auto fn, void *cookie) {
310+
char name[PROP_NAME_MAX];
311+
char value[PROP_VALUE_MAX];
312+
name[0] = '\0';
313+
value[0] = '\0';
314+
system_property_read(pi, name, value);
315+
fn(cookie, name, value, pi->serial);
316+
};
317+
}
308318
#endif
309319
if (__system_properties_init()) {
310320
LOGE("resetprop: __system_properties_init error\n");
@@ -313,7 +323,7 @@ struct Initialize {
313323
};
314324

315325
static void InitOnce() {
316-
static struct Initialize init;
326+
static Initialize init;
317327
}
318328

319329
#define consume_next(val) \

0 commit comments

Comments
 (0)