Magisk/native/jni/utils/include/utils.h

175 lines
5.8 KiB
C
Raw Normal View History

2017-04-07 00:21:20 +02:00
/* util.h - Header for all utility functions
*/
2017-04-04 21:44:13 +02:00
#ifndef _UTILS_H_
#define _UTILS_H_
#include <stdio.h>
2017-04-07 00:21:20 +02:00
#include <dirent.h>
2017-11-23 16:55:33 +01:00
#include <pthread.h>
2018-10-04 21:06:13 +02:00
#include <poll.h>
2017-04-08 01:37:43 +02:00
#include <sys/socket.h>
2017-04-14 21:23:09 +02:00
#include <sys/stat.h>
2017-04-04 21:44:13 +02:00
2018-10-25 03:08:06 +02:00
#ifdef __cplusplus
extern "C" {
#endif
2017-04-14 21:23:09 +02:00
#define UID_SHELL (get_shell_uid())
#define UID_ROOT 0
#define UID_SYSTEM (get_system_uid())
#define UID_RADIO (get_radio_uid())
// xwrap.cpp
2017-04-04 21:44:13 +02:00
FILE *xfopen(const char *pathname, const char *mode);
2017-05-07 21:11:14 +02:00
FILE *xfdopen(int fd, const char *mode);
2017-04-28 15:48:38 +02:00
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define xopen(...) GET_MACRO(__VA_ARGS__, xopen3, xopen2)(__VA_ARGS__)
int xopen2(const char *pathname, int flags);
int xopen3(const char *pathname, int flags, mode_t mode);
2017-10-13 18:08:12 +02:00
int xopenat(int dirfd, const char *pathname, int flags);
2017-04-06 00:12:29 +02:00
ssize_t xwrite(int fd, const void *buf, size_t count);
ssize_t xread(int fd, void *buf, size_t count);
ssize_t xxread(int fd, void *buf, size_t count);
2017-07-10 16:29:53 +02:00
int xpipe2(int pipefd[2], int flags);
2017-04-06 00:12:29 +02:00
int xsetns(int fd, int nstype);
2018-07-13 16:14:32 +02:00
int xunshare(int flags);
2017-04-07 00:21:20 +02:00
DIR *xopendir(const char *name);
2017-10-13 18:08:12 +02:00
DIR *xfdopendir(int fd);
2017-04-07 00:21:20 +02:00
struct dirent *xreaddir(DIR *dirp);
2017-04-08 01:37:43 +02:00
pid_t xsetsid();
int xsocket(int domain, int type, int protocol);
int xbind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
int xconnect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
int xlisten(int sockfd, int backlog);
int xaccept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
2017-04-08 01:37:43 +02:00
void *xmalloc(size_t size);
void *xcalloc(size_t nmemb, size_t size);
void *xrealloc(void *ptr, size_t size);
ssize_t xsendmsg(int sockfd, const struct msghdr *msg, int flags);
ssize_t xrecvmsg(int sockfd, struct msghdr *msg, int flags);
int xpthread_create(pthread_t *thread, const pthread_attr_t *attr,
2017-04-17 10:36:49 +02:00
void *(*start_routine) (void *), void *arg);
int xsocketpair(int domain, int type, int protocol, int sv[2]);
2017-04-14 21:23:09 +02:00
int xstat(const char *pathname, struct stat *buf);
2017-05-03 19:13:04 +02:00
int xlstat(const char *pathname, struct stat *buf);
2017-04-14 21:23:09 +02:00
int xdup2(int oldfd, int newfd);
int xdup3(int oldfd, int newfd, int flags);
2017-04-15 12:10:54 +02:00
ssize_t xreadlink(const char *pathname, char *buf, size_t bufsiz);
2017-10-13 18:08:12 +02:00
ssize_t xreadlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
2017-04-15 12:10:54 +02:00
int xsymlink(const char *target, const char *linkpath);
int xsymlinkat(const char *target, int newdirfd, const char *linkpath);
int xlinkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags);
2017-04-17 10:36:49 +02:00
int xmount(const char *source, const char *target,
const char *filesystemtype, unsigned long mountflags,
const void *data);
2017-05-03 19:13:04 +02:00
int xumount(const char *target);
int xumount2(const char *target, int flags);
2017-04-17 10:36:49 +02:00
int xrename(const char *oldpath, const char *newpath);
int xmkdir(const char *pathname, mode_t mode);
int xmkdirs(const char *pathname, mode_t mode);
2017-10-13 18:08:12 +02:00
int xmkdirat(int dirfd, const char *pathname, mode_t mode);
2017-04-28 15:48:38 +02:00
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);
2017-10-13 18:08:12 +02:00
pid_t xfork();
2018-10-04 21:06:13 +02:00
int xpoll(struct pollfd *fds, nfds_t nfds, int timeout);
2017-04-04 21:44:13 +02:00
// misc.cpp
2017-04-06 00:12:29 +02:00
2017-04-14 21:23:09 +02:00
unsigned get_shell_uid();
unsigned get_system_uid();
unsigned get_radio_uid();
int fork_dont_care();
void gen_rand_str(char *buf, int len);
int strend(const char *s1, const char *s2);
2017-06-24 16:37:45 +02:00
#define getline __getline
#define getdelim __getdelim
#define fsetxattr __fsetxattr
ssize_t __getline(char **lineptr, size_t *n, FILE *stream);
ssize_t __getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
int __fsetxattr(int fd, const char *name, const void *value, size_t size, int flags);
// file.cpp
2017-10-11 20:57:18 +02:00
2019-01-26 19:39:24 +01:00
#define do_align(p, a) (((p) + (a) - 1) / (a) * (a))
2019-01-26 12:00:23 +01:00
#define align_off(p, a) (do_align(p, a) - (p))
2018-01-29 15:16:02 +01:00
2018-10-26 23:02:56 +02:00
extern const char **excl_list;
2017-10-11 20:57:18 +02:00
struct file_attr {
struct stat st;
char con[128];
};
2018-09-27 06:09:59 +02:00
int fd_getpath(int fd, char *path, size_t size);
int fd_getpathat(int dirfd, const char *name, char *path, size_t size);
int mkdirs(const char *pathname, mode_t mode);
void in_order_walk(int dirfd, void (*callback)(int, struct dirent*));
2017-10-11 20:57:18 +02:00
void rm_rf(const char *path);
void frm_rf(int dirfd);
void mv_f(const char *source, const char *destination);
void mv_dir(int src, int dest);
void cp_afc(const char *source, const char *destination);
2018-02-01 20:22:38 +01:00
void link_dir(int src, int dest);
2017-10-11 20:57:18 +02:00
void clone_dir(int src, int dest);
int getattr(const char *path, struct file_attr *a);
int getattrat(int dirfd, const char *pathname, struct file_attr *a);
int fgetattr(int fd, struct file_attr *a);
int setattr(const char *path, struct file_attr *a);
int setattrat(int dirfd, const char *pathname, struct file_attr *a);
int fsetattr(int fd, struct file_attr *a);
2019-01-20 05:59:37 +01:00
void fclone_attr(int sourcefd, int targetfd);
2017-10-11 20:57:18 +02:00
void clone_attr(const char *source, const char *target);
2018-08-09 12:13:07 +02:00
void mmap_ro(const char *filename, void **buf, size_t *size);
void mmap_rw(const char *filename, void **buf, size_t *size);
void fd_full_read(int fd, void **buf, size_t *size);
2017-12-06 18:30:48 +01:00
void full_read(const char *filename, void **buf, size_t *size);
void full_read_at(int dirfd, const char *filename, void **buf, size_t *size);
2017-12-06 18:30:48 +01:00
void stream_full_read(int fd, void **buf, size_t *size);
2017-11-09 18:51:41 +01:00
void write_zero(int fd, size_t size);
2017-10-11 20:57:18 +02:00
2018-10-25 03:08:06 +02:00
#ifdef __cplusplus
}
2019-01-26 12:00:23 +01:00
#include <string>
#include <vector>
2019-01-26 19:39:24 +01:00
#define str_contains(s, ss) ((ss) != nullptr && (s).find(ss) != std::string::npos)
2019-01-26 12:00:23 +01:00
#define str_starts(s, ss) ((ss) != nullptr && (s).compare(0, strlen(ss), ss) == 0)
2019-01-26 19:39:24 +01:00
// file.cpp
2019-01-26 12:00:23 +01:00
std::vector<std::string> file_to_vector(const char *filename);
2019-01-26 19:39:24 +01:00
// misc.cpp
2019-01-26 19:00:19 +01:00
struct exec_t {
bool err = false;
int fd = -2;
void (*pre_exec)() = nullptr;
int (*fork)() = xfork;
2019-01-26 19:39:24 +01:00
const char **argv = nullptr;
2019-01-26 19:00:19 +01:00
};
int exec_command(exec_t &exec);
2019-01-26 19:39:24 +01:00
template <class ...Args>
int exec_command(exec_t &exec, Args &&...args) {
const char *argv[] = {args..., nullptr};
exec.argv = argv;
return exec_command(exec);
}
2019-01-26 19:00:19 +01:00
int exec_command_sync(const char **argv);
2019-01-26 19:39:24 +01:00
template <class ...Args>
int exec_command_sync(Args &&...args) {
const char *argv[] = {args..., nullptr};
return exec_command_sync(argv);
}
2019-01-26 12:00:23 +01:00
2018-10-25 03:08:06 +02:00
#endif
#endif