Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion native/src/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ LOCAL_SRC_FILES := \
init/mount.cpp \
init/rootdir.cpp \
init/getinfo.cpp \
init/selinux.cpp \
init/init-rs.cpp

LOCAL_LDFLAGS := -static
Expand Down
15 changes: 8 additions & 7 deletions native/src/base/cstr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cxx::{type_id, ExternType};
use cxx::{ExternType, type_id};
use libc::c_char;
use std::borrow::Borrow;
use std::cmp::min;
Expand Down Expand Up @@ -479,6 +479,11 @@ impl FsPath {
unsafe { mem::transmute(value.as_ref()) }
}

#[inline(always)]
pub const fn from_utfcstr(value: &Utf8CStr) -> &FsPath {
unsafe { mem::transmute(value) }
}

#[inline(always)]
pub fn from_mut<T: AsMut<Utf8CStr> + ?Sized>(value: &mut T) -> &mut FsPath {
unsafe { mem::transmute(value.as_mut()) }
Expand Down Expand Up @@ -781,14 +786,10 @@ macro_rules! cstr {

#[macro_export]
macro_rules! raw_cstr {
($str:expr) => {{
$crate::cstr!($str).as_ptr()
}};
($str:expr) => {{ $crate::cstr!($str).as_ptr() }};
}

#[macro_export]
macro_rules! path {
($str:expr) => {{
$crate::FsPath::from($crate::cstr!($str))
}};
($str:expr) => {{ $crate::FsPath::from_utfcstr($crate::cstr!($str)) }};
}
6 changes: 5 additions & 1 deletion native/src/base/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl FsPath {
}

pub fn create(&self, flags: i32, mode: mode_t) -> io::Result<File> {
Ok(File::from(open_fd!(self, flags, mode)?))
Ok(File::from(open_fd!(self, O_CREAT | flags, mode)?))
}

pub fn exists(&self) -> bool {
Expand Down Expand Up @@ -431,6 +431,10 @@ impl FsPath {
pub fn create_symlink_to(&self, target: &FsPath) -> io::Result<()> {
unsafe { libc::symlink(target.as_ptr(), self.as_ptr()).as_os_err() }
}

pub fn mkfifo(&self, mode: mode_t) -> io::Result<()> {
unsafe { libc::mkfifo(self.as_ptr(), mode).as_os_err() }
}
}

impl FsPathFollow {
Expand Down
15 changes: 14 additions & 1 deletion native/src/base/mount.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{FsPath, LibcReturn};
use crate::{FsPath, LibcReturn, Utf8CStr};
use libc::c_ulong;
use std::ptr;

Expand Down Expand Up @@ -29,6 +29,19 @@ impl FsPath {
}
}

pub fn remount_with_data(&self, data: &Utf8CStr) -> std::io::Result<()> {
unsafe {
libc::mount(
ptr::null(),
self.as_ptr(),
ptr::null(),
libc::MS_REMOUNT,
data.as_ptr().cast(),
)
.as_os_err()
}
}

pub fn move_mount_to(&self, path: &FsPath) -> std::io::Result<()> {
unsafe {
libc::mount(
Expand Down
1 change: 1 addition & 0 deletions native/src/include/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ pub const DEVICEDIR: &str = concatcp!(INTERNAL_DIR, "/device");
pub const PREINITDEV: &str = concatcp!(DEVICEDIR, "/preinit");
pub const ROOTOVL: &str = concatcp!(INTERNAL_DIR, "/rootdir");
pub const ROOTMNT: &str = concatcp!(ROOTOVL, "/.mount_list");
pub const SELINUXMOCK: &str = concatcp!(INTERNAL_DIR, "/selinux");
20 changes: 20 additions & 0 deletions native/src/init/init.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,30 @@

#include <base.hpp>
#include <stream.hpp>
#include <sepolicy.hpp>

#include "init-rs.hpp"

int magisk_proxy_main(int, char *argv[]);
rust::Utf8CStr backup_init();

// Expose some constants to Rust

static inline rust::Utf8CStr split_plat_cil() {
return SPLIT_PLAT_CIL;
};

static inline rust::Utf8CStr preload_lib() {
return PRELOAD_LIB;
}

static inline rust::Utf8CStr preload_policy() {
return PRELOAD_POLICY;
}

static inline rust::Utf8CStr preload_ack() {
return PRELOAD_ACK;
}


#endif
27 changes: 25 additions & 2 deletions native/src/init/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#![feature(try_blocks)]
#![allow(clippy::missing_safety_doc)]

use base::FsPath;
use logging::setup_klog;
// Has to be pub so all symbols in that crate is included
pub use magiskpolicy;
use mount::{is_device_mounted, switch_root};
use rootdir::{inject_magisk_rc, OverlayAttr};
use rootdir::{OverlayAttr, inject_magisk_rc};

#[path = "../include/consts.rs"]
mod consts;
Expand All @@ -16,6 +17,7 @@ mod init;
mod logging;
mod mount;
mod rootdir;
mod selinux;
mod twostage;

#[cxx::bridge]
Expand Down Expand Up @@ -56,6 +58,12 @@ pub mod ffi {

unsafe fn magisk_proxy_main(argc: i32, argv: *mut *mut c_char) -> i32;
fn backup_init() -> Utf8CStrRef<'static>;

// Constants
fn split_plat_cil() -> Utf8CStrRef<'static>;
fn preload_lib() -> Utf8CStrRef<'static>;
fn preload_policy() -> Utf8CStrRef<'static>;
fn preload_ack() -> Utf8CStrRef<'static>;
}

#[namespace = "rust"]
Expand All @@ -81,6 +89,7 @@ pub mod ffi {
type OverlayAttr;
fn parse_config_file(self: &mut MagiskInit);
fn mount_overlay(self: &mut MagiskInit, dest: Utf8CStrRef);
fn handle_sepolicy(self: &mut MagiskInit);
fn restore_overlay_contexts(self: &MagiskInit);
}
unsafe extern "C++" {
Expand All @@ -94,7 +103,21 @@ pub mod ffi {
fn collect_devices(self: &MagiskInit);
fn mount_preinit_dir(self: &MagiskInit);
unsafe fn find_block(self: &MagiskInit, partname: *const c_char) -> u64;
fn handle_sepolicy(self: &mut MagiskInit);
unsafe fn patch_fissiond(self: &mut MagiskInit, tmp_path: *const c_char);
}
}

#[inline(always)]
fn preload_lib() -> &'static FsPath {
FsPath::from(ffi::preload_lib())
}

#[inline(always)]
fn preload_policy() -> &'static FsPath {
FsPath::from(ffi::preload_policy())
}

#[inline(always)]
fn preload_ack() -> &'static FsPath {
FsPath::from(ffi::preload_ack())
}
Loading