Add missing functions in bionic

This commit is contained in:
topjohnwu 2019-06-23 14:54:48 -07:00
parent 4cc7aced15
commit 28cd6a75e7
7 changed files with 221 additions and 100 deletions

View File

@ -4,6 +4,7 @@ include $(CLEAR_VARS)
LOCAL_MODULE:= libutils LOCAL_MODULE:= libutils
LOCAL_C_INCLUDES := jni/include $(LIBUTILS) LOCAL_C_INCLUDES := jni/include $(LIBUTILS)
LOCAL_SRC_FILES := \ LOCAL_SRC_FILES := \
missing.cpp \
new.cpp \ new.cpp \
file.cpp \ file.cpp \
misc.cpp \ misc.cpp \

View File

@ -398,11 +398,12 @@ void parse_prop_file(const char *file, const function<bool (string_view, string_
} }
void parse_mnt(const char *file, const function<bool (mntent*)> &fn) { void parse_mnt(const char *file, const function<bool (mntent*)> &fn) {
unique_ptr<FILE, decltype(&fclose)> fp(xfopen(file, "rce"), &fclose); unique_ptr<FILE, decltype(&endmntent)> fp(setmntent(file, "re"), &endmntent);
if (fp) { if (fp) {
mntent *mentry; mntent mentry{};
while ((mentry = getmntent(fp.get())) != nullptr) { char buf[4096];
if (!fn(mentry)) while (getmntent_r(fp.get(), &mentry, buf, sizeof(buf))) {
if (!fn(&mentry))
break; break;
} }
} }

View File

@ -0,0 +1,59 @@
#pragma once
#include <sys/syscall.h>
#include <unistd.h>
#include <mntent.h>
#define getline __getline
#define getdelim __getdelim
#define setns __setns
#define unshare __unshare
#define accept4 __accept4
#define readlinkat __readlinkat
#define symlinkat __symlinkat
#define linkat __linkat
#define inotify_init1 __inotify_init1
#define getmntent_r __getmntent_r
#define setmntent __setmntent
#define endmntent __endmntent
#define hasmntopt __hasmntopt
__BEGIN_DECLS
ssize_t __getline(char **lineptr, size_t *n, FILE *stream);
ssize_t __getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
struct mntent *__getmntent_r(FILE* fp, struct mntent* e, char* buf, int buf_len);
FILE *__setmntent(const char* path, const char* mode);
int __endmntent(FILE* fp);
char *__hasmntopt(const struct mntent* mnt, const char* opt);
static inline int __setns(int fd, int nstype) {
return syscall(__NR_setns, fd, nstype);
}
static inline int __unshare(int flags) {
return syscall(__NR_unshare, flags);
}
static inline int __accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) {
return syscall(__NR_accept4, sockfd, addr, addrlen, flags);
}
static inline ssize_t __readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz) {
return syscall(__NR_readlinkat, dirfd, pathname, buf, bufsiz);
}
static inline int __symlinkat(const char *target, int newdirfd, const char *linkpath) {
return syscall(__NR_symlinkat, target, newdirfd, linkpath);
}
static inline int __linkat(int olddirfd, const char *oldpath,
int newdirfd, const char *newpath, int flags) {
return syscall(__NR_linkat, olddirfd, oldpath, newdirfd, newpath, flags);
}
static inline int __inotify_init1(int flags) {
return syscall(__NR_inotify_init1, flags);
}
__END_DECLS

View File

@ -12,6 +12,8 @@
#include <poll.h> #include <poll.h>
#include <mntent.h> #include <mntent.h>
#include "missing.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -87,12 +89,6 @@ void init_argv0(int argc, char **argv);
void set_nice_name(const char *name); void set_nice_name(const char *name);
int parse_int(const char *s); int parse_int(const char *s);
#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);
// file.cpp // file.cpp
#define do_align(p, a) (((p) + (a) - 1) / (a) * (a)) #define do_align(p, a) (((p) + (a) - 1) / (a) * (a))

View File

