2017-04-07 06:21:20 +08:00
|
|
|
/* util.h - Header for all utility functions
|
|
|
|
*/
|
|
|
|
|
2017-04-05 03:44:13 +08:00
|
|
|
#ifndef _UTILS_H_
|
|
|
|
#define _UTILS_H_
|
|
|
|
|
|
|
|
#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>
|
2017-04-08 07:37:43 +08:00
|
|
|
#include <sys/socket.h>
|
2017-04-15 03:23:09 +08:00
|
|
|
#include <sys/stat.h>
|
2017-04-05 03:44:13 +08:00
|
|
|
|
2017-04-08 07:37:43 +08:00
|
|
|
#include "vector.h"
|
2017-04-05 03:44:13 +08:00
|
|
|
|
2017-04-15 03:23:09 +08:00
|
|
|
#define UID_SHELL (get_shell_uid())
|
|
|
|
#define UID_ROOT 0
|
|
|
|
#define UID_SYSTEM (get_system_uid())
|
|
|
|
#define UID_RADIO (get_radio_uid())
|
|
|
|
|
2017-04-05 03:44:13 +08:00
|
|
|
// xwrap.c
|
|
|
|
|
|
|
|
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);
|
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-09 07:25:10 +08:00
|
|
|
int xsocketpair(int domain, int type, int protocol, int sv[2]);
|
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);
|
2017-04-15 03:23:09 +08:00
|
|
|
int xdup2(int oldfd, int newfd);
|
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);
|
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);
|
2017-10-14 00:08:12 +08:00
|
|
|
int xmkdir_p(const char *pathname, mode_t mode);
|
|
|
|
int xmkdirat(int dirfd, const char *pathname, mode_t mode);
|
2017-04-28 21:48:38 +08: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-14 00:08:12 +08:00
|
|
|
pid_t xfork();
|
2017-04-05 03:44:13 +08:00
|
|
|
|
2017-04-06 06:12:29 +08:00
|
|
|
// misc.c
|
|
|
|
|
2017-10-12 02:57:18 +08:00
|
|
|
extern int quit_signals[];
|
|
|
|
|
2017-04-15 03:23:09 +08:00
|
|
|
unsigned get_shell_uid();
|
|
|
|
unsigned get_system_uid();
|
|
|
|
unsigned get_radio_uid();
|
2017-04-06 06:12:29 +08:00
|
|
|
int check_data();
|
2017-04-20 22:45:56 +08:00
|
|
|
int file_to_vector(const char* filename, struct vector *v);
|
|
|
|
int vector_to_file(const char* filename, struct vector *v);
|
2017-04-18 19:32:50 +08:00
|
|
|
ssize_t fdgets(char *buf, size_t size, int fd);
|
2017-04-07 06:21:20 +08:00
|
|
|
void ps(void (*func)(int));
|
|
|
|
void ps_filter_proc_name(const char *filter, void (*func)(int));
|
2017-04-15 18:33:16 +08:00
|
|
|
void unlock_blocks();
|
2017-04-17 16:36:49 +08:00
|
|
|
void setup_sighandlers(void (*handler)(int));
|
2017-08-31 02:20:06 +08:00
|
|
|
int exec_command(int err, int *fd, void (*setupenv)(struct vector*), const char *argv0, ...);
|
2017-08-20 21:36:32 +08:00
|
|
|
int exec_command_sync(char *const argv0, ...);
|
2017-05-01 01:58:52 +08:00
|
|
|
int bind_mount(const char *from, const char *to);
|
2017-05-05 16:13:26 +08:00
|
|
|
void get_client_cred(int fd, struct ucred *cred);
|
2017-06-24 23:37:45 +09:00
|
|
|
int switch_mnt_ns(int pid);
|
2017-10-09 05:05:52 +08:00
|
|
|
int fork_dont_care();
|
2017-06-24 23:37:45 +09:00
|
|
|
|
2017-10-12 02:57:18 +08:00
|
|
|
// file.c
|
|
|
|
|
|
|
|
extern char **excl_list;
|
|
|
|
|
|
|
|
struct file_attr {
|
|
|
|
struct stat st;
|
|
|
|
char con[128];
|
|
|
|
};
|
|
|
|
|
2017-10-14 21:10:22 +08:00
|
|
|
int fd_getpath(int fd, char *path, size_t size);
|
2017-10-12 02:57:18 +08:00
|
|
|
int mkdir_p(const char *pathname, mode_t mode);
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
void fclone_attr(const int sourcefd, const int targetfd);
|
|
|
|
void clone_attr(const char *source, const char *target);
|
2017-10-12 03:39:39 +08:00
|
|
|
void restorecon(int dirfd, int force);
|
2017-11-06 05:41:03 +08:00
|
|
|
void mmap_ro(const char *filename, void **buf, size_t *size);
|
|
|
|
void mmap_rw(const char *filename, void **buf, size_t *size);
|
2017-11-10 20:25:41 +08:00
|
|
|
void full_read(int fd, void **buf, size_t *size);
|
2017-11-10 01:51:41 +08:00
|
|
|
void write_zero(int fd, size_t size);
|
|
|
|
void mem_align(size_t *pos, size_t align);
|
|
|
|
void file_align(int fd, size_t align, int out);
|
2017-10-12 02:57:18 +08:00
|
|
|
|
2017-06-24 23:37:45 +09:00
|
|
|
// img.c
|
2017-08-10 17:26:37 +08:00
|
|
|
|
|
|
|
#define round_size(a) ((((a) / 32) + 2) * 32)
|
|
|
|
#define SOURCE_TMP "/dev/source"
|
|
|
|
#define TARGET_TMP "/dev/target"
|
|
|
|
|
2017-06-03 03:58:26 +08:00
|
|
|
int create_img(const char *img, int size);
|
|
|
|
int get_img_size(const char *img, int *used, int *total);
|
|
|
|
int resize_img(const char *img, int size);
|
2017-06-24 23:37:45 +09:00
|
|
|
char *mount_image(const char *img, const char *target);
|
|
|
|
void umount_image(const char *target, const char *device);
|
2017-08-10 17:26:37 +08:00
|
|
|
int merge_img(const char *source, const char *target);
|
|
|
|
void trim_img(const char *img);
|
2017-04-06 06:12:29 +08:00
|
|
|
|
2017-04-09 07:25:10 +08:00
|
|
|
#endif
|