Skip to content

Commit

Permalink
Cleanup code and bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohnwu committed Feb 2, 2025
1 parent ad84e2a commit acf3756
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 198 deletions.
1 change: 0 additions & 1 deletion native/src/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ LOCAL_SRC_FILES := \
core/applets.cpp \
core/magisk.cpp \
core/daemon.cpp \
core/socket.cpp \
core/scripting.cpp \
core/selinux.cpp \
core/sqlite.cpp \
Expand Down
32 changes: 32 additions & 0 deletions native/src/core/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,38 @@ void MagiskD::reboot() const noexcept {
exec_command_sync("/system/bin/reboot");
}

bool get_client_cred(int fd, sock_cred *cred) {
socklen_t len = sizeof(ucred);
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, cred, &len) != 0)
return false;
char buf[4096];
len = sizeof(buf);
if (getsockopt(fd, SOL_SOCKET, SO_PEERSEC, buf, &len) != 0)
len = 0;
buf[len] = '\0';
cred->context = buf;
return true;
}

bool read_string(int fd, std::string &str) {
str.clear();
auto len = read_any<size_t>(fd);
str.resize(len);
return xxread(fd, str.data(), len) == len;
}

string read_string(int fd) {
string str;
read_string(fd, str);
return str;
}

void write_string(int fd, string_view str) {
if (fd < 0) return;
write_any(fd, str.size());
xwrite(fd, str.data(), str.size());
}

static void handle_request_async(int client, int code, const sock_cred &cred) {
auto &daemon = MagiskD::Get();
switch (code) {
Expand Down
7 changes: 2 additions & 5 deletions native/src/core/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ impl MagiskD {
self.sdk_int
}

pub fn set_module_list(&self, module_list: Vec<ModuleInfo>) {
self.module_list.set(module_list).ok();
}

pub fn app_data_dir(&self) -> &'static Utf8CStr {
if self.sdk_int >= 24 {
cstr!("/data/user_de")
Expand Down Expand Up @@ -152,7 +148,8 @@ impl MagiskD {
);
initialize_denylist();
setup_mounts();
self.handle_modules();
let modules = self.handle_modules();
self.module_list.set(modules).ok();

false
}
Expand Down
56 changes: 34 additions & 22 deletions native/src/core/db.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#![allow(improper_ctypes, improper_ctypes_definitions)]
use crate::daemon::{MagiskD, MAGISKD};
use crate::ffi::{
open_and_init_db, sqlite3, sqlite3_errstr, DbEntryKey, DbSettings, DbStatement, DbValues,
MntNsMode, MultiuserMode, RootAccess,
open_and_init_db, sqlite3, sqlite3_errstr, DbEntryKey, DbStatement, DbValues, MntNsMode,
};
use crate::socket::{IpcRead, IpcWrite};
use base::{LoggedResult, ResultExt, Utf8CStr};
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use std::ffi::c_void;
use std::fs::File;
use std::io::{BufReader, BufWriter};
Expand Down Expand Up @@ -55,16 +56,33 @@ where
}
}