@ -69,80 +69,6 @@ int strend(const char *s1, const char *s2) {
return strcmp(s1 + l1 - l2, s2); return strcmp(s1 + l1 - l2, s2);
} }
/* Original source: https://opensource.apple.com/source/cvs/cvs-19/cvs/lib/getline.c
* License: GPL 2 or later
* Adjusted to match POSIX */
#define MIN_CHUNK 64
ssize_t __getdelim(char **lineptr, size_t *n, int delim, FILE *stream) {
size_t nchars_avail;
char *read_pos;
if (!lineptr || !n || !stream) {
errno = EINVAL;
return -1;
}
if (!*lineptr) {
*n = MIN_CHUNK;
*lineptr = (char *) malloc(*n);
if (!*lineptr) {
errno = ENOMEM;
return -1;
}
}
nchars_avail = *n;
read_pos = *lineptr;
while (1) {
int save_errno;
int c = getc(stream);
save_errno = errno;
if (nchars_avail < 2) {
if (*n > MIN_CHUNK)
*n *= 2;
else
*n += MIN_CHUNK;
nchars_avail = *n + *lineptr - read_pos;
*lineptr = (char *) realloc(*lineptr, *n);
if (!*lineptr) {
errno = ENOMEM;
return -1;
}
read_pos = *n - nchars_avail + *lineptr;
}
if (ferror(stream)) {
errno = save_errno;
return -1;
}
if (c == EOF) {
if (read_pos == *lineptr)
return -1;
else
break;
}
*read_pos++ = c;
nchars_avail--;
if (c == delim)
break;
}
*read_pos = '\0';
return read_pos - *lineptr;
}
ssize_t __getline(char **lineptr, size_t *n, FILE *stream) {
return __getdelim(lineptr, n, '\n', stream);
}
int exec_command(exec_t &exec) { int exec_command(exec_t &exec) {
int pipefd[2] = {-1, -1}, outfd = -1; int pipefd[2] = {-1, -1}, outfd = -1;

View File

@ -0,0 +1,139 @@
/*
* Host all missing/incomplete implementation in bionic
* Copied from various sources
* */
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <mntent.h>
#include "missing.h"
/* Original source: https://opensource.apple.com/source/cvs/cvs-19/cvs/lib/getline.c
* License: GPL 2 or later
* Adjusted to match POSIX */
#define MIN_CHUNK 64
ssize_t __getdelim(char **lineptr, size_t *n, int delim, FILE *stream) {
size_t nchars_avail;
char *read_pos;
if (!lineptr || !n || !stream) {
errno = EINVAL;
return -1;
}
if (!*lineptr) {
*n = MIN_CHUNK;
*lineptr = (char *) malloc(*n);
if (!*lineptr) {
errno = ENOMEM;
return -1;
}
}
nchars_avail = *n;
read_pos = *lineptr;
for (;;) {
int save_errno;
int c = getc(stream);
save_errno = errno;
if (nchars_avail < 2) {
if (*n > MIN_CHUNK)
*n *= 2;
else
*n += MIN_CHUNK;
nchars_avail = *n + *lineptr - read_pos;
*lineptr = (char *) realloc(*lineptr, *n);
if (!*lineptr) {
errno = ENOMEM;
return -1;
}
read_pos = *n - nchars_avail + *lineptr;
}
if (ferror(stream)) {
errno = save_errno;
return -1;
}
if (c == EOF) {
if (read_pos == *lineptr)
return -1;
else
break;
}
*read_pos++ = c;
nchars_avail--;
if (c == delim)
break;
}
*read_pos = '\0';
return read_pos - *lineptr;
}
ssize_t __getline(char **lineptr, size_t *n, FILE *stream) {
return getdelim(lineptr, n, '\n', stream);
}
/* mntent functions are copied from AOSP libc/bionic/mntent.cpp */
struct mntent *__getmntent_r(FILE* fp, struct mntent* e, char* buf, int buf_len) {
memset(e, 0, sizeof(*e));
while (fgets(buf, buf_len, fp) != nullptr) {
// Entries look like "proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0".
// That is: mnt_fsname mnt_dir mnt_type mnt_opts 0 0.
int fsname0, fsname1, dir0, dir1, type0, type1, opts0, opts1;
if (sscanf(buf, " %n%*s%n %n%*s%n %n%*s%n %n%*s%n %d %d",
&fsname0, &fsname1, &dir0, &dir1, &type0, &type1, &opts0, &opts1,
&e->mnt_freq, &e->mnt_passno) == 2) {
e->mnt_fsname = &buf[fsname0];
buf[fsname1] = '\0';
e->mnt_dir = &buf[dir0];
buf[dir1] = '\0';
e->mnt_type = &buf[type0];
buf[type1] = '\0';
e->mnt_opts = &buf[opts0];
buf[opts1] = '\0';
return e;
}
}
return nullptr;
}
FILE *__setmntent(const char* path, const char* mode) {
return fopen(path, mode);
}
int __endmntent(FILE* fp) {
if (fp != nullptr) {
fclose(fp);
}
return 1;
}
char *__hasmntopt(const struct mntent* mnt, const char* opt) {
char* token = mnt->mnt_opts;
char* const end = mnt->mnt_opts + strlen(mnt->mnt_opts);
const size_t optLen = strlen(opt);
while (token) {
char* const tokenEnd = token + optLen;
if (tokenEnd > end) break;
if (memcmp(token, opt, optLen) == 0 &&
(*tokenEnd == '\0' || *tokenEnd == ',' || *tokenEnd == '=')) {
return token;
}
token = strchr(token, ',');
if (token) token++;
}
return nullptr;
}

View File

@ -20,14 +20,13 @@
#include <sys/mount.h> #include <sys/mount.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/sendfile.h> #include <sys/sendfile.h>
#include <sys/syscall.h>
#include <logging.h> #include <logging.h>
#include <utils.h> #include <utils.h>
FILE *xfopen(const char *pathname, const char *mode) { FILE *xfopen(const char *pathname, const char *mode) {
FILE *fp = fopen(pathname, mode); FILE *fp = fopen(pathname, mode);
if (fp == NULL) { if (fp == nullptr) {
PLOGE("fopen: %s", pathname); PLOGE("fopen: %s", pathname);
} }
return fp; return fp;
@ -35,7 +34,7 @@ FILE *xfopen(const char *pathname, const char *mode) {
FILE *xfdopen(int fd, const char *mode) { FILE *xfdopen(int fd, const char *mode) {
FILE *fp = fdopen(fd, mode); FILE *fp = fdopen(fd, mode);
if (fp == NULL) { if (fp == nullptr) {
PLOGE("fopen"); PLOGE("fopen");
} }
return fp; return fp;
@ -100,7 +99,7 @@ int xpipe2(int pipefd[2], int flags) {
} }
int xsetns(int fd, int nstype) { int xsetns(int fd, int nstype) {
int ret = (int) syscall(__NR_setns, fd, nstype); int ret = setns(fd, nstype);
if (ret == -1) { if (ret == -1) {
PLOGE("setns"); PLOGE("setns");
} }
@ -108,7 +107,7 @@ int xsetns(int fd, int nstype) {
} }
int xunshare(int flags) { int xunshare(int flags) {
int ret = (int) syscall(__NR_unshare, flags); int ret = unshare(flags);
if (ret == -1) { if (ret == -1) {
PLOGE("unshare"); PLOGE("unshare");
} }
@ -117,7 +116,7 @@ int xunshare(int flags) {
DIR *xopendir(const char *name) { DIR *xopendir(const char *name) {
DIR *d = opendir(name); DIR *d = opendir(name);
if (d == NULL) { if (d == nullptr) {
PLOGE("opendir: %s", name); PLOGE("opendir: %s", name);
} }
return d; return d;
@ -125,7 +124,7 @@ DIR *xopendir(const char *name) {
DIR *xfdopendir(int fd) { DIR *xfdopendir(int fd) {
DIR *d = fdopendir(fd); DIR *d = fdopendir(fd);
if (d == NULL) { if (d == nullptr) {
PLOGE("fdopendir"); PLOGE("fdopendir");
} }
return d; return d;
@ -134,7 +133,7 @@ DIR *xfdopendir(int fd) {
struct dirent *xreaddir(DIR *dirp) { struct dirent *xreaddir(DIR *dirp) {
errno = 0; errno = 0;
struct dirent *e = readdir(dirp); struct dirent *e = readdir(dirp);
if (errno && e == NULL) { if (errno && e == nullptr) {
PLOGE("readdir"); PLOGE("readdir");
} }
return e; return e;
@ -188,7 +187,7 @@ static int accept4_compat(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
} }
int xaccept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) { int xaccept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) {
int fd = (int) syscall(__NR_accept4, sockfd, addr, addrlen, flags); int fd = accept4(sockfd, addr, addrlen, flags);
if (fd == -1) { if (fd == -1) {
if (errno == ENOSYS) if (errno == ENOSYS)
return accept4_compat(sockfd, addr, addrlen, flags); return accept4_compat(sockfd, addr, addrlen, flags);
@ -199,7 +198,7 @@ int xaccept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) {
void *xmalloc(size_t size) { void *xmalloc(size_t size) {
void *p = malloc(size); void *p = malloc(size);
if (p == NULL) { if (p == nullptr) {
PLOGE("malloc"); PLOGE("malloc");
} }
return p; return p;
@ -207,7 +206,7 @@ void *xmalloc(size_t size) {
void *xcalloc(size_t nmemb, size_t size) { void *xcalloc(size_t nmemb, size_t size) {
void *p = calloc(nmemb, size); void *p = calloc(nmemb, size);
if (p == NULL) { if (p == nullptr) {
PLOGE("calloc"); PLOGE("calloc");
} }
return p; return p;
@ -215,7 +214,7 @@ void *xcalloc(size_t nmemb, size_t size) {
void *xrealloc(void *ptr, size_t size) { void *xrealloc(void *ptr, size_t size) {
void *p = realloc(ptr, size); void *p = realloc(ptr, size);
if (p == NULL) { if (p == nullptr) {
PLOGE("realloc"); PLOGE("realloc");
} }
return p; return p;
@ -297,7 +296,7 @@ ssize_t xreadlink(const char *pathname, char *buf, size_t bufsiz) {
} }
ssize_t xreadlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz) { ssize_t xreadlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz) {
ssize_t ret = syscall(__NR_readlinkat, dirfd, pathname, buf, bufsiz); ssize_t ret = readlinkat(dirfd, pathname, buf, bufsiz);
if (ret == -1) { if (ret == -1) {
PLOGE("readlinkat %s", pathname); PLOGE("readlinkat %s", pathname);
} else { } else {
@ -315,7 +314,7 @@ int xsymlink(const char *target, const char *linkpath) {
} }
int xsymlinkat(const char *target, int newdirfd, const char *linkpath) { int xsymlinkat(const char *target, int newdirfd, const char *linkpath) {
int ret = (int) syscall(__NR_symlinkat, target, newdirfd, linkpath); int ret = symlinkat(target, newdirfd, linkpath);
if (ret == -1) { if (ret == -1) {
PLOGE("symlinkat %s->%s", target, linkpath); PLOGE("symlinkat %s->%s", target, linkpath);
} }
@ -323,7 +322,7 @@ int xsymlinkat(const char *target, int newdirfd, const char *linkpath) {
} }
int xlinkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags) { int xlinkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags) {
int ret = (int) syscall(__NR_linkat, olddirfd, oldpath, newdirfd, newpath, flags); int ret = linkat(olddirfd, oldpath, newdirfd, newpath, flags);
if (ret == -1) { if (ret == -1) {
PLOGE("linkat %s->%s", oldpath, newpath); PLOGE("linkat %s->%s", oldpath, newpath);
} }
@ -422,7 +421,7 @@ int xpoll(struct pollfd *fds, nfds_t nfds, int timeout) {
} }
int xinotify_init1(int flags) { int xinotify_init1(int flags) {
int ret = syscall(__NR_inotify_init1, flags); int ret = inotify_init1(flags);
if (ret == -1) { if (ret == -1) {
PLOGE("inotify_init1"); PLOGE("inotify_init1");
} }