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
765d5d97
Commit
765d5d97
authored
Apr 19, 2020
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Small magiskinit cleanup
parent
43029f37
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
48 additions
and
77 deletions
+48
-77
init.cpp
native/jni/init/init.cpp
+14
-13
init.hpp
native/jni/init/init.hpp
+11
-24
rootdir.cpp
native/jni/init/rootdir.cpp
+1
-1
twostage.cpp
native/jni/init/twostage.cpp
+22
-39
No files found.
native/jni/init/init.cpp
View file @
765d5d97
...
@@ -209,30 +209,31 @@ int main(int argc, char *argv[]) {
...
@@ -209,30 +209,31 @@ int main(int argc, char *argv[]) {
return
1
;
return
1
;
setup_klog
();
setup_klog
();
unique_ptr
<
BaseInit
>
init
;
BaseInit
*
init
;
cmdline
cmd
{};
cmdline
cmd
{};
if
(
argc
>
1
&&
argv
[
1
]
==
"selinux_setup"
sv
)
{
if
(
argc
>
1
&&
argv
[
1
]
==
"selinux_setup"
sv
)
{
init
=
make_unique
<
SecondStageInit
>
(
argv
);
init
=
new
SecondStageInit
(
argv
);
}
else
{
}
else
{
// This will also mount /sys and /proc
// This will also mount /sys and /proc
load_kernel_info
(
&
cmd
);
load_kernel_info
(
&
cmd
);
if
(
access
(
"/apex"
,
F_OK
)
==
0
)
{
bool
two_stage
=
access
(
"/apex"
,
F_OK
)
==
0
;
if
(
cmd
.
force_normal_boot
)
if
(
cmd
.
skip_initramfs
)
{
init
=
make_unique
<
ForcedFirstStageInit
>
(
argv
,
&
cmd
);
if
(
two_stage
)
else
if
(
cmd
.
skip_initramfs
)
init
=
new
SARFirstStageInit
(
argv
,
&
cmd
);
init
=
make_unique
<
SARFirstStageInit
>
(
argv
,
&
cmd
);
else
else
init
=
make_unique
<
FirstStageInit
>
(
argv
,
&
cmd
);
init
=
new
SARInit
(
argv
,
&
cmd
);
}
else
if
(
cmd
.
skip_initramfs
)
{
init
=
make_unique
<
SARInit
>
(
argv
,
&
cmd
);
}
else
{
}
else
{
decompress_ramdisk
();
decompress_ramdisk
();
if
(
access
(
"/sbin/recovery"
,
F_OK
)
==
0
||
access
(
"/system/bin/recovery"
,
F_OK
)
==
0
)
if
(
cmd
.
force_normal_boot
)
init
=
make_unique
<
RecoveryInit
>
(
argv
,
&
cmd
);
init
=
new
FirstStageInit
(
argv
,
&
cmd
);
else
if
(
access
(
"/sbin/recovery"
,
F_OK
)
==
0
||
access
(
"/system/bin/recovery"
,
F_OK
)
==
0
)
init
=
new
RecoveryInit
(
argv
,
&
cmd
);
else
if
(
two_stage
)
init
=
new
FirstStageInit
(
argv
,
&
cmd
);
else
else
init
=
make_unique
<
RootFSInit
>
(
argv
,
&
cmd
);
init
=
new
RootFSInit
(
argv
,
&
cmd
);
}
}
}
}
...
...
native/jni/init/init.hpp
View file @
765d5d97
...
@@ -31,9 +31,9 @@ struct raw_data {
...
@@ -31,9 +31,9 @@ struct raw_data {
}
}
};
};
/*
*************
/*
*
*************
* Base classes
* Base classes
*
*************/
*
*
*************/
class
BaseInit
{
class
BaseInit
{
protected
:
protected
:
...
@@ -41,9 +41,9 @@ protected:
...
@@ -41,9 +41,9 @@ protected:
char
**
argv
;
char
**
argv
;
std
::
vector
<
std
::
string
>
mount_list
;
std
::
vector
<
std
::
string
>
mount_list
;
void
exec_init
(
const
char
*
init
=
"/init"
)
{
void
exec_init
()
{
cleanup
();
cleanup
();
execv
(
init
,
argv
);
execv
(
"/init"
,
argv
);
exit
(
1
);
exit
(
1
);
}
}
virtual
void
cleanup
();
virtual
void
cleanup
();
...
@@ -60,7 +60,7 @@ protected:
...
@@ -60,7 +60,7 @@ protected:
std
::
string
persist_dir
;
std
::
string
persist_dir
;
virtual
void
early_mount
()
=
0
;
virtual
void
early_mount
()
=
0
;
bool
patch_sepolicy
(
const
char
*
file
=
"/sepolicy"
);
bool
patch_sepolicy
(
const
char
*
file
);
public
:
public
:
MagiskInit
(
char
*
argv
[],
cmdline
*
cmd
)
:
BaseInit
(
argv
,
cmd
)
{}
MagiskInit
(
char
*
argv
[],
cmdline
*
cmd
)
:
BaseInit
(
argv
,
cmd
)
{}
};
};
...
@@ -82,22 +82,9 @@ public:
...
@@ -82,22 +82,9 @@ public:
}
}
};
};
/*
*************
/*
*
*************
* 2 Stage Init
* 2 Stage Init
* *************/
***************/
class
ForcedFirstStageInit
:
public
BaseInit
{
private
:
void
prepare
();
public
:
ForcedFirstStageInit
(
char
*
argv
[],
cmdline
*
cmd
)
:
BaseInit
(
argv
,
cmd
)
{
LOGD
(
"%s
\n
"
,
__FUNCTION__
);
};
void
start
()
override
{
prepare
();
exec_init
(
"/system/bin/init"
);
}
};
class
FirstStageInit
:
public
BaseInit
{
class
FirstStageInit
:
public
BaseInit
{
private
:
private
:
...
@@ -137,9 +124,9 @@ public:
...
@@ -137,9 +124,9 @@ public:
};
};
};
};
/*
***********
/*
*
***********
* Legacy SAR
* Legacy SAR
*
***********/
*
*
***********/
class
SARInit
:
public
SARBase
{
class
SARInit
:
public
SARBase
{
protected
:
protected
:
...
@@ -150,9 +137,9 @@ public:
...
@@ -150,9 +137,9 @@ public:
};
};
};
};
/*
**********
/*
*
**********
* Initramfs
* Initramfs
*
**********/
*
*
**********/
class
RootFSInit
:
public
MagiskInit
{
class
RootFSInit
:
public
MagiskInit
{
private
:
private
:
...
...
native/jni/init/rootdir.cpp
View file @
765d5d97
...
@@ -81,7 +81,7 @@ static void load_overlay_rc(const char *overlay) {
...
@@ -81,7 +81,7 @@ static void load_overlay_rc(const char *overlay) {
}
}
void
RootFSInit
::
setup_rootfs
()
{
void
RootFSInit
::
setup_rootfs
()
{
if
(
patch_sepolicy
())
{
if
(
patch_sepolicy
(
"/sepolicy"
))
{
char
*
addr
;
char
*
addr
;
size_t
size
;
size_t
size
;
mmap_rw
(
"/init"
,
addr
,
size
);
mmap_rw
(
"/init"
,
addr
,
size
);
...
...
native/jni/init/twostage.cpp
View file @
765d5d97
...
@@ -10,10 +10,10 @@
...
@@ -10,10 +10,10 @@
using
namespace
std
;
using
namespace
std
;
static
void
patch_fstab
(
const
string
&
fstab
)
{
static
void
patch_fstab
(
const
char
*
fstab
)
{
string
patched
=
fstab
+
".p"
;
string
patched
=
fstab
+
".p"
s
;
FILE
*
fp
=
xfopen
(
patched
.
data
(),
"we"
);
FILE
*
fp
=
xfopen
(
patched
.
data
(),
"we"
);
file_readline
(
fstab
.
data
()
,
[
=
](
string_view
l
)
->
bool
{
file_readline
(
fstab
,
[
=
](
string_view
l
)
->
bool
{
if
(
l
[
0
]
==
'#'
||
l
.
length
()
==
1
)
if
(
l
[
0
]
==
'#'
||
l
.
length
()
==
1
)
return
true
;
return
true
;
char
*
line
=
(
char
*
)
l
.
data
();
char
*
line
=
(
char
*
)
l
.
data
();
...
@@ -42,55 +42,38 @@ static void patch_fstab(const string &fstab) {
...
@@ -42,55 +42,38 @@ static void patch_fstab(const string &fstab) {
fclose
(
fp
);
fclose
(
fp
);
// Replace old fstab
// Replace old fstab
clone_attr
(
fstab
.
data
()
,
patched
.
data
());
clone_attr
(
fstab
,
patched
.
data
());
rename
(
patched
.
data
(),
fstab
.
data
()
);
rename
(
patched
.
data
(),
fstab
);
}
}
#define FSR "/first_stage_ramdisk"
#define FSR "/first_stage_ramdisk"
void
ForcedFirstStageInit
::
prepare
()
{
void
FirstStageInit
::
prepare
()
{
// It is actually possible to NOT have FSR, create it just in case
if
(
cmd
->
force_normal_boot
)
{
xmkdir
(
FSR
,
0755
);
xmkdirs
(
FSR
"/system/bin"
,
0755
);
if
(
auto
dir
=
xopen_dir
(
FSR
);
dir
)
{
string
fstab
(
FSR
"/"
);
for
(
dirent
*
de
;
(
de
=
xreaddir
(
dir
.
get
()));)
{
if
(
strstr
(
de
->
d_name
,
"fstab"
))
{
fstab
+=
de
->
d_name
;
break
;
}
}
if
(
fstab
.
length
()
==
sizeof
(
FSR
))
return
;
patch_fstab
(
fstab
);
}
else
{
return
;
}
// Move stuffs for next stage
xmkdir
(
FSR
"/system"
,
0755
);
xmkdir
(
FSR
"/system/bin"
,
0755
);
rename
(
"/init"
/* magiskinit */
,
FSR
"/system/bin/init"
);
rename
(
"/init"
/* magiskinit */
,
FSR
"/system/bin/init"
);
symlink
(
"/system/bin/init"
,
FSR
"/init"
);
symlink
(
"/system/bin/init"
,
FSR
"/init"
);
rename
(
"/.backup"
,
FSR
"/.backup"
);
rename
(
"/.backup"
,
FSR
"/.backup"
);
rename
(
"/overlay.d"
,
FSR
"/overlay.d"
);
rename
(
"/overlay.d"
,
FSR
"/overlay.d"
);
}
xsymlink
(
"/system/bin/init"
,
"/init"
);
void
FirstStageInit
::
prepare
()
{
chdir
(
FSR
);
auto
dir
=
xopen_dir
(
"/"
);
}
else
{
xmkdir
(
"/system"
,
0755
);
xmkdir
(
"/system/bin"
,
0755
);
rename
(
"/init"
/* magiskinit */
,
"/system/bin/init"
);
rename
(
"/.backup/init"
,
"/init"
);
}
// Patch fstab
auto
dir
=
xopen_dir
(
"."
);
for
(
dirent
*
de
;
(
de
=
xreaddir
(
dir
.
get
()));)
{
for
(
dirent
*
de
;
(
de
=
xreaddir
(
dir
.
get
()));)
{
if
(
strstr
(
de
->
d_name
,
"fstab"
))
{
if
(
strstr
(
de
->
d_name
,
"fstab"
))
{
patch_fstab
(
de
->
d_name
);
patch_fstab
(
de
->
d_name
);
break
;
break
;
}
}
}
}
chdir
(
"/"
);
// Move stuffs for next stage
xmkdir
(
"/system"
,
0755
);
xmkdir
(
"/system/bin"
,
0755
);
rename
(
"/init"
/* magiskinit */
,
"/system/bin/init"
);
rename
(
"/.backup/init"
,
"/init"
);
}
}
static
inline
long
xptrace
(
int
request
,
pid_t
pid
,
void
*
addr
,
void
*
data
)
{
static
inline
long
xptrace
(
int
request
,
pid_t
pid
,
void
*
addr
,
void
*
data
)
{
...
...
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