Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
M
Magisk
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Magisk
Commits
4ed34cd6
Commit
4ed34cd6
authored
Dec 14, 2022
by
LoveSy
Committed by
John Wu
Dec 24, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Eliminate unnecessarily copy on magiskinit
This patch reuses the abused /data tmpfs for magisktmp
parent
0d38c94c
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
30 additions
and
76 deletions
+30
-76
init.hpp
native/src/init/init.hpp
+4
-7
mount.cpp
native/src/init/mount.cpp
+15
-8
rootdir.cpp
native/src/init/rootdir.cpp
+7
-24
twostage.cpp
native/src/init/twostage.cpp
+4
-37
No files found.
native/src/init/init.hpp
View file @
4ed34cd6
...
...
@@ -25,6 +25,7 @@ struct BootConfig {
#define DEFAULT_DT_DIR "/proc/device-tree/firmware/android"
#define INIT_PATH "/system/bin/init"
#define REDIR_PATH "/data/magiskinit"
extern
std
::
vector
<
std
::
string
>
mount_list
;
...
...
@@ -48,6 +49,7 @@ protected:
char
**
argv
=
nullptr
;
[[
noreturn
]]
void
exec_init
();
void
prepare_data
();
public
:
BaseInit
(
char
*
argv
[],
BootConfig
*
config
=
nullptr
)
:
config
(
config
),
argv
(
argv
)
{}
virtual
~
BaseInit
()
=
default
;
...
...
@@ -58,8 +60,6 @@ class MagiskInit : public BaseInit {
private
:
void
mount_rules_dir
();
protected
:
mmap_data
self
;
mmap_data
magisk_cfg
;
std
::
string
custom_rules_dir
;
#if ENABLE_AVD_HACK
...
...
@@ -72,16 +72,12 @@ protected:
bool
hijack_sepolicy
();
void
setup_tmp
(
const
char
*
path
);
void
patch_rw_root
();
void
patch_ro_root
();
public
:
using
BaseInit
::
BaseInit
;
};
class
SARBase
:
public
MagiskInit
{
protected
:
std
::
vector
<
raw_file
>
overlays
;
void
backup_files
();
void
patch_ro_root
();
public
:
using
MagiskInit
::
MagiskInit
;
};
...
...
@@ -134,6 +130,7 @@ public:
LOGD
(
"%s
\n
"
,
__FUNCTION__
);
};
void
start
()
override
{
prepare_data
();
if
(
mount_system_root
())
first_stage_prep
();
else
...
...
native/src/init/mount.cpp
View file @
4ed34cd6
...
...
@@ -214,8 +214,6 @@ success:
}
bool
LegacySARInit
::
mount_system_root
()
{
backup_files
();
LOGD
(
"Mounting system_root
\n
"
);
// there's no /dev in stub cpio
...
...
@@ -296,11 +294,19 @@ void BaseInit::exec_init() {
exit
(
1
);
}
void
BaseInit
::
prepare_data
()
{
LOGD
(
"Setup data tmp
\n
"
);
xmkdir
(
"/data"
,
0755
);
xmount
(
"tmpfs"
,
"/data"
,
"tmpfs"
,
0
,
"mode=755"
);
cp_afc
(
"/init"
,
"/data/magiskinit"
);
cp_afc
(
"/.backup"
,
"/data/.backup"
);
cp_afc
(
"/overlay.d"
,
"/data/overlay.d"
);
}
void
MagiskInit
::
setup_tmp
(
const
char
*
path
)
{
LOGD
(
"Setup Magisk tmp at %s
\n
"
,
path
);
xmount
(
"tmpfs"
,
path
,
"tmpfs"
,
0
,
"mode=755"
);
chdir
(
path
);
chdir
(
"/data"
);
xmkdir
(
INTLROOT
,
0755
);
xmkdir
(
MIRRDIR
,
0
);
...
...
@@ -308,14 +314,15 @@ void MagiskInit::setup_tmp(const char *path) {
mount_rules_dir
();
int
fd
=
xopen
(
INTLROOT
"/config"
,
O_WRONLY
|
O_CREAT
,
0
);
xwrite
(
fd
,
magisk_cfg
.
buf
,
magisk_cfg
.
sz
);
close
(
fd
);
cp_afc
(
".backup/.magisk"
,
INTLROOT
"/config"
);
rm_rf
(
".backup"
);
// Create applet symlinks
for
(
int
i
=
0
;
applet_names
[
i
];
++
i
)
xsymlink
(
"./magisk"
,
applet_names
[
i
]);
xsymlink
(
"./magiskpolicy"
,
"supolicy"
);
xmount
(
"."
,
path
,
nullptr
,
MS_BIND
,
nullptr
);
chdir
(
"/"
);
}
native/src/init/rootdir.cpp
View file @
4ed34cd6
...
...
@@ -130,19 +130,6 @@ static void magic_mount(const string &sdir, const string &ddir = "") {
}
}
void
SARBase
::
backup_files
()
{
if
(
access
(
"/overlay.d"
,
F_OK
)
==
0
)
backup_folder
(
"/overlay.d"
,
overlays
);
else
if
(
access
(
"/data/overlay.d"
,
F_OK
)
==
0
)
backup_folder
(
"/data/overlay.d"
,
overlays
);
self
=
mmap_data
(
"/proc/self/exe"
);
if
(
access
(
"/.backup/.magisk"
,
R_OK
)
==
0
)
magisk_cfg
=
mmap_data
(
"/.backup/.magisk"
);
else
if
(
access
(
"/data/.backup/.magisk"
,
R_OK
)
==
0
)
magisk_cfg
=
mmap_data
(
"/data/.backup/.magisk"
);
}
static
void
patch_socket_name
(
const
char
*
path
)
{
static
char
rstr
[
16
]
=
{
0
};
if
(
rstr
[
0
]
==
'\0'
)
...
...
@@ -181,7 +168,9 @@ static void extract_files(bool sbin) {
#define ROOTMIR MIRRDIR "/system_root"
#define NEW_INITRC "/system/etc/init/hw/init.rc"
void
SARBase
::
patch_ro_root
()
{
void
MagiskInit
::
patch_ro_root
()
{
mount_list
.
emplace_back
(
"/data"
);
string
tmp_dir
;
if
(
access
(
"/sbin"
,
F_OK
)
==
0
)
{
...
...
@@ -205,7 +194,7 @@ void SARBase::patch_ro_root() {
if
(
tmp_dir
==
"/sbin"
)
recreate_sbin
(
ROOTMIR
"/sbin"
,
true
);
x
mkdir
(
ROOTOVL
,
0
);
x
rename
(
"overlay.d"
,
ROOTOVL
);
#if ENABLE_AVD_HACK
// Handle avd hack
...
...
@@ -222,9 +211,6 @@ void SARBase::patch_ro_root() {
}
#endif
// Handle overlay.d
restore_folder
(
ROOTOVL
,
overlays
);
overlays
.
clear
();
load_overlay_rc
(
ROOTOVL
);
if
(
access
(
ROOTOVL
"/sbin"
,
F_OK
)
==
0
)
{
// Move files in overlay.d/sbin into tmp_dir
...
...
@@ -260,9 +246,7 @@ void SARBase::patch_ro_root() {
}
void
RootFSInit
::
prepare
()
{
self
=
mmap_data
(
"/init"
);
magisk_cfg
=
mmap_data
(
"/.backup/.magisk"
);
prepare_data
();
LOGD
(
"Restoring /init
\n
"
);
rename
(
backup_init
(),
"/init"
);
}
...
...
@@ -270,6 +254,7 @@ void RootFSInit::prepare() {
#define PRE_TMPDIR "/magisk-tmp"
void
MagiskInit
::
patch_rw_root
()
{
mount_list
.
emplace_back
(
"/data"
);
// Create hardlink mirror of /sbin to /root
mkdir
(
"/root"
,
0777
);
clone_attr
(
"/sbin"
,
"/root"
);
...
...
@@ -307,9 +292,7 @@ void MagiskInit::patch_rw_root() {
chdir
(
"/"
);
// Dump magiskinit as magisk
int
fd
=
xopen
(
"/sbin/magisk"
,
O_WRONLY
|
O_CREAT
,
0755
);
write
(
fd
,
self
.
buf
,
self
.
sz
);
close
(
fd
);
cp_afc
(
REDIR_PATH
,
"/sbin/magisk"
);
}
int
magisk_proxy_main
(
int
argc
,
char
*
argv
[])
{
...
...
native/src/init/twostage.cpp
View file @
4ed34cd6
...
...
@@ -8,30 +8,15 @@
using
namespace
std
;
#define REDIR_PATH "/data/magiskinit"
void
FirstStageInit
::
prepare
()
{
xmkdirs
(
"/data"
,
0755
);
xmount
(
"tmpfs"
,
"/data"
,
"tmpfs"
,
0
,
"mode=755"
);
cp_afc
(
"/init"
/* magiskinit */
,
REDIR_PATH
);
prepare_data
();
restore_ramdisk_init
();
{
auto
init
=
mmap_data
(
"/init"
,
true
);
// Redirect original init to magiskinit
init
.
patch
({
make_pair
(
INIT_PATH
,
REDIR_PATH
)
});
}
// Copy files to tmpfs
cp_afc
(
".backup"
,
"/data/.backup"
);
cp_afc
(
"overlay.d"
,
"/data/overlay.d"
);
auto
init
=
mmap_data
(
"/init"
,
true
);
// Redirect original init to magiskinit
init
.
patch
({
make_pair
(
INIT_PATH
,
REDIR_PATH
)
});
}
void
LegacySARInit
::
first_stage_prep
()
{
xmkdir
(
"/data"
,
0755
);
xmount
(
"tmpfs"
,
"/data"
,
"tmpfs"
,
0
,
"mode=755"
);
// Patch init binary
int
src
=
xopen
(
"/init"
,
O_RDONLY
);
int
dest
=
xopen
(
"/data/init"
,
O_CREAT
|
O_WRONLY
,
0
);
...
...
@@ -43,29 +28,11 @@ void LegacySARInit::first_stage_prep() {
close
(
dest
);
}
xmount
(
"/data/init"
,
"/init"
,
nullptr
,
MS_BIND
,
nullptr
);
// Replace redirect init with magiskinit
dest
=
xopen
(
REDIR_PATH
,
O_CREAT
|
O_WRONLY
,
0
);
write
(
dest
,
self
.
buf
,
self
.
sz
);
fclone_attr
(
src
,
dest
);
close
(
src
);
close
(
dest
);
// Copy files to tmpfs
xmkdir
(
"/data/.backup"
,
0
);
xmkdir
(
"/data/overlay.d"
,
0
);
restore_folder
(
"/data/overlay.d"
,
overlays
);
int
cfg
=
xopen
(
"/data/.backup/.magisk"
,
O_WRONLY
|
O_CREAT
,
0
);
xwrite
(
cfg
,
magisk_cfg
.
buf
,
magisk_cfg
.
sz
);
close
(
cfg
);
}
bool
SecondStageInit
::
prepare
()
{
backup_files
();
umount2
(
"/init"
,
MNT_DETACH
);
umount2
(
"/proc/self/exe"
,
MNT_DETACH
);
umount2
(
"/data"
,
MNT_DETACH
);
// Make sure init dmesg logs won't get messed up
argv
[
0
]
=
(
char
*
)
INIT_PATH
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment