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
523e6629
Commit
523e6629
authored
Dec 26, 2018
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simpler su_info caching system
parent
23f8f350
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
39 deletions
+32
-39
su.h
native/jni/su/su.h
+4
-1
su_daemon.cpp
native/jni/su/su_daemon.cpp
+28
-38
No files found.
native/jni/su/su.h
View file @
523e6629
...
@@ -30,12 +30,15 @@ public:
...
@@ -30,12 +30,15 @@ public:
/* These should be guarded with global cache lock */
/* These should be guarded with global cache lock */
int
ref
;
int
ref
;
int
life
;
time_t
timestamp
;
su_info
(
unsigned
uid
);
su_info
(
unsigned
uid
);
~
su_info
();
~
su_info
();
void
lock
();
void
lock
();
void
unlock
();
void
unlock
();
bool
isFresh
();
void
newRef
();
void
deRef
();
private
:
private
:
pthread_mutex_t
_lock
;
/* Internal lock */
pthread_mutex_t
_lock
;
/* Internal lock */
...
...
native/jni/su/su_daemon.cpp
View file @
523e6629
...
@@ -5,6 +5,7 @@
...
@@ -5,6 +5,7 @@
#include <string.h>
#include <string.h>
#include <signal.h>
#include <signal.h>
#include <pwd.h>
#include <pwd.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/stat.h>
...
@@ -17,8 +18,6 @@
...
@@ -17,8 +18,6 @@
#include "pts.h"
#include "pts.h"
#include "selinux.h"
#include "selinux.h"
#define TIMEOUT 3
#define LOCK_CACHE() pthread_mutex_lock(&cache_lock)
#define LOCK_CACHE() pthread_mutex_lock(&cache_lock)
#define UNLOCK_CACHE() pthread_mutex_unlock(&cache_lock)
#define UNLOCK_CACHE() pthread_mutex_unlock(&cache_lock)
...
@@ -27,7 +26,7 @@ static su_info *cache;
...
@@ -27,7 +26,7 @@ static su_info *cache;
su_info
::
su_info
(
unsigned
uid
)
:
su_info
::
su_info
(
unsigned
uid
)
:
uid
(
uid
),
access
(
DEFAULT_SU_ACCESS
),
_lock
(
PTHREAD_MUTEX_INITIALIZER
),
uid
(
uid
),
access
(
DEFAULT_SU_ACCESS
),
_lock
(
PTHREAD_MUTEX_INITIALIZER
),
count
(
0
),
ref
(
0
),
life
(
0
),
mgr_st
({})
{}
count
(
0
),
ref
(
0
),
timestamp
(
0
),
mgr_st
({})
{}
su_info
::~
su_info
()
{
su_info
::~
su_info
()
{
pthread_mutex_destroy
(
&
_lock
);
pthread_mutex_destroy
(
&
_lock
);
...
@@ -41,21 +40,24 @@ void su_info::unlock() {
...
@@ -41,21 +40,24 @@ void su_info::unlock() {
pthread_mutex_unlock
(
&
_lock
);
pthread_mutex_unlock
(
&
_lock
);
}
}
static
void
*
info_collector
(
void
*
node
)
{
bool
su_info
::
isFresh
()
{
su_info
*
info
=
(
su_info
*
)
node
;
return
time
(
nullptr
)
-
timestamp
<
3
;
/* 3 seconds */
while
(
1
)
{
}
sleep
(
1
);
if
(
info
->
life
)
{
void
su_info
::
newRef
()
{
timestamp
=
time
(
nullptr
);
++
ref
;
}
void
su_info
::
deRef
()
{
LOCK_CACHE
();
LOCK_CACHE
();
if
(
--
info
->
life
==
0
&&
cache
&&
info
->
uid
==
cache
->
uid
)
--
ref
;
if
(
ref
==
0
&&
!
isFresh
())
{
if
(
cache
==
this
)
cache
=
nullptr
;
cache
=
nullptr
;
UNLOCK_CACHE
();
delete
this
;
}
if
(
!
info
->
life
&&
!
info
->
ref
)
{
delete
info
;
return
nullptr
;
}
}
}
UNLOCK_CACHE
();
}
}
static
void
database_check
(
su_info
*
info
)
{
static
void
database_check
(
su_info
*
info
)
{
...
@@ -88,30 +90,18 @@ static void database_check(su_info *info) {
...
@@ -88,30 +90,18 @@ static void database_check(su_info *info) {
}
}
static
struct
su_info
*
get_su_info
(
unsigned
uid
)
{
static
struct
su_info
*
get_su_info
(
unsigned
uid
)
{
su_info
*
info
;
su_info
*
info
=
nullptr
;
bool
cache_miss
=
false
;
// Get from cache or new instance
LOCK_CACHE
();
LOCK_CACHE
();
if
(
cache
&&
cache
->
uid
==
uid
&&
cache
->
isFresh
())
{
if
(
cache
&&
cache
->
uid
==
uid
)
{
info
=
cache
;
info
=
cache
;
}
else
{
}
else
{
cache_miss
=
true
;
if
(
cache
&&
cache
->
ref
==
0
)
info
=
new
su_info
(
uid
)
;
delete
cache
;
cache
=
info
;
cache
=
info
=
new
su_info
(
uid
)
;
}
}
info
->
newRef
();
// Update the cache status
info
->
life
=
TIMEOUT
;
++
info
->
ref
;
// Start a thread to maintain the cache
if
(
cache_miss
)
{
pthread_t
thread
;
xpthread_create
(
&
thread
,
nullptr
,
info_collector
,
info
);
pthread_detach
(
thread
);
}
UNLOCK_CACHE
();
UNLOCK_CACHE
();
LOGD
(
"su: request from uid=[%d] (#%d)
\n
"
,
info
->
uid
,
++
info
->
count
);
LOGD
(
"su: request from uid=[%d] (#%d)
\n
"
,
info
->
uid
,
++
info
->
count
);
...
@@ -209,6 +199,7 @@ void su_daemon_handler(int client, struct ucred *credential) {
...
@@ -209,6 +199,7 @@ void su_daemon_handler(int client, struct ucred *credential) {
// Fail fast
// Fail fast
if
(
info
->
access
.
policy
==
DENY
&&
info
->
str
[
SU_MANAGER
][
0
]
==
'\0'
)
{
if
(
info
->
access
.
policy
==
DENY
&&
info
->
str
[
SU_MANAGER
][
0
]
==
'\0'
)
{
LOGD
(
"su: fast deny
\n
"
);
LOGD
(
"su: fast deny
\n
"
);
info
->
deRef
();
write_int
(
client
,
DENY
);
write_int
(
client
,
DENY
);
close
(
client
);
close
(
client
);
return
;
return
;
...
@@ -221,8 +212,7 @@ void su_daemon_handler(int client, struct ucred *credential) {
...
@@ -221,8 +212,7 @@ void su_daemon_handler(int client, struct ucred *credential) {
*/
*/
int
child
=
xfork
();
int
child
=
xfork
();
if
(
child
)
{
if
(
child
)
{
// Decrement reference count
info
->
deRef
();
--
info
->
ref
;
// Wait result
// Wait result
LOGD
(
"su: waiting child pid=[%d]
\n
"
,
child
);
LOGD
(
"su: waiting child pid=[%d]
\n
"
,
child
);
...
...
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