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
52a2c695
Commit
52a2c695
authored
Jul 03, 2018
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Optimize log daemon
parent
70243d7a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
36 deletions
+21
-36
log_monitor.c
native/jni/core/log_monitor.c
+21
-36
No files found.
native/jni/core/log_monitor.c
View file @
52a2c695
...
...
@@ -17,7 +17,6 @@
int
loggable
=
1
;
static
int
sockfd
;
static
pthread_t
thread
=
-
1
;
static
pthread_mutex_t
lock
=
PTHREAD_MUTEX_INITIALIZER
;
enum
{
...
...
@@ -38,7 +37,7 @@ static int magisk_log_filter(const char *log) {
return
!
am_proc_start_filter
(
log
);
}
static
struct
log_listener
log_
events
[]
=
{
static
struct
log_listener
events
[]
=
{
{
/* HIDE_EVENT */
.
fd
=
-
1
,
.
filter
=
am_proc_start_filter
...
...
@@ -48,7 +47,7 @@ static struct log_listener log_events[] = {
.
filter
=
magisk_log_filter
}
};
#define EVENT_NUM (sizeof(
log_
events) / sizeof(struct log_listener))
#define EVENT_NUM (sizeof(events) / sizeof(struct log_listener))
static
void
test_logcat
()
{
int
log_fd
=
-
1
,
log_pid
;
...
...
@@ -56,28 +55,28 @@ static void test_logcat() {
log_pid
=
exec_command
(
0
,
&
log_fd
,
NULL
,
"logcat"
,
NULL
);
if
(
read
(
log_fd
,
buf
,
sizeof
(
buf
))
!=
sizeof
(
buf
))
{
loggable
=
0
;
LOGD
(
"
log_monitor
: cannot read from logcat, disable logging"
);
LOGD
(
"
magisklogd
: cannot read from logcat, disable logging"
);
}
kill
(
log_pid
,
SIGTERM
);
waitpid
(
log_pid
,
NULL
,
0
);
}
static
void
sigpipe_handler
(
int
sig
)
{
close
(
log_
events
[
HIDE_EVENT
].
fd
);
log_
events
[
HIDE_EVENT
].
fd
=
-
1
;
close
(
events
[
HIDE_EVENT
].
fd
);
events
[
HIDE_EVENT
].
fd
=
-
1
;
}
static
void
*
socket_thread
(
void
*
args
)
{
/* This would block, so separate thread */
while
(
1
)
{
int
fd
=
accept4
(
sockfd
,
NULL
,
NULL
,
SOCK_CLOEXEC
);
int
fd
=
x
accept4
(
sockfd
,
NULL
,
NULL
,
SOCK_CLOEXEC
);
switch
(
read_int
(
fd
))
{
case
HIDE_CONNECT
:
pthread_mutex_lock
(
&
lock
);
log_events
[
HIDE_EVENT
].
fd
=
fd
;
close
(
events
[
HIDE_EVENT
].
fd
);
events
[
HIDE_EVENT
].
fd
=
fd
;
pthread_mutex_unlock
(
&
lock
);
thread
=
-
1
;
return
NULL
;
break
;
default:
close
(
fd
);
break
;
...
...
@@ -107,20 +106,20 @@ static void *monitor_thread(void *args) {
void
log_daemon
()
{
setsid
();
strcpy
(
argv0
,
"magisklogd"
);
struct
sockaddr_un
sun
;
sockfd
=
setup_socket
(
&
sun
,
LOG_DAEMON
);
if
(
xbind
(
sockfd
,
(
struct
sockaddr
*
)
&
sun
,
sizeof
(
sun
)))
exit
(
1
);
xlisten
(
sockfd
,
1
);
xlisten
(
sockfd
,
1
0
);
LOGI
(
"Magisk v"
xstr
(
MAGISK_VERSION
)
"("
xstr
(
MAGISK_VER_CODE
)
") logger started
\n
"
);
strcpy
(
argv0
,
"magisklogd"
);
// Start invincible mode monitor
// TODO: Remove this when all crashes are fixed
// Start worker threads
pthread_t
t
;
pthread_create
(
&
t
,
NULL
,
monitor_thread
,
NULL
);
pthread_detach
(
t
);
xpthread_create
(
&
t
,
NULL
,
socket_thread
,
NULL
);
pthread_detach
(
t
);
// Set SIGPIPE handler
struct
sigaction
act
;
...
...
@@ -130,21 +129,12 @@ void log_daemon() {
// Setup log dumps
rename
(
LOGFILE
,
LOGFILE
".bak"
);
log_
events
[
LOG_EVENT
].
fd
=
xopen
(
LOGFILE
,
O_CREAT
|
O_WRONLY
|
O_TRUNC
|
O_CLOEXEC
,
0644
);
events
[
LOG_EVENT
].
fd
=
xopen
(
LOGFILE
,
O_CREAT
|
O_WRONLY
|
O_TRUNC
|
O_CLOEXEC
,
0644
);
int
log_fd
=
-
1
,
log_pid
;
char
line
[
PIPE_BUF
];
while
(
1
)
{
if
(
!
loggable
)
{
// Disable all services
for
(
int
i
=
0
;
i
<
EVENT_NUM
;
++
i
)
{
close
(
log_events
[
i
].
fd
);
log_events
[
i
].
fd
=
-
1
;
}
return
;
}
// Start logcat
log_pid
=
exec_command
(
0
,
&
log_fd
,
NULL
,
"/system/bin/logcat"
,
...
...
@@ -159,25 +149,20 @@ void log_daemon() {
size_t
len
=
strlen
(
line
);
pthread_mutex_lock
(
&
lock
);
for
(
int
i
=
0
;
i
<
EVENT_NUM
;
++
i
)
{
if
(
log_events
[
i
].
fd
>
0
&&
log_events
[
i
].
filter
(
line
))
write
(
log_events
[
i
].
fd
,
line
,
len
);
}
if
(
thread
<
0
&&
log_events
[
HIDE_EVENT
].
fd
<
0
)
{
// New thread to handle connection to main daemon
xpthread_create
(
&
thread
,
NULL
,
socket_thread
,
NULL
);
pthread_detach
(
thread
);
if
(
events
[
i
].
fd
>
0
&&
events
[
i
].
filter
(
line
))
write
(
events
[
i
].
fd
,
line
,
len
);
}
pthread_mutex_unlock
(
&
lock
);
if
(
kill
(
log_pid
,
0
))
break
;
}
// Cleanup
fclose
(
logs
);
log_fd
=
-
1
;
kill
(
log_pid
,
SIGTERM
);
waitpid
(
log_pid
,
NULL
,
0
);
test_logcat
();
LOGI
(
"magisklogd: logcat output EOF"
);
// Clear buffer
exec_command_sync
(
"logcat"
,
"-b"
,
"events"
,
"-b"
,
"main"
,
"-b"
,
"crash"
,
"-c"
,
NULL
);
}
}
...
...
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