| 
									
										
										
										
											2017-04-06 06:12:29 +08:00
										 |  |  | #include <sched.h>
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							| 
									
										
										
										
											2017-10-12 02:57:18 +08:00
										 |  |  | #include <pthread.h>
 | 
					
						
							| 
									
										
										
										
											2017-04-08 07:37:43 +08:00
										 |  |  | #include <sys/socket.h>
 | 
					
						
							| 
									
										
										
										
											2017-04-06 06:12:29 +08:00
										 |  |  | #include <sys/types.h>
 | 
					
						
							|  |  |  | #include <sys/stat.h>
 | 
					
						
							| 
									
										
										
										
											2017-04-17 16:36:49 +08:00
										 |  |  | #include <sys/mount.h>
 | 
					
						
							| 
									
										
										
										
											2017-04-28 21:48:38 +08:00
										 |  |  | #include <sys/mman.h>
 | 
					
						
							|  |  |  | #include <sys/sendfile.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-02 21:29:45 -08:00
										 |  |  | #include <sys/ptrace.h>
 | 
					
						
							| 
									
										
										
										
											2017-04-05 03:44:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 01:50:30 -07:00
										 |  |  | #include <utils.hpp>
 | 
					
						
							| 
									
										
										
										
											2017-04-05 03:44:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-18 04:20:21 -07:00
										 |  |  | using namespace std; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-05 03:44:13 +08:00
										 |  |  | FILE *xfopen(const char *pathname, const char *mode) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     FILE *fp = fopen(pathname, mode); | 
					
						
							|  |  |  |     if (fp == nullptr) { | 
					
						
							|  |  |  |         PLOGE("fopen: %s", pathname); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return fp; | 
					
						
							| 
									
										
										
										
											2017-04-06 06:12:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-08 03:11:14 +08:00
										 |  |  | FILE *xfdopen(int fd, const char *mode) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     FILE *fp = fdopen(fd, mode); | 
					
						
							|  |  |  |     if (fp == nullptr) { | 
					
						
							|  |  |  |         PLOGE("fopen"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return fp; | 
					
						
							| 
									
										
										
										
											2017-05-08 03:11:14 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-01 22:58:19 -07:00
										 |  |  | int xopen(const char *pathname, int flags) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int fd = open(pathname, flags); | 
					
						
							|  |  |  |     if (fd < 0) { | 
					
						
							|  |  |  |         PLOGE("open: %s", pathname); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return fd; | 
					
						
							| 
									
										
										
										
											2017-04-06 06:12:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-01 22:58:19 -07:00
										 |  |  | int xopen(const char *pathname, int flags, mode_t mode) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int fd = open(pathname, flags, mode); | 
					
						
							|  |  |  |     if (fd < 0) { | 
					
						
							|  |  |  |         PLOGE("open: %s", pathname); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return fd; | 
					
						
							| 
									
										
										
										
											2017-04-28 21:48:38 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 00:08:12 +08:00
										 |  |  | int xopenat(int dirfd, const char *pathname, int flags) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int fd = openat(dirfd, pathname, flags); | 
					
						
							|  |  |  |     if (fd < 0) { | 
					
						
							|  |  |  |         PLOGE("openat: %s", pathname); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return fd; | 
					
						
							| 
									
										
										
										
											2017-10-14 00:08:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-11 04:05:34 -07:00
										 |  |  | int xopenat(int dirfd, const char *pathname, int flags, mode_t mode) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int fd = openat(dirfd, pathname, flags, mode); | 
					
						
							|  |  |  |     if (fd < 0) { | 
					
						
							|  |  |  |         PLOGE("openat: %s", pathname); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return fd; | 
					
						
							| 
									
										
										
										
											2020-04-11 04:05:34 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-17 01:42:45 -08:00
										 |  |  | // Write exact same size as count
 | 
					
						
							| 
									
										
										
										
											2017-04-06 06:12:29 +08:00
										 |  |  | ssize_t xwrite(int fd, const void *buf, size_t count) { | 
					
						
							| 
									
										
										
										
											2021-01-17 01:42:45 -08:00
										 |  |  |     size_t write_sz = 0; | 
					
						
							|  |  |  |     ssize_t ret; | 
					
						
							|  |  |  |     do { | 
					
						
							|  |  |  |         ret = write(fd, (byte *) buf + write_sz, count - write_sz); | 
					
						
							|  |  |  |         if (ret < 0) { | 
					
						
							|  |  |  |             if (errno == EINTR) | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             PLOGE("write"); | 
					
						
							|  |  |  |             return ret; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         write_sz += ret; | 
					
						
							|  |  |  |     } while (write_sz != count && ret != 0); | 
					
						
							|  |  |  |     if (write_sz != count) { | 
					
						
							|  |  |  |         PLOGE("write (%zu != %zu)", count, write_sz); | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-01-17 01:42:45 -08:00
										 |  |  |     return write_sz; | 
					
						
							| 
									
										
										
										
											2017-04-06 06:12:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Read error other than EOF
 | 
					
						
							|  |  |  | ssize_t xread(int fd, void *buf, size_t count) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = read(fd, buf, count); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("read"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2017-04-06 06:12:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Read exact same size as count
 | 
					
						
							|  |  |  | ssize_t xxread(int fd, void *buf, size_t count) { | 
					
						
							| 
									
										
										
										
											2021-01-16 21:43:53 -08:00
										 |  |  |     size_t read_sz = 0; | 
					
						
							| 
									
										
										
										
											2021-01-17 01:42:45 -08:00
										 |  |  |     ssize_t ret; | 
					
						
							|  |  |  |     do { | 
					
						
							|  |  |  |         ret = read(fd, (byte *) buf + read_sz, count - read_sz); | 
					
						
							| 
									
										
										
										
											2021-01-16 21:43:53 -08:00
										 |  |  |         if (ret < 0) { | 
					
						
							| 
									
										
										
										
											2021-01-17 01:42:45 -08:00
										 |  |  |             if (errno == EINTR) | 
					
						
							|  |  |  |                 continue; | 
					
						
							| 
									
										
										
										
											2021-01-16 21:43:53 -08:00
										 |  |  |             PLOGE("read"); | 
					
						
							|  |  |  |             return ret; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         read_sz += ret; | 
					
						
							| 
									
										
										
										
											2021-01-17 01:42:45 -08:00
										 |  |  |     } while (read_sz != count && ret != 0); | 
					
						
							| 
									
										
										
										
											2021-01-16 21:43:53 -08:00
										 |  |  |     if (read_sz != count) { | 
					
						
							|  |  |  |         PLOGE("read (%zu != %zu)", count, read_sz); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return read_sz; | 
					
						
							| 
									
										
										
										
											2017-04-06 06:12:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-10 22:29:53 +08:00
										 |  |  | int xpipe2(int pipefd[2], int flags) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = pipe2(pipefd, flags); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("pipe2"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2017-04-06 06:12:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int xsetns(int fd, int nstype) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = setns(fd, nstype); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("setns"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2017-04-06 06:12:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-13 22:14:32 +08:00
										 |  |  | int xunshare(int flags) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = unshare(flags); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("unshare"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2018-07-13 22:14:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-07 06:21:20 +08:00
										 |  |  | DIR *xopendir(const char *name) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     DIR *d = opendir(name); | 
					
						
							|  |  |  |     if (d == nullptr) { | 
					
						
							|  |  |  |         PLOGE("opendir: %s", name); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return d; | 
					
						
							| 
									
										
										
										
											2017-04-07 06:21:20 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 00:08:12 +08:00
										 |  |  | DIR *xfdopendir(int fd) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     DIR *d = fdopendir(fd); | 
					
						
							|  |  |  |     if (d == nullptr) { | 
					
						
							|  |  |  |         PLOGE("fdopendir"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return d; | 
					
						
							| 
									
										
										
										
											2017-10-14 00:08:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-07 06:21:20 +08:00
										 |  |  | struct dirent *xreaddir(DIR *dirp) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     errno = 0; | 
					
						
							|  |  |  |     for (dirent *e;;) { | 
					
						
							|  |  |  |         e = readdir(dirp); | 
					
						
							|  |  |  |         if (e == nullptr) { | 
					
						
							|  |  |  |             if (errno) | 
					
						
							|  |  |  |                 PLOGE("readdir"); | 
					
						
							|  |  |  |             return nullptr; | 
					
						
							|  |  |  |         } else if (e->d_name == "."sv || e->d_name == ".."sv) { | 
					
						
							|  |  |  |             // Filter . and .. for users
 | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return e; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-04-07 06:21:20 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-08 07:37:43 +08:00
										 |  |  | pid_t xsetsid() { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     pid_t pid = setsid(); | 
					
						
							|  |  |  |     if (pid < 0) { | 
					
						
							|  |  |  |         PLOGE("setsid"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return pid; | 
					
						
							| 
									
										
										
										
											2017-04-08 07:37:43 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int xsocket(int domain, int type, int protocol) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int fd = socket(domain, type, protocol); | 
					
						
							|  |  |  |     if (fd < 0) { | 
					
						
							|  |  |  |         PLOGE("socket"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return fd; | 
					
						
							| 
									
										
										
										
											2017-04-08 07:37:43 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int xbind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = bind(sockfd, addr, addrlen); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("bind"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2017-04-08 07:37:43 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int xlisten(int sockfd, int backlog) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = listen(sockfd, backlog); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("listen"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2017-04-08 07:37:43 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-26 02:57:34 -05:00
										 |  |  | static int accept4_compat(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int fd = accept(sockfd, addr, addrlen); | 
					
						
							|  |  |  |     if (fd < 0) { | 
					
						
							|  |  |  |         PLOGE("accept"); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         if (flags & SOCK_CLOEXEC) | 
					
						
							|  |  |  |             fcntl(fd, F_SETFD, FD_CLOEXEC); | 
					
						
							|  |  |  |         if (flags & SOCK_NONBLOCK) { | 
					
						
							|  |  |  |             int i = fcntl(fd, F_GETFL); | 
					
						
							|  |  |  |             fcntl(fd, F_SETFL, i | O_NONBLOCK); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return fd; | 
					
						
							| 
									
										
										
										
											2018-11-26 02:57:34 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int xaccept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int fd = accept4(sockfd, addr, addrlen, flags); | 
					
						
							|  |  |  |     if (fd < 0) { | 
					
						
							|  |  |  |         if (errno == ENOSYS) | 
					
						
							|  |  |  |             return accept4_compat(sockfd, addr, addrlen, flags); | 
					
						
							|  |  |  |         PLOGE("accept4"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return fd; | 
					
						
							| 
									
										
										
										
											2017-04-08 07:37:43 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void *xmalloc(size_t size) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     void *p = malloc(size); | 
					
						
							|  |  |  |     if (p == nullptr) { | 
					
						
							|  |  |  |         PLOGE("malloc"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return p; | 
					
						
							| 
									
										
										
										
											2017-04-08 07:37:43 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void *xcalloc(size_t nmemb, size_t size) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     void *p = calloc(nmemb, size); | 
					
						
							|  |  |  |     if (p == nullptr) { | 
					
						
							|  |  |  |         PLOGE("calloc"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return p; | 
					
						
							| 
									
										
										
										
											2017-04-08 07:37:43 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void *xrealloc(void *ptr, size_t size) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     void *p = realloc(ptr, size); | 
					
						
							|  |  |  |     if (p == nullptr) { | 
					
						
							|  |  |  |         PLOGE("realloc"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return p; | 
					
						
							| 
									
										
										
										
											2017-04-08 07:37:43 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ssize_t xsendmsg(int sockfd, const struct msghdr *msg, int flags) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int sent = sendmsg(sockfd, msg, flags); | 
					
						
							|  |  |  |     if (sent < 0) { | 
					
						
							|  |  |  |         PLOGE("sendmsg"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return sent; | 
					
						
							| 
									
										
										
										
											2017-04-08 07:37:43 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ssize_t xrecvmsg(int sockfd, struct msghdr *msg, int flags) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int rec = recvmsg(sockfd, msg, flags); | 
					
						
							|  |  |  |     if (rec < 0) { | 
					
						
							|  |  |  |         PLOGE("recvmsg"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return rec; | 
					
						
							| 
									
										
										
										
											2017-04-08 07:37:43 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-16 21:43:53 -08:00
										 |  |  | int xpthread_create(pthread_t *thread, const pthread_attr_t *attr, | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |                     void *(*start_routine) (void *), void *arg) { | 
					
						
							|  |  |  |     errno = pthread_create(thread, attr, start_routine, arg); | 
					
						
							|  |  |  |     if (errno) { | 
					
						
							|  |  |  |         PLOGE("pthread_create"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return errno; | 
					
						
							| 
									
										
										
										
											2017-04-09 07:25:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-15 03:23:09 +08:00
										 |  |  | int xstat(const char *pathname, struct stat *buf) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = stat(pathname, buf); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("stat %s", pathname); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2017-04-15 03:23:09 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-04 01:13:04 +08:00
										 |  |  | int xlstat(const char *pathname, struct stat *buf) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = lstat(pathname, buf); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("lstat %s", pathname); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2017-05-04 01:13:04 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 06:34:22 -04:00
										 |  |  | int xfstat(int fd, struct stat *buf) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = fstat(fd, buf); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("fstat %d", fd); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2019-03-14 06:34:22 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-01 23:37:11 -07:00
										 |  |  | int xdup(int fd) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = dup(fd); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("dup"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2020-04-01 23:37:11 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-15 03:23:09 +08:00
										 |  |  | int xdup2(int oldfd, int newfd) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = dup2(oldfd, newfd); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("dup2"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2017-04-15 03:23:09 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-13 05:41:29 +08:00
										 |  |  | int xdup3(int oldfd, int newfd, int flags) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = dup3(oldfd, newfd, flags); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("dup3"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2018-07-13 05:41:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-15 18:10:54 +08:00
										 |  |  | ssize_t xreadlink(const char *pathname, char *buf, size_t bufsiz) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     ssize_t ret = readlink(pathname, buf, bufsiz); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("readlink %s", pathname); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         buf[ret] = '\0'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2017-10-14 00:08:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ssize_t xreadlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     // readlinkat() may fail on x86 platform, returning random value
 | 
					
						
							|  |  |  |     // instead of number of bytes placed in buf (length of link)
 | 
					
						
							| 
									
										
										
										
											2020-02-03 13:24:02 +08:00
										 |  |  | #if defined(__i386__) || defined(__x86_64__)
 | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     memset(buf, 0, bufsiz); | 
					
						
							|  |  |  |     ssize_t ret = readlinkat(dirfd, pathname, buf, bufsiz); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("readlinkat %s", pathname); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2020-02-03 13:24:02 +08:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     ssize_t ret = readlinkat(dirfd, pathname, buf, bufsiz); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("readlinkat %s", pathname); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         buf[ret] = '\0'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2020-02-03 13:24:02 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2017-04-15 18:10:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int xsymlink(const char *target, const char *linkpath) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = symlink(target, linkpath); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("symlink %s->%s", target, linkpath); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2017-04-15 18:10:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-13 05:41:29 +08:00
										 |  |  | int xsymlinkat(const char *target, int newdirfd, const char *linkpath) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = symlinkat(target, newdirfd, linkpath); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("symlinkat %s->%s", target, linkpath); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2018-07-13 05:41:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int xlinkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = linkat(olddirfd, oldpath, newdirfd, newpath, flags); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("linkat %s->%s", oldpath, newpath); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2018-07-13 05:41:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-17 16:36:49 +08:00
										 |  |  | int xmount(const char *source, const char *target, | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     const char *filesystemtype, unsigned long mountflags, | 
					
						
							|  |  |  |     const void *data) { | 
					
						
							|  |  |  |     int ret = mount(source, target, filesystemtype, mountflags, data); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("mount %s->%s", source, target); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2017-04-17 16:36:49 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-04 01:13:04 +08:00
										 |  |  | int xumount(const char *target) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = umount(target); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("umount %s", target); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2017-05-04 01:13:04 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int xumount2(const char *target, int flags) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = umount2(target, flags); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("umount2 %s", target); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2017-05-04 01:13:04 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-17 16:36:49 +08:00
										 |  |  | int xrename(const char *oldpath, const char *newpath) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = rename(oldpath, newpath); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("rename %s->%s", oldpath, newpath); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2017-04-17 16:36:49 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int xmkdir(const char *pathname, mode_t mode) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = mkdir(pathname, mode); | 
					
						
							|  |  |  |     if (ret < 0 && errno != EEXIST) { | 
					
						
							|  |  |  |         PLOGE("mkdir %s %u", pathname, mode); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2017-04-17 16:36:49 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-11 03:06:20 +08:00
										 |  |  | int xmkdirs(const char *pathname, mode_t mode) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = mkdirs(pathname, mode); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("mkdirs %s", pathname); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2017-10-14 00:08:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int xmkdirat(int dirfd, const char *pathname, mode_t mode) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = mkdirat(dirfd, pathname, mode); | 
					
						
							|  |  |  |     if (ret < 0 && errno != EEXIST) { | 
					
						
							|  |  |  |         PLOGE("mkdirat %s %u", pathname, mode); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2017-10-14 00:08:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 21:48:38 +08:00
										 |  |  | void *xmmap(void *addr, size_t length, int prot, int flags, | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int fd, off_t offset) { | 
					
						
							|  |  |  |     void *ret = mmap(addr, length, prot, flags, fd, offset); | 
					
						
							|  |  |  |     if (ret == MAP_FAILED) { | 
					
						
							|  |  |  |         PLOGE("mmap"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2017-04-28 21:48:38 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ssize_t xsendfile(int out_fd, int in_fd, off_t *offset, size_t count) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     ssize_t ret = sendfile(out_fd, in_fd, offset, count); | 
					
						
							|  |  |  |     if (count != ret) { | 
					
						
							|  |  |  |         PLOGE("sendfile"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2017-04-28 21:48:38 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 00:08:12 +08:00
										 |  |  | pid_t xfork() { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = fork(); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("fork"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2017-05-01 01:58:52 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-10-04 15:06:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | int xpoll(struct pollfd *fds, nfds_t nfds, int timeout) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = poll(fds, nfds, timeout); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("poll"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2018-10-04 15:06:13 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-03-01 17:08:08 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | int xinotify_init1(int flags) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = inotify_init1(flags); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("inotify_init1"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2019-03-01 17:08:08 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-04-11 04:05:34 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | char *xrealpath(const char *path, char *resolved_path) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     char buf[PATH_MAX]; | 
					
						
							|  |  |  |     char *ret = realpath(path, buf); | 
					
						
							|  |  |  |     if (ret == nullptr) { | 
					
						
							|  |  |  |         PLOGE("xrealpath"); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         strcpy(resolved_path, buf); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2020-04-11 04:05:34 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-19 04:53:16 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | int xmknod(const char *pathname, mode_t mode, dev_t dev) { | 
					
						
							| 
									
										
										
										
											2020-12-30 22:11:24 -08:00
										 |  |  |     int ret = mknod(pathname, mode, dev); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         PLOGE("mknod"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2020-05-19 04:53:16 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-01-02 21:29:45 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | long xptrace(int request, pid_t pid, void *addr, void *data) { | 
					
						
							|  |  |  |     long ret = ptrace(request, pid, addr, data); | 
					
						
							|  |  |  |     if (ret < 0) | 
					
						
							|  |  |  |         PLOGE("ptrace %d", pid); | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } |