GK SDK 源码库: XMIPCLinuxV100R005C00SPC030 (kernel/tools/open_source excluded)
This commit is contained in:
@@ -0,0 +1,719 @@
|
||||
/* portability.c - code to workaround the deficiencies of various platforms.
|
||||
*
|
||||
* Copyright 2012 Rob Landley <rob@landley.net>
|
||||
* Copyright 2012 Georgi Chorbadzhiyski <gf@unixsol.org>
|
||||
*/
|
||||
|
||||
#include "toys.h"
|
||||
|
||||
// We can't fork() on nommu systems, and vfork() requires an exec() or exit()
|
||||
// before resuming the parent (because they share a heap until then). And no,
|
||||
// we can't implement our own clone() call that does the equivalent of fork()
|
||||
// because nommu heaps use physical addresses so if we copy the heap all our
|
||||
// pointers are wrong. (You need an mmu in order to map two heaps to the same
|
||||
// address range without interfering with each other.) In the absence of
|
||||
// a portable way to tell malloc() to start a new heap without freeing the old
|
||||
// one, you pretty much need the exec().)
|
||||
|
||||
// So we exec ourselves (via /proc/self/exe, if anybody knows a way to
|
||||
// re-exec self without depending on the filesystem, I'm all ears),
|
||||
// and use the arguments to signal reentry.
|
||||
|
||||
#if CFG_TOYBOX_FORK
|
||||
pid_t xfork(void)
|
||||
{
|
||||
pid_t pid = fork();
|
||||
|
||||
if (pid < 0) perror_exit("fork");
|
||||
|
||||
return pid;
|
||||
}
|
||||
#endif
|
||||
|
||||
int xgetrandom(void *buf, unsigned buflen, unsigned flags)
|
||||
{
|
||||
int fd;
|
||||
|
||||
// Linux keeps getrandom() in <sys/random.h> and getentropy() in <unistd.h>
|
||||
// BSD/macOS only has getentropy(), but it's in <sys/random.h> (to be fair,
|
||||
// they were there first). getrandom() and getentropy() both went into glibc
|
||||
// in the same release (2.25 in 2017), so this test still works.
|
||||
#if __has_include(<sys/random.h>) && !defined(__UCLIBC__)
|
||||
if (!getentropy(buf, buflen)) return 1;
|
||||
if (errno!=ENOSYS && !(flags&WARN_ONLY)) perror_exit("getrandom");
|
||||
#endif
|
||||
fd = xopen(flags ? "/dev/random" : "/dev/urandom",O_RDONLY|(flags&WARN_ONLY));
|
||||
if (fd == -1) return 0;
|
||||
xreadall(fd, buf, buflen);
|
||||
close(fd);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Get list of mounted filesystems, including stat and statvfs info.
|
||||
// Returns a reversed list, which is good for finding overmounts and such.
|
||||
|
||||
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
|
||||
|
||||
#include <sys/mount.h>
|
||||
|
||||
struct mtab_list *xgetmountlist(char *path)
|
||||
{
|
||||
struct mtab_list *mtlist = 0, *mt;
|
||||
struct statfs *entries;
|
||||
int i, count;
|
||||
|
||||
if (path) error_exit("xgetmountlist");
|
||||
if ((count = getmntinfo(&entries, 0)) == 0) perror_exit("getmntinfo");
|
||||
|
||||
// The "test" part of the loop is done before the first time through and
|
||||
// again after each "increment", so putting the actual load there avoids
|
||||
// duplicating it. If the load was NULL, the loop stops.
|
||||
|
||||
for (i = 0; i < count; ++i) {
|
||||
struct statfs *me = &entries[i];
|
||||
|
||||
mt = xzalloc(sizeof(struct mtab_list) + strlen(me->f_fstypename) +
|
||||
strlen(me->f_mntonname) + strlen(me->f_mntfromname) + strlen("") + 4);
|
||||
dlist_add_nomalloc((void *)&mtlist, (void *)mt);
|
||||
|
||||
// Collect details about mounted filesystem.
|
||||
// Don't report errors, just leave data zeroed.
|
||||
stat(me->f_mntonname, &(mt->stat));
|
||||
statvfs(me->f_mntonname, &(mt->statvfs));
|
||||
|
||||
// Remember information from struct statfs.
|
||||
mt->dir = stpcpy(mt->type, me->f_fstypename)+1;
|
||||
mt->device = stpcpy(mt->dir, me->f_mntonname)+1;
|
||||
mt->opts = stpcpy(mt->device, me->f_mntfromname)+1;
|
||||
strcpy(mt->opts, ""); /* TODO: reverse from f_flags? */
|
||||
}
|
||||
|
||||
return mtlist;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <mntent.h>
|
||||
|
||||
static void octal_deslash(char *s)
|
||||
{
|
||||
char *o = s;
|
||||
|
||||
while (*s) {
|
||||
if (*s == '\\') {
|
||||
int i, oct = 0;
|
||||
|
||||
for (i = 1; i < 4; i++) {
|
||||
if (!isdigit(s[i])) break;
|
||||
oct = (oct<<3)+s[i]-'0';
|
||||
}
|
||||
if (i == 4) {
|
||||
*o++ = oct;
|
||||
s += i;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
*o++ = *s++;
|
||||
}
|
||||
|
||||
*o = 0;
|
||||
}
|
||||
|
||||
// Check if this type matches list.
|
||||
// Odd syntax: typelist all yes = if any, typelist all no = if none.
|
||||
|
||||
int mountlist_istype(struct mtab_list *ml, char *typelist)
|
||||
{
|
||||
int len, skip;
|
||||
char *t;
|
||||
|
||||
if (!typelist) return 1;
|
||||
|
||||
// leading "no" indicates whether entire list is inverted
|
||||
skip = strncmp(typelist, "no", 2);
|
||||
|
||||
for (;;) {
|
||||
if (!(t = comma_iterate(&typelist, &len))) break;
|
||||
if (!skip) {
|
||||
// later "no" after first are ignored
|
||||
strstart(&t, "no");
|
||||
if (!strncmp(t, ml->type, len-2)) {
|
||||
skip = 1;
|
||||
break;
|
||||
}
|
||||
} else if (!strncmp(t, ml->type, len) && !ml->type[len]) {
|
||||
skip = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return !skip;
|
||||
}
|
||||
|
||||
struct mtab_list *xgetmountlist(char *path)
|
||||
{
|
||||
struct mtab_list *mtlist = 0, *mt;
|
||||
struct mntent *me;
|
||||
FILE *fp;
|
||||
char *p = path ? path : "/proc/mounts";
|
||||
|
||||
if (!(fp = setmntent(p, "r"))) perror_exit("bad %s", p);
|
||||
|
||||
// The "test" part of the loop is done before the first time through and
|
||||
// again after each "increment", so putting the actual load there avoids
|
||||
// duplicating it. If the load was NULL, the loop stops.
|
||||
|
||||
while ((me = getmntent(fp))) {
|
||||
mt = xzalloc(sizeof(struct mtab_list) + strlen(me->mnt_fsname) +
|
||||
strlen(me->mnt_dir) + strlen(me->mnt_type) + strlen(me->mnt_opts) + 4);
|
||||
dlist_add_nomalloc((void *)&mtlist, (void *)mt);
|
||||
|
||||
// Collect details about mounted filesystem
|
||||
// Don't report errors, just leave data zeroed
|
||||
if (!path) {
|
||||
stat(me->mnt_dir, &(mt->stat));
|
||||
statvfs(me->mnt_dir, &(mt->statvfs));
|
||||
}
|
||||
|
||||
// Remember information from /proc/mounts
|
||||
mt->dir = stpcpy(mt->type, me->mnt_type)+1;
|
||||
mt->device = stpcpy(mt->dir, me->mnt_dir)+1;
|
||||
mt->opts = stpcpy(mt->device, me->mnt_fsname)+1;
|
||||
strcpy(mt->opts, me->mnt_opts);
|
||||
|
||||
octal_deslash(mt->dir);
|
||||
octal_deslash(mt->device);
|
||||
}
|
||||
endmntent(fp);
|
||||
|
||||
return mtlist;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
|
||||
|
||||
#include <sys/event.h>
|
||||
|
||||
struct xnotify *xnotify_init(int max)
|
||||
{
|
||||
struct xnotify *not = xzalloc(sizeof(struct xnotify));
|
||||
|
||||
not->max = max;
|
||||
if ((not->kq = kqueue()) == -1) perror_exit("kqueue");
|
||||
not->paths = xmalloc(max * sizeof(char *));
|
||||
not->fds = xmalloc(max * sizeof(int));
|
||||
|
||||
return not;
|
||||
}
|
||||
|
||||
int xnotify_add(struct xnotify *not, int fd, char *path)
|
||||
{
|
||||
struct kevent event;
|
||||
|
||||
if (not->count == not->max) error_exit("xnotify_add overflow");
|
||||
EV_SET(&event, fd, EVFILT_VNODE, EV_ADD|EV_CLEAR, NOTE_WRITE, 0, NULL);
|
||||
if (kevent(not->kq, &event, 1, NULL, 0, NULL) == -1 || event.flags & EV_ERROR)
|
||||
error_exit("xnotify_add failed on %s", path);
|
||||
not->paths[not->count] = path;
|
||||
not->fds[not->count++] = fd;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int xnotify_wait(struct xnotify *not, char **path)
|
||||
{
|
||||
struct kevent event;
|
||||
int i;
|
||||
|
||||
for (;;) {
|
||||
if (kevent(not->kq, NULL, 0, &event, 1, NULL) != -1) {
|
||||
// We get the fd for free, but still have to search for the path.
|
||||
for (i = 0; i<not->count; i++) if (not->fds[i]==event.ident) {
|
||||
*path = not->paths[i];
|
||||
|
||||
return event.ident;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <sys/inotify.h>
|
||||
|
||||
struct xnotify *xnotify_init(int max)
|
||||
{
|
||||
struct xnotify *not = xzalloc(sizeof(struct xnotify));
|
||||
|
||||
not->max = max;
|
||||
if ((not->kq = inotify_init()) < 0) perror_exit("inotify_init");
|
||||
not->paths = xmalloc(max * sizeof(char *));
|
||||
not->fds = xmalloc(max * 2 * sizeof(int));
|
||||
|
||||
return not;
|
||||
}
|
||||
|
||||
int xnotify_add(struct xnotify *not, int fd, char *path)
|
||||
{
|
||||
int i = 2*not->count;
|
||||
|
||||
if (not->max == not->count) error_exit("xnotify_add overflow");
|
||||
if ((not->fds[i] = inotify_add_watch(not->kq, path, IN_MODIFY))==-1)
|
||||
perror_exit("xnotify_add failed on %s", path);
|
||||
not->fds[i+1] = fd;
|
||||
not->paths[not->count++] = path;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int xnotify_wait(struct xnotify *not, char **path)
|
||||
{
|
||||
struct inotify_event ev;
|
||||
int i;
|
||||
|
||||
for (;;) {
|
||||
if (sizeof(ev)!=read(not->kq, &ev, sizeof(ev))) perror_exit("inotify");
|
||||
|
||||
for (i = 0; i<not->count; i++) if (ev.wd==not->fds[2*i]) {
|
||||
*path = not->paths[i];
|
||||
|
||||
return not->fds[2*i+1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __APPLE__
|
||||
|
||||
ssize_t xattr_get(const char *path, const char *name, void *value, size_t size)
|
||||
{
|
||||
return getxattr(path, name, value, size, 0, 0);
|
||||
}
|
||||
|
||||
ssize_t xattr_lget(const char *path, const char *name, void *value, size_t size)
|
||||
{
|
||||
return getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
|
||||
}
|
||||
|
||||
ssize_t xattr_fget(int fd, const char *name, void *value, size_t size)
|
||||
{
|
||||
return fgetxattr(fd, name, value, size, 0, 0);
|
||||
}
|
||||
|
||||
ssize_t xattr_list(const char *path, char *list, size_t size)
|
||||
{
|
||||
return listxattr(path, list, size, 0);
|
||||
}
|
||||
|
||||
ssize_t xattr_llist(const char *path, char *list, size_t size)
|
||||
{
|
||||
return listxattr(path, list, size, XATTR_NOFOLLOW);
|
||||
}
|
||||
|
||||
ssize_t xattr_flist(int fd, char *list, size_t size)
|
||||
{
|
||||
return flistxattr(fd, list, size, 0);
|
||||
}
|
||||
|
||||
ssize_t xattr_set(const char* path, const char* name,
|
||||
const void* value, size_t size, int flags)
|
||||
{
|
||||
return setxattr(path, name, value, size, 0, flags);
|
||||
}
|
||||
|
||||
ssize_t xattr_lset(const char* path, const char* name,
|
||||
const void* value, size_t size, int flags)
|
||||
{
|
||||
return setxattr(path, name, value, size, 0, flags | XATTR_NOFOLLOW);
|
||||
}
|
||||
|
||||
ssize_t xattr_fset(int fd, const char* name,
|
||||
const void* value, size_t size, int flags)
|
||||
{
|
||||
return fsetxattr(fd, name, value, size, 0, flags);
|
||||
}
|
||||
|
||||
#elif !defined(__FreeBSD__) && !defined(__OpenBSD__)
|
||||
|
||||
ssize_t xattr_get(const char *path, const char *name, void *value, size_t size)
|
||||
{
|
||||
return getxattr(path, name, value, size);
|
||||
}
|
||||
|
||||
ssize_t xattr_lget(const char *path, const char *name, void *value, size_t size)
|
||||
{
|
||||
return lgetxattr(path, name, value, size);
|
||||
}
|
||||
|
||||
ssize_t xattr_fget(int fd, const char *name, void *value, size_t size)
|
||||
{
|
||||
return fgetxattr(fd, name, value, size);
|
||||
}
|
||||
|
||||
ssize_t xattr_list(const char *path, char *list, size_t size)
|
||||
{
|
||||
return listxattr(path, list, size);
|
||||
}
|
||||
|
||||
ssize_t xattr_llist(const char *path, char *list, size_t size)
|
||||
{
|
||||
return llistxattr(path, list, size);
|
||||
}
|
||||
|
||||
ssize_t xattr_flist(int fd, char *list, size_t size)
|
||||
{
|
||||
return flistxattr(fd, list, size);
|
||||
}
|
||||
|
||||
ssize_t xattr_set(const char* path, const char* name,
|
||||
const void* value, size_t size, int flags)
|
||||
{
|
||||
return setxattr(path, name, value, size, flags);
|
||||
}
|
||||
|
||||
ssize_t xattr_lset(const char* path, const char* name,
|
||||
const void* value, size_t size, int flags)
|
||||
{
|
||||
return lsetxattr(path, name, value, size, flags);
|
||||
}
|
||||
|
||||
ssize_t xattr_fset(int fd, const char* name,
|
||||
const void* value, size_t size, int flags)
|
||||
{
|
||||
return fsetxattr(fd, name, value, size, flags);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __APPLE__
|
||||
// In the absence of a mknodat system call, fchdir to dirfd and back
|
||||
// around a regular mknod call...
|
||||
int mknodat(int dirfd, const char *path, mode_t mode, dev_t dev)
|
||||
{
|
||||
int old_dirfd = open(".", O_RDONLY), result;
|
||||
|
||||
if (old_dirfd == -1 || fchdir(dirfd) == -1) return -1;
|
||||
result = mknod(path, mode, dev);
|
||||
if (fchdir(old_dirfd) == -1) perror_exit("mknodat couldn't return");
|
||||
return result;
|
||||
}
|
||||
|
||||
// As of 10.15, macOS offers an fcntl F_PREALLOCATE rather than fallocate()
|
||||
// or posix_fallocate() calls.
|
||||
int posix_fallocate(int fd, off_t offset, off_t length)
|
||||
{
|
||||
int e = errno, result;
|
||||
fstore_t f;
|
||||
|
||||
f.fst_flags = F_ALLOCATEALL;
|
||||
f.fst_posmode = F_PEOFPOSMODE;
|
||||
f.fst_offset = offset;
|
||||
f.fst_length = length;
|
||||
if (fcntl(fd, F_PREALLOCATE, &f) == -1) result = errno;
|
||||
else result = ftruncate(fd, length);
|
||||
errno = e;
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Signals required by POSIX 2008:
|
||||
// http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html
|
||||
|
||||
#define SIGNIFY(x) {SIG##x, #x}
|
||||
|
||||
static const struct signame signames[] = {
|
||||
{0, "0"},
|
||||
// POSIX
|
||||
SIGNIFY(ABRT), SIGNIFY(ALRM), SIGNIFY(BUS),
|
||||
SIGNIFY(FPE), SIGNIFY(HUP), SIGNIFY(ILL), SIGNIFY(INT), SIGNIFY(KILL),
|
||||
SIGNIFY(PIPE), SIGNIFY(QUIT), SIGNIFY(SEGV), SIGNIFY(TERM),
|
||||
SIGNIFY(USR1), SIGNIFY(USR2), SIGNIFY(SYS), SIGNIFY(TRAP),
|
||||
SIGNIFY(VTALRM), SIGNIFY(XCPU), SIGNIFY(XFSZ),
|
||||
// Non-POSIX signals that cause termination
|
||||
SIGNIFY(PROF), SIGNIFY(IO),
|
||||
// signals only present/absent on some targets (mips and macos)
|
||||
#ifdef SIGEMT
|
||||
SIGNIFY(EMT),
|
||||
#endif
|
||||
#ifdef SIGINFO
|
||||
SIGNIFY(INFO),
|
||||
#endif
|
||||
#ifdef SIGPOLL
|
||||
SIGNIFY(POLL),
|
||||
#endif
|
||||
#ifdef SIGPWR
|
||||
SIGNIFY(PWR),
|
||||
#endif
|
||||
#ifdef SIGSTKFLT
|
||||
SIGNIFY(STKFLT),
|
||||
#endif
|
||||
|
||||
// Note: sigatexit relies on all the signals with a default disposition that
|
||||
// terminates the process coming *before* SIGCHLD.
|
||||
|
||||
// POSIX signals that don't cause termination
|
||||
SIGNIFY(CHLD), SIGNIFY(CONT), SIGNIFY(STOP), SIGNIFY(TSTP),
|
||||
SIGNIFY(TTIN), SIGNIFY(TTOU), SIGNIFY(URG),
|
||||
// Non-POSIX signals that don't cause termination
|
||||
SIGNIFY(WINCH),
|
||||
};
|
||||
|
||||
#undef SIGNIFY
|
||||
|
||||
void xsignal_all_killers(void *handler)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 1; signames[i].num != SIGCHLD; i++)
|
||||
if (signames[i].num != SIGKILL) xsignal(signames[i].num, handler);
|
||||
}
|
||||
|
||||
// Convert a string like "9", "KILL", "SIGHUP", or "SIGRTMIN+2" to a number.
|
||||
int sig_to_num(char *sigstr)
|
||||
{
|
||||
int i, offset;
|
||||
char *s;
|
||||
|
||||
// Numeric?
|
||||
offset = estrtol(sigstr, &s, 10);
|
||||
if (!errno && !*s) return offset;
|
||||
|
||||
// Skip leading "SIG".
|
||||
strcasestart(&sigstr, "sig");
|
||||
|
||||
// Named signal?
|
||||
for (i=0; i<ARRAY_LEN(signames); i++)
|
||||
if (!strcasecmp(sigstr, signames[i].name)) return signames[i].num;
|
||||
|
||||
// Real-time signal?
|
||||
#ifdef SIGRTMIN
|
||||
if (strcasestart(&sigstr, "rtmin")) i = SIGRTMIN;
|
||||
else if (strcasestart(&sigstr, "rtmax")) i = SIGRTMAX;
|
||||
else return -1;
|
||||
|
||||
// No offset?
|
||||
if (!*sigstr) return i;
|
||||
|
||||
// We allow any offset that's still a real-time signal: SIGRTMIN+20 is fine.
|
||||
// Others are more restrictive, only accepting what they show with -l.
|
||||
offset = estrtol(sigstr, &s, 10);
|
||||
if (errno || *s) return -1;
|
||||
i += offset;
|
||||
if (i >= SIGRTMIN && i <= SIGRTMAX) return i;
|
||||
#endif
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *num_to_sig(int sig)
|
||||
{
|
||||
int i;
|
||||
|
||||
// A named signal?
|
||||
for (i=0; i<ARRAY_LEN(signames); i++)
|
||||
if (signames[i].num == sig) return signames[i].name;
|
||||
|
||||
// A real-time signal?
|
||||
#ifdef SIGRTMIN
|
||||
if (sig == SIGRTMIN) return "RTMIN";
|
||||
if (sig == SIGRTMAX) return "RTMAX";
|
||||
if (sig > SIGRTMIN && sig < SIGRTMAX) {
|
||||
if (sig-SIGRTMIN <= SIGRTMAX-sig) sprintf(libbuf, "RTMIN+%d", sig-SIGRTMIN);
|
||||
else sprintf(libbuf, "RTMAX-%d", SIGRTMAX-sig);
|
||||
return libbuf;
|
||||
}
|
||||
#endif
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int dev_minor(int dev)
|
||||
{
|
||||
#if defined(__linux__)
|
||||
return ((dev&0xfff00000)>>12)|(dev&0xff);
|
||||
#elif defined(__APPLE__)
|
||||
return dev&0xffffff;
|
||||
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
|
||||
return minor(dev);
|
||||
#else
|
||||
#error
|
||||
#endif
|
||||
}
|
||||
|
||||
int dev_major(int dev)
|
||||
{
|
||||
#if defined(__linux__)
|
||||
return (dev&0xfff00)>>8;
|
||||
#elif defined(__APPLE__)
|
||||
return (dev>>24)&0xff;
|
||||
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
|
||||
return major(dev);
|
||||
#else
|
||||
#error
|
||||
#endif
|
||||
}
|
||||
|
||||
int dev_makedev(int major, int minor)
|
||||
{
|
||||
#if defined(__linux__)
|
||||
return (minor&0xff)|((major&0xfff)<<8)|((minor&0xfff00)<<12);
|
||||
#elif defined(__APPLE__)
|
||||
return (minor&0xffffff)|((major&0xff)<<24);
|
||||
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
|
||||
return makedev(major, minor);
|
||||
#else
|
||||
#error
|
||||
#endif
|
||||
}
|
||||
|
||||
char *fs_type_name(struct statfs *statfs)
|
||||
{
|
||||
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
|
||||
// macOS has an `f_type` field, but assigns values dynamically as filesystems
|
||||
// are registered. They do give you the name directly though, so use that.
|
||||
return statfs->f_fstypename;
|
||||
#else
|
||||
char *s = NULL;
|
||||
struct {unsigned num; char *name;} nn[] = {
|
||||
{0xADFF, "affs"}, {0x5346544e, "ntfs"}, {0x1Cd1, "devpts"},
|
||||
{0x137D, "ext"}, {0xEF51, "ext2"}, {0xEF53, "ext3"},
|
||||
{0x1BADFACE, "bfs"}, {0x9123683E, "btrfs"}, {0x28cd3d45, "cramfs"},
|
||||
{0x3153464a, "jfs"}, {0x7275, "romfs"}, {0x01021994, "tmpfs"},
|
||||
{0x3434, "nilfs"}, {0x6969, "nfs"}, {0x9fa0, "proc"},
|
||||
{0x534F434B, "sockfs"}, {0x62656572, "sysfs"}, {0x517B, "smb"},
|
||||
{0x4d44, "msdos"}, {0x4006, "fat"}, {0x43415d53, "smackfs"},
|
||||
{0x73717368, "squashfs"}
|
||||
};
|
||||
int i;
|
||||
|
||||
for (i=0; i<ARRAY_LEN(nn); i++)
|
||||
if (nn[i].num == statfs->f_type) s = nn[i].name;
|
||||
if (!s) sprintf(s = libbuf, "0x%x", (unsigned)statfs->f_type);
|
||||
return s;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(__APPLE__)
|
||||
#include <sys/disk.h>
|
||||
int get_block_device_size(int fd, unsigned long long* size)
|
||||
{
|
||||
unsigned long block_size, block_count;
|
||||
|
||||
if (!ioctl(fd, DKIOCGETBLOCKSIZE, &block_size) &&
|
||||
!ioctl(fd, DKIOCGETBLOCKCOUNT, &block_count)) {
|
||||
*size = block_count * block_size;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#elif defined(__linux__)
|
||||
int get_block_device_size(int fd, unsigned long long* size)
|
||||
{
|
||||
return (ioctl(fd, BLKGETSIZE64, size) >= 0);
|
||||
}
|
||||
#elif defined(__OpenBSD__)
|
||||
#include <sys/dkio.h>
|
||||
#include <sys/disklabel.h>
|
||||
int get_block_device_size(int fd, unsigned long long* size)
|
||||
{
|
||||
struct disklabel lab;
|
||||
int status = (ioctl(fd, DIOCGDINFO, &lab) >= 0);
|
||||
*size = lab.d_secsize * lab.d_nsectors;
|
||||
return status;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Return bytes copied from in to out. If bytes <0 copy all of in to out.
|
||||
// If consumed isn't null, amount read saved there (return is written or error)
|
||||
long long sendfile_len(int in, int out, long long bytes, long long *consumed)
|
||||
{
|
||||
long long total = 0, len, ww;
|
||||
int try_cfr = 1;
|
||||
|
||||
if (consumed) *consumed = 0;
|
||||
if (in>=0) while (bytes != total) {
|
||||
ww = 0;
|
||||
len = bytes-total;
|
||||
|
||||
errno = 0;
|
||||
if (try_cfr) {
|
||||
if (bytes<0 || bytes>(1<<30)) len = (1<<30);
|
||||
// glibc added this constant in git at the end of 2017, shipped 2018-02.
|
||||
// Android's had the constant for years, but you'll get SIGSYS if you use
|
||||
// this system call before Android U (2023's release).
|
||||
#if defined(__NR_copy_file_range) && !defined(__ANDROID__)
|
||||
len = syscall(__NR_copy_file_range, in, 0, out, 0, len, 0);
|
||||
#else
|
||||
errno = EINVAL;
|
||||
len = -1;
|
||||
#endif
|
||||
if (len < 0 && errno == EINVAL) {
|
||||
try_cfr = 0;
|
||||
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
if (bytes<0 || len>sizeof(libbuf)) len = sizeof(libbuf);
|
||||
ww = len = read(in, libbuf, len);
|
||||
}
|
||||
if (len<1 && errno==EAGAIN) continue;
|
||||
if (len<1) break;
|
||||
if (consumed) *consumed += len;
|
||||
if (ww && writeall(out, libbuf, len) != len) return -1;
|
||||
total += len;
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
#ifdef __APPLE__
|
||||
// The absolute minimum POSIX timer implementation to build timeout(1).
|
||||
// Note that although timeout(1) uses POSIX timers to get the monotonic clock,
|
||||
// that doesn't seem to be an option on macOS (without using other libraries),
|
||||
// so we just mangle that back into a regular setitimer(ITIMER_REAL) call.
|
||||
int timer_create(clock_t c, struct sigevent *se, timer_t *t)
|
||||
{
|
||||
if (se->sigev_notify != SIGEV_SIGNAL || se->sigev_signo != SIGALRM)
|
||||
error_exit("unimplemented");
|
||||
*t = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int timer_settime(timer_t t, int flags, struct itimerspec *new, void *old)
|
||||
{
|
||||
struct itimerval mangled;
|
||||
|
||||
if (flags != 0 || old != 0) error_exit("unimplemented");
|
||||
memset(&mangled, 0, sizeof(mangled));
|
||||
mangled.it_value.tv_sec = new->it_value.tv_sec;
|
||||
mangled.it_value.tv_usec = new->it_value.tv_nsec / 1000;
|
||||
return setitimer(ITIMER_REAL, &mangled, NULL);
|
||||
}
|
||||
// glibc requires -lrt for linux syscalls, which pulls in libgcc_eh.a for
|
||||
// static linking, and gcc 9.3 leaks pthread calls from that breaking the build
|
||||
// These are both just linux syscalls: wrap them ourselves
|
||||
#elif defined(__GLIBC__) && !defined(__UCLIBC__)
|
||||
int timer_create_wrap(clockid_t c, struct sigevent *se, timer_t *t)
|
||||
{
|
||||
// convert overengineered structure to what kernel actually uses
|
||||
struct ksigevent { void *sv; int signo, notify, tid; } kk = {
|
||||
0, se->sigev_signo, se->sigev_notify, 0
|
||||
};
|
||||
int timer;
|
||||
|
||||
if (syscall(SYS_timer_create, c, &kk, &timer)<0) return -1;
|
||||
*t = (timer_t)(long)timer;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int timer_settime_wrap(timer_t t, int flags, struct itimerspec *val,
|
||||
struct itimerspec *old)
|
||||
{
|
||||
return syscall(SYS_timer_settime, t, flags, val, old);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,412 @@
|
||||
// Workarounds for horrible build environment idiosyncrasies.
|
||||
|
||||
// Instead of polluting the code with strange #ifdefs to work around bugs
|
||||
// in specific compiler, library, or OS versions, localize all that here
|
||||
// and in portability.c
|
||||
|
||||
// Always use long file support.
|
||||
// This must come before we #include any system header file to take effect!
|
||||
#define _FILE_OFFSET_BITS 64
|
||||
|
||||
// For musl
|
||||
#define _ALL_SOURCE
|
||||
#include <regex.h>
|
||||
#ifndef REG_STARTEND
|
||||
#define REG_STARTEND 0
|
||||
#endif
|
||||
|
||||
#ifdef __APPLE__
|
||||
// macOS 10.13 doesn't have the POSIX 2008 direct access to timespec in
|
||||
// struct stat, but we can ask it to give us something equivalent...
|
||||
// (This must come before any #include!)
|
||||
#define _DARWIN_C_SOURCE
|
||||
// ...and then use macros to paper over the difference.
|
||||
#define st_atim st_atimespec
|
||||
#define st_ctim st_ctimespec
|
||||
#define st_mtim st_mtimespec
|
||||
#endif
|
||||
|
||||
// Test for gcc (using compiler builtin #define)
|
||||
|
||||
#ifdef __GNUC__
|
||||
#ifndef __clang__
|
||||
#define QUIET = 0 // shut up false positive "may be used uninitialized" warning
|
||||
#else
|
||||
#define QUIET
|
||||
#endif
|
||||
#define printf_format __attribute__((format(printf, 1, 2)))
|
||||
#else
|
||||
#define printf_format
|
||||
#endif
|
||||
|
||||
// This isn't in the spec, but it's how we determine what libc we're using.
|
||||
|
||||
// Types various replacement prototypes need.
|
||||
// This also lets us determine what libc we're using. Systems that
|
||||
// have <features.h> will transitively include it, and ones that don't --
|
||||
// macOS -- won't break.
|
||||
#include <sys/types.h>
|
||||
|
||||
// Various constants old build environments might not have even if kernel does
|
||||
|
||||
#ifndef AT_FDCWD
|
||||
#define AT_FDCWD -100
|
||||
#endif
|
||||
|
||||
#ifndef AT_SYMLINK_NOFOLLOW
|
||||
#define AT_SYMLINK_NOFOLLOW 0x100
|
||||
#endif
|
||||
|
||||
#ifndef AT_REMOVEDIR
|
||||
#define AT_REMOVEDIR 0x200
|
||||
#endif
|
||||
|
||||
#ifndef RLIMIT_RTTIME
|
||||
#define RLIMIT_RTTIME 15
|
||||
#endif
|
||||
|
||||
// Introduced in Linux 3.1
|
||||
#ifndef SEEK_DATA
|
||||
#define SEEK_DATA 3
|
||||
#endif
|
||||
#ifndef SEEK_HOLE
|
||||
#define SEEK_HOLE 4
|
||||
#endif
|
||||
|
||||
// We don't define GNU_dammit because we're not part of the gnu project, and
|
||||
// don't want to get any FSF on us. Unfortunately glibc (gnu libc)
|
||||
// won't give us Linux syscall wrappers without claiming to be part of the
|
||||
// gnu project (because Stallman's "GNU owns Linux" revisionist history
|
||||
// crusade includes the kernel, even though Linux was inspired by Minix).
|
||||
|
||||
// We use most non-posix Linux syscalls directly through the syscall() wrapper,
|
||||
// but even many posix-2008 functions aren't provided by glibc unless you
|
||||
// claim it's in the name of Gnu.
|
||||
|
||||
#if defined(__GLIBC__)
|
||||
// "Function prototypes shall be provided." but aren't.
|
||||
// http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html
|
||||
char *crypt(const char *key, const char *salt);
|
||||
|
||||
// According to posix, #include header, get a function definition. But glibc...
|
||||
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/wcwidth.html
|
||||
#include <wchar.h>
|
||||
int wcwidth(wchar_t wc);
|
||||
|
||||
// see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html
|
||||
#include <time.h>
|
||||
char *strptime(const char *buf, const char *format, struct tm *tm);
|
||||
|
||||
// They didn't like posix basename so they defined another function with the
|
||||
// same name and if you include libgen.h it #defines basename to something
|
||||
// else (where they implemented the real basename), and that define breaks
|
||||
// the table entry for the basename command. They didn't make a new function
|
||||
// with a different name for their new behavior because gnu.
|
||||
//
|
||||
// Solution: don't use their broken header, provide an inline to redirect the
|
||||
// correct name to the broken name.
|
||||
|
||||
char *dirname(char *path);
|
||||
char *__xpg_basename(char *path);
|
||||
#if !defined(__UCLIBC__)
|
||||
static inline char *basename(char *path) { return __xpg_basename(path); }
|
||||
#endif
|
||||
char *strcasestr(const char *haystack, const char *needle);
|
||||
void *memmem(const void *haystack, size_t haystack_length,
|
||||
const void *needle, size_t needle_length);
|
||||
#endif // defined(glibc)
|
||||
|
||||
// getopt_long(), getopt_long_only(), and struct option.
|
||||
#include <getopt.h>
|
||||
|
||||
#if !defined(__GLIBC__) || defined(__UCLIBC__)
|
||||
// POSIX basename.
|
||||
#include <libgen.h>
|
||||
#endif
|
||||
|
||||
// Work out how to do endianness
|
||||
|
||||
#ifdef __APPLE__
|
||||
|
||||
#include <libkern/OSByteOrder.h>
|
||||
|
||||
#ifdef __BIG_ENDIAN__
|
||||
#define IS_BIG_ENDIAN 1
|
||||
#else
|
||||
#define IS_BIG_ENDIAN 0
|
||||
#endif
|
||||
|
||||
#define bswap_16(x) OSSwapInt16(x)
|
||||
#define bswap_32(x) OSSwapInt32(x)
|
||||
#define bswap_64(x) OSSwapInt64(x)
|
||||
|
||||
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
|
||||
|
||||
#include <sys/endian.h>
|
||||
|
||||
#if _BYTE_ORDER == _BIG_ENDIAN
|
||||
#define IS_BIG_ENDIAN 1
|
||||
#else
|
||||
#define IS_BIG_ENDIAN 0
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
#include <byteswap.h>
|
||||
#include <endian.h>
|
||||
|
||||
#if __BYTE_ORDER == __BIG_ENDIAN
|
||||
#define IS_BIG_ENDIAN 1
|
||||
#else
|
||||
#define IS_BIG_ENDIAN 0
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#if IS_BIG_ENDIAN
|
||||
#define IS_LITTLE_ENDIAN 0
|
||||
#define SWAP_BE16(x) (x)
|
||||
#define SWAP_BE32(x) (x)
|
||||
#define SWAP_BE64(x) (x)
|
||||
#define SWAP_LE16(x) bswap_16(x)
|
||||
#define SWAP_LE32(x) bswap_32(x)
|
||||
#define SWAP_LE64(x) bswap_64(x)
|
||||
#else
|
||||
#define IS_LITTLE_ENDIAN 1
|
||||
#define SWAP_BE16(x) bswap_16(x)
|
||||
#define SWAP_BE32(x) bswap_32(x)
|
||||
#define SWAP_BE64(x) bswap_64(x)
|
||||
#define SWAP_LE16(x) (x)
|
||||
#define SWAP_LE32(x) (x)
|
||||
#define SWAP_LE64(x) (x)
|
||||
#endif
|
||||
|
||||
// Linux headers not listed by POSIX or LSB
|
||||
#include <sys/mount.h>
|
||||
#ifdef __linux__
|
||||
#include <sys/statfs.h>
|
||||
#include <sys/swap.h>
|
||||
#include <sys/sysinfo.h>
|
||||
#endif
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include <util.h>
|
||||
#elif !defined(__FreeBSD__) && !defined(__OpenBSD__)
|
||||
#include <pty.h>
|
||||
#else
|
||||
#include <termios.h>
|
||||
#ifndef IUTF8
|
||||
#define IUTF8 0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef __linux__
|
||||
#include <sys/personality.h>
|
||||
#else
|
||||
#define PER_LINUX32 0
|
||||
int personality(int);
|
||||
#endif
|
||||
|
||||
#if defined(__APPLE__) || defined(__linux__)
|
||||
// Linux and macOS has both have getxattr and friends in <sys/xattr.h>, but
|
||||
// they aren't compatible.
|
||||
#include <sys/xattr.h>
|
||||
ssize_t xattr_get(const char *, const char *, void *, size_t);
|
||||
ssize_t xattr_lget(const char *, const char *, void *, size_t);
|
||||
ssize_t xattr_fget(int fd, const char *, void *, size_t);
|
||||
ssize_t xattr_list(const char *, char *, size_t);
|
||||
ssize_t xattr_llist(const char *, char *, size_t);
|
||||
ssize_t xattr_flist(int, char *, size_t);
|
||||
ssize_t xattr_set(const char*, const char*, const void*, size_t, int);
|
||||
ssize_t xattr_lset(const char*, const char*, const void*, size_t, int);
|
||||
ssize_t xattr_fset(int, const char*, const void*, size_t, int);
|
||||
#endif
|
||||
|
||||
#if defined(__APPLE__)
|
||||
// macOS doesn't have these functions, but we can fake them.
|
||||
int mknodat(int, const char*, mode_t, dev_t);
|
||||
int posix_fallocate(int, off_t, off_t);
|
||||
|
||||
// macOS keeps newlocale(3) in the non-POSIX <xlocale.h> rather than <locale.h>.
|
||||
#include <xlocale.h>
|
||||
#endif
|
||||
|
||||
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
|
||||
static inline long statfs_bsize(struct statfs *sf) { return sf->f_iosize; }
|
||||
static inline long statfs_frsize(struct statfs *sf) { return sf->f_bsize; }
|
||||
#else
|
||||
static inline long statfs_bsize(struct statfs *sf) { return sf->f_bsize; }
|
||||
static inline long statfs_frsize(struct statfs *sf) { return sf->f_frsize; }
|
||||
#endif
|
||||
|
||||
|
||||
// Android is missing some headers and functions
|
||||
// "generated/config.h" is included first
|
||||
#if __has_include(<shadow.h>)
|
||||
#include <shadow.h>
|
||||
#endif
|
||||
#if __has_include(<utmpx.h>)
|
||||
#include <utmpx.h>
|
||||
#else
|
||||
struct utmpx {int ut_type;};
|
||||
#define USER_PROCESS 0
|
||||
static inline struct utmpx *getutxent(void) {return 0;}
|
||||
static inline void setutxent(void) {;}
|
||||
static inline void endutxent(void) {;}
|
||||
#endif
|
||||
|
||||
// Some systems don't define O_NOFOLLOW, and it varies by architecture, so...
|
||||
#include <fcntl.h>
|
||||
#ifndef O_NOFOLLOW
|
||||
#define O_NOFOLLOW 0
|
||||
#endif
|
||||
#ifndef O_NOATIME
|
||||
#define O_NOATIME 01000000
|
||||
#endif
|
||||
#ifndef O_CLOEXEC
|
||||
#define O_CLOEXEC 02000000
|
||||
#endif
|
||||
#ifndef O_PATH
|
||||
#define O_PATH 010000000
|
||||
#endif
|
||||
#ifndef SCHED_RESET_ON_FORK
|
||||
#define SCHED_RESET_ON_FORK (1<<30)
|
||||
#endif
|
||||
|
||||
// Glibc won't give you linux-kernel constants unless you say "no, a BUD lite"
|
||||
// even though linux has nothing to do with the FSF and never has.
|
||||
#ifndef F_SETPIPE_SZ
|
||||
#define F_SETPIPE_SZ 1031
|
||||
#endif
|
||||
|
||||
#ifndef F_GETPIPE_SZ
|
||||
#define F_GETPIPE_SZ 1032
|
||||
#endif
|
||||
|
||||
#if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
|
||||
&& __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
|
||||
typedef double FLOAT;
|
||||
#else
|
||||
typedef float FLOAT;
|
||||
#endif
|
||||
|
||||
#ifndef __uClinux__
|
||||
pid_t xfork(void);
|
||||
#endif
|
||||
|
||||
// gratuitously memsets ALL the extra space with zeroes (not just a terminator)
|
||||
// but to make up for it truncating doesn't null terminate the output at all.
|
||||
// There are occasions to use it, but it is NOT A GENERAL PURPOSE FUNCTION.
|
||||
// #define strncpy(...) @@strncpyisbadmmkay@@
|
||||
// strncat writes a null terminator one byte PAST the buffer size it's given.
|
||||
#define strncat(...) strncatisbadmmkay(__VA_ARGS__)
|
||||
|
||||
// Support building the Android tools on glibc, so hermetic AOSP builds can
|
||||
// use toybox before they're ready to switch to host bionic.
|
||||
#ifdef __BIONIC__
|
||||
#include <android/log.h>
|
||||
#else
|
||||
typedef enum android_LogPriority {
|
||||
ANDROID_LOG_UNKNOWN = 0,
|
||||
ANDROID_LOG_DEFAULT,
|
||||
ANDROID_LOG_VERBOSE,
|
||||
ANDROID_LOG_DEBUG,
|
||||
ANDROID_LOG_INFO,
|
||||
ANDROID_LOG_WARN,
|
||||
ANDROID_LOG_ERROR,
|
||||
ANDROID_LOG_FATAL,
|
||||
ANDROID_LOG_SILENT,
|
||||
} android_LogPriority;
|
||||
#endif
|
||||
#if !defined(__BIONIC__) || defined(__ANDROID_NDK__)
|
||||
// Android NDKv18 has liblog.so but not liblog.a for static builds.
|
||||
static inline int stub_out_log_write(int pri, const char *tag, const char *msg)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
#ifdef __ANDROID_NDK__
|
||||
#define __android_log_write(a, b, c) stub_out_log_write(a, b, c)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
// libprocessgroup is an Android platform library not included in the NDK.
|
||||
#if defined(__BIONIC__)
|
||||
#if __has_include(<processgroup/sched_policy.h>)
|
||||
#include <processgroup/sched_policy.h>
|
||||
#define GOT_IT
|
||||
#endif
|
||||
#endif
|
||||
#ifdef GOT_IT
|
||||
#undef GOT_IT
|
||||
#else
|
||||
static inline int get_sched_policy(int tid, void *policy) {return 0;}
|
||||
static inline char *get_sched_policy_name(int policy) {return "unknown";}
|
||||
#endif
|
||||
|
||||
#ifndef SYSLOG_NAMES
|
||||
typedef struct {char *c_name; int c_val;} CODE;
|
||||
extern CODE prioritynames[], facilitynames[];
|
||||
#endif
|
||||
|
||||
#if __has_include (<sys/random.h>)
|
||||
#include <sys/random.h>
|
||||
#endif
|
||||
int xgetrandom(void *buf, unsigned len, unsigned flags);
|
||||
|
||||
// Android's bionic libc doesn't have confstr.
|
||||
#ifdef __BIONIC__
|
||||
#define _CS_PATH 0
|
||||
#define _CS_V7_ENV 1
|
||||
#include <string.h>
|
||||
static inline void confstr(int a, char *b, int c) {strcpy(b, a ? "POSIXLY_CORRECT=1" : "/bin:/usr/bin");}
|
||||
#endif
|
||||
|
||||
// Paper over the differences between BSD kqueue and Linux inotify for tail.
|
||||
|
||||
struct xnotify {
|
||||
char **paths;
|
||||
int max, *fds, count, kq;
|
||||
};
|
||||
|
||||
struct xnotify *xnotify_init(int max);
|
||||
int xnotify_add(struct xnotify *not, int fd, char *path);
|
||||
int xnotify_wait(struct xnotify *not, char **path);
|
||||
|
||||
int sig_to_num(char *s);
|
||||
char *num_to_sig(int sig);
|
||||
|
||||
struct signame {
|
||||
int num;
|
||||
char *name;
|
||||
};
|
||||
void xsignal_all_killers(void *handler);
|
||||
|
||||
// Different OSes encode major/minor device numbers differently.
|
||||
int dev_minor(int dev);
|
||||
int dev_major(int dev);
|
||||
int dev_makedev(int major, int minor);
|
||||
|
||||
char *fs_type_name(struct statfs *statfs);
|
||||
|
||||
int get_block_device_size(int fd, unsigned long long *size);
|
||||
|
||||
#ifdef __APPLE__
|
||||
// Apple doesn't have POSIX timers; this is "just enough" for timeout(1).
|
||||
typedef int timer_t;
|
||||
struct itimerspec {
|
||||
struct timespec it_value;
|
||||
};
|
||||
int timer_create(clock_t c, struct sigevent *se, timer_t *t);
|
||||
int timer_settime(timer_t t, int flags, struct itimerspec *new, void *old);
|
||||
#elif defined(__GLIBC__)
|
||||
// Work around a glibc bug that interacts badly with a gcc bug.
|
||||
#include <syscall.h>
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
int timer_create_wrap(clockid_t c, struct sigevent *se, timer_t *t);
|
||||
#define timer_create(...) timer_create_wrap(__VA_ARGS__)
|
||||
int timer_settime_wrap(timer_t t, int flags, struct itimerspec *val,
|
||||
struct itimerspec *old);
|
||||
#define timer_settime(...) timer_settime_wrap(__VA_ARGS__)
|
||||
#endif
|
||||
Reference in New Issue
Block a user