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
db8dd9f1
Commit
db8dd9f1
authored
Jun 30, 2019
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Init code rearrangement
parent
e8b73ba6
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
46 additions
and
63 deletions
+46
-63
early_mount.cpp
native/jni/init/early_mount.cpp
+7
-1
init.cpp
native/jni/init/init.cpp
+2
-37
init.h
native/jni/init/init.h
+26
-12
rootdir.cpp
native/jni/init/rootdir.cpp
+11
-13
No files found.
native/jni/init/early_mount.cpp
View file @
db8dd9f1
#include <sys/mount.h>
#include <sys/sysmacros.h>
#include <sys/sysmacros.h>
#include <string.h>
#include <string.h>
#include <stdio.h>
#include <stdio.h>
...
@@ -77,6 +76,13 @@ static dev_t setup_block(const char *partname, char *block_dev = nullptr) {
...
@@ -77,6 +76,13 @@ static dev_t setup_block(const char *partname, char *block_dev = nullptr) {
}
}
}
}
static
bool
is_lnk
(
const
char
*
name
)
{
struct
stat
st
;
if
(
lstat
(
name
,
&
st
))
return
false
;
return
S_ISLNK
(
st
.
st_mode
);
}
bool
MagiskInit
::
read_dt_fstab
(
const
char
*
name
,
char
*
partname
,
char
*
fstype
)
{
bool
MagiskInit
::
read_dt_fstab
(
const
char
*
name
,
char
*
partname
,
char
*
fstype
)
{
char
path
[
128
];
char
path
[
128
];
int
fd
;
int
fd
;
...
...
native/jni/init/init.cpp
View file @
db8dd9f1
#include <sys/stat.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/sysmacros.h>
#include <sys/sysmacros.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <string.h>
#include <fcntl.h>
#include <fcntl.h>
#include <libgen.h>
#include <libgen.h>
...
@@ -48,7 +45,7 @@ static void setup_klog() {
...
@@ -48,7 +45,7 @@ static void setup_klog() {
// Prevent file descriptor confusion
// Prevent file descriptor confusion
mknod
(
"/null"
,
S_IFCHR
|
0666
,
makedev
(
1
,
3
));
mknod
(
"/null"
,
S_IFCHR
|
0666
,
makedev
(
1
,
3
));
int
null
=
open
(
"/null"
,
O_RDWR
|
O_CLOEXEC
);
int
null
=
x
open
(
"/null"
,
O_RDWR
|
O_CLOEXEC
);
unlink
(
"/null"
);
unlink
(
"/null"
);
xdup3
(
null
,
STDIN_FILENO
,
O_CLOEXEC
);
xdup3
(
null
,
STDIN_FILENO
,
O_CLOEXEC
);
xdup3
(
null
,
STDOUT_FILENO
,
O_CLOEXEC
);
xdup3
(
null
,
STDOUT_FILENO
,
O_CLOEXEC
);
...
@@ -122,38 +119,6 @@ static int dump_manager(const char *path, mode_t mode) {
...
@@ -122,38 +119,6 @@ static int dump_manager(const char *path, mode_t mode) {
return
0
;
return
0
;
}
}
void
BaseInit
::
cleanup
()
{
umount
(
"/sys"
);
umount
(
"/proc"
);
umount
(
"/dev"
);
}
void
BaseInit
::
re_exec_init
()
{
LOGD
(
"Re-exec /init
\n
"
);
cleanup
();
execv
(
"/init"
,
argv
);
exit
(
1
);
}
void
RootFSInit
::
start
()
{
early_mount
();
setup_rootfs
();
re_exec_init
();
}
void
SARCommon
::
start
()
{
early_mount
();
patch_rootdir
();
re_exec_init
();
}
void
FirstStageInit
::
start
()
{
patch_fstab
();
cleanup
();
execv
(
"/system/bin/init"
,
argv
);
exit
(
1
);
}
class
RecoveryInit
:
public
BaseInit
{
class
RecoveryInit
:
public
BaseInit
{
public
:
public
:
RecoveryInit
(
char
*
argv
[],
cmdline
*
cmd
)
:
BaseInit
(
argv
,
cmd
)
{};
RecoveryInit
(
char
*
argv
[],
cmdline
*
cmd
)
:
BaseInit
(
argv
,
cmd
)
{};
...
@@ -161,7 +126,7 @@ public:
...
@@ -161,7 +126,7 @@ public:
LOGD
(
"Ramdisk is recovery, abort
\n
"
);
LOGD
(
"Ramdisk is recovery, abort
\n
"
);
rename
(
"/.backup/init"
,
"/init"
);
rename
(
"/.backup/init"
,
"/init"
);
rm_rf
(
"/.backup"
);
rm_rf
(
"/.backup"
);
re_
exec_init
();
exec_init
();
}
}
};
};
...
...
native/jni/init/init.h
View file @
db8dd9f1
#include <sys/mount.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdlib.h>
struct
cmdline
{
struct
cmdline
{
...
@@ -21,8 +23,16 @@ protected:
...
@@ -21,8 +23,16 @@ protected:
cmdline
*
cmd
;
cmdline
*
cmd
;
char
**
argv
;
char
**
argv
;
void
re_exec_init
();
void
exec_init
(
const
char
*
init
=
"/init"
)
{
virtual
void
cleanup
();
cleanup
();
execv
(
init
,
argv
);
exit
(
1
);
}
virtual
void
cleanup
()
{
umount
(
"/sys"
);
umount
(
"/proc"
);
umount
(
"/dev"
);
}
public
:
public
:
BaseInit
(
char
*
argv
[],
cmdline
*
cmd
)
:
cmd
(
cmd
),
argv
(
argv
)
{}
BaseInit
(
char
*
argv
[],
cmdline
*
cmd
)
:
cmd
(
cmd
),
argv
(
argv
)
{}
virtual
~
BaseInit
()
=
default
;
virtual
~
BaseInit
()
=
default
;
...
@@ -52,7 +62,11 @@ protected:
...
@@ -52,7 +62,11 @@ protected:
virtual
void
setup_rootfs
();
virtual
void
setup_rootfs
();
public
:
public
:
RootFSInit
(
char
*
argv
[],
cmdline
*
cmd
)
:
MagiskInit
(
argv
,
cmd
)
{};
RootFSInit
(
char
*
argv
[],
cmdline
*
cmd
)
:
MagiskInit
(
argv
,
cmd
)
{};
void
start
()
override
;
void
start
()
override
{
early_mount
();
setup_rootfs
();
exec_init
();
}
};
};
class
SARCommon
:
public
MagiskInit
{
class
SARCommon
:
public
MagiskInit
{
...
@@ -63,7 +77,11 @@ protected:
...
@@ -63,7 +77,11 @@ protected:
void
patch_rootdir
();
void
patch_rootdir
();
public
:
public
:
SARCommon
(
char
*
argv
[],
cmdline
*
cmd
)
:
MagiskInit
(
argv
,
cmd
)
{};
SARCommon
(
char
*
argv
[],
cmdline
*
cmd
)
:
MagiskInit
(
argv
,
cmd
)
{};
void
start
()
override
;
void
start
()
override
{
early_mount
();
patch_rootdir
();
exec_init
();
}
};
};
/* *******************
/* *******************
...
@@ -75,7 +93,10 @@ protected:
...
@@ -75,7 +93,10 @@ protected:
void
patch_fstab
();
void
patch_fstab
();
public
:
public
:
FirstStageInit
(
char
*
argv
[],
cmdline
*
cmd
)
:
BaseInit
(
argv
,
cmd
)
{};
FirstStageInit
(
char
*
argv
[],
cmdline
*
cmd
)
:
BaseInit
(
argv
,
cmd
)
{};
void
start
()
override
;
void
start
()
override
{
patch_fstab
();
exec_init
(
"/system/bin/init"
);
}
};
};
class
SecondStageInit
:
public
SARCommon
{
class
SecondStageInit
:
public
SARCommon
{
...
@@ -120,13 +141,6 @@ public:
...
@@ -120,13 +141,6 @@ public:
SARCompatInit
(
char
*
argv
[],
cmdline
*
cmd
)
:
RootFSInit
(
argv
,
cmd
)
{};
SARCompatInit
(
char
*
argv
[],
cmdline
*
cmd
)
:
RootFSInit
(
argv
,
cmd
)
{};
};
};
static
inline
bool
is_lnk
(
const
char
*
name
)
{
struct
stat
st
;
if
(
lstat
(
name
,
&
st
))
return
false
;
return
S_ISLNK
(
st
.
st_mode
);
}
void
load_kernel_info
(
cmdline
*
cmd
);
void
load_kernel_info
(
cmdline
*
cmd
);
int
dump_magisk
(
const
char
*
path
,
mode_t
mode
);
int
dump_magisk
(
const
char
*
path
,
mode_t
mode
);
int
magisk_proxy_main
(
int
argc
,
char
*
argv
[]);
int
magisk_proxy_main
(
int
argc
,
char
*
argv
[]);
native/jni/init/rootdir.cpp
View file @
db8dd9f1
#include <sys/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <fcntl.h>
#include <fcntl.h>
#include <magisk.h>
#include <magisk.h>
...
@@ -396,19 +395,18 @@ int magisk_proxy_main(int argc, char *argv[]) {
...
@@ -396,19 +395,18 @@ int magisk_proxy_main(int argc, char *argv[]) {
sbin_overlay
(
self
,
config
);
sbin_overlay
(
self
,
config
);
// Create symlinks pointing back to /root
// Create symlinks pointing back to /root
{
char
path
[
256
];
char
path
[
256
];
int
sbin
=
xopen
(
"/sbin"
,
O_RDONLY
|
O_CLOEXEC
);
int
sbin
=
xopen
(
"/sbin"
,
O_RDONLY
|
O_CLOEXEC
);
DIR
*
dir
=
xopendir
(
"/root"
);
unique_ptr
<
DIR
,
decltype
(
&
closedir
)
>
dir
(
xopendir
(
"/root"
),
&
closedir
);
struct
dirent
*
entry
;
struct
dirent
*
entry
;
while
((
entry
=
xreaddir
(
dir
)))
{
while
((
entry
=
xreaddir
(
dir
.
get
())))
{
if
(
entry
->
d_name
==
"."
sv
||
entry
->
d_name
==
".."
sv
)
if
(
entry
->
d_name
==
"."
sv
||
entry
->
d_name
==
".."
sv
)
continue
;
continue
;
sprintf
(
path
,
"/root/%s"
,
entry
->
d_name
);
sprintf
(
path
,
"/root/%s"
,
entry
->
d_name
);
xsymlinkat
(
path
,
sbin
,
entry
->
d_name
);
xsymlinkat
(
path
,
sbin
,
entry
->
d_name
);
}
close
(
sbin
);
}
}
close
(
sbin
);
closedir
(
dir
);
setenv
(
"REMOUNT_ROOT"
,
"1"
,
1
);
setenv
(
"REMOUNT_ROOT"
,
"1"
,
1
);
execv
(
"/sbin/magisk"
,
argv
);
execv
(
"/sbin/magisk"
,
argv
);
...
...
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