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
6e299018
Commit
6e299018
authored
Sep 02, 2022
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Preserve logd_fd after specialization
Also add more comments regarding FD checks
parent
555a54ec
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
216 additions
and
142 deletions
+216
-142
logging.cpp
native/src/core/logging.cpp
+22
-21
entry.cpp
native/src/zygisk/entry.cpp
+33
-18
hook.cpp
native/src/zygisk/hook.cpp
+160
-102
module.hpp
native/src/zygisk/module.hpp
+1
-1
No files found.
native/src/core/logging.cpp
View file @
6e299018
...
...
@@ -125,7 +125,9 @@ static void *logfile_writer(void *arg) {
}
void
magisk_log_write
(
int
prio
,
const
char
*
msg
,
int
len
)
{
if
(
logd_fd
>=
0
)
{
if
(
logd_fd
<
0
)
return
;
// Truncate
len
=
std
::
min
(
MAX_MSG_LEN
,
len
);
...
...
@@ -146,7 +148,6 @@ void magisk_log_write(int prio, const char *msg, int len) {
// Stop trying to write to file
close
(
logd_fd
.
exchange
(
-
1
));
}
}
}
void
start_log_daemon
()
{
...
...
native/src/zygisk/entry.cpp
View file @
6e299018
...
...
@@ -76,36 +76,51 @@ extern "C" void zygisk_inject_entry(void *handle) {
// The following code runs in zygote/app process
extern
"C"
void
zygisk_log_write
(
int
prio
,
const
char
*
msg
,
int
len
)
{
// If we don't have log pipe set, ask magiskd for it
// This could happen multiple times in zygote because it was closed to prevent crashing
// If we don't have the log pipe set, request magiskd for it. This could actually happen
// multiple times in the zygote daemon (parent process) because we had to close this
// file descriptor to prevent crashing.
//
// For some reason, zygote sanitizes and checks FDs *before* forking. This results in the fact
// that *every* time before zygote forks, it has to close all logging related FDs in order
// to pass FD checks, just to have it re-initialized immediately after any
// logging happens ¯\_(ツ)_/¯.
//
// To be consistent with this behavior, we also have to close the log pipe to magiskd
// to make zygote NOT crash if necessary. For nativeForkAndSpecialize, we can actually
// add this FD into fds_to_ignore to pass the check. For other cases, we accomplish this by
// hooking __android_log_close and closing it at the same time as the rest of logging FDs.
if
(
logd_fd
<
0
)
{
// Change logging temporarily to prevent infinite recursion and stack overflow
android_logging
();
if
(
int
fd
=
zygisk_request
(
ZygiskRequest
::
GET_LOG_PIPE
);
fd
>=
0
)
{
int
log_pipe
=
-
1
;
if
(
read_int
(
fd
)
==
0
)
{
log
d_fd
=
recv_fd
(
fd
);
log
_pipe
=
recv_fd
(
fd
);
}
close
(
fd
);
}
if
(
log_pipe
>=
0
)
{
// Only re-enable zygisk logging if possible
logd_fd
=
log_pipe
;
zygisk_logging
();
}
}
else
{
return
;
}
}
// Block SIGPIPE
sigset_t
mask
;
sigset_t
orig_mask
;
bool
sig
=
false
;
// Make sure SIGPIPE won't crash zygote
if
(
logd_fd
>=
0
)
{
sig
=
true
;
sigemptyset
(
&
mask
);
sigaddset
(
&
mask
,
SIGPIPE
);
pthread_sigmask
(
SIG_BLOCK
,
&
mask
,
&
orig_mask
);
}
magisk_log_write
(
prio
,
msg
,
len
);
if
(
sig
)
{
// Consume SIGPIPE if exists, then restore mask
timespec
ts
{};
sigtimedwait
(
&
mask
,
nullptr
,
&
ts
);
pthread_sigmask
(
SIG_SETMASK
,
&
orig_mask
,
nullptr
);
}
}
static
inline
bool
should_load_modules
(
uint32_t
flags
)
{
...
...
native/src/zygisk/hook.cpp
View file @
6e299018
This diff is collapsed.
Click to expand it.
native/src/zygisk/module.hpp
View file @
6e299018
...
...
@@ -173,7 +173,7 @@ struct ZygiskModule {
ZygiskModule
(
int
id
,
void
*
handle
,
void
*
entry
);
static
bool
RegisterModule
(
api_abi_base
*
api
,
long
*
module
);
static
bool
RegisterModule
Impl
(
api_abi_base
*
api
,
long
*
module
);
private
:
const
int
id
;
...
...
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