impl Default for RootAccess {
fn default() -> Self {
RootAccess::AppsAndAdb
}
#[derive(Default)]
pub struct DbSettings {
pub root_access: RootAccess,
pub multiuser_mode: MultiuserMode,
pub mnt_ns: MntNsMode,
pub boot_count: i32,
pub denylist: bool,
pub zygisk: bool,
}

impl Default for MultiuserMode {
fn default() -> Self {
MultiuserMode::OwnerOnly
}
#[repr(i32)]
#[derive(Default, FromPrimitive)]
pub enum RootAccess {
Disabled,
AppsOnly,
AdbOnly,
#[default]
AppsAndAdb,
}

#[repr(i32)]
#[derive(Default, FromPrimitive)]
pub enum MultiuserMode {
#[default]
OwnerOnly,
OwnerManaged,
User,
}

impl Default for MntNsMode {
Expand Down Expand Up @@ -100,8 +118,10 @@ impl SqlTable for DbSettings {
}
}
match key {
"root_access" => self.root_access = RootAccess { repr: value },
"multiuser_mode" => self.multiuser_mode = MultiuserMode { repr: value },
"root_access" => self.root_access = RootAccess::from_i32(value).unwrap_or_default(),
"multiuser_mode" => {
self.multiuser_mode = MultiuserMode::from_i32(value).unwrap_or_default()
}
"mnt_ns" => self.mnt_ns = MntNsMode { repr: value },
"denylist" => self.denylist = value != 0,
"zygisk" => self.zygisk = value != 0,
Expand Down Expand Up @@ -226,8 +246,8 @@ impl MagiskD {
pub fn get_db_setting(&self, key: DbEntryKey) -> i32 {
// Get default values
let mut val = match key {
DbEntryKey::RootAccess => RootAccess::default().repr,
DbEntryKey::SuMultiuserMode => MultiuserMode::default().repr,
DbEntryKey::RootAccess => RootAccess::default() as i32,
DbEntryKey::SuMultiuserMode => MultiuserMode::default() as i32,
DbEntryKey::SuMntNs => MntNsMode::default().repr,
DbEntryKey::DenylistConfig => 0,
DbEntryKey::ZygiskConfig => self.is_emulator as i32,
Expand Down Expand Up @@ -302,14 +322,6 @@ impl MagiskD {
}

impl MagiskD {
pub fn get_db_settings_for_cxx(&self, cfg: &mut DbSettings) -> bool {
cfg.zygisk = self.is_emulator;
self.db_exec_with_rows("SELECT * FROM settings", &[], cfg)
.sql_result()
.log()
.is_ok()
}

pub fn set_db_setting_for_cxx(&self, key: DbEntryKey, value: i32) -> bool {
self.set_db_setting(key, value).log().is_ok()
}
Expand Down
41 changes: 40 additions & 1 deletion native/src/core/include/core.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <sys/socket.h>
#include <pthread.h>
#include <poll.h>
#include <string>
Expand All @@ -9,7 +10,6 @@

#include <base.hpp>

#include "socket.hpp"
#include "../core-rs.hpp"

#define AID_ROOT 0
Expand Down Expand Up @@ -40,6 +40,45 @@ bool setup_magisk_env();
bool check_key_combo();
void restore_zygisk_prop();

// Sockets
struct sock_cred : public ucred {
std::string context;
};

template<typename T> requires(std::is_trivially_copyable_v<T>)
T read_any(int fd) {
T val;
if (xxread(fd, &val, sizeof(val)) != sizeof(val))
return -1;
return val;
}

template<typename T> requires(std::is_trivially_copyable_v<T>)
void write_any(int fd, T val) {
if (fd < 0) return;
xwrite(fd, &val, sizeof(val));
}

template<typename T> requires(std::is_trivially_copyable_v<T>)
void write_vector(int fd, const std::vector<T> &vec) {
write_any(fd, vec.size());
xwrite(fd, vec.data(), vec.size() * sizeof(T));
}

template<typename T> requires(std::is_trivially_copyable_v<T>)
bool read_vector(int fd, std::vector<T> &vec) {
auto size = read_any<size_t>(fd);
vec.resize(size);
return xread(fd, vec.data(), size * sizeof(T)) == size * sizeof(T);
}

bool get_client_cred(int fd, sock_cred *cred);
static inline int read_int(int fd) { return read_any<int>(fd); }
static inline void write_int(int fd, int val) { write_any(fd, val); }
std::string read_string(int fd);
bool read_string(int fd, std::string &str);
void write_string(int fd, std::string_view str);

// Poll control
using poll_callback = void(*)(pollfd*);
void register_poll(const pollfd *pfd, poll_callback callback);
Expand Down
49 changes: 0 additions & 49 deletions native/src/core/include/socket.hpp

This file was deleted.

47 changes: 2 additions & 45 deletions native/src/core/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mod socket;
mod su;
mod zygisk;

#[allow(clippy::needless_lifetimes)]
#[cxx::bridge]
pub mod ffi {
#[repr(i32)]
Expand Down Expand Up @@ -71,51 +72,20 @@ pub mod ffi {
SuManager,
}

#[repr(i32)]
enum RootAccess {
Disabled,
AppsOnly,
AdbOnly,
AppsAndAdb,
}

#[repr(i32)]
enum MultiuserMode {
OwnerOnly,
OwnerManaged,
User,
}

#[repr(i32)]
enum MntNsMode {
Global,
Requester,
Isolate,
}

#[derive(Default)]
struct DbSettings {
root_access: RootAccess,
multiuser_mode: MultiuserMode,
mnt_ns: MntNsMode,
boot_count: i32,
denylist: bool,
zygisk: bool,
}

#[repr(i32)]
enum SuPolicy {
Query,
Deny,
Allow,
}

struct RootSettings {
policy: SuPolicy,
log: bool,
notify: bool,
}

struct ModuleInfo {
name: String,
z32: i32,
Expand Down Expand Up @@ -242,14 +212,6 @@ pub mod ffi {

// Default constructors
extern "Rust" {
#[Self = DbSettings]
#[cxx_name = "New"]
fn default() -> DbSettings;

#[Self = RootSettings]
#[cxx_name = "New"]
fn default() -> RootSettings;

#[Self = SuRequest]
#[cxx_name = "New"]
fn default() -> SuRequest;
Expand All @@ -268,17 +230,12 @@ pub mod ffi {
fn su_daemon_handler(&self, client: i32, cred: &UCred);
#[cxx_name = "get_manager"]
unsafe fn get_manager_for_cxx(&self, user: i32, ptr: *mut CxxString, install: bool) -> i32;
fn set_module_list(&self, module_list: Vec<ModuleInfo>);

#[cxx_name = "get_db_settings"]
fn get_db_settings_for_cxx(&self, cfg: &mut DbSettings) -> bool;
fn get_db_setting(&self, key: DbEntryKey) -> i32;
#[cxx_name = "set_db_setting"]
fn set_db_setting_for_cxx(&self, key: DbEntryKey, value: i32) -> bool;
#[cxx_name = "db_exec"]
fn db_exec_for_cxx(&self, client_fd: i32);
#[cxx_name = "get_root_settings"]
fn get_root_settings_for_cxx(&self, uid: i32, settings: &mut RootSettings) -> bool;

#[Self = MagiskD]
#[cxx_name = "Get"]
Expand All @@ -287,7 +244,7 @@ pub mod ffi {
unsafe extern "C++" {
#[allow(dead_code)]
fn reboot(self: &MagiskD);
fn handle_modules(self: &MagiskD);
fn handle_modules(self: &MagiskD) -> Vec<ModuleInfo>;
}
}

Expand Down
Loading

0 comments on commit acf3756

Please sign in to comment.