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
48f829b7
Commit
48f829b7
authored
Sep 21, 2022
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Minor refactoring
parent
0b82fe19
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
35 additions
and
37 deletions
+35
-37
files.cpp
native/src/base/files.cpp
+1
-1
files.hpp
native/src/base/files.hpp
+3
-0
files.rs
native/src/base/files.rs
+8
-8
xwrap.hpp
native/src/base/xwrap.hpp
+1
-1
xwrap.rs
native/src/base/xwrap.rs
+18
-23
mount.cpp
native/src/init/mount.cpp
+2
-2
su_daemon.cpp
native/src/su/su_daemon.cpp
+2
-2
No files found.
native/src/base/files.cpp
View file @
48f829b7
...
@@ -384,7 +384,7 @@ void parse_mnt(const char *file, const function<bool(mntent*)> &fn) {
...
@@ -384,7 +384,7 @@ void parse_mnt(const char *file, const function<bool(mntent*)> &fn) {
void
backup_folder
(
const
char
*
dir
,
vector
<
raw_file
>
&
files
)
{
void
backup_folder
(
const
char
*
dir
,
vector
<
raw_file
>
&
files
)
{
char
path
[
PATH_MAX
];
char
path
[
PATH_MAX
];
x
canonical_
path
(
dir
,
path
,
sizeof
(
path
));
x
real
path
(
dir
,
path
,
sizeof
(
path
));
int
len
=
strlen
(
path
);
int
len
=
strlen
(
path
);
pre_order_walk
(
xopen
(
dir
,
O_RDONLY
),
[
&
](
int
dfd
,
dirent
*
entry
)
->
walk_result
{
pre_order_walk
(
xopen
(
dir
,
O_RDONLY
),
[
&
](
int
dfd
,
dirent
*
entry
)
->
walk_result
{
int
fd
=
xopenat
(
dfd
,
entry
->
d_name
,
O_RDONLY
);
int
fd
=
xopenat
(
dfd
,
entry
->
d_name
,
O_RDONLY
);
...
...
native/src/base/files.hpp
View file @
48f829b7
...
@@ -74,6 +74,9 @@ void mv_dir(int src, int dest);
...
@@ -74,6 +74,9 @@ void mv_dir(int src, int dest);
void
cp_afc
(
const
char
*
src
,
const
char
*
dest
);
void
cp_afc
(
const
char
*
src
,
const
char
*
dest
);
void
link_path
(
const
char
*
src
,
const
char
*
dest
);
void
link_path
(
const
char
*
src
,
const
char
*
dest
);
void
link_dir
(
int
src
,
int
dest
);
void
link_dir
(
int
src
,
int
dest
);
static
inline
ssize_t
realpath
(
const
char
*
path
,
char
*
buf
,
size_t
bufsiz
)
{
return
canonical_path
(
path
,
buf
,
bufsiz
);
}
int
getattr
(
const
char
*
path
,
file_attr
*
a
);
int
getattr
(
const
char
*
path
,
file_attr
*
a
);
int
getattrat
(
int
dirfd
,
const
char
*
name
,
file_attr
*
a
);
int
getattrat
(
int
dirfd
,
const
char
*
name
,
file_attr
*
a
);
int
fgetattr
(
int
fd
,
file_attr
*
a
);
int
fgetattr
(
int
fd
,
file_attr
*
a
);
...
...
native/src/base/files.rs
View file @
48f829b7
...
@@ -5,7 +5,7 @@ use libc::{c_char, mode_t, EEXIST, ENOENT, O_CLOEXEC, O_PATH};
...
@@ -5,7 +5,7 @@ use libc::{c_char, mode_t, EEXIST, ENOENT, O_CLOEXEC, O_PATH};
use
crate
::{
bfmt_cstr
,
errno
,
xopen
};
use
crate
::{
bfmt_cstr
,
errno
,
xopen
};
mod
unsafe_impl
{
pub
mod
unsafe_impl
{
use
std
::
ffi
::
CStr
;
use
std
::
ffi
::
CStr
;
use
libc
::
c_char
;
use
libc
::
c_char
;
...
@@ -23,11 +23,11 @@ mod unsafe_impl {
...
@@ -23,11 +23,11 @@ mod unsafe_impl {
#[no_mangle]
#[no_mangle]
unsafe
extern
"C"
fn
canonical_path
(
path
:
*
const
c_char
,
buf
:
*
mut
u8
,
bufsz
:
usize
)
->
isize
{
unsafe
extern
"C"
fn
canonical_path
(
path
:
*
const
c_char
,
buf
:
*
mut
u8
,
bufsz
:
usize
)
->
isize
{
super
::
canonical_
path
(
CStr
::
from_ptr
(
path
),
slice_from_ptr_mut
(
buf
,
bufsz
))
super
::
real
path
(
CStr
::
from_ptr
(
path
),
slice_from_ptr_mut
(
buf
,
bufsz
))
}
}
}
}
pub
fn
__open_fd_impl
(
path
:
&
CStr
,
flags
:
i32
,
mode
:
mode_t
)
->
Option
<
OwnedFd
>
{
pub
fn
__
x
open_fd_impl
(
path
:
&
CStr
,
flags
:
i32
,
mode
:
mode_t
)
->
Option
<
OwnedFd
>
{
let
fd
=
xopen
(
path
.as_ptr
(),
flags
,
mode
);
let
fd
=
xopen
(
path
.as_ptr
(),
flags
,
mode
);
if
fd
>=
0
{
if
fd
>=
0
{
unsafe
{
Some
(
OwnedFd
::
from_raw_fd
(
fd
))
}
unsafe
{
Some
(
OwnedFd
::
from_raw_fd
(
fd
))
}
...
@@ -37,12 +37,12 @@ pub fn __open_fd_impl(path: &CStr, flags: i32, mode: mode_t) -> Option<OwnedFd>
...
@@ -37,12 +37,12 @@ pub fn __open_fd_impl(path: &CStr, flags: i32, mode: mode_t) -> Option<OwnedFd>
}
}
#[macro_export]
#[macro_export]
macro_rules!
open_fd
{
macro_rules!
x
open_fd
{
(
$path:expr
,
$flags:expr
)
=>
{
(
$path:expr
,
$flags:expr
)
=>
{
crate
::
__open_fd_impl
(
$path
,
$flags
,
0
)
crate
::
__
x
open_fd_impl
(
$path
,
$flags
,
0
)
};
};
(
$path:expr
,
$flags:expr
,
$mode:expr
)
=>
{
(
$path:expr
,
$flags:expr
,
$mode:expr
)
=>
{
crate
::
__open_fd_impl
(
$path
,
$flags
,
$mode
)
crate
::
__
x
open_fd_impl
(
$path
,
$flags
,
$mode
)
};
};
}
}
...
@@ -57,8 +57,8 @@ pub fn fd_path(fd: RawFd, buf: &mut [u8]) -> isize {
...
@@ -57,8 +57,8 @@ pub fn fd_path(fd: RawFd, buf: &mut [u8]) -> isize {
}
}
// Inspired by https://android.googlesource.com/platform/bionic/+/master/libc/bionic/realpath.cpp
// Inspired by https://android.googlesource.com/platform/bionic/+/master/libc/bionic/realpath.cpp
pub
fn
canonical_
path
(
path
:
&
CStr
,
buf
:
&
mut
[
u8
])
->
isize
{
pub
fn
real
path
(
path
:
&
CStr
,
buf
:
&
mut
[
u8
])
->
isize
{
if
let
Some
(
fd
)
=
open_fd!
(
path
,
O_PATH
|
O_CLOEXEC
)
{
if
let
Some
(
fd
)
=
x
open_fd!
(
path
,
O_PATH
|
O_CLOEXEC
)
{
let
mut
st1
:
libc
::
stat
;
let
mut
st1
:
libc
::
stat
;
let
mut
st2
:
libc
::
stat
;
let
mut
st2
:
libc
::
stat
;
unsafe
{
unsafe
{
...
...
native/src/base/xwrap.hpp
View file @
48f829b7
...
@@ -58,7 +58,7 @@ void *xmmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset
...
@@ -58,7 +58,7 @@ void *xmmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset
ssize_t
xsendfile
(
int
out_fd
,
int
in_fd
,
off_t
*
offset
,
size_t
count
);
ssize_t
xsendfile
(
int
out_fd
,
int
in_fd
,
off_t
*
offset
,
size_t
count
);
pid_t
xfork
();
pid_t
xfork
();
int
xpoll
(
pollfd
*
fds
,
nfds_t
nfds
,
int
timeout
);
int
xpoll
(
pollfd
*
fds
,
nfds_t
nfds
,
int
timeout
);
ssize_t
x
canonical_
path
(
const
char
*
path
,
char
*
buf
,
size_t
bufsiz
);
ssize_t
x
real
path
(
const
char
*
path
,
char
*
buf
,
size_t
bufsiz
);
int
xmknod
(
const
char
*
pathname
,
mode_t
mode
,
dev_t
dev
);
int
xmknod
(
const
char
*
pathname
,
mode_t
mode
,
dev_t
dev
);
}
// extern "C"
}
// extern "C"
native/src/base/xwrap.rs
View file @
48f829b7
...
@@ -7,7 +7,7 @@ use libc::{
...
@@ -7,7 +7,7 @@ use libc::{
ssize_t
,
SYS_dup3
,
ssize_t
,
SYS_dup3
,
};
};
use
crate
::{
c
anonical_path
,
cstr
,
errno
,
error
,
mkdirs
,
perror
,
ptr_to_str
,
readlink
};
use
crate
::{
c
str
,
errno
,
error
,
mkdirs
,
perror
,
ptr_to_str
,
realpath
};
mod
unsafe_impl
{
mod
unsafe_impl
{
use
std
::
ffi
::
CStr
;
use
std
::
ffi
::
CStr
;
...
@@ -16,6 +16,7 @@ mod unsafe_impl {
...
@@ -16,6 +16,7 @@ mod unsafe_impl {
use
cfg_if
::
cfg_if
;
use
cfg_if
::
cfg_if
;
use
libc
::{
c_char
,
nfds_t
,
off_t
,
pollfd
};
use
libc
::{
c_char
,
nfds_t
,
off_t
,
pollfd
};
use
crate
::
unsafe_impl
::
read_link
;
use
crate
::{
perror
,
ptr_to_str
,
slice_from_ptr
,
slice_from_ptr_mut
};
use
crate
::{
perror
,
ptr_to_str
,
slice_from_ptr
,
slice_from_ptr_mut
};
#[no_mangle]
#[no_mangle]
...
@@ -34,13 +35,17 @@ mod unsafe_impl {
...
@@ -34,13 +35,17 @@ mod unsafe_impl {
}
}
#[no_mangle]
#[no_mangle]
unsafe
extern
"C"
fn
x
canonical_
path
(
path
:
*
const
c_char
,
buf
:
*
mut
u8
,
bufsz
:
usize
)
->
isize
{
unsafe
extern
"C"
fn
x
real
path
(
path
:
*
const
c_char
,
buf
:
*
mut
u8
,
bufsz
:
usize
)
->
isize
{
super
::
x
canonical_
path
(
CStr
::
from_ptr
(
path
),
slice_from_ptr_mut
(
buf
,
bufsz
))
super
::
x
real
path
(
CStr
::
from_ptr
(
path
),
slice_from_ptr_mut
(
buf
,
bufsz
))
}
}
#[no_mangle]
#[no_mangle]
unsafe
extern
"C"
fn
xreadlink
(
path
:
*
const
c_char
,
buf
:
*
mut
u8
,
bufsz
:
usize
)
->
isize
{
pub
unsafe
extern
"C"
fn
xreadlink
(
path
:
*
const
c_char
,
buf
:
*
mut
u8
,
bufsz
:
usize
)
->
isize
{
super
::
xreadlink
(
CStr
::
from_ptr
(
path
),
slice_from_ptr_mut
(
buf
,
bufsz
))
let
r
=
read_link
(
path
,
buf
,
bufsz
);
if
r
<
0
{
perror!
(
"readlink"
);
}
return
r
;
}
}
#[no_mangle]
#[no_mangle]
...
@@ -139,16 +144,6 @@ pub extern "C" fn xopenat(dirfd: RawFd, path: *const c_char, flags: i32, mode: m
...
@@ -139,16 +144,6 @@ pub extern "C" fn xopenat(dirfd: RawFd, path: *const c_char, flags: i32, mode: m
}
}
}
}
#[macro_export]
macro_rules!
xopen
{
(
$path:expr
,
$flags:expr
)
=>
{
xopen
(
$path
,
$flags
,
0
)
};
(
$path:expr
,
$flags:expr
,
$mode:expr
)
=>
{
xopen
(
$path
,
$flags
,
$mode
)
};
}
// Fully write data slice
// Fully write data slice
pub
fn
xwrite
(
fd
:
RawFd
,
data
:
&
[
u8
])
->
isize
{
pub
fn
xwrite
(
fd
:
RawFd
,
data
:
&
[
u8
])
->
isize
{
unsafe
{
unsafe
{
...
@@ -496,14 +491,12 @@ pub extern "C" fn xdup3(oldfd: RawFd, newfd: RawFd, flags: i32) -> RawFd {
...
@@ -496,14 +491,12 @@ pub extern "C" fn xdup3(oldfd: RawFd, newfd: RawFd, flags: i32) -> RawFd {
}
}
}
}
#[inline]
pub
fn
xreadlink
(
path
:
&
CStr
,
data
:
&
mut
[
u8
])
->
isize
{
pub
fn
xreadlink
(
path
:
&
CStr
,
data
:
&
mut
[
u8
])
->
isize
{
let
r
=
readlink
(
path
,
data
);
unsafe
{
unsafe_impl
::
xreadlink
(
path
.as_ptr
(),
data
.as_mut_ptr
(),
data
.len
())
}
if
r
<
0
{
perror!
(
"readlink {}"
,
path
.to_str
()
.unwrap_or
(
""
))
}
return
r
;
}
}
#[inline]
pub
fn
xreadlinkat
(
dirfd
:
RawFd
,
path
:
&
CStr
,
data
:
&
mut
[
u8
])
->
isize
{
pub
fn
xreadlinkat
(
dirfd
:
RawFd
,
path
:
&
CStr
,
data
:
&
mut
[
u8
])
->
isize
{
unsafe
{
unsafe_impl
::
xreadlinkat
(
dirfd
,
path
.as_ptr
(),
data
.as_mut_ptr
(),
data
.len
())
}
unsafe
{
unsafe_impl
::
xreadlinkat
(
dirfd
,
path
.as_ptr
(),
data
.as_mut_ptr
(),
data
.len
())
}
}
}
...
@@ -632,6 +625,7 @@ pub extern "C" fn xmkdirat(dirfd: RawFd, path: *const c_char, mode: mode_t) -> i
...
@@ -632,6 +625,7 @@ pub extern "C" fn xmkdirat(dirfd: RawFd, path: *const c_char, mode: mode_t) -> i
}
}
}
}
#[inline]
pub
fn
xsendfile
(
out_fd
:
RawFd
,
in_fd
:
RawFd
,
offset
:
Option
<&
mut
off_t
>
,
count
:
usize
)
->
isize
{
pub
fn
xsendfile
(
out_fd
:
RawFd
,
in_fd
:
RawFd
,
offset
:
Option
<&
mut
off_t
>
,
count
:
usize
)
->
isize
{
unsafe
{
unsafe
{
let
p
=
offset
.map_or
(
ptr
::
null_mut
(),
|
it
|
it
);
let
p
=
offset
.map_or
(
ptr
::
null_mut
(),
|
it
|
it
);
...
@@ -669,14 +663,15 @@ pub extern "C" fn xfork() -> i32 {
...
@@ -669,14 +663,15 @@ pub extern "C" fn xfork() -> i32 {
}
}
}
}
#[inline]
pub
fn
xpoll
(
fds
:
&
mut
[
pollfd
],
timeout
:
i32
)
->
i32
{
pub
fn
xpoll
(
fds
:
&
mut
[
pollfd
],
timeout
:
i32
)
->
i32
{
unsafe
{
unsafe_impl
::
xpoll
(
fds
.as_mut_ptr
(),
fds
.len
()
as
nfds_t
,
timeout
)
}
unsafe
{
unsafe_impl
::
xpoll
(
fds
.as_mut_ptr
(),
fds
.len
()
as
nfds_t
,
timeout
)
}
}
}
pub
fn
x
canonical_
path
(
path
:
&
CStr
,
buf
:
&
mut
[
u8
])
->
isize
{
pub
fn
x
real
path
(
path
:
&
CStr
,
buf
:
&
mut
[
u8
])
->
isize
{
let
r
=
canonical_
path
(
path
,
buf
);
let
r
=
real
path
(
path
,
buf
);
if
r
<
0
{
if
r
<
0
{
perror!
(
"
canonical_
path {}"
,
path
.to_str
()
.unwrap_or
(
""
))
perror!
(
"
real
path {}"
,
path
.to_str
()
.unwrap_or
(
""
))
}
}
return
r
;
return
r
;
}
}
...
...
native/src/init/mount.cpp
View file @
48f829b7
...
@@ -118,8 +118,8 @@ static void switch_root(const string &path) {
...
@@ -118,8 +118,8 @@ static void switch_root(const string &path) {
void
MagiskInit
::
mount_rules_dir
()
{
void
MagiskInit
::
mount_rules_dir
()
{
char
path
[
128
];
char
path
[
128
];
x
canonical_
path
(
BLOCKDIR
,
blk_info
.
block_dev
,
sizeof
(
blk_info
.
block_dev
));
x
real
path
(
BLOCKDIR
,
blk_info
.
block_dev
,
sizeof
(
blk_info
.
block_dev
));
x
canonical_
path
(
MIRRDIR
,
path
,
sizeof
(
path
));
x
real
path
(
MIRRDIR
,
path
,
sizeof
(
path
));
char
*
b
=
blk_info
.
block_dev
+
strlen
(
blk_info
.
block_dev
);
char
*
b
=
blk_info
.
block_dev
+
strlen
(
blk_info
.
block_dev
);
char
*
p
=
path
+
strlen
(
path
);
char
*
p
=
path
+
strlen
(
path
);
...
...
native/src/su/su_daemon.cpp
View file @
48f829b7
...
@@ -411,8 +411,8 @@ void su_daemon_handler(int client, const sock_cred *cred) {
...
@@ -411,8 +411,8 @@ void su_daemon_handler(int client, const sock_cred *cred) {
umask
(
022
);
umask
(
022
);
char
path
[
32
];
char
path
[
32
];
ssprintf
(
path
,
sizeof
(
path
),
"/proc/%d/cwd"
,
ctx
.
pid
);
ssprintf
(
path
,
sizeof
(
path
),
"/proc/%d/cwd"
,
ctx
.
pid
);
char
cwd
[
PATH_MAX
];
char
cwd
[
4096
];
if
(
canonical_path
(
path
,
cwd
,
sizeof
(
cwd
))
)
if
(
realpath
(
path
,
cwd
,
sizeof
(
cwd
))
>
0
)
chdir
(
cwd
);
chdir
(
cwd
);
ssprintf
(
path
,
sizeof
(
path
),
"/proc/%d/environ"
,
ctx
.
pid
);
ssprintf
(
path
,
sizeof
(
path
),
"/proc/%d/environ"
,
ctx
.
pid
);
auto
env
=
full_read
(
path
);
auto
env
=
full_read
(
path
);
...
...
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