GK SDK 源码库: XMIPCLinuxV100R005C00SPC030 (kernel/tools/open_source excluded)
This commit is contained in:
Symlink
+1
@@ -0,0 +1 @@
|
||||
../../build/initrd.mk
|
||||
@@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env make
|
||||
###############################################################################
|
||||
#
|
||||
# Create By Lane 2024/1/29
|
||||
#
|
||||
###############################################################################
|
||||
|
||||
# $(call _rt_file,<libc>)
|
||||
_rt_file = $(shell $(CONFIG_XMEDIA_USR_ARM_TOOLCHAINS)-gcc \
|
||||
$(SDK_USR_CFLAGS) -print-file-name=$(1))
|
||||
|
||||
# $(call rt_file,<libc1, libc2, ...>)
|
||||
rt_file = $(foreach lib,$(1),$(shell if [ -f "$(call _rt_file,$(lib))" ]; \
|
||||
then ( echo "$(call _rt_file,$(lib))"; ) else ( echo ""; ) fi ))
|
||||
|
||||
# $(call rt_notdir,<libc1, libc2, ...>)
|
||||
rt_notdir = $(if $(strip $(call rt_file,$(1))),\
|
||||
$(notdir $(call rt_file,$(1))),\
|
||||
$(info *** $(CONFIG_XMEDIA_ARM_TOOLCHAINS_NAME)-gcc $(SDK_USR_CFLAGS)) \
|
||||
$(error not found $(1), please check compiler and cflags))
|
||||
|
||||
# libc.so
|
||||
# RT-LIBS = $(call rt_notdir,libc.so) //拷贝c库会增加initrd的大小,toybox采用静态链接, 不需要动态c库,
|
||||
# 根据实际业务app需求来确定是否拷贝动态c库。
|
||||
|
||||
initrd_unstrip: $(INITRD_PREFIX)/init \
|
||||
$(addprefix $(INITRD_PREFIX)/lib/,$(RT-LIBS))
|
||||
|
||||
$(INITRD_PREFIX)/init:
|
||||
$(call mkdir,$(@D))
|
||||
tar xzf $(INITRD_SRCDIR)/scripts/initramdisk.tgz -C $(INITRD_PREFIX)/..
|
||||
touch $@
|
||||
|
||||
# To compatible with old dynamic lib linked with uClibc, create a link ld-uClibc.so.0.
|
||||
$(INITRD_PREFIX)/lib/libc.so:
|
||||
@$(call mkdir,$(@D))
|
||||
DIR=$(dir $(call rt_file,$(@F))); \
|
||||
cp -avf $${DIR}/*.so $(@D)
|
||||
cd $(@D);ln -s $(notdir $@) ld-musl-arm.so.1;chmod +x $@;cd -
|
||||
cd $(@D);ln -s $(notdir $@) ld-uClibc.so.0;chmod +x $@;cd -
|
||||
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env make
|
||||
###############################################################################
|
||||
#
|
||||
# Create By Lane 2024/1/29
|
||||
#
|
||||
###############################################################################
|
||||
|
||||
TOYBOX := toybox-0.8.8
|
||||
|
||||
TOYBOX_PREFIX = $(INITRD_BUILDDIR)/$(TOYBOX)/.install
|
||||
|
||||
MKFLAGS_TOYBOX = $(MKFLAGS)
|
||||
MKFLAGS_TOYBOX += CROSS_COMPILE=$(CONFIG_XMEDIA_USR_ARM_TOOLCHAINS)-
|
||||
MKFLAGS_TOYBOX += PREFIX=$(TOYBOX_PREFIX)
|
||||
MKFLAGS_TOYBOX += CFLAGS="-mthumb -Os" LDFLAGS="--static" HOSTCC=gcc CC=gcc STRIP=strip
|
||||
|
||||
initrd_unstrip: $(INITRD_PREFIX)/usr/bin/toybox
|
||||
|
||||
clean: $(TOYBOX).clean
|
||||
|
||||
$(TOYBOX).clean:
|
||||
$(AT)rm -rf $(INITRD_BUILDDIR)/$(TOYBOX)
|
||||
|
||||
$(INITRD_PREFIX)/usr/bin/toybox: $(INITRD_BUILDDIR)/$(TOYBOX)/.built
|
||||
$(call mkdir,$(@D))
|
||||
cp -arf $(TOYBOX_PREFIX)/* $(INITRD_PREFIX)
|
||||
touch $@
|
||||
|
||||
$(INITRD_BUILDDIR)/$(TOYBOX)/.built: $(INITRD_BUILDDIR)/$(TOYBOX)/.extracted
|
||||
make -C $(<D) $(MKFLAGS_TOYBOX) toybox
|
||||
make -C $(<D) $(MKFLAGS_TOYBOX) install
|
||||
touch $@
|
||||
|
||||
$(INITRD_BUILDDIR)/$(TOYBOX)/.extracted:
|
||||
$(call mkdir,$(@D))
|
||||
tar -xf $(OPEN_SOURCE_DIR)/toybox/$(TOYBOX).tar.gz -C $(INITRD_BUILDDIR)
|
||||
cp -arf $(INITRD_SRCDIR)/toybox/$(CONFIG_XMEDIA_TOYBOX_CFG_NAME).patch/* $(@D)
|
||||
cp -f $(INITRD_SRCDIR)/toybox/$(CONFIG_XMEDIA_TOYBOX_CFG_NAME).config $(@D)/.config
|
||||
touch $@
|
||||
+381
@@ -0,0 +1,381 @@
|
||||
#
|
||||
# Automatically generated make config: don't edit
|
||||
# ToyBox version: KCONFIG_VERSION
|
||||
# Fri Jan 6 06:19:23 2023
|
||||
#
|
||||
# CONFIG_TOYBOX_ON_ANDROID is not set
|
||||
CONFIG_TOYBOX_FORK=y
|
||||
|
||||
#
|
||||
# Posix commands
|
||||
#
|
||||
# CONFIG_BASENAME is not set
|
||||
# CONFIG_CAL is not set
|
||||
CONFIG_CAT=y
|
||||
# CONFIG_CHGRP is not set
|
||||
# CONFIG_CHOWN is not set
|
||||
# CONFIG_CHMOD is not set
|
||||
# CONFIG_CKSUM is not set
|
||||
# CONFIG_CRC32 is not set
|
||||
# CONFIG_CMP is not set
|
||||
# CONFIG_COMM is not set
|
||||
# CONFIG_CP is not set
|
||||
# CONFIG_MV is not set
|
||||
# CONFIG_INSTALL is not set
|
||||
# CONFIG_CPIO is not set
|
||||
# CONFIG_CUT is not set
|
||||
# CONFIG_DATE is not set
|
||||
# CONFIG_DF is not set
|
||||
# CONFIG_DIRNAME is not set
|
||||
# CONFIG_DU is not set
|
||||
CONFIG_ECHO=y
|
||||
# CONFIG_ENV is not set
|
||||
# CONFIG_EXPAND is not set
|
||||
# CONFIG_FALSE is not set
|
||||
# CONFIG_FILE is not set
|
||||
# CONFIG_FIND is not set
|
||||
# CONFIG_GETCONF is not set
|
||||
# CONFIG_GREP is not set
|
||||
# CONFIG_EGREP is not set
|
||||
# CONFIG_FGREP is not set
|
||||
# CONFIG_HEAD is not set
|
||||
# CONFIG_ICONV is not set
|
||||
# CONFIG_ID is not set
|
||||
# CONFIG_ID_Z is not set
|
||||
# CONFIG_GROUPS is not set
|
||||
# CONFIG_LOGNAME is not set
|
||||
# CONFIG_WHOAMI is not set
|
||||
# CONFIG_KILL is not set
|
||||
# CONFIG_KILLALL5 is not set
|
||||
# CONFIG_LINK is not set
|
||||
# CONFIG_LN is not set
|
||||
# CONFIG_LOGGER is not set
|
||||
CONFIG_LS=y
|
||||
# CONFIG_MKDIR is not set
|
||||
# CONFIG_MKDIR_Z is not set
|
||||
# CONFIG_MKFIFO is not set
|
||||
# CONFIG_MKFIFO_Z is not set
|
||||
# CONFIG_NICE is not set
|
||||
# CONFIG_NL is not set
|
||||
# CONFIG_NOHUP is not set
|
||||
# CONFIG_OD is not set
|
||||
# CONFIG_PASTE is not set
|
||||
# CONFIG_PATCH is not set
|
||||
# CONFIG_PRINTF is not set
|
||||
# CONFIG_PS is not set
|
||||
# CONFIG_TOP is not set
|
||||
# CONFIG_IOTOP is not set
|
||||
# CONFIG_PGREP is not set
|
||||
# CONFIG_PKILL is not set
|
||||
# CONFIG_PWD is not set
|
||||
# CONFIG_RENICE is not set
|
||||
CONFIG_RM=y
|
||||
# CONFIG_RMDIR is not set
|
||||
CONFIG_SED=y
|
||||
# CONFIG_SLEEP is not set
|
||||
# CONFIG_SORT is not set
|
||||
# CONFIG_SORT_FLOAT is not set
|
||||
# CONFIG_SPLIT is not set
|
||||
# CONFIG_STRINGS is not set
|
||||
# CONFIG_TAIL is not set
|
||||
# CONFIG_TAR is not set
|
||||
# CONFIG_TEE is not set
|
||||
CONFIG_TEST=y
|
||||
CONFIG_TEST_GLUE=y
|
||||
# CONFIG_TIME is not set
|
||||
# CONFIG_TOUCH is not set
|
||||
# CONFIG_TRUE is not set
|
||||
# CONFIG_TTY is not set
|
||||
# CONFIG_ULIMIT is not set
|
||||
# CONFIG_ARCH is not set
|
||||
# CONFIG_LINUX32 is not set
|
||||
# CONFIG_UNAME is not set
|
||||
# CONFIG_UNIQ is not set
|
||||
# CONFIG_UNLINK is not set
|
||||
# CONFIG_UUDECODE is not set
|
||||
# CONFIG_UUENCODE is not set
|
||||
# CONFIG_WC is not set
|
||||
# CONFIG_WHO is not set
|
||||
# CONFIG_XARGS is not set
|
||||
|
||||
#
|
||||
# Pending (unfinished) commands
|
||||
#
|
||||
# CONFIG_ARP is not set
|
||||
# CONFIG_ARPING is not set
|
||||
# CONFIG_BC is not set
|
||||
# CONFIG_BOOTCHARTD is not set
|
||||
# CONFIG_BRCTL is not set
|
||||
# CONFIG_CHSH is not set
|
||||
# CONFIG_CROND is not set
|
||||
# CONFIG_CRONTAB is not set
|
||||
# CONFIG_DD is not set
|
||||
# CONFIG_DHCP is not set
|
||||
# CONFIG_DHCP6 is not set
|
||||
# CONFIG_DHCPD is not set
|
||||
# CONFIG_DEBUG_DHCP is not set
|
||||
# CONFIG_DIFF is not set
|
||||
# CONFIG_DUMPLEASES is not set
|
||||
# CONFIG_EXPR is not set
|
||||
# CONFIG_FDISK is not set
|
||||
# CONFIG_FOLD is not set
|
||||
# CONFIG_FSCK is not set
|
||||
# CONFIG_GETFATTR is not set
|
||||
# CONFIG_GETOPT is not set
|
||||
# CONFIG_GETTY is not set
|
||||
# CONFIG_GROUPADD is not set
|
||||
# CONFIG_GROUPDEL is not set
|
||||
# CONFIG_HEXDUMP is not set
|
||||
# CONFIG_HD is not set
|
||||
# CONFIG_INIT is not set
|
||||
# CONFIG_IP is not set
|
||||
# CONFIG_IPCRM is not set
|
||||
# CONFIG_IPCS is not set
|
||||
# CONFIG_KLOGD is not set
|
||||
# CONFIG_KLOGD_SOURCE_RING_BUFFER is not set
|
||||
# CONFIG_LAST is not set
|
||||
# CONFIG_LSOF is not set
|
||||
# CONFIG_MAN is not set
|
||||
# CONFIG_MDEV is not set
|
||||
# CONFIG_MDEV_CONF is not set
|
||||
# CONFIG_MKE2FS is not set
|
||||
# CONFIG_MKE2FS_JOURNAL is not set
|
||||
# CONFIG_MKE2FS_GEN is not set
|
||||
# CONFIG_MKE2FS_LABEL is not set
|
||||
# CONFIG_MKE2FS_EXTENDED is not set
|
||||
# CONFIG_MODPROBE is not set
|
||||
# CONFIG_MORE is not set
|
||||
# CONFIG_ROUTE is not set
|
||||
CONFIG_SH=y
|
||||
# CONFIG_CD is not set
|
||||
# CONFIG_DECLARE is not set
|
||||
# CONFIG_EXIT is not set
|
||||
# CONFIG_SET is not set
|
||||
# CONFIG_UNSET is not set
|
||||
# CONFIG_EVAL is not set
|
||||
# CONFIG_EXEC is not set
|
||||
# CONFIG_EXPORT is not set
|
||||
# CONFIG_JOBS is not set
|
||||
# CONFIG_LOCAL is not set
|
||||
# CONFIG_SHIFT is not set
|
||||
# CONFIG_SOURCE is not set
|
||||
# CONFIG_WAIT is not set
|
||||
# CONFIG_STRACE is not set
|
||||
# CONFIG_STTY is not set
|
||||
# CONFIG_SULOGIN is not set
|
||||
# CONFIG_SYSLOGD is not set
|
||||
# CONFIG_TCPSVD is not set
|
||||
# CONFIG_TELNET is not set
|
||||
# CONFIG_TELNETD is not set
|
||||
# CONFIG_TFTP is not set
|
||||
# CONFIG_TFTPD is not set
|
||||
# CONFIG_TR is not set
|
||||
# CONFIG_TRACEROUTE is not set
|
||||
# CONFIG_USERADD is not set
|
||||
# CONFIG_USERDEL is not set
|
||||
# CONFIG_VI is not set
|
||||
# CONFIG_XZCAT is not set
|
||||
|
||||
#
|
||||
# Other commands
|
||||
#
|
||||
# CONFIG_ACPI is not set
|
||||
# CONFIG_ASCII is not set
|
||||
# CONFIG_UNICODE is not set
|
||||
# CONFIG_BASE64 is not set
|
||||
# CONFIG_BASE32 is not set
|
||||
# CONFIG_BLKDISCARD is not set
|
||||
# CONFIG_BLKID is not set
|
||||
# CONFIG_FSTYPE is not set
|
||||
# CONFIG_BLOCKDEV is not set
|
||||
# CONFIG_BUNZIP2 is not set
|
||||
# CONFIG_BZCAT is not set
|
||||
# CONFIG_CHCON is not set
|
||||
# CONFIG_CHROOT is not set
|
||||
# CONFIG_CHRT is not set
|
||||
# CONFIG_CLEAR is not set
|
||||
# CONFIG_COUNT is not set
|
||||
# CONFIG_DEVMEM is not set
|
||||
# CONFIG_DOS2UNIX is not set
|
||||
# CONFIG_UNIX2DOS is not set
|
||||
# CONFIG_EJECT is not set
|
||||
# CONFIG_FACTOR is not set
|
||||
# CONFIG_FALLOCATE is not set
|
||||
# CONFIG_FLOCK is not set
|
||||
# CONFIG_FMT is not set
|
||||
# CONFIG_FREE is not set
|
||||
# CONFIG_FREERAMDISK is not set
|
||||
# CONFIG_FSFREEZE is not set
|
||||
# CONFIG_FSYNC is not set
|
||||
# CONFIG_GPIODETECT is not set
|
||||
# CONFIG_GPIOFIND is not set
|
||||
# CONFIG_GPIOINFO is not set
|
||||
# CONFIG_GPIOGET is not set
|
||||
# CONFIG_GPIOSET is not set
|
||||
# CONFIG_HELP is not set
|
||||
# CONFIG_HEXEDIT is not set
|
||||
# CONFIG_HWCLOCK is not set
|
||||
# CONFIG_I2CDETECT is not set
|
||||
# CONFIG_I2CDUMP is not set
|
||||
# CONFIG_I2CGET is not set
|
||||
# CONFIG_I2CSET is not set
|
||||
# CONFIG_INOTIFYD is not set
|
||||
CONFIG_INSMOD=y
|
||||
# CONFIG_IONICE is not set
|
||||
# CONFIG_IORENICE is not set
|
||||
# CONFIG_LOGIN is not set
|
||||
# CONFIG_LOSETUP is not set
|
||||
# CONFIG_LSATTR is not set
|
||||
# CONFIG_CHATTR is not set
|
||||
# CONFIG_LSMOD is not set
|
||||
# CONFIG_LSPCI is not set
|
||||
# CONFIG_LSUSB is not set
|
||||
# CONFIG_MAKEDEVS is not set
|
||||
# CONFIG_MCOOKIE is not set
|
||||
# CONFIG_MIX is not set
|
||||
# CONFIG_MKPASSWD is not set
|
||||
# CONFIG_MKSWAP is not set
|
||||
# CONFIG_MODINFO is not set
|
||||
# CONFIG_MOUNTPOINT is not set
|
||||
# CONFIG_NBD_CLIENT is not set
|
||||
# CONFIG_UNSHARE is not set
|
||||
# CONFIG_NSENTER is not set
|
||||
# CONFIG_ONEIT is not set
|
||||
# CONFIG_OPENVT is not set
|
||||
# CONFIG_CHVT is not set
|
||||
# CONFIG_DEALLOCVT is not set
|
||||
# CONFIG_PARTPROBE is not set
|
||||
# CONFIG_PIVOT_ROOT is not set
|
||||
# CONFIG_PMAP is not set
|
||||
# CONFIG_PRINTENV is not set
|
||||
# CONFIG_PWDX is not set
|
||||
# CONFIG_PWGEN is not set
|
||||
# CONFIG_READAHEAD is not set
|
||||
# CONFIG_READELF is not set
|
||||
# CONFIG_READLINK is not set
|
||||
# CONFIG_REALPATH is not set
|
||||
# CONFIG_REBOOT is not set
|
||||
# CONFIG_RESET is not set
|
||||
# CONFIG_REV is not set
|
||||
# CONFIG_RMMOD is not set
|
||||
# CONFIG_RTCWAKE is not set
|
||||
# CONFIG_SETFATTR is not set
|
||||
# CONFIG_SETSID is not set
|
||||
# CONFIG_SHA3SUM is not set
|
||||
# CONFIG_SHRED is not set
|
||||
# CONFIG_STAT is not set
|
||||
# CONFIG_SWAPOFF is not set
|
||||
# CONFIG_SWAPON is not set
|
||||
# CONFIG_SWITCH_ROOT is not set
|
||||
# CONFIG_SYSCTL is not set
|
||||
# CONFIG_TAC is not set
|
||||
# CONFIG_NPROC is not set
|
||||
# CONFIG_TASKSET is not set
|
||||
# CONFIG_TIMEOUT is not set
|
||||
# CONFIG_TRUNCATE is not set
|
||||
# CONFIG_UCLAMPSET is not set
|
||||
# CONFIG_UPTIME is not set
|
||||
# CONFIG_USLEEP is not set
|
||||
# CONFIG_UUIDGEN is not set
|
||||
# CONFIG_VCONFIG is not set
|
||||
# CONFIG_VMSTAT is not set
|
||||
# CONFIG_W is not set
|
||||
# CONFIG_WATCH is not set
|
||||
# CONFIG_WATCHDOG is not set
|
||||
# CONFIG_WHICH is not set
|
||||
# CONFIG_XXD is not set
|
||||
# CONFIG_YES is not set
|
||||
|
||||
#
|
||||
# Networking commands
|
||||
#
|
||||
# CONFIG_FTPGET is not set
|
||||
# CONFIG_FTPPUT is not set
|
||||
# CONFIG_HOST is not set
|
||||
# CONFIG_HTTPD is not set
|
||||
# CONFIG_IFCONFIG is not set
|
||||
# CONFIG_MICROCOM is not set
|
||||
# CONFIG_NETCAT is not set
|
||||
# CONFIG_NETSTAT is not set
|
||||
# CONFIG_PING is not set
|
||||
# CONFIG_RFKILL is not set
|
||||
# CONFIG_SNTP is not set
|
||||
# CONFIG_TUNCTL is not set
|
||||
# CONFIG_WGET is not set
|
||||
# CONFIG_WGET_LIBTLS is not set
|
||||
|
||||
#
|
||||
# Linux Standard Base commands
|
||||
#
|
||||
# CONFIG_DMESG is not set
|
||||
# CONFIG_GZIP is not set
|
||||
# CONFIG_GUNZIP is not set
|
||||
# CONFIG_ZCAT is not set
|
||||
# CONFIG_HOSTNAME is not set
|
||||
# CONFIG_DNSDOMAINNAME is not set
|
||||
# CONFIG_KILLALL is not set
|
||||
# CONFIG_MD5SUM is not set
|
||||
# CONFIG_SHA1SUM is not set
|
||||
# CONFIG_SHA224SUM is not set
|
||||
# CONFIG_SHA256SUM is not set
|
||||
# CONFIG_SHA384SUM is not set
|
||||
# CONFIG_SHA512SUM is not set
|
||||
# CONFIG_MKNOD is not set
|
||||
# CONFIG_MKNOD_Z is not set
|
||||
# CONFIG_MKTEMP is not set
|
||||
CONFIG_MOUNT=y
|
||||
# CONFIG_PASSWD is not set
|
||||
# CONFIG_PASSWD_SAD is not set
|
||||
# CONFIG_PIDOF is not set
|
||||
# CONFIG_SEQ is not set
|
||||
# CONFIG_SU is not set
|
||||
# CONFIG_SYNC is not set
|
||||
# CONFIG_UMOUNT is not set
|
||||
|
||||
#
|
||||
# Example commands
|
||||
#
|
||||
# CONFIG_DEMO_MANY_OPTIONS is not set
|
||||
# CONFIG_DEMO_NUMBER is not set
|
||||
# CONFIG_DEMO_SCANKEY is not set
|
||||
# CONFIG_DEMO_UTF8TOWC is not set
|
||||
# CONFIG_HELLO is not set
|
||||
# CONFIG_HOSTID is not set
|
||||
# CONFIG_LOGPATH is not set
|
||||
# CONFIG_SKELETON is not set
|
||||
# CONFIG_SKELETON_ALIAS is not set
|
||||
|
||||
#
|
||||
# Android commands
|
||||
#
|
||||
# CONFIG_GETENFORCE is not set
|
||||
# CONFIG_LOAD_POLICY is not set
|
||||
# CONFIG_LOG is not set
|
||||
# CONFIG_RESTORECON is not set
|
||||
# CONFIG_RUNCON is not set
|
||||
# CONFIG_SENDEVENT is not set
|
||||
# CONFIG_SETENFORCE is not set
|
||||
|
||||
#
|
||||
#
|
||||
#
|
||||
|
||||
#
|
||||
# Toybox global settings
|
||||
#
|
||||
CONFIG_TOYBOX=y
|
||||
# CONFIG_TOYBOX_SUID is not set
|
||||
CONFIG_TOYBOX_LSM_NONE=y
|
||||
# CONFIG_TOYBOX_SELINUX is not set
|
||||
# CONFIG_TOYBOX_SMACK is not set
|
||||
# CONFIG_TOYBOX_LIBCRYPTO is not set
|
||||
# CONFIG_TOYBOX_LIBZ is not set
|
||||
# CONFIG_TOYBOX_FLOAT is not set
|
||||
# CONFIG_TOYBOX_HELP is not set
|
||||
# CONFIG_TOYBOX_HELP_DASHDASH is not set
|
||||
# CONFIG_TOYBOX_FREE is not set
|
||||
# CONFIG_TOYBOX_NORECURSE is not set
|
||||
# CONFIG_TOYBOX_DEBUG is not set
|
||||
CONFIG_TOYBOX_UID_SYS=100
|
||||
CONFIG_TOYBOX_UID_USR=500
|
||||
# CONFIG_TOYBOX_FORCE_NOMMU is not set
|
||||
@@ -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
|
||||
@@ -0,0 +1,301 @@
|
||||
/* Toybox infrastructure.
|
||||
*
|
||||
* Copyright 2006 Rob Landley <rob@landley.net>
|
||||
*/
|
||||
|
||||
#include "toys.h"
|
||||
|
||||
// Populate toy_list[].
|
||||
|
||||
#undef NEWTOY
|
||||
#undef OLDTOY
|
||||
#define NEWTOY(name, opts, flags) {#name, name##_main, OPTSTR_##name, flags},
|
||||
#define OLDTOY(name, oldname, flags) \
|
||||
{#name, oldname##_main, OPTSTR_##oldname, flags},
|
||||
|
||||
struct toy_list toy_list[] = {
|
||||
#include "generated/newtoys.h"
|
||||
};
|
||||
|
||||
// global context for this command.
|
||||
|
||||
struct toy_context toys;
|
||||
union global_union this;
|
||||
char *toybox_version = TOYBOX_VERSION, toybuf[4096], libbuf[4096];
|
||||
|
||||
struct toy_list *toy_find(char *name)
|
||||
{
|
||||
int top, bottom, middle;
|
||||
|
||||
if (!CFG_TOYBOX || strchr(name, '/')) return 0;
|
||||
|
||||
// Multiplexer name works as prefix, else skip first entry (it's out of order)
|
||||
if (!toys.which && strstart(&name, toy_list->name)) return toy_list;
|
||||
bottom = 1;
|
||||
|
||||
// Binary search to find this command.
|
||||
top = ARRAY_LEN(toy_list)-1;
|
||||
for (;;) {
|
||||
int result;
|
||||
|
||||
middle = (top+bottom)/2;
|
||||
if (middle<bottom || middle>top) return 0;
|
||||
result = strcmp(name,toy_list[middle].name);
|
||||
if (!result) return toy_list+middle;
|
||||
if (result<0) top = --middle;
|
||||
else bottom = ++middle;
|
||||
}
|
||||
}
|
||||
|
||||
// Figure out whether or not anything is using the option parsing logic,
|
||||
// because the compiler can't figure out whether or not to optimize it away
|
||||
// on its' own. NEED_OPTIONS becomes a constant allowing if() to optimize
|
||||
// stuff out via dead code elimination.
|
||||
|
||||
#undef NEWTOY
|
||||
#undef OLDTOY
|
||||
#define NEWTOY(name, opts, flags) opts ||
|
||||
#define OLDTOY(name, oldname, flags) OPTSTR_##oldname ||
|
||||
static const int NEED_OPTIONS =
|
||||
#include "generated/newtoys.h"
|
||||
0; // Ends the opts || opts || opts...
|
||||
|
||||
// Populate help text array
|
||||
|
||||
#undef NEWTOY
|
||||
#undef OLDTOY
|
||||
#define NEWTOY(name,opt,flags) HELP_##name "\0"
|
||||
#if CFG_TOYBOX
|
||||
#define OLDTOY(name,oldname,flags) "\xff" #oldname "\0"
|
||||
#else
|
||||
#define OLDTOY(name, oldname, flags) HELP_##oldname "\0"
|
||||
#endif
|
||||
|
||||
#include "generated/help.h"
|
||||
static char *help_data =
|
||||
#include "generated/newtoys.h"
|
||||
;
|
||||
|
||||
void show_help(FILE *out, int full)
|
||||
{
|
||||
int i = toys.which-toy_list;
|
||||
char *s, *ss;
|
||||
|
||||
if (!(full&2))
|
||||
fprintf(out, "Toybox %s"USE_TOYBOX(" multicall binary")"%s\n\n",
|
||||
toybox_version, (CFG_TOYBOX && i) ? " (see toybox --help)"
|
||||
: " (see https://landley.net/toybox)");
|
||||
|
||||
if (CFG_TOYBOX_HELP) {
|
||||
for (;;) {
|
||||
s = help_data;
|
||||
while (i--) s += strlen(s) + 1;
|
||||
// If it's an alias, restart search for real name
|
||||
if (*s != 255) break;
|
||||
i = toy_find(++s)-toy_list;
|
||||
}
|
||||
|
||||
if (full) fprintf(out, "%s\n", s);
|
||||
else {
|
||||
strstart(&s, "usage: ");
|
||||
for (ss = s; *ss && *ss!='\n'; ss++);
|
||||
fprintf(out, "%.*s\n", (int)(ss-s), s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void unknown(char *name)
|
||||
{
|
||||
toys.exitval = 127;
|
||||
toys.which = toy_list;
|
||||
help_exit("Unknown command %s", name);
|
||||
}
|
||||
|
||||
// Parse --help and --version for (almost) all commands
|
||||
void check_help(char **arg)
|
||||
{
|
||||
if (!CFG_TOYBOX_HELP_DASHDASH || !*arg) return;
|
||||
if (!CFG_TOYBOX || toys.which != toy_list)
|
||||
if (toys.which->flags&TOYFLAG_NOHELP) return;
|
||||
|
||||
if (!strcmp(*arg, "--help")) {
|
||||
if (CFG_TOYBOX && toys.which == toy_list && arg[1])
|
||||
if (!(toys.which = toy_find(arg[1]))) unknown(arg[1]);
|
||||
show_help(stdout, 1);
|
||||
xexit();
|
||||
}
|
||||
|
||||
if (!strcmp(*arg, "--version")) {
|
||||
xprintf("toybox %s\n", toybox_version);
|
||||
xexit();
|
||||
}
|
||||
}
|
||||
|
||||
// Setup toybox global state for this command.
|
||||
void toy_singleinit(struct toy_list *which, char *argv[])
|
||||
{
|
||||
toys.which = which;
|
||||
toys.argv = argv;
|
||||
toys.toycount = ARRAY_LEN(toy_list);
|
||||
|
||||
if (NEED_OPTIONS && which->options) get_optflags();
|
||||
else {
|
||||
check_help(toys.optargs = argv+1);
|
||||
for (toys.optc = 0; toys.optargs[toys.optc]; toys.optc++);
|
||||
}
|
||||
|
||||
if (!(CFG_TOYBOX && which == toy_list) && !(which->flags & TOYFLAG_NOFORK)) {
|
||||
toys.old_umask = umask(0);
|
||||
if (!(which->flags & TOYFLAG_UMASK)) umask(toys.old_umask);
|
||||
|
||||
// Try user's locale, but merge in the en_US.UTF-8 locale's character
|
||||
// type data if the user's locale isn't UTF-8. (We can't merge in C.UTF-8
|
||||
// because that locale doesn't exist on macOS.)
|
||||
// setlocale(LC_CTYPE, "");
|
||||
// if (strcmp("UTF-8", nl_langinfo(CODESET)))
|
||||
// uselocale(newlocale(LC_CTYPE_MASK, "en_US.UTF-8", NULL));
|
||||
|
||||
setvbuf(stdout, 0, (which->flags & TOYFLAG_LINEBUF) ? _IOLBF : _IONBF, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// Full init needed by multiplexer or reentrant calls, calls singleinit at end
|
||||
void toy_init(struct toy_list *which, char *argv[])
|
||||
{
|
||||
void *oldwhich = toys.which;
|
||||
|
||||
// Drop permissions for non-suid commands.
|
||||
if (CFG_TOYBOX_SUID) {
|
||||
if (!toys.which) toys.which = toy_list;
|
||||
|
||||
uid_t uid = getuid(), euid = geteuid();
|
||||
|
||||
if (!(which->flags & TOYFLAG_STAYROOT)) {
|
||||
if (uid != euid) {
|
||||
if (setuid(uid)) perror_exit("setuid %d->%d", euid, uid); // drop root
|
||||
euid = uid;
|
||||
toys.wasroot++;
|
||||
}
|
||||
} else if (CFG_TOYBOX_DEBUG && uid && which != toy_list)
|
||||
error_msg("Not installed suid root");
|
||||
|
||||
if ((which->flags & TOYFLAG_NEEDROOT) && euid) {
|
||||
toys.which = which;
|
||||
check_help(argv+1);
|
||||
help_exit("Not root");
|
||||
}
|
||||
}
|
||||
|
||||
// Free old toys contents (to be reentrant), but leave rebound if any
|
||||
// don't blank old optargs if our new argc lives in the old optargs.
|
||||
if (argv<toys.optargs || argv>toys.optargs+toys.optc) free(toys.optargs);
|
||||
memset(&toys, 0, offsetof(struct toy_context, rebound));
|
||||
if (oldwhich) memset(&this, 0, sizeof(this));
|
||||
|
||||
// Continue to portion of init needed by standalone commands
|
||||
toy_singleinit(which, argv);
|
||||
}
|
||||
|
||||
// Run an internal toybox command.
|
||||
// Only returns if it can't run command internally, otherwise xexit() when done.
|
||||
static void toy_exec_which(struct toy_list *which, char *argv[])
|
||||
{
|
||||
// Return if we can't find it (which includes no multiplexer case),
|
||||
if (!which || (which->flags&TOYFLAG_NOFORK)) return;
|
||||
|
||||
// Return if stack depth getting noticeable (proxy for leaked heap, etc).
|
||||
|
||||
// Compiler writers have decided subtracting char * is undefined behavior,
|
||||
// so convert to integers. (LP64 says sizeof(long)==sizeof(pointer).)
|
||||
// Signed typecast so stack growth direction is irrelevant: we're measuring
|
||||
// the distance between two pointers on the same stack, hence the labs().
|
||||
if (!CFG_TOYBOX_NORECURSE && toys.stacktop)
|
||||
if (labs((long)toys.stacktop-(long)&which)>6000) return;
|
||||
|
||||
// Return if we need to re-exec to acquire root via suid bit.
|
||||
if (toys.which && (which->flags&TOYFLAG_ROOTONLY) && toys.wasroot) return;
|
||||
|
||||
// Run command
|
||||
toy_init(which, argv);
|
||||
if (toys.which) toys.which->toy_main();
|
||||
xexit();
|
||||
}
|
||||
|
||||
// Lookup internal toybox command to run via argv[0]
|
||||
void toy_exec(char *argv[])
|
||||
{
|
||||
toy_exec_which(toy_find(*argv), argv);
|
||||
}
|
||||
|
||||
// Multiplexer command, first argument is command to run, rest are args to that.
|
||||
// If first argument starts with - output list of command install paths.
|
||||
void toybox_main(void)
|
||||
{
|
||||
char *toy_paths[] = {"usr/", "bin/", "sbin/", 0}, *s = toys.argv[1];
|
||||
int i, len = 0;
|
||||
unsigned width = 80;
|
||||
|
||||
// fast path: try to exec immediately.
|
||||
// (Leave toys.which null to disable suid return logic.)
|
||||
// Try dereferencing symlinks until we hit a recognized name
|
||||
while (s) {
|
||||
char *ss = basename(s);
|
||||
struct toy_list *tl = toy_find(ss);
|
||||
|
||||
if (tl==toy_list && s!=toys.argv[1]) unknown(ss);
|
||||
toy_exec_which(tl, toys.argv+1);
|
||||
s = (0<readlink(s, libbuf, sizeof(libbuf))) ? libbuf : 0;
|
||||
}
|
||||
|
||||
// For early error reporting
|
||||
toys.which = toy_list;
|
||||
|
||||
if (toys.argv[1] && strcmp(toys.argv[1], "--long")) unknown(toys.argv[1]);
|
||||
|
||||
// Output list of commands.
|
||||
terminal_size(&width, 0);
|
||||
for (i = 1; i<ARRAY_LEN(toy_list); i++) {
|
||||
int fl = toy_list[i].flags;
|
||||
if (fl & TOYMASK_LOCATION) {
|
||||
if (toys.argv[1]) {
|
||||
int j;
|
||||
for (j = 0; toy_paths[j]; j++)
|
||||
if (fl & (1<<j)) len += printf("%s", toy_paths[j]);
|
||||
}
|
||||
len += printf("%s",toy_list[i].name);
|
||||
if (++len > width-15) len = 0;
|
||||
xputc(len ? ' ' : '\n');
|
||||
}
|
||||
}
|
||||
xputc('\n');
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// don't segfault if our environment is crazy
|
||||
if (!*argv) return 127;
|
||||
|
||||
// Snapshot stack location so we can detect recursion depth later.
|
||||
// Nommu has special reentry path, !stacktop = "vfork/exec self happened"
|
||||
if (!CFG_TOYBOX_FORK && (0x80 & **argv)) **argv &= 0x7f;
|
||||
else {
|
||||
int stack_start; // here so probe var won't permanently eat stack
|
||||
|
||||
toys.stacktop = &stack_start;
|
||||
}
|
||||
|
||||
// Android before O had non-default SIGPIPE, 7 years = remove in Sep 2024.
|
||||
if (CFG_TOYBOX_ON_ANDROID) signal(SIGPIPE, SIG_DFL);
|
||||
|
||||
if (CFG_TOYBOX) {
|
||||
// Call the multiplexer with argv[] as its arguments so it can toy_find()
|
||||
toys.argv = argv-1;
|
||||
toybox_main();
|
||||
} else {
|
||||
// single command built standalone with no multiplexer is first list entry
|
||||
toy_singleinit(toy_list, argv);
|
||||
toy_list->toy_main();
|
||||
}
|
||||
|
||||
xexit();
|
||||
}
|
||||
@@ -0,0 +1,394 @@
|
||||
/* mount.c - mount filesystems
|
||||
*
|
||||
* Copyright 2014 Rob Landley <rob@landley.net>
|
||||
*
|
||||
* See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/mount.html
|
||||
*
|
||||
* Note: -hV is bad spec, haven't implemented -FsLU yet
|
||||
* no mtab (/proc/mounts does it) so -n is NOP.
|
||||
* TODO mount -o loop,autoclear (linux git 96c5865559ce)
|
||||
* TODO mount jffs2.img dir (block2mtd)
|
||||
* TODO fstab user
|
||||
* TODO mount [^/]*:def = nfs, \\samba
|
||||
|
||||
USE_MOUNT(NEWTOY(mount, "?RO:afnrvwt:o*[-rw]", TOYFLAG_BIN|TOYFLAG_STAYROOT))
|
||||
//USE_NFSMOUNT(NEWTOY(nfsmount, "<2>2", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_STAYROOT))
|
||||
|
||||
config MOUNT
|
||||
bool "mount"
|
||||
default y
|
||||
help
|
||||
usage: mount [-afFrsvw] [-t TYPE] [-o OPTION,] [[DEVICE] DIR]
|
||||
|
||||
Mount new filesystem(s) on directories. With no arguments, display existing
|
||||
mounts.
|
||||
|
||||
-a Mount all entries in /etc/fstab (with -t, only entries of that TYPE)
|
||||
-O Only mount -a entries that have this option
|
||||
-f Fake it (don't actually mount)
|
||||
-r Read only (same as -o ro)
|
||||
-w Read/write (default, same as -o rw)
|
||||
-t Specify filesystem type
|
||||
-v Verbose
|
||||
|
||||
OPTIONS is a comma separated list of options, which can also be supplied
|
||||
as --longopts.
|
||||
|
||||
Autodetects loopback mounts (a file on a directory) and bind mounts (file
|
||||
on file, directory on directory), so you don't need to say --bind or --loop.
|
||||
You can also "mount -a /path" to mount everything in /etc/fstab under /path,
|
||||
even if it's noauto. DEVICE starting with UUID= is identified by blkid -U.
|
||||
|
||||
#config SMBMOUNT
|
||||
# bool "smbmount"
|
||||
# default n
|
||||
# helo
|
||||
# usage: smbmount SHARE DIR
|
||||
#
|
||||
# Mount smb share with user/pasword prompt as necessary.
|
||||
#
|
||||
#config NFSMOUNT
|
||||
# bool "nfsmount"
|
||||
# default n
|
||||
# help
|
||||
# usage: nfsmount SHARE DIR
|
||||
#
|
||||
# Invoke an eldrich horror from the dawn of time.
|
||||
*/
|
||||
|
||||
#define FOR_mount
|
||||
#include "toys.h"
|
||||
#include <linux/fs.h>
|
||||
|
||||
GLOBALS(
|
||||
struct arg_list *o;
|
||||
char *t, *O;
|
||||
|
||||
unsigned long flags;
|
||||
char *opts;
|
||||
int okuser;
|
||||
)
|
||||
|
||||
// mount.tests should check for all of this:
|
||||
// TODO detect existing identical mount (procfs with different dev name?)
|
||||
// TODO user, users, owner, group, nofail
|
||||
// TODO -p (passfd)
|
||||
// TODO -a -t notype,type2
|
||||
// TODO --subtree
|
||||
// TODO make "mount --bind,ro old new" work (implicit -o remount)
|
||||
// TODO mount -a
|
||||
// TODO mount -o remount
|
||||
// TODO fstab: lookup default options for mount
|
||||
// TODO implement -v
|
||||
// TODO "mount -a -o remount,ro" should detect overmounts
|
||||
// TODO work out how that differs from "mount -ar"
|
||||
// TODO what if you --bind mount a block device somewhere (file, dir, dev)
|
||||
// TODO "touch servername; mount -t cifs servername path"
|
||||
// TODO mount -o remount a user mount
|
||||
// TODO mount image.img sub (auto-loopback) then umount image.img
|
||||
// TODO mount UUID=blah
|
||||
|
||||
// Strip flags out of comma separated list of options, return flags,.
|
||||
// TODO: flip order and it's tagged array?
|
||||
static long flag_opts(char *new, long flags, char **more)
|
||||
{
|
||||
struct {
|
||||
char *name;
|
||||
long flags;
|
||||
} opts[] = {
|
||||
{"loop", 0}, {"defaults", 0}, {"quiet", 0}, // NOPs
|
||||
{"user", 0}, {"nouser", 0}, // checked in fstab, ignored in -o
|
||||
{"bind", MS_REC}, {"rbind", ~MS_REC}, // Autodetected but override defaults
|
||||
{"ro", MS_RDONLY}, {"rw", ~MS_RDONLY},
|
||||
{"nosuid", MS_NOSUID}, {"suid", ~MS_NOSUID},
|
||||
{"nodev", MS_NODEV}, {"dev", ~MS_NODEV},
|
||||
{"noexec", MS_NOEXEC}, {"exec", ~MS_NOEXEC},
|
||||
{"sync", MS_SYNCHRONOUS}, {"async", ~MS_SYNCHRONOUS},
|
||||
{"noatime", MS_NOATIME}, {"atime", ~MS_NOATIME},
|
||||
{"norelatime", ~MS_RELATIME}, {"relatime", MS_RELATIME},
|
||||
{"nodiratime", MS_NODIRATIME}, {"diratime", ~MS_NODIRATIME},
|
||||
{"loud", ~MS_SILENT},
|
||||
{"shared", MS_SHARED}, {"rshared", MS_SHARED|MS_REC},
|
||||
{"slave", MS_SLAVE}, {"rslave", MS_SLAVE|MS_REC},
|
||||
{"private", MS_PRIVATE}, {"rprivate", MS_SLAVE|MS_REC},
|
||||
{"unbindable", MS_UNBINDABLE}, {"runbindable", MS_UNBINDABLE|MS_REC},
|
||||
{"remount", MS_REMOUNT}, {"move", MS_MOVE},
|
||||
// mand dirsync rec iversion strictatime
|
||||
};
|
||||
|
||||
if (new) for (;;) {
|
||||
char *comma = strchr(new, ',');
|
||||
int i;
|
||||
|
||||
if (comma) *comma = 0;
|
||||
|
||||
// If we recognize an option, apply flags
|
||||
for (i = 0; i < ARRAY_LEN(opts); i++) if (!strcasecmp(opts[i].name, new)) {
|
||||
long ll = opts[i].flags;
|
||||
|
||||
if (ll < 0) flags &= ll;
|
||||
else flags |= ll;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// If we didn't recognize it, keep string version
|
||||
if (more && i == ARRAY_LEN(opts)) {
|
||||
i = *more ? strlen(*more) : 0;
|
||||
*more = xrealloc(*more, i + strlen(new) + 2);
|
||||
if (i) (*more)[i++] = ',';
|
||||
strcpy(i+*more, new);
|
||||
}
|
||||
|
||||
if (!comma) break;
|
||||
*comma = ',';
|
||||
new = comma + 1;
|
||||
}
|
||||
|
||||
return flags;
|
||||
}
|
||||
|
||||
static void mount_filesystem(char *dev, char *dir, char *type,
|
||||
unsigned long flags, char *opts)
|
||||
{
|
||||
FILE *fp = 0;
|
||||
int rc = EINVAL;
|
||||
char *buf = 0;
|
||||
|
||||
if (FLAG(f)) return;
|
||||
|
||||
if (getuid()) {
|
||||
if (TT.okuser) TT.okuser = 0;
|
||||
else {
|
||||
error_msg("'%s' not user mountable in fstab", dev);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (strstart(&dev, "UUID=")) {
|
||||
char *s = xrunread((char *[]){"blkid", "-U", dev, 0}, 0);
|
||||
|
||||
if (!s || strlen(s)>=sizeof(toybuf)) return error_msg("No uuid %s", dev);
|
||||
strcpy(dev = toybuf, s);
|
||||
free(s);
|
||||
}
|
||||
|
||||
// Autodetect bind mount or filesystem type
|
||||
|
||||
if (type && !strcmp(type, "auto")) type = 0;
|
||||
if (flags & MS_MOVE) {
|
||||
if (type) error_exit("--move with -t");
|
||||
} else if (!type) {
|
||||
struct stat stdev, stdir;
|
||||
|
||||
// file on file or dir on dir is a --bind mount.
|
||||
if (!stat(dev, &stdev) && !stat(dir, &stdir)
|
||||
&& ((S_ISREG(stdev.st_mode) && S_ISREG(stdir.st_mode))
|
||||
|| (S_ISDIR(stdev.st_mode) && S_ISDIR(stdir.st_mode))))
|
||||
{
|
||||
flags ^= MS_REC;
|
||||
flags |= MS_BIND;
|
||||
} else fp = xfopen("/proc/filesystems", "r");
|
||||
} else if (!strcmp(type, "ignore")) return;
|
||||
else if (!strcmp(type, "swap"))
|
||||
toys.exitval |= xrun((char *[]){"swapon", "--", dev, 0});
|
||||
|
||||
for (;;) {
|
||||
int fd = -1, ro = 0;
|
||||
|
||||
// If type wasn't specified, try all of them in order.
|
||||
if (fp && !buf) {
|
||||
size_t i;
|
||||
|
||||
if (getline(&buf, &i, fp)<1) {
|
||||
error_msg("%s: need -t", dev);
|
||||
break;
|
||||
}
|
||||
type = buf;
|
||||
// skip nodev devices
|
||||
if (!isspace(*type)) {
|
||||
free(buf);
|
||||
buf = 0;
|
||||
|
||||
continue;
|
||||
}
|
||||
// trim whitespace
|
||||
while (isspace(*type)) type++;
|
||||
i = strlen(type);
|
||||
if (i) type[i-1] = 0;
|
||||
}
|
||||
if (FLAG(v)) printf("try '%s' type '%s' on '%s'\n", dev, type, dir);
|
||||
for (;;) {
|
||||
rc = mount(dev, dir, type, flags, opts);
|
||||
// Did we succeed, fail unrecoverably, or already try read-only?
|
||||
if (!rc || (errno != EACCES && errno != EROFS) || (flags&MS_RDONLY))
|
||||
break;
|
||||
// If we haven't already tried it, use the BLKROSET ioctl to ensure
|
||||
// that the underlying device isn't read-only.
|
||||
if (fd == -1) {
|
||||
if (FLAG(v))
|
||||
printf("trying BLKROSET ioctl on '%s'\n", dev);
|
||||
if (-1 != (fd = open(dev, O_RDONLY))) {
|
||||
rc = ioctl(fd, BLKROSET, &ro);
|
||||
close(fd);
|
||||
if (!rc) continue;
|
||||
}
|
||||
}
|
||||
fprintf(stderr, "'%s' is read-only\n", dev);
|
||||
flags |= MS_RDONLY;
|
||||
}
|
||||
|
||||
// Trying to autodetect loop mounts like bind mounts above (file on dir)
|
||||
// isn't good enough because "mount -t ext2 fs.img dir" is valid, but if
|
||||
// you _do_ accept loop mounts with -t how do you tell "-t cifs" isn't
|
||||
// looking for a block device if it's not in /proc/filesystems yet
|
||||
// because the fs module won't be loaded until you try the mount, and
|
||||
// if you can't then DEVICE existing as a file would cause a false
|
||||
// positive loopback mount (so "touch servername" becomes a potential
|
||||
// denial of service attack...)
|
||||
//
|
||||
// Solution: try the mount, let the kernel tell us it wanted a block
|
||||
// device, then do the loopback setup and retry the mount.
|
||||
|
||||
if (rc && errno == ENOTBLK) {
|
||||
char *losetup[] = {"losetup", (flags&MS_RDONLY)?"-fsr":"-fs", dev, 0};
|
||||
|
||||
if ((dev = xrunread(losetup, 0))) continue;
|
||||
error_msg("%s failed", *losetup);
|
||||
break;
|
||||
}
|
||||
|
||||
free(buf);
|
||||
buf = 0;
|
||||
if (!rc) break;
|
||||
if (fp && (errno == EINVAL || errno == EBUSY)) continue;
|
||||
|
||||
perror_msg("'%s'->'%s'", dev, dir);
|
||||
|
||||
break;
|
||||
}
|
||||
if (fp) fclose(fp);
|
||||
}
|
||||
|
||||
void mount_main(void)
|
||||
{
|
||||
char *opts = 0, *dev = 0, *dir = 0, **ss;
|
||||
long flags = MS_SILENT;
|
||||
struct arg_list *o;
|
||||
struct mtab_list *mtl, *mtl2 = 0, *mm, *remount;
|
||||
|
||||
// TODO
|
||||
// remount
|
||||
// - overmounts
|
||||
// shared subtree
|
||||
// -o parsed after fstab options
|
||||
// test if mountpoint already exists (-o noremount?)
|
||||
|
||||
// First pass; just accumulate string, don't parse flags yet. (This is so
|
||||
// we can modify fstab entries with -a, or mtab with remount.)
|
||||
for (o = TT.o; o; o = o->next) comma_collate(&opts, o->arg);
|
||||
if (FLAG(r)) comma_collate(&opts, "ro");
|
||||
if (FLAG(w)) comma_collate(&opts, "rw");
|
||||
if (FLAG(R)) comma_collate(&opts, "rbind");
|
||||
|
||||
// Treat each --option as -o option
|
||||
for (ss = toys.optargs; *ss; ss++) {
|
||||
char *sss = *ss;
|
||||
|
||||
// If you realy, really want to mount a file named "--", we support it.
|
||||
if (sss[0]=='-' && sss[1]=='-' && sss[2]) comma_collate(&opts, sss+2);
|
||||
else if (!dev) dev = sss;
|
||||
else if (!dir) dir = sss;
|
||||
// same message as lib/args.c ">2" which we can't use because --opts count
|
||||
else error_exit("Max 2 arguments\n");
|
||||
}
|
||||
|
||||
if (FLAG(a) && dir) error_exit("-a with >1 arg");
|
||||
|
||||
// For remount we need _last_ match (in case of overmounts), so traverse
|
||||
// in reverse order. (Yes I'm using remount as a boolean for a bit here,
|
||||
// the double cast is to get gcc to shut up about it.)
|
||||
remount = (void *)(long)comma_scan(opts, "remount", 0);
|
||||
if ((FLAG(a) && !access("/proc/mounts", R_OK)) || remount) {
|
||||
mm = dlist_terminate(mtl = mtl2 = xgetmountlist(0));
|
||||
if (remount) remount = mm;
|
||||
}
|
||||
|
||||
// Do we need to do an /etc/fstab trawl?
|
||||
// This covers -a, -o remount, one argument, all user mounts
|
||||
if (FLAG(a) || (dev && (!dir || getuid() || remount))) {
|
||||
if (!remount) dlist_terminate(mtl = xgetmountlist("/etc/fstab"));
|
||||
|
||||
for (mm = remount ? remount : mtl; mm; mm = (remount ? mm->prev : mm->next))
|
||||
{
|
||||
char *aopts = 0;
|
||||
struct mtab_list *mmm = 0;
|
||||
int aflags, noauto, len;
|
||||
|
||||
// Check for noauto and get it out of the option list. (Unknown options
|
||||
// that make it to the kernel give filesystem drivers indigestion.)
|
||||
noauto = comma_scan(mm->opts, "noauto", 1);
|
||||
|
||||
if (FLAG(a)) {
|
||||
// "mount -a /path" to mount all entries under /path
|
||||
if (dev) {
|
||||
len = strlen(dev);
|
||||
if (strncmp(dev, mm->dir, len)
|
||||
|| (mm->dir[len] && mm->dir[len] != '/')) continue;
|
||||
} else if (noauto) continue; // never present in the remount case
|
||||
if (!mountlist_istype(mm, TT.t) || !comma_scanall(mm->opts, TT.O))
|
||||
continue;
|
||||
} else {
|
||||
if (dir && strcmp(dir, mm->dir)) continue;
|
||||
if (strcmp(dev, mm->device) && (dir || strcmp(dev, mm->dir))) continue;
|
||||
}
|
||||
|
||||
// Don't overmount the same dev on the same directory
|
||||
// (Unless root explicitly says to in non -a mode.)
|
||||
if (mtl2 && !remount)
|
||||
for (mmm = mtl2; mmm; mmm = mmm->next)
|
||||
if (!strcmp(mm->dir, mmm->dir) && !strcmp(mm->device, mmm->device))
|
||||
break;
|
||||
|
||||
// user only counts from fstab, not opts.
|
||||
if (!mmm) {
|
||||
TT.okuser = comma_scan(mm->opts, "user", 1);
|
||||
aflags = flag_opts(mm->opts, flags, &aopts);
|
||||
aflags = flag_opts(opts, aflags, &aopts);
|
||||
|
||||
mount_filesystem(mm->device, mm->dir, mm->type, aflags, aopts);
|
||||
} // TODO else if (getuid()) error_msg("already there") ?
|
||||
free(aopts);
|
||||
|
||||
if (!FLAG(a)) break;
|
||||
}
|
||||
if (CFG_TOYBOX_FREE) {
|
||||
llist_traverse(mtl, free);
|
||||
llist_traverse(mtl2, free);
|
||||
}
|
||||
if (!mm && !FLAG(a))
|
||||
error_exit("'%s' not in %s", dir ? dir : dev,
|
||||
remount ? "/proc/mounts" : "fstab");
|
||||
|
||||
// show mounts from /proc/mounts
|
||||
} else if (!dev) {
|
||||
for (mtl = xgetmountlist(0); mtl && (mm = dlist_pop(&mtl)); free(mm)) {
|
||||
char *s = 0;
|
||||
|
||||
if (TT.t && strcmp(TT.t, mm->type)) continue;
|
||||
if (*mm->device == '/') s = xabspath(mm->device, 0);
|
||||
xprintf("%s on %s type %s (%s)\n",
|
||||
s ? s : mm->device, mm->dir, mm->type, mm->opts);
|
||||
free(s);
|
||||
}
|
||||
|
||||
// two arguments
|
||||
} else {
|
||||
char *more = 0;
|
||||
|
||||
flags = flag_opts(opts, flags, &more);
|
||||
mount_filesystem(dev, dir, TT.t, flags, more);
|
||||
if (CFG_TOYBOX_FREE) free(more);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/* insmod.c - Load a module into the Linux kernel.
|
||||
*
|
||||
* Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
|
||||
|
||||
USE_INSMOD(NEWTOY(insmod, "<1", TOYFLAG_SBIN|TOYFLAG_NEEDROOT))
|
||||
|
||||
config INSMOD
|
||||
bool "insmod"
|
||||
default y
|
||||
help
|
||||
usage: insmod MODULE [OPTION...]
|
||||
|
||||
Load the module named MODULE passing options if given.
|
||||
*/
|
||||
|
||||
#include "toys.h"
|
||||
|
||||
#ifndef SYS_finit_module
|
||||
#define SYS_finit_module 379
|
||||
#endif
|
||||
|
||||
void insmod_main(void)
|
||||
{
|
||||
int fd = xopenro(*toys.optargs);
|
||||
int i, rc;
|
||||
|
||||
i = 1;
|
||||
while (toys.optargs[i] &&
|
||||
strlen(toybuf) + strlen(toys.optargs[i]) + 2 < sizeof(toybuf))
|
||||
{
|
||||
strcat(toybuf, toys.optargs[i++]);
|
||||
strcat(toybuf, " ");
|
||||
}
|
||||
|
||||
// finit_module doesn't work on stdin, so we fall back to init_module...
|
||||
rc = syscall(SYS_finit_module, fd, toybuf, 0);
|
||||
if (rc && (fd == 0 || errno == ENOSYS)) {
|
||||
off_t len = 0;
|
||||
char *path = !strcmp(*toys.optargs, "-") ? "/dev/stdin" : *toys.optargs;
|
||||
char *buf = readfileat(AT_FDCWD, path, NULL, &len);
|
||||
|
||||
if (!buf) perror_exit("couldn't read %s", path);
|
||||
rc = syscall(SYS_init_module, buf, len, toybuf);
|
||||
if (CFG_TOYBOX_FREE) free(buf);
|
||||
}
|
||||
|
||||
if (rc) perror_exit("failed to load %s", toys.optargs[0]);
|
||||
|
||||
if (CFG_TOYBOX_FREE) close(fd);
|
||||
}
|
||||
Reference in New Issue
Block a user