Magisk/native/jni/utils/misc.h

99 lines
2.2 KiB
C
Raw Normal View History

2019-07-02 07:58:19 +02:00
#pragma once
#define UID_ROOT 0
2019-09-13 09:14:58 +02:00
#define UID_SHELL 2000
2019-07-02 07:58:19 +02:00
#ifdef __cplusplus
extern "C" {
#endif
int fork_dont_care();
int fork_no_zombie();
int strend(const char *s1, const char *s2);
char *rtrim(char *str);
void init_argv0(int argc, char **argv);
void set_nice_name(const char *name);
int parse_int(const char *s);
uint32_t binary_gcd(uint32_t u, uint32_t v);
2019-07-02 07:58:19 +02:00
#ifdef __cplusplus
}
#include <string>
#include <functional>
#include <string_view>
void gen_rand_str(char *buf, int len, bool varlen = true);
2019-07-02 07:58:19 +02:00
#define str_contains(s, ss) ((ss) != nullptr && (s).find(ss) != std::string::npos)
#define str_starts(s, ss) ((ss) != nullptr && (s).compare(0, strlen(ss), ss) == 0)
2019-09-26 05:55:39 +02:00
class mutex_guard {
2019-07-02 07:58:19 +02:00
public:
2019-09-26 05:55:39 +02:00
explicit mutex_guard(pthread_mutex_t &m): mutex(&m) {
2019-07-02 07:58:19 +02:00
pthread_mutex_lock(mutex);
}
2019-09-26 05:55:39 +02:00
explicit mutex_guard(pthread_mutex_t *m): mutex(m) {
2019-07-02 07:58:19 +02:00
pthread_mutex_lock(mutex);
}
2019-09-26 05:55:39 +02:00
~mutex_guard() {
2019-07-02 07:58:19 +02:00
pthread_mutex_unlock(mutex);
}
private:
pthread_mutex_t *mutex;
};
2019-09-26 05:55:39 +02:00
class run_finally {
2019-07-02 07:58:19 +02:00
public:
2019-09-26 05:55:39 +02:00
explicit run_finally(std::function<void()> &&fn): fn(std::move(fn)) {}
2019-07-02 07:58:19 +02:00
void disable() { fn = nullptr; }
2019-09-26 05:55:39 +02:00
~run_finally() { if (fn) fn(); }
2019-07-02 07:58:19 +02:00
private:
std::function<void ()> fn;
};
static inline int parse_int(std::string s) { return parse_int(s.data()); }
static inline int parse_int(std::string_view s) { return parse_int(s.data()); }
int new_daemon_thread(void *(*start_routine) (void *), void *arg = nullptr,
const pthread_attr_t *attr = nullptr);
int new_daemon_thread(std::function<void()> &&fn);
struct exec_t {
bool err = false;
int fd = -2;
void (*pre_exec)() = nullptr;
int (*fork)() = xfork;
const char **argv = nullptr;
};
int exec_command(exec_t &exec);
template <class ...Args>
int exec_command(exec_t &exec, Args &&...args) {
const char *argv[] = {args..., nullptr};
exec.argv = argv;
return exec_command(exec);
}
int exec_command_sync(exec_t &exec);
template <class ...Args>
int exec_command_sync(exec_t &exec, Args &&...args) {
const char *argv[] = {args..., nullptr};
exec.argv = argv;
return exec_command_sync(exec);
}
template <class ...Args>
int exec_command_sync(Args &&...args) {
exec_t exec{};
return exec_command_sync(exec, args...);
}
bool ends_with(const std::string_view &s1, const std::string_view &s2);
#endif