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
b0567ead
Commit
b0567ead
authored
Dec 18, 2017
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reduce effort and memory of log monitor
parent
5fc20583
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
112 additions
and
88 deletions
+112
-88
log_monitor.c
core/jni/core/log_monitor.c
+50
-28
logging.h
core/jni/include/logging.h
+7
-1
proc_monitor.c
core/jni/magiskhide/proc_monitor.c
+55
-59
No files found.
core/jni/core/log_monitor.c
View file @
b0567ead
...
@@ -13,9 +13,36 @@
...
@@ -13,9 +13,36 @@
#include "magisk.h"
#include "magisk.h"
#include "utils.h"
#include "utils.h"
int
logcat_events
[]
=
{
-
1
,
-
1
,
-
1
};
extern
int
is_daemon_init
;
extern
int
is_daemon_init
;
static
int
am_proc_start_filter
(
const
char
*
log
)
{
return
strstr
(
log
,
"am_proc_start"
)
!=
NULL
;
}
static
int
magisk_log_filter
(
const
char
*
log
)
{
char
*
ss
;
return
(
ss
=
strstr
(
log
,
" Magisk"
))
&&
(
ss
[
-
1
]
!=
'D'
)
&&
(
ss
[
-
1
]
!=
'V'
);
}
static
int
magisk_debug_log_filter
(
const
char
*
log
)
{
return
strstr
(
log
,
"Magisk"
)
!=
NULL
;
}
struct
log_listener
log_events
[]
=
{
{
/* HIDE_EVENT */
.
fd
=
-
1
,
.
filter
=
am_proc_start_filter
},
{
/* LOG_EVENT */
.
fd
=
-
1
,
.
filter
=
magisk_log_filter
},
{
/* DEBUG_EVENT */
.
fd
=
-
1
,
.
filter
=
magisk_debug_log_filter
}
};
#ifdef MAGISK_DEBUG
#ifdef MAGISK_DEBUG
static
int
debug_log_pid
,
debug_log_fd
;
static
int
debug_log_pid
,
debug_log_fd
;
#endif
#endif
...
@@ -30,10 +57,10 @@ static void *logger_thread(void *args) {
...
@@ -30,10 +57,10 @@ static void *logger_thread(void *args) {
// Start logcat
// Start logcat
log_pid
=
exec_command
(
0
,
&
log_fd
,
NULL
,
"logcat"
,
"-b"
,
"all"
,
"-v"
,
"threadtime"
,
"-s"
,
"am_proc_start"
,
"Magisk"
,
NULL
);
log_pid
=
exec_command
(
0
,
&
log_fd
,
NULL
,
"logcat"
,
"-b"
,
"all"
,
"-v"
,
"threadtime"
,
"-s"
,
"am_proc_start"
,
"Magisk"
,
NULL
);
while
(
fdgets
(
line
,
sizeof
(
line
),
log_fd
))
{
while
(
fdgets
(
line
,
sizeof
(
line
),
log_fd
))
{
for
(
int
i
=
0
;
i
<
(
sizeof
(
log
cat_events
)
/
sizeof
(
int
));
++
i
)
{
for
(
int
i
=
0
;
i
<
(
sizeof
(
log
_events
)
/
sizeof
(
struct
log_listener
));
++
i
)
{
if
(
log
cat_events
[
i
]
>
0
)
{
if
(
log
_events
[
i
].
fd
>
0
&&
log_events
[
i
].
filter
(
line
)
)
{
char
*
s
=
strdup
(
line
);
char
*
s
=
strdup
(
line
);
xwrite
(
log
cat_events
[
i
]
,
&
s
,
sizeof
(
s
));
xwrite
(
log
_events
[
i
].
fd
,
&
s
,
sizeof
(
s
));
}
}
}
}
if
(
kill
(
log_pid
,
0
))
if
(
kill
(
log_pid
,
0
))
...
@@ -66,32 +93,29 @@ static void *magisk_log_thread(void *args) {
...
@@ -66,32 +93,29 @@ static void *magisk_log_thread(void *args) {
return
NULL
;
return
NULL
;
// Register our listener
// Register our listener
log
cat_events
[
LOG_EVENT
]
=
pipefd
[
1
];
log
_events
[
LOG_EVENT
].
fd
=
pipefd
[
1
];
LOGD
(
"log_monitor: magisk log dumper start"
);
LOGD
(
"log_monitor: magisk log dumper start"
);
FILE
*
log
;
FILE
*
log
;
char
*
ss
;
for
(
char
*
line
;
xxread
(
pipefd
[
0
],
&
line
,
sizeof
(
line
))
>
0
;
free
(
line
))
{
for
(
char
*
line
;
xxread
(
pipefd
[
0
],
&
line
,
sizeof
(
line
))
>
0
;
free
(
line
))
{
if
((
ss
=
strstr
(
line
,
" Magisk"
))
&&
(
ss
[
-
1
]
!=
'D'
)
&&
(
ss
[
-
1
]
!=
'V'
))
{
if
(
!
have_data
)
{
if
(
!
have_data
)
{
if
((
have_data
=
check_data
()))
{
if
((
have_data
=
check_data
()))
{
// Dump buffered logs to file
// Dump buffered logs to file
log
=
xfopen
(
LOGFILE
,
"w"
);
log
=
xfopen
(
LOGFILE
,
"w"
);
setbuf
(
log
,
NULL
);
setbuf
(
log
,
NULL
);
char
*
tmp
;
char
*
tmp
;
vec_for_each
(
&
logs
,
tmp
)
{
vec_for_each
(
&
logs
,
tmp
)
{
fprintf
(
log
,
"%s"
,
tmp
);
fprintf
(
log
,
"%s"
,
tmp
);
free
(
tmp
);
free
(
tmp
);
}
vec_destroy
(
&
logs
);
}
else
{
vec_push_back
(
&
logs
,
strdup
(
line
));
}
}
vec_destroy
(
&
logs
);
}
else
{
vec_push_back
(
&
logs
,
strdup
(
line
));
}
}
if
(
have_data
)
fprintf
(
log
,
"%s"
,
line
);
}
}
if
(
have_data
)
fprintf
(
log
,
"%s"
,
line
);
}
}
return
NULL
;
return
NULL
;
}
}
...
@@ -106,13 +130,11 @@ static void *debug_magisk_log_thread(void *args) {
...
@@ -106,13 +130,11 @@ static void *debug_magisk_log_thread(void *args) {
LOGD
(
"log_monitor: debug log dumper start"
);
LOGD
(
"log_monitor: debug log dumper start"
);
// Register our listener
// Register our listener
logcat_events
[
DEBUG_EVENT
]
=
pipefd
[
1
];
log_events
[
DEBUG_EVENT
].
fd
=
pipefd
[
1
];
for
(
char
*
line
;
xxread
(
pipefd
[
0
],
&
line
,
sizeof
(
line
))
>
0
;
free
(
line
))
fprintf
(
log
,
"%s"
,
line
);
for
(
char
*
line
;
xxread
(
pipefd
[
0
],
&
line
,
sizeof
(
line
))
>
0
;
free
(
line
))
{
char
*
ss
;
if
((
ss
=
strstr
(
line
,
"Magisk"
)))
fprintf
(
log
,
"%s"
,
line
);
}
return
NULL
;
return
NULL
;
}
}
...
...
core/jni/include/logging.h
View file @
b0567ead
...
@@ -51,7 +51,13 @@ enum {
...
@@ -51,7 +51,13 @@ enum {
LOG_EVENT
,
LOG_EVENT
,
DEBUG_EVENT
DEBUG_EVENT
};
};
extern
int
logcat_events
[];
struct
log_listener
{
int
fd
;
int
(
*
filter
)
(
const
char
*
);
};
extern
struct
log_listener
log_events
[];
void
monitor_logs
();
void
monitor_logs
();
void
start_debug_full_log
();
void
start_debug_full_log
();
...
...
core/jni/magiskhide/proc_monitor.c
View file @
b0567ead
...
@@ -28,7 +28,7 @@ static void term_thread(int sig) {
...
@@ -28,7 +28,7 @@ static void term_thread(int sig) {
destroy_list
();
destroy_list
();
hideEnabled
=
0
;
hideEnabled
=
0
;
// Unregister listener
// Unregister listener
log
cat_events
[
HIDE_EVENT
]
=
-
1
;
log
_events
[
HIDE_EVENT
].
fd
=
-
1
;
close
(
pipefd
[
0
]);
close
(
pipefd
[
0
]);
close
(
pipefd
[
1
]);
close
(
pipefd
[
1
]);
pipefd
[
0
]
=
pipefd
[
1
]
=
-
1
;
pipefd
[
0
]
=
pipefd
[
1
]
=
-
1
;
...
@@ -193,79 +193,75 @@ void proc_monitor() {
...
@@ -193,79 +193,75 @@ void proc_monitor() {
// Register our listener to logcat monitor
// Register our listener to logcat monitor
xpipe2
(
pipefd
,
O_CLOEXEC
);
xpipe2
(
pipefd
,
O_CLOEXEC
);
log
cat_events
[
HIDE_EVENT
]
=
pipefd
[
1
];
log
_events
[
HIDE_EVENT
].
fd
=
pipefd
[
1
];
for
(
char
*
log
,
*
line
;
1
;
free
(
log
))
{
for
(
char
*
log
,
*
line
;;
free
(
log
))
{
if
(
read
(
pipefd
[
0
],
&
log
,
sizeof
(
log
))
!=
sizeof
(
log
))
{
if
(
read
(
pipefd
[
0
],
&
log
,
sizeof
(
log
))
!=
sizeof
(
log
))
{
/* It might be interrupted */
/* It might be interrupted */
log
=
NULL
;
log
=
NULL
;
continue
;
continue
;
}
}
char
*
ss
;
char
*
ss
=
strchr
(
log
,
'['
);
if
((
ss
=
strstr
(
log
,
"am_proc_start"
))
&&
(
ss
=
strchr
(
ss
,
'['
)))
{
int
pid
,
ret
,
comma
=
0
;
int
pid
,
ret
,
comma
=
0
;
char
*
pos
=
ss
,
processName
[
256
],
ns
[
32
];
char
*
pos
=
ss
,
processName
[
256
],
ns
[
32
];
while
(
1
)
{
while
(
1
)
{
pos
=
strchr
(
pos
,
','
);
pos
=
strchr
(
pos
,
','
);
if
(
pos
==
NULL
)
if
(
pos
==
NULL
)
break
;
break
;
pos
[
0
]
=
' '
;
pos
[
0
]
=
' '
;
++
comma
;
++
comma
;
}
}
if
(
comma
==
6
)
ret
=
sscanf
(
ss
,
"[%*d %d %*d %*d %256s"
,
&
pid
,
processName
);
else
ret
=
sscanf
(
ss
,
"[%*d %d %*d %256s"
,
&
pid
,
processName
);
if
(
comma
==
6
)
if
(
ret
!=
2
)
ret
=
sscanf
(
ss
,
"[%*d %d %*d %*d %256s"
,
&
pid
,
processName
);
continue
;
else
ret
=
sscanf
(
ss
,
"[%*d %d %*d %256s"
,
&
pid
,
processName
);
// Critical region
pthread_mutex_lock
(
&
hide_lock
);
if
(
ret
!=
2
)
vec_for_each
(
hide_list
,
line
)
{
continue
;
if
(
strcmp
(
processName
,
line
)
==
0
)
{
while
(
1
)
{
ret
=
0
;
ret
=
1
;
for
(
int
i
=
0
;
i
<
zygote_num
;
++
i
)
{
// Critical region
read_namespace
(
pid
,
ns
,
sizeof
(
ns
));
pthread_mutex_lock
(
&
hide_lock
);
if
(
strcmp
(
ns
,
zygote_ns
[
i
])
==
0
)
{
vec_for_each
(
hide_list
,
line
)
{
usleep
(
50
);
if
(
strcmp
(
processName
,
line
)
==
0
)
{
ret
=
0
;
while
(
1
)
{
break
;
ret
=
1
;
for
(
int
i
=
0
;
i
<
zygote_num
;
++
i
)
{
read_namespace
(
pid
,
ns
,
sizeof
(
ns
));
if
(
strcmp
(
ns
,
zygote_ns
[
i
])
==
0
)
{
usleep
(
50
);
ret
=
0
;
break
;
}
}
}
if
(
ret
)
break
;
}
}
if
(
ret
)
break
;
}
// Send pause signal ASAP
// Send pause signal ASAP
if
(
kill
(
pid
,
SIGSTOP
)
==
-
1
)
continue
;
if
(
kill
(
pid
,
SIGSTOP
)
==
-
1
)
continue
;
LOGI
(
"proc_monitor: %s (PID=%d ns=%s)
\n
"
,
processName
,
pid
,
ns
);
LOGI
(
"proc_monitor: %s (PID=%d ns=%s)
\n
"
,
processName
,
pid
,
ns
);
xmount
(
NULL
,
"/"
,
NULL
,
MS_REMOUNT
,
NULL
);
xmount
(
NULL
,
"/"
,
NULL
,
MS_REMOUNT
,
NULL
);
unlink
(
"/magisk"
);
unlink
(
"/magisk"
);
unlink
(
"/data/magisk"
);
unlink
(
"/data/magisk"
);
unlink
(
"/data/magisk.img"
);
unlink
(
"/data/magisk.img"
);
unlink
(
MAGISKRC
);
unlink
(
MAGISKRC
);
xmount
(
NULL
,
"/"
,
NULL
,
MS_REMOUNT
|
MS_RDONLY
,
NULL
);
xmount
(
NULL
,
"/"
,
NULL
,
MS_REMOUNT
|
MS_RDONLY
,
NULL
);
++
hide_queue
;
++
hide_queue
;
/*
/*
* The setns system call do not support multithread processes
* The setns system call do not support multithread processes
* We have to fork a new process, setns, then do the unmounts
* We have to fork a new process, setns, then do the unmounts
*/
*/
int
selfpid
=
getpid
();
int
selfpid
=
getpid
();
if
(
fork_dont_care
()
==
0
)
if
(
fork_dont_care
()
==
0
)
hide_daemon
(
pid
,
selfpid
);
hide_daemon
(
pid
,
selfpid
);
break
;
break
;
}
}
}
pthread_mutex_unlock
(
&
hide_lock
);
}
}
pthread_mutex_unlock
(
&
hide_lock
);
}
}
}
}
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