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
20860da4
Commit
20860da4
authored
Aug 12, 2021
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleaner daemon handlers
parent
3ea10b7c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
41 deletions
+55
-41
daemon.cpp
native/jni/core/daemon.cpp
+40
-33
daemon.hpp
native/jni/include/daemon.hpp
+15
-8
No files found.
native/jni/core/daemon.cpp
View file @
20860da4
...
...
@@ -39,15 +39,16 @@ static bool verify_client(pid_t pid) {
static
bool
check_zygote
(
pid_t
pid
)
{
char
buf
[
32
];
sprintf
(
buf
,
"/proc/%d/attr/current"
,
pid
);
auto
fp
=
open_file
(
buf
,
"r"
);
if
(
!
fp
)
if
(
auto
fp
=
open_file
(
buf
,
"r"
))
{
fscanf
(
fp
.
get
(),
"%s"
,
buf
);
return
buf
==
"u:r:zygote:s0"
sv
;
}
else
{
return
false
;
fscanf
(
fp
.
get
(),
"%s"
,
buf
);
return
buf
==
"u:r:zygote:s0"
sv
;
}
}
static
void
request_handler
(
int
client
,
int
req_
code
,
ucred
cred
)
{
switch
(
req_
code
)
{
static
void
handle_request_async
(
int
client
,
int
code
,
ucred
cred
)
{
switch
(
code
)
{
case
MAGISKHIDE
:
magiskhide_handler
(
client
,
&
cred
);
break
;
...
...
@@ -78,26 +79,43 @@ static void request_handler(int client, int req_code, ucred cred) {
}
}
static
void
handle_request_sync
(
int
client
,
int
code
)
{
switch
(
code
)
{
case
CHECK_VERSION
:
write_string
(
client
,
MAGISK_VERSION
":MAGISK"
);
break
;
case
CHECK_VERSION_CODE
:
write_int
(
client
,
MAGISK_VER_CODE
);
break
;
case
GET_PATH
:
write_string
(
client
,
MAGISKTMP
.
data
());
break
;
case
START_DAEMON
:
setup_logfile
(
true
);
break
;
}
}
static
void
handle_request
(
int
client
)
{
int
req_
code
;
int
code
;
// Verify client credentials
ucred
cred
;
get_client_cred
(
client
,
&
cred
);
bool
is_root
=
cred
.
uid
==
0
;
bool
is_zygote
=
check_zygote
(
cred
.
pid
);
bool
is_client
=
verify_client
(
cred
.
pid
);
bool
is_zygote
=
!
is_client
&&
check_zygote
(
cred
.
pid
);
if
(
!
is_root
&&
!
is_zygote
&&
!
is_client
)
goto
shortcut
;
goto
done
;
req_
code
=
read_int
(
client
);
if
(
req_code
<
0
||
req_code
>=
DAEMON_CODE_END
)
goto
shortcut
;
code
=
read_int
(
client
);
if
(
code
<
0
||
(
code
&
DAEMON_CODE_MASK
)
>=
DAEMON_CODE_END
)
goto
done
;
// Check client permissions
switch
(
req_
code
)
{
switch
(
code
)
{
case
POST_FS_DATA
:
case
LATE_START
:
case
BOOT_COMPLETE
:
...
...
@@ -105,44 +123,33 @@ static void handle_request(int client) {
case
GET_PATH
:
if
(
!
is_root
)
{
write_int
(
client
,
ROOT_REQUIRED
);
goto
shortcut
;
goto
done
;
}
break
;
case
REMOVE_MODULES
:
if
(
cred
.
uid
!=
UID_SHELL
&&
cred
.
uid
!=
UID_ROOT
)
{
if
(
!
is_root
&&
cred
.
uid
!=
UID_SHELL
)
{
write_int
(
client
,
1
);
goto
shortcut
;
goto
done
;
}
break
;
case
MAGISKHIDE
:
// accept hide request from zygote
if
(
!
is_root
&&
!
is_zygote
)
{
write_int
(
client
,
ROOT_REQUIRED
);
goto
shortcut
;
goto
done
;
}
break
;
}
// Simple requests
switch
(
req_code
)
{
case
CHECK_VERSION
:
write_string
(
client
,
MAGISK_VERSION
":MAGISK"
);
goto
shortcut
;
case
CHECK_VERSION_CODE
:
write_int
(
client
,
MAGISK_VER_CODE
);
goto
shortcut
;
case
GET_PATH
:
write_string
(
client
,
MAGISKTMP
.
data
());
goto
shortcut
;
case
START_DAEMON
:
setup_logfile
(
true
);
goto
shortcut
;
if
(
code
&
SYNC_FLAG
)
{
handle_request_sync
(
client
,
code
);
goto
done
;
}
// Create new thread to handle complex requests
new_daemon_thread
([
=
]
{
return
request_handler
(
client
,
req_
code
,
cred
);
});
new_daemon_thread
([
=
]
{
handle_request_async
(
client
,
code
,
cred
);
});
return
;
shortcut
:
done
:
close
(
client
);
}
...
...
native/jni/include/daemon.hpp
View file @
20860da4
...
...
@@ -2,27 +2,34 @@
#include <pthread.h>
#include <string>
#include <limits>
#include <socket.hpp>
// Daemon command code flags/masks
enum
:
int
{
SYNC_FLAG
=
(
1
<<
30
),
DAEMON_CODE_MASK
=
std
::
numeric_limits
<
int
>::
max
()
>>
1
};
// Daemon command codes
enum
{
START_DAEMON
,
SUPERUSER
,
CHECK_VERSION
,
CHECK_VERSION_CODE
,
enum
:
int
{
START_DAEMON
=
SYNC_FLAG
|
0
,
CHECK_VERSION
=
SYNC_FLAG
|
1
,
CHECK_VERSION_CODE
=
SYNC_FLAG
|
2
,
GET_PATH
=
SYNC_FLAG
|
3
,
SUPERUSER
=
4
,
POST_FS_DATA
,
LATE_START
,
BOOT_COMPLETE
,
MAGISKHIDE
,
SQLITE_CMD
,
REMOVE_MODULES
,
GET_PATH
,
DAEMON_CODE_END
,
};
// Return codes for daemon
enum
{
enum
:
int
{
DAEMON_ERROR
=
-
1
,
DAEMON_SUCCESS
=
0
,
ROOT_REQUIRED
,
...
...
@@ -30,7 +37,7 @@ enum {
};
// Daemon state
enum
{
enum
:
int
{
STATE_NONE
,
STATE_POST_FS_DATA
,
STATE_POST_FS_DATA_DONE
,
...
...
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