Magisk/native/src/base/misc.cpp

293 lines
6.6 KiB
C++
Raw Normal View History

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/prctl.h>
#include <sys/sysmacros.h>
2017-04-06 22:21:20 +00:00
#include <fcntl.h>
2017-04-14 19:23:09 +00:00
#include <pwd.h>
2017-11-15 13:00:52 +00:00
#include <unistd.h>
#include <syscall.h>
#include <random>
2020-04-26 06:19:36 +00:00
#include <string>
2017-04-05 22:12:29 +00:00
2022-05-12 09:03:42 +00:00
#include <base.hpp>
2017-04-05 22:12:29 +00:00
2020-04-26 06:19:36 +00:00
using namespace std;
2023-06-07 23:49:40 +00:00
bool byte_view::contains(byte_view pattern) const {
2023-06-07 23:52:42 +00:00
return _buf != nullptr && memmem(_buf, _sz, pattern._buf, pattern._sz) != nullptr;
2023-06-07 23:49:40 +00:00
}
bool byte_view::equals(byte_view o) const {
return _sz == o._sz && memcmp(_buf, o._buf, _sz) == 0;
}
heap_data byte_view::clone() const {
heap_data copy(_sz);
memcpy(copy._buf, _buf, _sz);
return copy;
}
void byte_data::swap(byte_data &o) {
std::swap(_buf, o._buf);
std::swap(_sz, o._sz);
}
2023-06-21 01:17:26 +00:00
rust::Vec<size_t> byte_data::patch(byte_view from, byte_view to) {
rust::Vec<size_t> v;
2023-06-07 23:49:40 +00:00
if (_buf == nullptr)
return v;
auto p = _buf;
auto eof = _buf + _sz;
while (p < eof) {
p = static_cast<uint8_t *>(memmem(p, eof - p, from.buf(), from.sz()));
if (p == nullptr)
return v;
memset(p, 0, from.sz());
memcpy(p, to.buf(), to.sz());
v.push_back(p - _buf);
p += from.sz();
}
return v;
}
2023-06-21 01:17:26 +00:00
rust::Vec<size_t> mut_u8_patch(
rust::Slice<uint8_t> buf,
rust::Slice<const uint8_t> from,
rust::Slice<const uint8_t> to) {
byte_data data(buf);
return data.patch(from, to);
}
int fork_dont_care() {
if (int pid = xfork()) {
waitpid(pid, nullptr, 0);
return pid;
} else if (xfork()) {
exit(0);
}
return 0;
}
2017-12-06 17:30:48 +00:00
int fork_no_orphan() {
int pid = xfork();
if (pid)
return pid;
2021-10-17 11:36:18 +00:00
prctl(PR_SET_PDEATHSIG, SIGKILL);
if (getppid() == 1)
exit(1);
return 0;
}
2019-01-26 18:00:19 +00:00
int exec_command(exec_t &exec) {
2022-08-19 09:21:52 +00:00
auto pipefd = array<int, 2>{-1, -1};
int outfd = -1;
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 = 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;
}
// Unblock all signals
sigset_t set;
sigfillset(&set);
pthread_sigmask(SIG_UNBLOCK, &set, nullptr);
if (outfd >= 0) {
xdup2(outfd, STDOUT_FILENO);
if (exec.err)
xdup2(outfd, STDERR_FILENO);
close(outfd);
}
// Call the pre-exec callback
if (exec.pre_exec)
exec.pre_exec();
execve(exec.argv[0], (char **) exec.argv, environ);
PLOGE("execve %s", exec.argv[0]);
exit(-1);
2018-11-03 08:03:11 +00:00
}
int exec_command_sync(exec_t &exec) {
int pid = exec_command(exec);
if (pid < 0)
return -1;
int status;
waitpid(pid, &status, 0);
return WEXITSTATUS(status);
2019-01-26 18:00:19 +00:00
}
int new_daemon_thread(thread_entry entry, void *arg) {
pthread_t thread;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2022-08-19 09:21:52 +00:00
errno = pthread_create(&thread, &attr, entry, arg);
if (errno) {
PLOGE("pthread_create");
}
return errno;
}
2019-02-15 09:31:39 +00:00
static char *argv0;
static size_t name_len;
void init_argv0(int argc, char **argv) {
argv0 = argv[0];
name_len = (argv[argc - 1] - argv[0]) + strlen(argv[argc - 1]) + 1;
2019-02-15 09:31:39 +00:00
}
void set_nice_name(const char *name) {
memset(argv0, 0, name_len);
2022-09-15 08:17:05 +00:00
strscpy(argv0, name, name_len);
prctl(PR_SET_NAME, name);
2019-02-15 09:31:39 +00:00
}
template<typename T, int base>
static T parse_num(string_view s) {
T val = 0;
for (char c : s) {
2023-03-07 22:52:09 +00:00
if (isdigit(c)) {
c -= '0';
} else if (base > 10 && isalpha(c)) {
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
} else {
return -1;
}
if (c >= base) {
return -1;
}
val *= base;
val += c;
2023-03-07 22:52:09 +00:00
}
return val;
2023-03-07 22:52:09 +00:00
}
2019-03-08 01:31:35 +00:00
/*
* Bionic's atoi runs through strtol().
* Use our own implementation for faster conversion.
*/
2021-10-23 21:38:30 +00:00
int parse_int(string_view s) {
return parse_num<int, 10>(s);
2023-03-07 22:52:09 +00:00
}
uint64_t parse_uint64_hex(string_view s) {
return parse_num<uint64_t, 16>(s);
2019-03-08 01:31:35 +00:00
}
uint32_t binary_gcd(uint32_t u, uint32_t v) {
if (u == 0) return v;
if (v == 0) return u;
auto shift = __builtin_ctz(u | v);
u >>= __builtin_ctz(u);
do {
v >>= __builtin_ctz(v);
if (u > v) {
auto t = v;
v = u;
u = t;
}
v -= u;
} while (v != 0);
return u << shift;
}
int switch_mnt_ns(int pid) {
char mnt[32];
ssprintf(mnt, sizeof(mnt), "/proc/%d/ns/mnt", pid);
if (access(mnt, R_OK) == -1) return 1; // Maybe process died..
int fd, ret;
fd = xopen(mnt, O_RDONLY);
if (fd < 0) return 1;
// Switch to its namespace
ret = xsetns(fd, 0);
close(fd);
return ret;
}
2020-04-26 06:19:36 +00:00
string &replace_all(string &str, string_view from, string_view to) {
size_t pos = 0;
while((pos = str.find(from, pos)) != string::npos) {
str.replace(pos, from.length(), to);
pos += to.length();
}
return str;
2020-04-26 06:19:36 +00:00
}
2023-04-04 01:50:36 +00:00
template <typename T>
static auto split_impl(string_view s, string_view delims) {
vector<T> result;
size_t base = 0;
size_t found;
while (true) {
found = s.find_first_of(delims, base);
2023-04-04 01:50:36 +00:00
result.emplace_back(s.substr(base, found - base));
if (found == string::npos)
break;
base = found + 1;
}
return result;
}
2023-04-04 01:50:36 +00:00
vector<string> split(string_view s, string_view delims) {
return split_impl<string>(s, delims);
}
2023-04-04 01:50:36 +00:00
vector<string_view> split_view(string_view s, string_view delims) {
return split_impl<string_view>(s, delims);
}
#undef vsnprintf
int vssprintf(char *dest, size_t size, const char *fmt, va_list ap) {
2022-11-01 09:04:50 +00:00
if (size > 0) {
*dest = 0;
return std::min(vsnprintf(dest, size, fmt, ap), (int) size - 1);
}
return -1;
}
int ssprintf(char *dest, size_t size, const char *fmt, ...) {
va_list va;
va_start(va, fmt);
int r = vssprintf(dest, size, fmt, va);
va_end(va);
return r;
}
2022-09-15 08:17:05 +00:00
#undef strlcpy
size_t strscpy(char *dest, const char *src, size_t size) {
return std::min(strlcpy(dest, src, size), size - 1);
}
2023-12-26 15:08:06 +00:00
extern "C" void cxx$utf8str$new(rust::Utf8CStr *self, const void *s, size_t len);
extern "C" const char *cxx$utf8str$ptr(const rust::Utf8CStr *self);
extern "C" size_t cxx$utf8str$len(const rust::Utf8CStr *self);
rust::Utf8CStr::Utf8CStr(const char *s, size_t len) {
cxx$utf8str$new(this, s, len);
}
const char *rust::Utf8CStr::data() const {
return cxx$utf8str$ptr(this);
}
size_t rust::Utf8CStr::length() const {
return cxx$utf8str$len(this);
}