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
46ee2c3f
Commit
46ee2c3f
authored
Jul 05, 2018
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve handshake between the 2 daemons
parent
5d5ec085
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
63 additions
and
73 deletions
+63
-73
daemon.c
native/jni/core/daemon.c
+7
-2
log_monitor.c
native/jni/core/log_monitor.c
+53
-65
daemon.h
native/jni/include/daemon.h
+3
-2
logging.h
native/jni/include/logging.h
+0
-4
No files found.
native/jni/core/daemon.c
View file @
46ee2c3f
...
...
@@ -83,7 +83,7 @@ static void *request_handler(void *args) {
case
LATE_START
:
late_start
(
client
);
break
;
case
MONITOR
:
case
HANDSHAKE
:
/* Do NOT close the client, make it hold */
break
;
default:
...
...
@@ -121,7 +121,12 @@ void main_daemon() {
close
(
fd
);
// Start the log monitor
monitor_logs
();
loggable
=
exec_command_sync
(
"/system/bin/logcat"
,
"-d"
,
"-f"
,
"/dev/null"
,
NULL
)
==
0
;
if
(
loggable
)
{
connect_daemon2
(
LOG_DAEMON
,
&
fd
);
write_int
(
fd
,
HANDSHAKE
);
close
(
fd
);
}
struct
sockaddr_un
sun
;
fd
=
setup_socket
(
&
sun
,
MAIN_DAEMON
);
...
...
native/jni/core/log_monitor.c
View file @
46ee2c3f
...
...
@@ -55,24 +55,6 @@ static void sigpipe_handler(int sig) {
events
[
HIDE_EVENT
].
fd
=
-
1
;
}
static
void
*
socket_thread
(
void
*
args
)
{
/* This would block, so separate thread */
while
(
1
)
{
int
fd
=
xaccept4
(
sockfd
,
NULL
,
NULL
,
SOCK_CLOEXEC
);
switch
(
read_int
(
fd
))
{
case
HIDE_CONNECT
:
pthread_mutex_lock
(
&
lock
);
close
(
events
[
HIDE_EVENT
].
fd
);
events
[
HIDE_EVENT
].
fd
=
fd
;
pthread_mutex_unlock
(
&
lock
);
break
;
default:
close
(
fd
);
break
;
}
}
}
static
void
*
monitor_thread
(
void
*
args
)
{
// Block SIGPIPE to prevent interruption
sigset_t
block_set
;
...
...
@@ -83,14 +65,45 @@ static void *monitor_thread(void *args) {
sleep
(
5
);
int
fd
;
char
b
;
do
{
while
(
1
)
{
fd
=
connect_daemon
();
write_int
(
fd
,
MONITOR
);
write_int
(
fd
,
HANDSHAKE
);
// This should hold unless the daemon is killed
read
(
fd
,
&
b
,
sizeof
(
b
));
// The main daemon crashed, spawn a new one
close
(
fd
);
}
while
(
1
);
}
}
static
void
*
logcat_thread
(
void
*
args
)
{
int
log_fd
=
-
1
,
log_pid
;
char
line
[
4096
];
while
(
1
)
{
// Start logcat
log_pid
=
exec_array
(
0
,
&
log_fd
,
NULL
,
(
char
**
)
vec_entry
(
&
log_cmd
));
FILE
*
logs
=
fdopen
(
log_fd
,
"r"
);
while
(
fgets
(
line
,
sizeof
(
line
),
logs
))
{
if
(
line
[
0
]
==
'-'
)
continue
;
size_t
len
=
strlen
(
line
);
pthread_mutex_lock
(
&
lock
);
for
(
int
i
=
0
;
i
<
EVENT_NUM
;
++
i
)
{
if
(
events
[
i
].
fd
>
0
&&
events
[
i
].
filter
(
line
))
write
(
events
[
i
].
fd
,
line
,
len
);
}
pthread_mutex_unlock
(
&
lock
);
}
fclose
(
logs
);
log_fd
=
-
1
;
kill
(
log_pid
,
SIGTERM
);
waitpid
(
log_pid
,
NULL
,
0
);
LOGI
(
"magisklogd: logcat output EOF"
);
// Clear buffer
log_pid
=
exec_array
(
0
,
NULL
,
NULL
,
(
char
**
)
vec_entry
(
&
clear_cmd
));
waitpid
(
log_pid
,
NULL
,
0
);
}
}
void
log_daemon
()
{
...
...
@@ -103,13 +116,6 @@ void log_daemon() {
LOGI
(
"Magisk v"
xstr
(
MAGISK_VERSION
)
"("
xstr
(
MAGISK_VER_CODE
)
") logger started
\n
"
);
strcpy
(
argv0
,
"magisklogd"
);
// 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
;
memset
(
&
act
,
0
,
sizeof
(
act
));
...
...
@@ -135,44 +141,26 @@ void log_daemon() {
vec_push_back
(
&
clear_cmd
,
"-c"
);
vec_push_back
(
&
clear_cmd
,
NULL
);
int
log_fd
=
-
1
,
log_pid
;
char
line
[
PIPE_BUF
];
// Start worker threads
pthread_t
thread
;
pthread_create
(
&
thread
,
NULL
,
monitor_thread
,
NULL
);
pthread_detach
(
thread
);
xpthread_create
(
&
thread
,
NULL
,
logcat_thread
,
NULL
);
pthread_detach
(
thread
);
while
(
1
)
{
// Start logcat
log_pid
=
exec_array
(
0
,
&
log_fd
,
NULL
,
(
char
**
)
vec_entry
(
&
log_cmd
));
FILE
*
logs
=
fdopen
(
log_fd
,
"r"
);
while
(
fgets
(
line
,
sizeof
(
line
),
logs
))
{
if
(
line
[
0
]
==
'-'
)
continue
;
size_t
len
=
strlen
(
line
);
while
(
1
)
{
int
fd
=
xaccept4
(
sockfd
,
NULL
,
NULL
,
SOCK_CLOEXEC
);
switch
(
read_int
(
fd
))
{
case
HIDE_CONNECT
:
pthread_mutex_lock
(
&
lock
);
for
(
int
i
=
0
;
i
<
EVENT_NUM
;
++
i
)
{
if
(
events
[
i
].
fd
>
0
&&
events
[
i
].
filter
(
line
))
write
(
events
[
i
].
fd
,
line
,
len
);
}
close
(
events
[
HIDE_EVENT
].
fd
);
events
[
HIDE_EVENT
].
fd
=
fd
;
pthread_mutex_unlock
(
&
lock
);
}
fclose
(
logs
);
log_fd
=
-
1
;
kill
(
log_pid
,
SIGTERM
);
waitpid
(
log_pid
,
NULL
,
0
);
LOGI
(
"magisklogd: logcat output EOF"
);
// Clear buffer
log_pid
=
exec_array
(
0
,
NULL
,
NULL
,
(
char
**
)
vec_entry
(
&
clear_cmd
));
waitpid
(
log_pid
,
NULL
,
0
);
}
}
/* Start new threads to monitor logcat and dump to logfile */
void
monitor_logs
()
{
loggable
=
exec_command_sync
(
"/system/bin/logcat"
,
"-d"
,
"-f"
,
"/dev/null"
,
NULL
)
==
0
;
if
(
loggable
)
{
int
fd
;
connect_daemon2
(
LOG_DAEMON
,
&
fd
);
write_int
(
fd
,
DO_NOTHING
);
break
;
case
HANDSHAKE
:
default:
close
(
fd
);
break
;
}
}
}
native/jni/include/daemon.h
View file @
46ee2c3f
...
...
@@ -25,8 +25,8 @@ enum {
ADD_HIDELIST
,
RM_HIDELIST
,
LS_HIDELIST
,
MONITOR
,
H
IDE_CONNECT
HIDE_CONNECT
,
H
ANDSHAKE
};
// Return codes for daemon
...
...
@@ -55,6 +55,7 @@ void auto_start_magiskhide();
// log_monitor.c
extern
int
loggable
;
void
log_daemon
();
// socket.c
...
...
native/jni/include/logging.h
View file @
46ee2c3f
...
...
@@ -46,10 +46,6 @@
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define PLOGE(fmt, args...) LOGE(fmt " failed with %d: %s", ##args, errno, strerror(errno))
extern
int
loggable
;
void
monitor_logs
();
#endif
/********************
...
...
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