Minor changes

This commit is contained in:
topjohnwu 2025-01-04 01:45:23 -08:00 committed by John Wu
parent 24650eefe4
commit 7edd8be169
8 changed files with 24 additions and 28 deletions

View File

@ -7,7 +7,6 @@
#include <string> #include <string>
#include <consts.hpp> #include <consts.hpp>
#include <sqlite.hpp>
#include <base.hpp> #include <base.hpp>
#include <core.hpp> #include <core.hpp>
#include <selinux.hpp> #include <selinux.hpp>

View File

@ -7,7 +7,6 @@
#include <base.hpp> #include <base.hpp>
#include <core.hpp> #include <core.hpp>
#include <selinux.hpp> #include <selinux.hpp>
#include <sqlite.hpp>
#include <flags.h> #include <flags.h>
using namespace std; using namespace std;

View File

@ -42,15 +42,15 @@ impl SqliteReturn for i32 {
} }
trait SqlTable { trait SqlTable {
fn on_row(&mut self, columns: &[String], data: &DbValues); fn on_row(&mut self, columns: &[String], values: &DbValues);
} }
impl<T> SqlTable for T impl<T> SqlTable for T
where where
T: FnMut(&[String], &DbValues), T: FnMut(&[String], &DbValues),
{ {
fn on_row(&mut self, columns: &[String], data: &DbValues) { fn on_row(&mut self, columns: &[String], values: &DbValues) {
self.call_mut((columns, data)) self.call_mut((columns, values))
} }
} }
@ -92,14 +92,14 @@ impl DbEntryKey {
} }
impl SqlTable for DbSettings { impl SqlTable for DbSettings {
fn on_row(&mut self, columns: &[String], data: &DbValues) { fn on_row(&mut self, columns: &[String], values: &DbValues) {
let mut key = ""; let mut key = "";
let mut value = 0; let mut value = 0;
for (i, column) in columns.iter().enumerate() { for (i, column) in columns.iter().enumerate() {
if column == "key" { if column == "key" {
key = data.get_text(i as i32); key = values.get_text(i as i32);
} else if column == "value" { } else if column == "value" {
value = data.get_int(i as i32); value = values.get_int(i as i32);
} }
} }
match key { match key {

View File

@ -222,15 +222,15 @@ static bool ensure_data() {
LOGI("denylist: initializing internal data structures\n"); LOGI("denylist: initializing internal data structures\n");
default_new(pkg_to_procs_); default_new(pkg_to_procs_);
bool res = db_exec("SELECT * FROM denylist", {}, [](StringSlice columns, DbValues &data) { bool res = db_exec("SELECT * FROM denylist", {}, [](StringSlice columns, const DbValues &values) {
const char *package_name; const char *package_name;
const char *process; const char *process;
for (int i = 0; i < columns.size(); ++i) { for (int i = 0; i < columns.size(); ++i) {
const auto &name = columns[i]; const auto &name = columns[i];
if (name == "package_name") { if (name == "package_name") {
package_name = data.get_text(i); package_name = values.get_text(i);
} else if (name == "process") { } else if (name == "process") {
process = data.get_text(i); process = values.get_text(i);
} }
} }
add_hide_set(package_name, process); add_hide_set(package_name, process);

View File

@ -6,7 +6,7 @@
#define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */ #define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_CREATE 0x00000004 /* Ok for sqlite3_open_v2() */ #define SQLITE_OPEN_CREATE 0x00000004 /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */ #define SQLITE_OPEN_NOMUTEX 0x00008000 /* Ok for sqlite3_open_v2() */
#define SQLITE_OK 0 /* Successful result */ #define SQLITE_OK 0 /* Successful result */
#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */ #define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
@ -32,7 +32,7 @@ struct DbStatement {
using StringSlice = rust::Slice<rust::String>; using StringSlice = rust::Slice<rust::String>;
using sql_bind_callback = int(*)(void*, int, DbStatement&); using sql_bind_callback = int(*)(void*, int, DbStatement&);
using sql_exec_callback = void(*)(void*, StringSlice, DbValues&); using sql_exec_callback = void(*)(void*, StringSlice, const DbValues&);
sqlite3 *open_and_init_db(); sqlite3 *open_and_init_db();
@ -40,7 +40,7 @@ sqlite3 *open_and_init_db();
* C++ APIs * * C++ APIs *
************/ ************/
using db_exec_callback = std::function<void(StringSlice, DbValues&)>; using db_exec_callback = std::function<void(StringSlice, const DbValues&)>;
struct DbArg { struct DbArg {
enum { enum {

View File

@ -9,9 +9,6 @@ using namespace std;
#define DB_VERSION 12 #define DB_VERSION 12
#define DB_VERSION_STR "12" #define DB_VERSION_STR "12"
#define DBLOGV(...)
//#define DBLOGV(...) LOGD("magiskdb: " __VA_ARGS__)
// SQLite APIs // SQLite APIs
static int (*sqlite3_open_v2)(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs); static int (*sqlite3_open_v2)(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs);
@ -169,7 +166,7 @@ int DbStatement::bind_text(int index, rust::Str val) {
} }
#define sql_chk_log(fn, ...) if (int rc = fn(__VA_ARGS__); rc != SQLITE_OK) { \ #define sql_chk_log(fn, ...) if (int rc = fn(__VA_ARGS__); rc != SQLITE_OK) { \
LOGE("sqlite3(db.cpp:%d): %s\n", __LINE__, sqlite3_errstr(rc)); \ LOGE("sqlite3(line:%d): %s\n", __LINE__, sqlite3_errstr(rc)); \
return false; \ return false; \
} }
@ -182,15 +179,16 @@ static bool open_and_init_db_impl(sqlite3 **dbOut) {
unique_ptr<sqlite3, decltype(sqlite3_close)> db(nullptr, sqlite3_close); unique_ptr<sqlite3, decltype(sqlite3_close)> db(nullptr, sqlite3_close);
{ {
sqlite3 *sql; sqlite3 *sql;
// We open the connection with SQLITE_OPEN_NOMUTEX because we are guarding it ourselves
sql_chk_log(sqlite3_open_v2, MAGISKDB, &sql, sql_chk_log(sqlite3_open_v2, MAGISKDB, &sql,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, nullptr); SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX, nullptr);
db.reset(sql); db.reset(sql);
} }
int ver = 0; int ver = 0;
bool upgrade = false; bool upgrade = false;
auto ver_cb = [](void *ver, auto, DbValues &data) { auto ver_cb = [](void *ver, auto, const DbValues &values) {
*static_cast<int *>(ver) = data.get_int(0); *static_cast<int *>(ver) = values.get_int(0);
}; };
sql_chk_log(sql_exec_impl, db.get(), "PRAGMA user_version", nullptr, nullptr, ver_cb, &ver); sql_chk_log(sql_exec_impl, db.get(), "PRAGMA user_version", nullptr, nullptr, ver_cb, &ver);
if (ver > DB_VERSION) { if (ver > DB_VERSION) {
@ -329,9 +327,9 @@ bool db_exec(const char *sql, DbArgs args, db_exec_callback exec_fn) {
} }
sql_exec_callback exec_cb = nullptr; sql_exec_callback exec_cb = nullptr;
if (exec_fn) { if (exec_fn) {
exec_cb = [](void *v, StringSlice columns, DbValues &data) { exec_cb = [](void *v, StringSlice columns, const DbValues &values) {
auto fn = static_cast<db_exec_callback*>(v); auto fn = static_cast<db_exec_callback*>(v);
fn->operator()(columns, data); fn->operator()(columns, values);
}; };
} }
sql_chk_log(sql_exec_rs, sql, bind_cb, &bind_fn, exec_cb, &exec_fn); sql_chk_log(sql_exec_rs, sql, bind_cb, &bind_fn, exec_cb, &exec_fn);

View File

@ -27,7 +27,7 @@ struct su_access {
su_access() : policy(QUERY), log(1), notify(1) {} su_access() : policy(QUERY), log(1), notify(1) {}
void operator()(StringSlice columns, DbValues &data); void operator()(StringSlice columns, const DbValues &data);
void silent_deny() { void silent_deny() {
policy = DENY; policy = DENY;
log = 0; log = 0;

View File

@ -42,7 +42,7 @@ void su_info::refresh() {
timestamp = ts.tv_sec * 1000L + ts.tv_nsec / 1000000L; timestamp = ts.tv_sec * 1000L + ts.tv_nsec / 1000000L;
} }
void su_access::operator()(StringSlice columns, DbValues &data) { void su_access::operator()(StringSlice columns, const DbValues &data) {
for (int i = 0; i < columns.size(); ++i) { for (int i = 0; i < columns.size(); ++i) {
const auto &name = columns[i]; const auto &name = columns[i];
if (name == "policy") { if (name == "policy") {
@ -130,13 +130,13 @@ bool uid_granted_root(int uid) {
bool granted = false; bool granted = false;
db_exec("SELECT policy FROM policies WHERE uid=? AND (until=0 OR until>?)", db_exec("SELECT policy FROM policies WHERE uid=? AND (until=0 OR until>?)",
{ uid, time(nullptr) }, { uid, time(nullptr) },
[&](auto, DbValues &data) { granted = data.get_int(0) == ALLOW; }); [&](auto, const DbValues &values) { granted = values.get_int(0) == ALLOW; });
return granted; return granted;
} }
struct policy_uid_list : public vector<int> { struct policy_uid_list : public vector<int> {
void operator()(StringSlice, DbValues &data) { void operator()(StringSlice, const DbValues &values) {
push_back(data.get_int(0)); push_back(values.get_int(0));
} }
}; };