2017-04-07 06:21:20 +08:00
|
|
|
/* util.h - Header for all utility functions
|
|
|
|
*/
|
|
|
|
|
2019-02-24 23:09:34 -05:00
|
|
|
#pragma once
|
2017-04-05 03:44:13 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2017-04-07 06:21:20 +08:00
|
|
|
#include <dirent.h>
|
2017-11-23 23:55:33 +08:00
|
|
|
#include <pthread.h>
|
2018-10-04 15:06:13 -04:00
|
|
|
#include <poll.h>
|
2017-04-08 07:37:43 +08:00
|
|
|
#include <sys/socket.h>
|
2017-04-15 03:23:09 +08:00
|
|
|
#include <sys/stat.h>
|
2019-02-22 02:56:18 -05:00
|
|
|
#include <sys/mman.h>
|
2017-04-05 03:44:13 +08:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2017-04-15 03:23:09 +08:00
|
|
|
#define UID_SHELL (get_shell_uid())
|
|
|
|
#define UID_ROOT 0
|
|
|
|
|
2018-11-08 05:03:59 -05:00
|
|
|
// xwrap.cpp
|
2017-04-05 03:44:13 +08:00
|
|
|
|
|
|
|
FILE *xfopen(const char *pathname, const char *mode);
|
2017-05-08 03:11:14 +08:00
|
|
|
FILE *xfdopen(int fd, const char *mode);
|
2017-04-28 21:48:38 +08: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-14 00:08:12 +08:00
|
|
|
int xopenat(int dirfd, const char *pathname, int flags);
|
2017-04-06 06:12:29 +08: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 22:29:53 +08:00
|
|
|
int xpipe2(int pipefd[2], int flags);
|
2017-04-06 06:12:29 +08:00
|
|
|
int xsetns(int fd, int nstype);
|
2018-07-13 22:14:32 +08:00
|
|
|
int xunshare(int flags);
|
2017-04-07 06:21:20 +08:00
|
|
|
DIR *xopendir(const char *name);
|
2017-10-14 00:08:12 +08:00
|
|
|
DIR *xfdopendir(int fd);
|
2017-04-07 06:21:20 +08:00
|
|
|
struct dirent *xreaddir(DIR *dirp);
|
2017-04-08 07:37:43 +08: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);
|
2017-07-08 23:51:58 +08:00
|
|
|
int xaccept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
|
2017-04-08 07:37:43 +08: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);
|
2017-04-09 07:25:10 +08:00
|
|
|
int xpthread_create(pthread_t *thread, const pthread_attr_t *attr,
|
2017-04-17 16:36:49 +08:00
|
|
|
void *(*start_routine) (void *), void *arg);
|
2017-04-15 03:23:09 +08:00
|
|
|
int xstat(const char *pathname, struct stat *buf);
|
2017-05-04 01:13:04 +08:00
|
|
|
int xlstat(const char *pathname, struct stat *buf);
|
2019-03-14 06:34:22 -04:00
|
|
|
int xfstat(int fd, struct stat *buf);
|
2017-04-15 03:23:09 +08:00
|
|
|
int xdup2(int oldfd, int newfd);
|
2018-07-13 05:41:29 +08:00
|
|
|
int xdup3(int oldfd, int newfd, int flags);
|
2017-04-15 18:10:54 +08:00
|
|
|
ssize_t xreadlink(const char *pathname, char *buf, size_t bufsiz);
|
2017-10-14 00:08:12 +08:00
|
|
|
ssize_t xreadlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
|
2017-04-15 18:10:54 +08:00
|
|
|
int xsymlink(const char *target, const char *linkpath);
|
2018-07-13 05:41:29 +08:00
|
|
|
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 16:36:49 +08:00
|
|
|
int xmount(const char *source, const char *target,
|
|
|
|
const char *filesystemtype, unsigned long mountflags,
|
|
|
|
const void *data);
|
2017-05-04 01:13:04 +08:00
|
|
|
int xumount(const char *target);
|
|
|
|
int xumount2(const char *target, int flags);
|
2017-04-17 16:36:49 +08:00
|
|
|
int xrename(const char *oldpath, const char *newpath);
|
|
|
|
int xmkdir(const char *pathname, mode_t mode);
|
2018-01-11 03:06:20 +08:00
|
|
|
int xmkdirs(const char *pathname, mode_t mode);
|
2017-10-14 00:08:12 +08:00
|
|
|
int xmkdirat(int dirfd, const char *pathname, mode_t mode);
|
2019-02-24 23:09:34 -05:00
|
|
|
void *xmmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
|
2017-04-28 21:48:38 +08:00
|
|
|
ssize_t xsendfile(int out_fd, int in_fd, off_t *offset, size_t count);
|
2017-10-14 00:08:12 +08:00
|
|
|
pid_t xfork();
|
2018-10-04 15:06:13 -04:00
|
|
|
int xpoll(struct pollfd *fds, nfds_t nfds, int timeout);
|
2019-03-01 17:08:08 -05:00
|
|
|
int xinotify_init1(int flags);
|
2017-04-05 03:44:13 +08:00
|
|
|
|
2018-11-08 05:03:59 -05:00
|
|
|
// misc.cpp
|
2017-04-06 06:12:29 +08:00
|
|
|
|
2017-04-15 03:23:09 +08:00
|
|
|
unsigned get_shell_uid();
|
2017-10-09 05:05:52 +08:00
|
|
|
int fork_dont_care();
|
2019-02-14 17:36:18 -05:00
|
|
|
int fork_no_zombie();
|
2018-02-11 03:40:09 +08:00
|
|
|
void gen_rand_str(char *buf, int len);
|
2018-07-21 05:12:22 +08:00
|
|
|
int strend(const char *s1, const char *s2);
|
2019-02-26 16:04:17 +08:00
|
|
|
char *rtrim(char *str);
|
2019-02-15 04:31:39 -05:00
|
|
|
void init_argv0(int argc, char **argv);
|
|
|
|
void set_nice_name(const char *name);
|
2019-03-07 20:31:35 -05:00
|
|
|
int parse_int(const char *s);
|
2017-06-24 23:37:45 +09:00
|
|
|
|
2018-09-06 01:33:17 -04:00
|
|
|
#define getline __getline
|
|
|
|
#define getdelim __getdelim
|
|
|
|
|
|
|
|
ssize_t __getline(char **lineptr, size_t *n, FILE *stream);
|
|
|
|
ssize_t __getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
|
|
|
|
|
2018-11-08 05:03:59 -05:00
|
|
|
// file.cpp
|
2017-10-12 02:57:18 +08:00
|
|
|
|
2019-01-26 13:39:24 -05:00
|
|
|
#define do_align(p, a) (((p) + (a) - 1) / (a) * (a))
|
2019-01-26 06:00:23 -05:00
|
|
|
#define align_off(p, a) (do_align(p, a) - (p))
|
2018-01-29 22:16:02 +08:00
|
|
|
|
2018-10-26 17:02:56 -04:00
|
|
|
extern const char **excl_list;
|
2017-10-12 02:57:18 +08:00
|
|
|
|
|
|
|
struct file_attr {
|
|
|
|
struct stat st;
|
|
|
|
char con[128];
|
|
|
|
};
|
|
|
|
|
2019-03-14 06:34:22 -04:00
|
|
|
ssize_t fd_path(int fd, char *path, size_t size);
|
|
|
|
int fd_pathat(int dirfd, const char *name, char *path, size_t size);
|
2018-01-11 03:06:20 +08:00
|
|
|
int mkdirs(const char *pathname, mode_t mode);
|
2019-02-14 00:52:59 -05:00
|
|
|
void post_order_walk(int dirfd, void (*fn)(int, struct dirent *));
|
2017-10-12 02:57:18 +08: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-02 03:22:38 +08:00
|
|
|
void link_dir(int src, int dest);
|
2017-10-12 02:57:18 +08:00
|
|
|
void clone_dir(int src, int dest);
|
|
|
|
int getattr(const char *path, struct file_attr *a);
|
2019-03-14 06:34:22 -04:00
|
|
|
int getattrat(int dirfd, const char *name, struct file_attr *a);
|
2017-10-12 02:57:18 +08:00
|
|
|
int fgetattr(int fd, struct file_attr *a);
|
|
|
|
int setattr(const char *path, struct file_attr *a);
|
2019-03-14 06:34:22 -04:00
|
|
|
int setattrat(int dirfd, const char *name, struct file_attr *a);
|
2017-10-12 02:57:18 +08:00
|
|
|
int fsetattr(int fd, struct file_attr *a);
|
2019-01-19 23:59:37 -05:00
|
|
|
void fclone_attr(int sourcefd, int targetfd);
|
2017-10-12 02:57:18 +08:00
|
|
|
void clone_attr(const char *source, const char *target);
|
2018-08-09 18:13:07 +08:00
|
|
|
void mmap_ro(const char *filename, void **buf, size_t *size);
|
2017-12-07 03:21:13 +08:00
|
|
|
void fd_full_read(int fd, void **buf, size_t *size);
|
2017-12-07 01:30:48 +08:00
|
|
|
void full_read(const char *filename, void **buf, size_t *size);
|
2017-11-10 01:51:41 +08:00
|
|
|
void write_zero(int fd, size_t size);
|
2017-10-12 02:57:18 +08:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
2019-01-26 06:00:23 -05:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2019-02-17 22:30:23 -05:00
|
|
|
#include <functional>
|
|
|
|
#include <string_view>
|
2019-01-26 06:00:23 -05:00
|
|
|
|
2019-01-26 13:39:24 -05:00
|
|
|
#define str_contains(s, ss) ((ss) != nullptr && (s).find(ss) != std::string::npos)
|
2019-01-26 06:00:23 -05:00
|
|
|
#define str_starts(s, ss) ((ss) != nullptr && (s).compare(0, strlen(ss), ss) == 0)
|
|
|
|
|
2019-02-19 01:50:41 -05:00
|
|
|
// RAII
|
2019-01-26 13:39:24 -05:00
|
|
|
|
2019-02-16 02:24:35 -05:00
|
|
|
class MutexGuard {
|
|
|
|
public:
|
|
|
|
explicit MutexGuard(pthread_mutex_t &m): mutex(&m) {
|
|
|
|
pthread_mutex_lock(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit MutexGuard(pthread_mutex_t *m): mutex(m) {
|
|
|
|
pthread_mutex_lock(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
~MutexGuard() {
|
|
|
|
pthread_mutex_unlock(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
pthread_mutex_t *mutex;
|
|
|
|
};
|
|
|
|
|
2019-02-19 01:50:41 -05:00
|
|
|
class RunFinally {
|
|
|
|
public:
|
|
|
|
explicit RunFinally(std::function<void()> &&fn): fn(std::move(fn)) {}
|
|
|
|
|
2019-03-02 04:24:41 -05:00
|
|
|
void disable() { fn = nullptr; }
|
|
|
|
|
|
|
|
~RunFinally() { if (fn) fn(); }
|
2019-02-19 01:50:41 -05:00
|
|
|
|
|
|
|
private:
|
2019-03-02 04:24:41 -05:00
|
|
|
std::function<void ()> fn;
|
2019-02-19 01:50:41 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// file.cpp
|
|
|
|
|
2019-03-05 20:27:09 -05:00
|
|
|
void file_readline(const char *file, const std::function<bool (std::string_view)> &fn, bool trim = false);
|
|
|
|
void parse_prop_file(const char *file, const std::function
|
|
|
|
<bool(std::string_view, std::string_view)> &fn);
|
2019-02-24 23:09:34 -05:00
|
|
|
void *__mmap(const char *filename, size_t *size, bool rw);
|
|
|
|
|
|
|
|
template <typename B>
|
|
|
|
void mmap_ro(const char *filename, B &buf, size_t &sz) {
|
|
|
|
buf = (B) __mmap(filename, &sz, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename B, typename L>
|
|
|
|
void mmap_ro(const char *filename, B &buf, L &sz) {
|
|
|
|
size_t __sz;
|
|
|
|
buf = (B) __mmap(filename, &__sz, false);
|
|
|
|
sz = __sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename B>
|
|
|
|
void mmap_rw(const char *filename, B &buf, size_t &sz) {
|
|
|
|
buf = (B) __mmap(filename, &sz, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename B, typename L>
|
|
|
|
void mmap_rw(const char *filename, B &buf, L &sz) {
|
|
|
|
size_t __sz;
|
|
|
|
buf = (B) __mmap(filename, &__sz, true);
|
|
|
|
sz = __sz;
|
|
|
|
}
|
2019-02-19 01:50:41 -05:00
|
|
|
|
|
|
|
// misc.cpp
|
|
|
|
|
2019-03-08 01:01:42 -05:00
|
|
|
static inline int parse_int(char *s) { return parse_int((const char *) s); }
|
|
|
|
|
2019-03-07 20:31:35 -05:00
|
|
|
template <class S>
|
2019-03-08 01:01:42 -05:00
|
|
|
int parse_int(S __s) { return parse_int(__s.data()); }
|
2019-03-07 20:31:35 -05:00
|
|
|
|
2019-02-14 17:36:18 -05:00
|
|
|
int new_daemon_thread(void *(*start_routine) (void *), void *arg = nullptr,
|
|
|
|
const pthread_attr_t *attr = nullptr);
|
|
|
|
|
2019-01-26 13:00:19 -05:00
|
|
|
struct exec_t {
|
|
|
|
bool err = false;
|
|
|
|
int fd = -2;
|
|
|
|
void (*pre_exec)() = nullptr;
|
|
|
|
int (*fork)() = xfork;
|
2019-01-26 13:39:24 -05:00
|
|
|
const char **argv = nullptr;
|
2019-01-26 13:00:19 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
int exec_command(exec_t &exec);
|
2019-01-26 13:39:24 -05: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-02-12 16:13:31 -05:00
|
|
|
int exec_command_sync(exec_t &exec);
|
2019-01-26 13:39:24 -05:00
|
|
|
template <class ...Args>
|
2019-02-12 16:13:31 -05:00
|
|
|
int exec_command_sync(exec_t &exec, Args &&...args) {
|
2019-01-26 13:39:24 -05:00
|
|
|
const char *argv[] = {args..., nullptr};
|
2019-02-12 16:13:31 -05:00
|
|
|
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...);
|
2019-01-26 13:39:24 -05:00
|
|
|
}
|
2019-01-26 06:00:23 -05:00
|
|
|
|
2019-02-18 03:05:13 -05:00
|
|
|
bool ends_with(const std::string_view &s1, const std::string_view &s2);
|
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
#endif
|