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
7565ea27
Commit
7565ea27
authored
Jan 26, 2019
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove strdup2
parent
9275975b
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
80 additions
and
52 deletions
+80
-52
bootstages.cpp
native/jni/daemon/bootstages.cpp
+3
-3
log_daemon.cpp
native/jni/daemon/log_daemon.cpp
+15
-13
utils.h
native/jni/utils/include/utils.h
+10
-1
misc.cpp
native/jni/utils/misc.cpp
+52
-35
No files found.
native/jni/daemon/bootstages.cpp
View file @
7565ea27
...
...
@@ -441,17 +441,17 @@ static bool magisk_env() {
if
(
str_contains
(
line
,
" /system_root "
))
{
bind_mount
(
"/system_root/system"
,
MIRRDIR
"/system"
);
sscanf
(
line
.
c_str
(),
"%s"
,
buf
);
system_block
=
strdup
2
(
buf
);
system_block
=
strdup
(
buf
);
system_as_root
=
true
;
}
else
if
(
!
system_as_root
&&
str_contains
(
line
,
" /system "
))
{
sscanf
(
line
.
c_str
(),
"%s %*s %s"
,
buf
,
buf2
);
system_block
=
strdup
2
(
buf
);
system_block
=
strdup
(
buf
);
xmount
(
system_block
,
MIRRDIR
"/system"
,
buf2
,
MS_RDONLY
,
nullptr
);
VLOGI
(
"mount"
,
system_block
,
MIRRDIR
"/system"
);
}
else
if
(
str_contains
(
line
,
" /vendor "
))
{
seperate_vendor
=
true
;
sscanf
(
line
.
c_str
(),
"%s %*s %s"
,
buf
,
buf2
);
vendor_block
=
strdup
2
(
buf
);
vendor_block
=
strdup
(
buf
);
xmkdir
(
MIRRDIR
"/vendor"
,
0755
);
xmount
(
vendor_block
,
MIRRDIR
"/vendor"
,
buf2
,
MS_RDONLY
,
nullptr
);
VLOGI
(
"mount"
,
vendor_block
,
MIRRDIR
"/system"
);
...
...
native/jni/daemon/log_daemon.cpp
View file @
7565ea27
...
...
@@ -82,33 +82,35 @@ static void *monitor_thread(void *) {
}
static
void
*
logcat_thread
(
void
*
)
{
int
log_
fd
=
-
1
,
log_
pid
;
int
log_pid
;
char
line
[
4096
];
while
(
1
)
{
while
(
true
)
{
// Start logcat
log_pid
=
exec_command
(
false
,
&
log_fd
,
nullptr
,
log_cmd
.
data
());
FILE
*
logs
=
fdopen
(
log_fd
,
"r"
);
exec_t
exec
{
.
fd
=
-
1
,
.
argv
=
log_cmd
.
data
()
};
log_pid
=
exec_command
(
exec
);
FILE
*
logs
=
fdopen
(
exec
.
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
(
event
s
[
i
].
fd
>
0
&&
events
[
i
]
.
filter
(
line
))
write
(
event
s
[
i
]
.
fd
,
line
,
len
);
for
(
auto
&
event
:
events
)
{
if
(
event
.
fd
>
0
&&
event
.
filter
(
line
))
write
(
event
.
fd
,
line
,
len
);
}
pthread_mutex_unlock
(
&
lock
);
}
fclose
(
logs
);
log_fd
=
-
1
;
kill
(
log_pid
,
SIGTERM
);
waitpid
(
log_pid
,
nullptr
,
0
);
LOGI
(
"magisklogd: logcat output EOF"
);
// Clear buffer
log_pid
=
exec_command
(
false
,
nullptr
,
nullptr
,
clear_cmd
.
data
());
waitpid
(
log_pid
,
nullptr
,
0
);
exec_command_sync
(
clear_cmd
.
data
());
}
}
...
...
@@ -131,10 +133,10 @@ static void log_daemon() {
log_cmd
.
push_back
(
MIRRDIR
"/system/bin/logcat"
);
// Test whether these buffers actually works
const
char
*
b
[]
=
{
"main"
,
"events"
,
"crash"
};
for
(
int
i
=
0
;
i
<
3
;
++
i
)
{
if
(
exec_command_sync
(
MIRRDIR
"/system/bin/logcat"
,
"-b"
,
b
[
i
]
,
"-d"
,
"-f"
,
"/dev/null"
,
nullptr
)
==
0
)
{
for
(
auto
&
buffer
:
b
)
{
if
(
exec_command_sync
(
MIRRDIR
"/system/bin/logcat"
,
"-b"
,
b
uffer
,
"-d"
,
"-f"
,
"/dev/null"
,
nullptr
)
==
0
)
{
log_cmd
.
push_back
(
"-b"
);
log_cmd
.
push_back
(
b
[
i
]
);
log_cmd
.
push_back
(
b
uffer
);
}
}
chmod
(
"/dev/null"
,
0666
);
...
...
native/jni/utils/include/utils.h
View file @
7565ea27
...
...
@@ -142,10 +142,19 @@ void write_zero(int fd, size_t size);
#define str_starts(s, ss) ((ss) != nullptr && (s).compare(0, strlen(ss), ss) == 0)
std
::
vector
<
std
::
string
>
file_to_vector
(
const
char
*
filename
);
char
*
strdup2
(
const
char
*
s
,
size_t
*
size
=
nullptr
);
struct
exec_t
{
bool
err
=
false
;
int
fd
=
-
2
;
void
(
*
pre_exec
)()
=
nullptr
;
const
char
**
argv
=
nullptr
;
int
(
*
fork
)()
=
xfork
;
};
int
exec_command
(
exec_t
&
exec
);
int
exec_command
(
bool
err
,
int
*
fd
,
void
(
*
pre_exec
)(),
const
char
**
argv
);
int
exec_command
(
bool
err
,
int
*
fd
,
void
(
*
cb
)(),
const
char
*
argv0
,
...);
int
exec_command_sync
(
const
char
**
argv
);
int
exec_command_sync
(
const
char
*
argv0
,
...);
#endif
...
...
native/jni/utils/misc.cpp
View file @
7565ea27
...
...
@@ -155,30 +155,25 @@ int __fsetxattr(int fd, const char *name, const void *value, size_t size, int fl
return
(
int
)
syscall
(
__NR_fsetxattr
,
fd
,
name
,
value
,
size
,
flags
);
}
/*
fd == nullptr -> Ignore output
*fd < 0 -> Open pipe and set *fd to the read end
*fd >= 0 -> STDOUT (or STDERR) will be redirected to *fd
*pre_exec -> A callback function called after forking, before execvp
*/
int
exec_command
(
bool
err
,
int
*
fd
,
void
(
*
pre_exec
)(
void
),
const
char
**
argv
)
{
int
pipefd
[
2
],
outfd
=
-
1
;
int
exec_command
(
exec_t
&
exec
)
{
int
pipefd
[
2
]
=
{
-
1
,
-
1
},
outfd
=
-
1
;
if
(
fd
)
{
if
(
*
fd
<
0
)
{
if
(
xpipe2
(
pipefd
,
O_CLOEXEC
)
==
-
1
)
return
-
1
;
outfd
=
pipefd
[
1
];
}
else
{
outfd
=
*
fd
;
}
if
(
exec
.
fd
==
-
1
)
{
if
(
xpipe2
(
pipefd
,
O_CLOEXEC
)
==
-
1
)
return
-
1
;
outfd
=
pipefd
[
1
];
}
else
if
(
exec
.
fd
>=
0
)
{
outfd
=
exec
.
fd
;
}
int
pid
=
xfork
();
if
(
pid
!=
0
)
{
if
(
fd
&&
*
fd
<
0
)
{
// Give the read end and close write end
*
fd
=
pipefd
[
0
];
int
pid
=
exec
.
fork
();
if
(
pid
<
0
)
{
close
(
pipefd
[
0
]);
close
(
pipefd
[
1
]);
return
-
1
;
}
else
if
(
pid
)
{
if
(
exec
.
fd
==
-
1
)
{
exec
.
fd
=
pipefd
[
0
];
close
(
pipefd
[
1
]);
}
return
pid
;
...
...
@@ -186,21 +181,39 @@ int exec_command(bool err, int *fd, void (*pre_exec)(void), const char **argv) {
if
(
outfd
>=
0
)
{
xdup2
(
outfd
,
STDOUT_FILENO
);
if
(
err
)
if
(
e
xec
.
e
rr
)
xdup2
(
outfd
,
STDERR_FILENO
);
close
(
outfd
);
}
// Call the pre-exec callback
if
(
pre_exec
)
pre_exec
();
if
(
exec
.
pre_exec
)
exec
.
pre_exec
();
execve
(
argv
[
0
],
(
char
**
)
argv
,
environ
);
PLOGE
(
"execve %s"
,
argv
[
0
]);
execve
(
exec
.
argv
[
0
],
(
char
**
)
exec
.
argv
,
environ
);
PLOGE
(
"execve %s"
,
exec
.
argv
[
0
]);
exit
(
-
1
);
}
static
int
v_exec_command
(
bool
err
,
int
*
fd
,
void
(
*
cb
)(
void
),
const
char
*
argv0
,
va_list
argv
)
{
/*
fd == nullptr -> Ignore output
*fd < 0 -> Open pipe and set *fd to the read end
*fd >= 0 -> STDOUT (or STDERR) will be redirected to *fd
*pre_exec -> A callback function called after forking, before execvp
*/
int
exec_command
(
bool
err
,
int
*
fd
,
void
(
*
pre_exec
)(),
const
char
**
argv
)
{
exec_t
exec
{
.
err
=
err
,
.
fd
=
fd
?
*
fd
:
-
2
,
.
pre_exec
=
pre_exec
,
.
argv
=
argv
};
int
pid
=
exec_command
(
exec
);
if
(
fd
)
*
fd
=
exec
.
fd
;
return
pid
;
}
static
int
v_exec_command
(
bool
err
,
int
*
fd
,
void
(
*
cb
)(),
const
char
*
argv0
,
va_list
argv
)
{
// Collect va_list into vector
vector
<
const
char
*>
args
;
args
.
push_back
(
argv0
);
...
...
@@ -211,6 +224,18 @@ static int v_exec_command(bool err, int *fd, void (*cb)(void), const char *argv0
return
pid
;
}
int
exec_command_sync
(
const
char
**
argv
)
{
exec_t
exec
{
.
argv
=
argv
};
int
pid
,
status
;
pid
=
exec_command
(
exec
);
if
(
pid
<
0
)
return
-
1
;
waitpid
(
pid
,
&
status
,
0
);
return
WEXITSTATUS
(
status
);
}
int
exec_command_sync
(
const
char
*
argv0
,
...)
{
va_list
argv
;
va_start
(
argv
,
argv0
);
...
...
@@ -230,11 +255,3 @@ int exec_command(bool err, int *fd, void (*cb)(void), const char *argv0, ...) {
va_end
(
argv
);
return
pid
;
}
char
*
strdup2
(
const
char
*
s
,
size_t
*
size
)
{
size_t
len
=
strlen
(
s
)
+
1
;
char
*
buf
=
new
char
[
len
];
memcpy
(
buf
,
s
,
len
);
if
(
size
)
*
size
=
len
;
return
buf
;
}
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