mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-27 14:58:30 +00:00
Greatly expand system() shell code features
The cosmopolitan command interpreter now has 13 builtin commands, variable support, support for ; / && / || syntax, asynchronous support, and plenty of unit tests with bug fixes. This change fixes a bug in posix_spawn() with null envp arg. strace logging now uses atomic writes for scatter functions. Breaking change renaming GetCpuCount() to _getcpucount(). TurfWar is now updated to use the new token bucket algorithm. WIN32 affinity masks now inherit across fork() and execve().
This commit is contained in:
parent
e7329b7cba
commit
b41f91c658
80 changed files with 1370 additions and 344 deletions
|
@ -99,7 +99,7 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
|
|||
startinfo.hStdError = __getfdhandleactual(2);
|
||||
|
||||
// spawn the process
|
||||
rc = ntspawn(program, argv, envp, 0, 0, 0, 1, 0, 0, &startinfo, &procinfo);
|
||||
rc = ntspawn(program, argv, envp, 0, 0, 0, true, 0, 0, &startinfo, &procinfo);
|
||||
if (rc == -1) {
|
||||
STRACE("panic: unrecoverable ntspawn(%#s) error: %m", program);
|
||||
__imp_ExitProcess(6543);
|
||||
|
|
|
@ -34,7 +34,7 @@
|
|||
#define HW_NCPUONLINE_NETBSD 16
|
||||
#define ALL_PROCESSOR_GROUPS 0xffff
|
||||
|
||||
static unsigned GetCpuCountLinux(void) {
|
||||
static unsigned _getcpucount_linux(void) {
|
||||
cpu_set_t s = {0};
|
||||
if (sys_sched_getaffinity(0, sizeof(s), &s) != -1) {
|
||||
return CPU_COUNT(&s);
|
||||
|
@ -43,7 +43,7 @@ static unsigned GetCpuCountLinux(void) {
|
|||
}
|
||||
}
|
||||
|
||||
static unsigned GetCpuCountBsd(void) {
|
||||
static unsigned _getcpucount_bsd(void) {
|
||||
size_t n;
|
||||
int c, cmd[2];
|
||||
n = sizeof(c);
|
||||
|
@ -62,12 +62,12 @@ static unsigned GetCpuCountBsd(void) {
|
|||
}
|
||||
}
|
||||
|
||||
static unsigned GetCpuCountImpl(void) {
|
||||
static unsigned _getcpucount_impl(void) {
|
||||
if (!IsWindows()) {
|
||||
if (!IsBsd()) {
|
||||
return GetCpuCountLinux();
|
||||
return _getcpucount_linux();
|
||||
} else {
|
||||
return GetCpuCountBsd();
|
||||
return _getcpucount_bsd();
|
||||
}
|
||||
} else {
|
||||
return GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS);
|
||||
|
@ -77,18 +77,24 @@ static unsigned GetCpuCountImpl(void) {
|
|||
static int g_cpucount;
|
||||
|
||||
// precompute because process affinity on linux may change later
|
||||
__attribute__((__constructor__)) static void init(void) {
|
||||
g_cpucount = GetCpuCountImpl();
|
||||
__attribute__((__constructor__)) static void _getcpucount_init(void) {
|
||||
g_cpucount = _getcpucount_impl();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns number of CPUs in system.
|
||||
*
|
||||
* This is the same as the standard interface:
|
||||
*
|
||||
* sysconf(_SC_NPROCESSORS_ONLN);
|
||||
*
|
||||
* Except this function isn't a bloated diamond dependency.
|
||||
*
|
||||
* On Intel systems with HyperThreading this will return the number of
|
||||
* cores multiplied by two.
|
||||
*
|
||||
* @return cpu count or 0 if it couldn't be determined
|
||||
*/
|
||||
unsigned GetCpuCount(void) {
|
||||
unsigned _getcpucount(void) {
|
||||
return g_cpucount;
|
||||
}
|
||||
|
|
|
@ -20,10 +20,10 @@
|
|||
#include "libc/calls/syscall_support-nt.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/nt/accounting.h"
|
||||
#include "libc/runtime/sysconf.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
#define FT(x) (x.dwLowDateTime | (uint64_t)x.dwHighDateTime << 32)
|
||||
|
||||
|
@ -60,7 +60,7 @@ static textstartup void sys_getloadavg_nt_init(void) {
|
|||
double a[3];
|
||||
if (IsWindows()) {
|
||||
load = 1;
|
||||
cpus = GetCpuCount() / 2;
|
||||
cpus = _getcpucount() / 2;
|
||||
cpus = MAX(1, cpus);
|
||||
GetSystemTimes(&idle1, &kern1, &user1);
|
||||
}
|
||||
|
|
|
@ -16,10 +16,11 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/intrin/pushpop.h"
|
||||
#include "libc/calls/ntspawn.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/calls/syscall_support-nt.internal.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/intrin/pushpop.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/nt/enum/filemapflags.h"
|
||||
#include "libc/nt/enum/pageflags.h"
|
||||
|
@ -87,7 +88,8 @@ textwindows int ntspawn(
|
|||
mkntenvblock(block->envvars, envp, extravar, block->buf) != -1 &&
|
||||
CreateProcess(prog16, block->cmdline, opt_lpProcessAttributes,
|
||||
opt_lpThreadAttributes, bInheritHandles,
|
||||
dwCreationFlags | kNtCreateUnicodeEnvironment,
|
||||
dwCreationFlags | kNtCreateUnicodeEnvironment |
|
||||
kNtInheritParentAffinity,
|
||||
block->envvars, opt_lpCurrentDirectory, lpStartupInfo,
|
||||
opt_out_lpProcessInformation)) {
|
||||
rc = 0;
|
||||
|
|
|
@ -129,12 +129,7 @@ static ssize_t Preadv(int fd, struct iovec *iov, int iovlen, int64_t off) {
|
|||
ssize_t preadv(int fd, struct iovec *iov, int iovlen, int64_t off) {
|
||||
ssize_t rc;
|
||||
rc = Preadv(fd, iov, iovlen, off);
|
||||
#if defined(SYSDEBUG) && _DATATRACE
|
||||
if (UNLIKELY(__strace > 0)) {
|
||||
kprintf(STRACE_PROLOGUE "preadv(%d, [", fd);
|
||||
DescribeIov(iov, iovlen, rc != -1 ? rc : 0);
|
||||
kprintf("], %d, %'ld) → %'ld% m\n", iovlen, off, rc);
|
||||
}
|
||||
#endif
|
||||
STRACE("preadv(%d, [%s], %d, %'ld) → %'ld% m", fd,
|
||||
DescribeIovec(rc, iov, iovlen), iovlen, off, rc);
|
||||
return rc;
|
||||
}
|
||||
|
|
|
@ -135,12 +135,7 @@ static ssize_t Pwritev(int fd, const struct iovec *iov, int iovlen,
|
|||
ssize_t pwritev(int fd, const struct iovec *iov, int iovlen, int64_t off) {
|
||||
ssize_t rc;
|
||||
rc = Pwritev(fd, iov, iovlen, off);
|
||||
#if defined(SYSDEBUG) && _DATATRACE
|
||||
if (UNLIKELY(__strace > 0)) {
|
||||
kprintf(STRACE_PROLOGUE "pwritev(%d, ", fd);
|
||||
DescribeIov(iov, iovlen, rc != -1 ? rc : 0);
|
||||
kprintf(", %d, %'ld) → %'ld% m\n", iovlen, off, rc);
|
||||
}
|
||||
#endif
|
||||
STRACE("pwritev(%d, %s, %d, %'ld) → %'ld% m", fd,
|
||||
DescribeIovec(rc != -1 ? rc : -2, iov, iovlen), iovlen, off, rc);
|
||||
return rc;
|
||||
}
|
||||
|
|
|
@ -73,17 +73,7 @@ ssize_t readv(int fd, const struct iovec *iov, int iovlen) {
|
|||
rc = einval();
|
||||
}
|
||||
|
||||
#if defined(SYSDEBUG) && _DATATRACE
|
||||
if (UNLIKELY(__strace > 0)) {
|
||||
if (rc == -1 && errno == EFAULT) {
|
||||
STRACE("readv(%d, %p, %d) → %'zd% m", fd, iov, iovlen, rc);
|
||||
} else {
|
||||
kprintf(STRACE_PROLOGUE "readv(%d, [", fd);
|
||||
DescribeIov(iov, iovlen, rc != -1 ? rc : 0);
|
||||
kprintf("], %d) → %'ld% m\n", iovlen, rc);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
STRACE("readv(%d, [%s], %d) → %'ld% m", fd, DescribeIovec(rc, iov, iovlen),
|
||||
iovlen, rc);
|
||||
return rc;
|
||||
}
|
||||
|
|
|
@ -60,6 +60,8 @@ static dontinline textwindows int sys_sched_setaffinity_nt(
|
|||
/**
|
||||
* Asks kernel to only schedule process on particular CPUs.
|
||||
*
|
||||
* Affinity masks are inherited across fork() and execve() boundaries.
|
||||
*
|
||||
* @param pid is the process or process id (or 0 for caller)
|
||||
* @param size is bytes in bitset, which should be `sizeof(cpuset_t)`
|
||||
* @return 0 on success, or -1 w/ errno
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
*
|
||||
* @return 0 on success, or -1 w/ errno
|
||||
* @raise ENOSYS on XNU, Windows, OpenBSD
|
||||
* @vforksafe
|
||||
*/
|
||||
int sched_setparam(int pid, const struct sched_param *param) {
|
||||
int rc, policy;
|
||||
|
|
|
@ -91,6 +91,7 @@
|
|||
* @raise EINVAL if `param` is NULL
|
||||
* @raise EINVAL if `policy` is invalid
|
||||
* @raise EINVAL if `param` has value out of ranges defined by `policy`
|
||||
* @vforksafe
|
||||
*/
|
||||
int sched_setscheduler(int pid, int policy, const struct sched_param *param) {
|
||||
int rc, old;
|
||||
|
|
|
@ -13,7 +13,6 @@ ssize_t pwritev(int, const struct iovec *, int, int64_t);
|
|||
ssize_t readv(int, const struct iovec *, int);
|
||||
ssize_t vmsplice(int, const struct iovec *, int64_t, uint32_t);
|
||||
ssize_t writev(int, const struct iovec *, int);
|
||||
void DescribeIov(const struct iovec *, int, ssize_t);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define COSMOPOLITAN_LIBC_CALLS_STRUCT_IOVEC_INTERNAL_H_
|
||||
#include "libc/calls/struct/fd.internal.h"
|
||||
#include "libc/calls/struct/iovec.h"
|
||||
#include "libc/mem/alloca.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
|
@ -24,6 +25,9 @@ ssize_t sys_send_nt(int, const struct iovec *, size_t, uint32_t) hidden;
|
|||
ssize_t sys_sendto_nt(int, const struct iovec *, size_t, uint32_t, void *,
|
||||
uint32_t) hidden;
|
||||
|
||||
const char *DescribeIovec(char[300], ssize_t, const struct iovec *, int);
|
||||
#define DescribeIovec(x, y, z) DescribeIovec(alloca(300), x, y, z)
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_CALLS_STRUCT_IOVEC_INTERNAL_H_ */
|
||||
|
|
|
@ -16,7 +16,7 @@ int sys_fchmodat_nt(int, const char *, uint32_t, int) hidden;
|
|||
int sys_fcntl_nt(int, int, uintptr_t) hidden;
|
||||
int sys_fdatasync_nt(int) hidden;
|
||||
int sys_flock_nt(int, int) hidden;
|
||||
int sys_fork_nt(void) hidden;
|
||||
int sys_fork_nt(uint32_t) hidden;
|
||||
int sys_ftruncate_nt(int64_t, uint64_t) hidden;
|
||||
int sys_getloadavg_nt(double *, int) hidden;
|
||||
int sys_getppid_nt(void) hidden;
|
||||
|
|
|
@ -24,6 +24,7 @@ void cosmo2flock(uintptr_t) hidden;
|
|||
void flock2cosmo(uintptr_t) hidden;
|
||||
int _ptsname(int, char *, size_t) hidden;
|
||||
int _isptmaster(int) hidden;
|
||||
int _fork(uint32_t) hidden;
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
|
|
|
@ -77,13 +77,7 @@ ssize_t writev(int fd, const struct iovec *iov, int iovlen) {
|
|||
rc = einval();
|
||||
}
|
||||
|
||||
#if defined(SYSDEBUG) && _DATATRACE
|
||||
if (UNLIKELY(__strace > 0)) {
|
||||
kprintf(STRACE_PROLOGUE "writev(%d, ", fd);
|
||||
DescribeIov(iov, iovlen, rc != -1 ? rc : 0);
|
||||
kprintf(", %d) → %'ld% m\n", iovlen, rc);
|
||||
}
|
||||
#endif
|
||||
|
||||
STRACE("writev(%d, %s, %d) → %'ld% m", fd,
|
||||
DescribeIovec(rc != -1 ? rc : -2, iov, iovlen), iovlen, rc);
|
||||
return rc;
|
||||
}
|
||||
|
|
|
@ -45,6 +45,8 @@
|
|||
* on the the prefixes 0 (octal), 0x (hexadecimal), 0b (binary), or
|
||||
* decimal (base 10) by default
|
||||
* @return the decoded signed saturated number
|
||||
* @raise EINVAL if `base` isn't 0 or 2..36
|
||||
* @raise ERANGE on overflow
|
||||
*/
|
||||
long strtol(const char *s, char **endptr, int base) {
|
||||
char t = 0;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_FMT_STRTOL_H_
|
||||
#define COSMOPOLITAN_LIBC_FMT_STRTOL_H_
|
||||
#include "libc/errno.h"
|
||||
|
||||
#define CONSUME_SPACES(s, c) \
|
||||
if (endptr) *endptr = s; \
|
||||
|
@ -10,7 +11,7 @@
|
|||
if (c == '-' || c == '+') c = *++s
|
||||
|
||||
#define GET_RADIX(s, c, r) \
|
||||
if (!(2 <= r && r <= 36)) { \
|
||||
if (!r) { \
|
||||
if (c == '0') { \
|
||||
t |= 1; \
|
||||
c = *++s; \
|
||||
|
@ -26,6 +27,9 @@
|
|||
} else { \
|
||||
r = 10; \
|
||||
} \
|
||||
} else if (!(2 <= r && r <= 36)) { \
|
||||
errno = EINVAL; \
|
||||
return 0; \
|
||||
} else if (c == '0') { \
|
||||
t |= 1; \
|
||||
c = *++s; \
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include "libc/errno.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/fmt/strtol.internal.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/str/tab.internal.h"
|
||||
|
||||
|
@ -45,8 +46,12 @@ unsigned long wcstoul(const wchar_t *s, wchar_t **endptr, int base) {
|
|||
if ((c = kBase36[c & 255]) && --c < base) {
|
||||
t |= 1;
|
||||
do {
|
||||
x *= base;
|
||||
x += c;
|
||||
if (__builtin_mul_overflow(x, base, &x) ||
|
||||
__builtin_add_overflow(x, c, &x)) {
|
||||
if (endptr) *endptr = s + 1;
|
||||
errno = ERANGE;
|
||||
return ULONG_MAX;
|
||||
}
|
||||
} while ((c = kBase36[*++s & 255]) && --c < base);
|
||||
}
|
||||
if (t && endptr) *endptr = s;
|
||||
|
|
|
@ -16,10 +16,11 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/calls/syscall_support-nt.internal.h"
|
||||
#include "libc/intrin/describeflags.internal.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/nt/process.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/thunk/msabi.h"
|
||||
|
||||
__msabi extern typeof(CreateProcess) *const __imp_CreateProcessW;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
|
@ -16,27 +16,53 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/struct/iovec.h"
|
||||
#include "libc/calls/struct/iovec.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/intrin/describeflags.internal.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/macros.internal.h"
|
||||
|
||||
void DescribeIov(const struct iovec *iov, int iovlen, ssize_t rem) {
|
||||
int i;
|
||||
#define N 300
|
||||
|
||||
#define append(...) o += ksnprintf(buf + o, N - o, __VA_ARGS__)
|
||||
|
||||
const char *(DescribeIovec)(char buf[N], ssize_t rc, const struct iovec *iov,
|
||||
int iovlen) {
|
||||
const char *d;
|
||||
int i, j, o = 0;
|
||||
|
||||
if (!iov) return "NULL";
|
||||
if (rc == -1) return "n/a";
|
||||
if (rc == -2) rc = SSIZE_MAX;
|
||||
if ((!IsAsan() && kisdangerous(iov)) ||
|
||||
(IsAsan() && !__asan_is_valid(iov, iovlen * sizeof(struct iovec)))) {
|
||||
kprintf("%p", iov);
|
||||
return;
|
||||
(IsAsan() && !__asan_is_valid(iov, sizeof(*iov) * iovlen))) {
|
||||
ksnprintf(buf, N, "%p", iov);
|
||||
return buf;
|
||||
}
|
||||
kprintf("{");
|
||||
for (i = 0; rem && i < MIN(5, iovlen); ++i) {
|
||||
kprintf(
|
||||
"%s{%#.*hhs%s, %'zu}", i ? ", " : "",
|
||||
MAX(0, MIN(40, MIN(rem, iov[i].iov_len))), iov[i].iov_base,
|
||||
MAX(0, MIN(40, MIN(rem, iov[i].iov_len))) < iov[i].iov_len ? "..." : "",
|
||||
iov[i].iov_len);
|
||||
rem -= iov[i].iov_len;
|
||||
|
||||
append("{");
|
||||
|
||||
for (i = 0; rc && i < iovlen; ++i) {
|
||||
if (iov[i].iov_len < rc) {
|
||||
j = iov[i].iov_len;
|
||||
rc -= iov[i].iov_len;
|
||||
} else {
|
||||
j = rc;
|
||||
rc = 0;
|
||||
}
|
||||
if (j > 40) {
|
||||
j = 40;
|
||||
d = "...";
|
||||
} else {
|
||||
d = "";
|
||||
}
|
||||
append("%s{%#.*hhs%s, %'zu}", i ? ", " : "", j, iov[i].iov_base, d,
|
||||
iov[i].iov_len);
|
||||
}
|
||||
kprintf("%s}", iovlen > 5 ? "..." : "");
|
||||
|
||||
append("%s}", iovlen > 2 ? ", ..." : "");
|
||||
|
||||
return buf;
|
||||
}
|
5
libc/isystem/monetary.h
Normal file
5
libc/isystem/monetary.h
Normal file
|
@ -0,0 +1,5 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_ISYSTEM_MONETARY_H_
|
||||
#define COSMOPOLITAN_LIBC_ISYSTEM_MONETARY_H_
|
||||
#include "libc/str/locale.h"
|
||||
#include "libc/str/str.h"
|
||||
#endif /* COSMOPOLITAN_LIBC_ISYSTEM_MONETARY_H_ */
|
|
@ -2,6 +2,7 @@
|
|||
#define LIBC_ISYSTEM_SYS_SOCKET_H_
|
||||
#include "libc/calls/weirdtypes.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/sock/struct/cmsghdr.h"
|
||||
#include "libc/sock/struct/linger.h"
|
||||
#include "libc/sock/struct/msghdr.h"
|
||||
#include "libc/sock/struct/sockaddr.h"
|
||||
|
|
|
@ -16,27 +16,32 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/intrin/safemacros.internal.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/mem/internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
/**
|
||||
* Copies variable to environment.
|
||||
*
|
||||
* @return 0 on success, or -1 w/ errno
|
||||
* @raise EINVAL if `name` is empty or contains `'='`
|
||||
* @raise ENOMEM if we require more vespene gas
|
||||
* @see putenv(), getenv()
|
||||
*/
|
||||
int setenv(const char *name, const char *value, int overwrite) {
|
||||
int rc;
|
||||
char *s;
|
||||
size_t namelen, valuelen;
|
||||
if (isempty(name) || strchr(name, '=')) return einval();
|
||||
namelen = strlen(name);
|
||||
valuelen = strlen(value);
|
||||
s = malloc(namelen + valuelen + 2);
|
||||
if (!(s = malloc(namelen + valuelen + 2))) return -1;
|
||||
memcpy(mempcpy(mempcpy(s, name, namelen), "=", 1), value, valuelen + 1);
|
||||
rc = PutEnvImpl(s, overwrite);
|
||||
STRACE("setenv(%#s, %#s, %d) → %d", name, value, overwrite, rc);
|
||||
STRACE("setenv(%#s, %#s, %d) → %d% m", name, value, overwrite, rc);
|
||||
return rc;
|
||||
}
|
||||
|
|
|
@ -16,39 +16,51 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/syscall_support-sysv.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/nt/enum/processcreationflags.h"
|
||||
#include "libc/paths.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
|
||||
/**
|
||||
* Daemonizes process.
|
||||
* Runs process in background.
|
||||
*
|
||||
* On Unix this calls fork() and setsid(). On Windows this is
|
||||
* implemented using CreateProcess(kNtDetachedProcess).
|
||||
*
|
||||
* @return 0 on success, or -1 w/ errno
|
||||
*/
|
||||
int daemon(int nochdir, int noclose) {
|
||||
int fd;
|
||||
|
||||
switch (fork()) {
|
||||
switch (_fork(kNtDetachedProcess)) {
|
||||
case -1:
|
||||
return (-1);
|
||||
return -1;
|
||||
case 0:
|
||||
break;
|
||||
default:
|
||||
_Exit(0);
|
||||
}
|
||||
|
||||
if (setsid() == -1) {
|
||||
return -1;
|
||||
if (!IsWindows()) {
|
||||
if (setsid() == -1) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!nochdir) {
|
||||
chdir("/");
|
||||
_unassert(!chdir("/"));
|
||||
}
|
||||
|
||||
if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR)) != -1) {
|
||||
dup2(fd, 0);
|
||||
dup2(fd, 1);
|
||||
dup2(fd, 2);
|
||||
_unassert(dup2(fd, 0) == 0);
|
||||
_unassert(dup2(fd, 1) == 1);
|
||||
_unassert(dup2(fd, 2) == 2);
|
||||
if (fd > 2) {
|
||||
close(fd);
|
||||
_unassert(!close(fd));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include "libc/calls/internal.h"
|
||||
#include "libc/calls/ntspawn.h"
|
||||
#include "libc/calls/state.internal.h"
|
||||
#include "libc/calls/syscall-nt.internal.h"
|
||||
#include "libc/calls/syscall_support-nt.internal.h"
|
||||
#include "libc/calls/wincrash.internal.h"
|
||||
#include "libc/errno.h"
|
||||
|
@ -260,7 +261,7 @@ textwindows void WinMainForked(void) {
|
|||
longjmp(jb, 1);
|
||||
}
|
||||
|
||||
textwindows int sys_fork_nt(void) {
|
||||
textwindows int sys_fork_nt(uint32_t dwCreationFlags) {
|
||||
bool ok;
|
||||
jmp_buf jb;
|
||||
uint32_t oldprot;
|
||||
|
@ -302,7 +303,7 @@ textwindows int sys_fork_nt(void) {
|
|||
}
|
||||
#endif
|
||||
if (ntspawn(GetProgramExecutableName(), args, environ, forkvar, 0, 0,
|
||||
true, 0, 0, &startinfo, &procinfo) != -1) {
|
||||
true, dwCreationFlags, 0, &startinfo, &procinfo) != -1) {
|
||||
CloseHandle(procinfo.hThread);
|
||||
ok = WriteAll(writer, jb, sizeof(jb)) &&
|
||||
WriteAll(writer, &_mmi.i, sizeof(_mmi.i)) &&
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
#include "libc/calls/struct/sigset.internal.h"
|
||||
#include "libc/calls/syscall-nt.internal.h"
|
||||
#include "libc/calls/syscall-sysv.internal.h"
|
||||
#include "libc/calls/syscall_support-sysv.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/nt/process.h"
|
||||
|
@ -28,15 +29,7 @@
|
|||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/thread/tls.h"
|
||||
|
||||
/**
|
||||
* Creates new process.
|
||||
*
|
||||
* @return 0 to child, child pid to parent, or -1 w/ errno
|
||||
* @raise EAGAIN if `RLIMIT_NPROC` was exceeded or system lacked resources
|
||||
* @raise ENOMEM if we require more vespene gas
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
int fork(void) {
|
||||
int _fork(uint32_t dwCreationFlags) {
|
||||
axdx_t ad;
|
||||
sigset_t old, all;
|
||||
int ax, dx, parent;
|
||||
|
@ -52,7 +45,7 @@ int fork(void) {
|
|||
ax &= dx - 1;
|
||||
}
|
||||
} else {
|
||||
ax = sys_fork_nt();
|
||||
ax = sys_fork_nt(dwCreationFlags);
|
||||
}
|
||||
if (!ax) {
|
||||
if (!IsWindows()) {
|
||||
|
@ -73,3 +66,15 @@ int fork(void) {
|
|||
}
|
||||
return ax;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new process.
|
||||
*
|
||||
* @return 0 to child, child pid to parent, or -1 w/ errno
|
||||
* @raise EAGAIN if `RLIMIT_NPROC` was exceeded or system lacked resources
|
||||
* @raise ENOMEM if we require more vespene gas
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
int fork(void) {
|
||||
return _fork(0);
|
||||
}
|
||||
|
|
|
@ -103,6 +103,7 @@ void _intsort(int *, size_t);
|
|||
void _longsort(long *, size_t);
|
||||
bool _isheap(void *);
|
||||
int NtGetVersion(void) pureconst;
|
||||
unsigned _getcpucount(void) pureconst;
|
||||
long _missingno();
|
||||
void __oom_hook(size_t);
|
||||
void _loadxmm(void *);
|
||||
|
|
|
@ -49,7 +49,7 @@ long sysconf(int name) {
|
|||
case _SC_PAGESIZE:
|
||||
return FRAMESIZE;
|
||||
case _SC_NPROCESSORS_ONLN:
|
||||
n = GetCpuCount();
|
||||
n = _getcpucount();
|
||||
return n > 0 ? n : -1;
|
||||
default:
|
||||
return -1;
|
||||
|
|
|
@ -147,7 +147,6 @@
|
|||
COSMOPOLITAN_C_START_
|
||||
|
||||
long sysconf(int);
|
||||
unsigned GetCpuCount(void);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
// @return pid of child process or 0 if forked process
|
||||
// @returnstwice
|
||||
// @vforksafe
|
||||
vfork:
|
||||
vfork: xor %edi,%edi # dwCreationFlags
|
||||
#ifdef __SANITIZE_ADDRESS__
|
||||
jmp fork # TODO: asan and vfork don't mix?
|
||||
.endfn vfork,globl
|
||||
|
|
|
@ -47,7 +47,7 @@
|
|||
* @asyncsignalsafe
|
||||
* @restartable (unless SO_RCVTIMEO)
|
||||
*/
|
||||
ssize_t recvfrom(int fd, void *buf, size_t size, uint32_t flags,
|
||||
ssize_t recvfrom(int fd, void *buf, size_t size, int flags,
|
||||
struct sockaddr *opt_out_srcaddr,
|
||||
uint32_t *opt_inout_srcaddrsize) {
|
||||
ssize_t rc;
|
||||
|
|
|
@ -32,6 +32,8 @@
|
|||
/**
|
||||
* Sends a message from a socket.
|
||||
*
|
||||
* Note: Ancillary data currently isn't polyfilled across platforms.
|
||||
*
|
||||
* @param fd is the file descriptor returned by socket()
|
||||
* @param msg is a pointer to a struct msghdr containing all the allocated
|
||||
* buffers where to store incoming data.
|
||||
|
@ -46,6 +48,7 @@ ssize_t recvmsg(int fd, struct msghdr *msg, int flags) {
|
|||
ssize_t rc, got;
|
||||
struct msghdr msg2;
|
||||
union sockaddr_storage_bsd bsd;
|
||||
|
||||
if (IsAsan() && !__asan_is_valid_msghdr(msg)) {
|
||||
rc = efault();
|
||||
} else if (!IsWindows()) {
|
||||
|
@ -88,6 +91,7 @@ ssize_t recvmsg(int fd, struct msghdr *msg, int flags) {
|
|||
} else {
|
||||
rc = ebadf();
|
||||
}
|
||||
|
||||
#if defined(SYSDEBUG) && _DATATRACE
|
||||
if (__strace > 0) {
|
||||
if (!msg || (rc == -1 && errno == EFAULT)) {
|
||||
|
@ -99,11 +103,11 @@ ssize_t recvmsg(int fd, struct msghdr *msg, int flags) {
|
|||
if (msg->msg_controllen)
|
||||
kprintf(".control=%#.*hhs, ", msg->msg_controllen, msg->msg_control);
|
||||
if (msg->msg_flags) kprintf(".flags=%#x, ", msg->msg_flags);
|
||||
kprintf(".iov=", fd);
|
||||
DescribeIov(msg->msg_iov, msg->msg_iovlen, rc != -1 ? rc : 0);
|
||||
kprintf("}], %#x) → %'ld% m\n", flags, rc);
|
||||
kprintf(".iov=%s", DescribeIovec(rc, msg->msg_iov, msg->msg_iovlen));
|
||||
kprintf("], %#x) → %'ld% m\n", flags, rc);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
|
|
@ -18,13 +18,13 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/calls/struct/iovec.h"
|
||||
#include "libc/calls/struct/iovec.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/intrin/describeflags.internal.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/sock/internal.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/sock/struct/msghdr.h"
|
||||
|
@ -91,9 +91,8 @@ ssize_t sendmsg(int fd, const struct msghdr *msg, int flags) {
|
|||
if (msg->msg_controllen)
|
||||
kprintf(", .control=%#.*hhs, ", msg->msg_controllen, msg->msg_control);
|
||||
if (msg->msg_flags) kprintf(".flags=%#x, ", msg->msg_flags);
|
||||
kprintf(", .iov=", fd);
|
||||
DescribeIov(msg->msg_iov, msg->msg_iovlen, rc != -1 ? rc : 0);
|
||||
kprintf("}");
|
||||
kprintf(", .iov=%s",
|
||||
DescribeIovec(rc != -1 ? rc : -2, msg->msg_iov, msg->msg_iovlen));
|
||||
}
|
||||
kprintf(", %#x) → %'ld% m\n", flags, rc);
|
||||
}
|
||||
|
|
|
@ -52,7 +52,7 @@
|
|||
* @asyncsignalsafe
|
||||
* @restartable (unless SO_RCVTIMEO)
|
||||
*/
|
||||
ssize_t sendto(int fd, const void *buf, size_t size, uint32_t flags,
|
||||
ssize_t sendto(int fd, const void *buf, size_t size, int flags,
|
||||
const struct sockaddr *opt_addr, uint32_t addrsize) {
|
||||
ssize_t rc;
|
||||
uint32_t bsdaddrsize;
|
||||
|
|
42
libc/sock/struct/cmsghdr.h
Normal file
42
libc/sock/struct/cmsghdr.h
Normal file
|
@ -0,0 +1,42 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_SOCK_STRUCT_CMSGHDR_H_
|
||||
#define COSMOPOLITAN_LIBC_SOCK_STRUCT_CMSGHDR_H_
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
#define CMSG_DATA(cmsg) ((unsigned char *)(((struct cmsghdr *)(cmsg)) + 1))
|
||||
|
||||
#define CMSG_FIRSTHDR(mhdr) \
|
||||
((size_t)(mhdr)->msg_controllen >= sizeof(struct cmsghdr) \
|
||||
? (struct cmsghdr *)(mhdr)->msg_control \
|
||||
: (struct cmsghdr *)0)
|
||||
|
||||
#define CMSG_NXTHDR(mhdr, cmsg) \
|
||||
((cmsg)->cmsg_len < sizeof(struct cmsghdr) || \
|
||||
__CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= \
|
||||
__MHDR_END(mhdr) - (unsigned char *)(cmsg) \
|
||||
? 0 \
|
||||
: (struct cmsghdr *)__CMSG_NEXT(cmsg))
|
||||
|
||||
#define CMSG_ALIGN(len) \
|
||||
(((len) + sizeof(size_t) - 1) & (size_t) ~(sizeof(size_t) - 1))
|
||||
|
||||
#define CMSG_SPACE(len) (CMSG_ALIGN(len) + CMSG_ALIGN(sizeof(struct cmsghdr)))
|
||||
|
||||
#define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
|
||||
|
||||
#define __CMSG_LEN(cmsg) \
|
||||
(((cmsg)->cmsg_len + sizeof(long) - 1) & ~(long)(sizeof(long) - 1))
|
||||
#define __CMSG_NEXT(cmsg) ((unsigned char *)(cmsg) + __CMSG_LEN(cmsg))
|
||||
#define __MHDR_END(mhdr) \
|
||||
((unsigned char *)(mhdr)->msg_control + (mhdr)->msg_controllen)
|
||||
|
||||
struct cmsghdr { /* linux abi */
|
||||
uint32_t cmsg_len;
|
||||
uint32_t __pad1;
|
||||
int32_t cmsg_level;
|
||||
int32_t cmsg_type;
|
||||
};
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_SOCK_STRUCT_CMSGHDR_H_ */
|
14
libc/sock/struct/cmsghdr.internal.h
Normal file
14
libc/sock/struct/cmsghdr.internal.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_SOCK_STRUCT_CMSGHDR_INTERNAL_H_
|
||||
#define COSMOPOLITAN_LIBC_SOCK_STRUCT_CMSGHDR_INTERNAL_H_
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
struct cmsghdr_bsd {
|
||||
uint32_t cmsg_len;
|
||||
int32_t cmsg_level;
|
||||
int32_t cmsg_type;
|
||||
};
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_SOCK_STRUCT_CMSGHDR_INTERNAL_H_ */
|
|
@ -8,9 +8,9 @@ struct msghdr { /* Linux+NT ABI */
|
|||
void *msg_name; /* optional address */
|
||||
uint32_t msg_namelen; /* size of msg_name */
|
||||
struct iovec *msg_iov; /* scatter/gather array */
|
||||
int msg_iovlen; /* iovec count */
|
||||
void *msg_control; /* credentials and stuff */
|
||||
uint32_t msg_controllen; /* size of msg_control */
|
||||
int msg_iovlen; /* # elements in msg_iov */
|
||||
void *msg_control; /* ancillary data c. cmsghdr */
|
||||
uint32_t msg_controllen; /* ancillary data buffer len */
|
||||
uint32_t __pad0; /* reconcile abi */
|
||||
int msg_flags; /* MSG_XXX */
|
||||
};
|
||||
|
|
|
@ -41,8 +41,8 @@ int bind(int, const struct sockaddr *, uint32_t);
|
|||
int connect(int, const struct sockaddr *, uint32_t);
|
||||
int getsockname(int, struct sockaddr *, uint32_t *);
|
||||
int getpeername(int, struct sockaddr *, uint32_t *);
|
||||
ssize_t recvfrom(int, void *, size_t, uint32_t, struct sockaddr *, uint32_t *);
|
||||
ssize_t sendto(int, const void *, size_t, uint32_t, const struct sockaddr *,
|
||||
ssize_t recvfrom(int, void *, size_t, int, struct sockaddr *, uint32_t *);
|
||||
ssize_t sendto(int, const void *, size_t, int, const struct sockaddr *,
|
||||
uint32_t);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
|
|
|
@ -16,15 +16,26 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/stat.h"
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/fmt/itoa.h"
|
||||
#include "libc/fmt/magnumstrs.internal.h"
|
||||
#include "libc/intrin/safemacros.internal.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/append.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/s.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/x/x.h"
|
||||
#include "third_party/double-conversion/wrapper.h"
|
||||
|
||||
/**
|
||||
* @fileoverview Cosmopolitan Command Interpreter
|
||||
|
@ -33,27 +44,53 @@
|
|||
* enough shell script language support to support our build config.
|
||||
*/
|
||||
|
||||
#define STATE_SHELL 0
|
||||
#define STATE_STR 1
|
||||
#define STATE_QUO 2
|
||||
#define STATE_CMD 0
|
||||
#define STATE_VAR 1
|
||||
#define STATE_SINGLE 2
|
||||
#define STATE_QUOTED 3
|
||||
#define STATE_QUOTED_VAR 4
|
||||
#define STATE_WHITESPACE 5
|
||||
|
||||
static char *p;
|
||||
static char *q;
|
||||
static int vari;
|
||||
static size_t n;
|
||||
static char *cmd;
|
||||
static char var[32];
|
||||
static int lastchild;
|
||||
static int exitstatus;
|
||||
static char *args[8192];
|
||||
static const char *prog;
|
||||
static char errbuf[512];
|
||||
static char argbuf[ARG_MAX];
|
||||
static bool unsupported[256];
|
||||
|
||||
static ssize_t Write(int fd, const char *s) {
|
||||
return write(fd, s, strlen(s));
|
||||
}
|
||||
|
||||
static void Log(const char *s, ...) {
|
||||
va_list va;
|
||||
va_start(va, s);
|
||||
errbuf[0] = 0;
|
||||
do {
|
||||
strlcat(errbuf, s, sizeof(argbuf));
|
||||
} while ((s = va_arg(va, const char *)));
|
||||
strlcat(errbuf, "\n", sizeof(argbuf));
|
||||
Write(2, errbuf);
|
||||
va_end(va);
|
||||
}
|
||||
|
||||
static wontreturn void Wexit(int rc, const char *s, ...) {
|
||||
va_list va;
|
||||
va_start(va, s);
|
||||
errbuf[0] = 0;
|
||||
do {
|
||||
write(2, s, strlen(s));
|
||||
strlcat(errbuf, s, sizeof(argbuf));
|
||||
} while ((s = va_arg(va, const char *)));
|
||||
Write(2, errbuf);
|
||||
va_end(va);
|
||||
exit(rc);
|
||||
_Exit(rc);
|
||||
}
|
||||
|
||||
static wontreturn void UnsupportedSyntax(unsigned char c) {
|
||||
|
@ -86,86 +123,391 @@ static void Open(const char *path, int fd, int flags) {
|
|||
}
|
||||
|
||||
static wontreturn void Exec(void) {
|
||||
const char *s;
|
||||
if (!n) {
|
||||
Wexit(5, prog, ": error: too few args\n", 0);
|
||||
}
|
||||
_npassert(args[0][0]);
|
||||
if (!n) Wexit(5, prog, ": error: too few args\n", 0);
|
||||
execvp(args[0], args);
|
||||
SysExit(127, "execve", args[0]);
|
||||
}
|
||||
|
||||
static int GetSignalByName(const char *s) {
|
||||
for (int i = 0; kSignalNames[i].x != MAGNUM_TERMINATOR; ++i) {
|
||||
if (!strcmp(s, MAGNUM_STRING(kSignalNames, i) + 3)) {
|
||||
return MAGNUM_NUMBER(kSignalNames, i);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int True(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int False(void) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static wontreturn void Exit(void) {
|
||||
_Exit(n > 1 ? atoi(args[1]) : 0);
|
||||
}
|
||||
|
||||
static int Wait(void) {
|
||||
char ibuf[12];
|
||||
int e, rc, ws, pid;
|
||||
if (n > 1) {
|
||||
if (waitpid(atoi(args[1]), &ws, 0) == -1) {
|
||||
SysExit(22, "waitpid", prog);
|
||||
}
|
||||
rc = WIFEXITED(ws) ? WEXITSTATUS(ws) : 128 + WTERMSIG(ws);
|
||||
exitstatus = rc;
|
||||
} else {
|
||||
for (e = errno;;) {
|
||||
if (waitpid(-1, &ws, 0) == -1) {
|
||||
if (errno == ECHILD) {
|
||||
errno = e;
|
||||
break;
|
||||
}
|
||||
SysExit(22, "waitpid", prog);
|
||||
}
|
||||
}
|
||||
rc = 0;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int Echo(void) {
|
||||
int i = 1;
|
||||
bool once = false;
|
||||
const char *l = " ";
|
||||
if (i < n && !strcmp(args[i], "-l")) {
|
||||
++i, l = "\n";
|
||||
}
|
||||
for (; i < n; ++i) {
|
||||
if (once) {
|
||||
Write(1, l);
|
||||
} else {
|
||||
once = true;
|
||||
}
|
||||
Write(1, args[i]);
|
||||
}
|
||||
Write(1, "\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Read(void) {
|
||||
char *b = 0;
|
||||
unsigned char c;
|
||||
int a = 1, rc = 1;
|
||||
if (n >= 3 && !strcmp(args[1], "-p")) {
|
||||
Write(1, args[2]);
|
||||
a = 3;
|
||||
}
|
||||
appendr(&b, 0);
|
||||
while (read(0, &c, 1) > 0) {
|
||||
if (c == '\n') {
|
||||
rc = 0;
|
||||
break;
|
||||
}
|
||||
appendw(&b, c);
|
||||
}
|
||||
if (a < n) {
|
||||
setenv(args[1], b, true);
|
||||
}
|
||||
free(b);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int Cd(void) {
|
||||
const char *s = n > 1 ? args[1] : getenv("HOME");
|
||||
if (s) {
|
||||
if (!chdir(s)) {
|
||||
return 0;
|
||||
} else {
|
||||
Log("chdir: ", s, ": ", _strerdoc(errno), 0);
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
Log("chdir: missing argument", 0);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
static int Mkdir(void) {
|
||||
int i = 1;
|
||||
int (*f)(const char *, unsigned) = mkdir;
|
||||
if (n >= 3 && !strcmp(args[1], "-p")) ++i, f = makedirs;
|
||||
for (; i < n; ++i) {
|
||||
if (f(args[i], 0755)) {
|
||||
Log("mkdir: ", args[i], ": ", _strerdoc(errno), 0);
|
||||
return errno;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Kill(void) {
|
||||
int sig, rc = 0, i = 1;
|
||||
if (i < n && args[i][0] == '-') {
|
||||
sig = GetSignalByName(args[i++] + 1);
|
||||
if (!sig) return 1;
|
||||
} else {
|
||||
sig = SIGTERM;
|
||||
}
|
||||
for (; i < n; ++i) {
|
||||
if (kill(atoi(args[i]), sig)) {
|
||||
Log("kill: ", args[i], ": ", _strerdoc(errno), 0);
|
||||
rc = 1;
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int Toupper(void) {
|
||||
int i, n;
|
||||
char b[512];
|
||||
while ((n = read(0, b, 512)) > 0) {
|
||||
for (i = 0; i < n; ++i) {
|
||||
b[i] = toupper(b[i]);
|
||||
}
|
||||
write(1, b, n);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Usleep(void) {
|
||||
struct timespec t, *p = 0;
|
||||
if (n > 1) {
|
||||
t = _timespec_frommicros(atoi(args[1]));
|
||||
p = &t;
|
||||
}
|
||||
return clock_nanosleep(0, 0, p, 0);
|
||||
}
|
||||
|
||||
static int Test(void) {
|
||||
struct stat st;
|
||||
if (n && !strcmp(args[n - 1], "]")) --n;
|
||||
if (n == 4 && !strcmp(args[2], "=")) {
|
||||
return !!strcmp(args[1], args[3]);
|
||||
} else if (n == 4 && !strcmp(args[2], "!=")) {
|
||||
return !strcmp(args[1], args[3]);
|
||||
} else if (n == 3 && !strcmp(args[1], "-n")) {
|
||||
return !(strlen(args[2]) > 0);
|
||||
} else if (n == 3 && !strcmp(args[1], "-z")) {
|
||||
return !(strlen(args[2]) == 0);
|
||||
} else if (n == 3 && !strcmp(args[1], "-e")) {
|
||||
return !!stat(args[2], &st);
|
||||
} else if (n == 3 && !strcmp(args[1], "-f")) {
|
||||
return !stat(args[2], &st) && S_ISREG(st.st_mode);
|
||||
} else if (n == 3 && !strcmp(args[1], "-d")) {
|
||||
return !stat(args[2], &st) && S_ISDIR(st.st_mode);
|
||||
} else if (n == 3 && !strcmp(args[1], "-h")) {
|
||||
return !stat(args[2], &st) && S_ISLNK(st.st_mode);
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
static int TryBuiltin(void) {
|
||||
if (!n) return 0;
|
||||
if (!strcmp(args[0], "exit")) Exit();
|
||||
if (!strcmp(args[0], "cd")) return Cd();
|
||||
if (!strcmp(args[0], "[")) return Test();
|
||||
if (!strcmp(args[0], "wait")) return Wait();
|
||||
if (!strcmp(args[0], "echo")) return Echo();
|
||||
if (!strcmp(args[0], "read")) return Read();
|
||||
if (!strcmp(args[0], "true")) return True();
|
||||
if (!strcmp(args[0], "test")) return Test();
|
||||
if (!strcmp(args[0], "kill")) return Kill();
|
||||
if (!strcmp(args[0], "mkdir")) return Mkdir();
|
||||
if (!strcmp(args[0], "false")) return False();
|
||||
if (!strcmp(args[0], "usleep")) return Usleep();
|
||||
if (!strcmp(args[0], "toupper")) return Toupper();
|
||||
return -1;
|
||||
}
|
||||
|
||||
static wontreturn void Launch(void) {
|
||||
int rc;
|
||||
if ((rc = TryBuiltin()) != -1) _Exit(rc);
|
||||
Exec();
|
||||
}
|
||||
|
||||
static void Pipe(void) {
|
||||
int pid, pfds[2];
|
||||
if (pipe2(pfds, O_CLOEXEC)) {
|
||||
SysExit(8, "pipe2", prog);
|
||||
}
|
||||
if ((pid = vfork()) == -1) {
|
||||
SysExit(9, "vfork", prog);
|
||||
}
|
||||
if (pipe2(pfds, O_CLOEXEC)) SysExit(8, "pipe2", prog);
|
||||
if ((pid = fork()) == -1) SysExit(9, "vfork", prog);
|
||||
if (!pid) {
|
||||
dup2(pfds[1], 1);
|
||||
Exec();
|
||||
_unassert(dup2(pfds[1], 1) == 1);
|
||||
// we can't rely on cloexec because builtins
|
||||
if (pfds[0] != 1) _unassert(!close(pfds[0]));
|
||||
if (pfds[1] != 1) _unassert(!close(pfds[1]));
|
||||
Launch();
|
||||
}
|
||||
dup2(pfds[0], 0);
|
||||
_unassert(!dup2(pfds[0], 0));
|
||||
if (pfds[1]) _unassert(!close(pfds[1]));
|
||||
n = 0;
|
||||
}
|
||||
|
||||
static int Run(void) {
|
||||
int exitstatus, ws, pid;
|
||||
if ((exitstatus = TryBuiltin()) == -1) {
|
||||
if ((pid = vfork()) == -1) SysExit(21, "vfork", prog);
|
||||
if (!pid) Exec();
|
||||
if (waitpid(pid, &ws, 0) == -1) SysExit(22, "waitpid", prog);
|
||||
exitstatus = WIFEXITED(ws) ? WEXITSTATUS(ws) : 128 + WTERMSIG(ws);
|
||||
}
|
||||
n = 0;
|
||||
return exitstatus;
|
||||
}
|
||||
|
||||
static void Async(void) {
|
||||
if ((lastchild = fork()) == -1) SysExit(21, "vfork", prog);
|
||||
if (!lastchild) Launch();
|
||||
n = 0;
|
||||
}
|
||||
|
||||
static const char *IntToStr(int x) {
|
||||
static char ibuf[12];
|
||||
FormatInt32(ibuf, x);
|
||||
return ibuf;
|
||||
}
|
||||
|
||||
static const char *GetEnv(const char *key) {
|
||||
if (key[0] == '$' && !key[1]) {
|
||||
return IntToStr(getpid());
|
||||
} else if (key[0] == '!' && !key[1]) {
|
||||
return IntToStr(lastchild);
|
||||
} else if (key[0] == '?' && !key[1]) {
|
||||
return IntToStr(exitstatus);
|
||||
} else {
|
||||
return getenv(key);
|
||||
}
|
||||
}
|
||||
|
||||
static bool IsVarName(int c) {
|
||||
return isalnum(c) || c == '_' ||
|
||||
(!vari && (c == '?' || c == '!' || c == '$'));
|
||||
}
|
||||
|
||||
static inline void Append(int c) {
|
||||
_unassert(q + 1 < argbuf + sizeof(argbuf));
|
||||
*q++ = c;
|
||||
}
|
||||
|
||||
static char *Tokenize(void) {
|
||||
char *r;
|
||||
int c, t;
|
||||
while (*p == ' ' || *p == '\t' || *p == '\n' ||
|
||||
(p[0] == '\\' && p[1] == '\n')) {
|
||||
++p;
|
||||
}
|
||||
if (!*p) return 0;
|
||||
t = STATE_SHELL;
|
||||
for (r = q;; ++p) {
|
||||
const char *s;
|
||||
int c, t, j, k, rc;
|
||||
for (r = q, t = STATE_WHITESPACE;; ++p) {
|
||||
switch (t) {
|
||||
|
||||
case STATE_SHELL:
|
||||
case STATE_WHITESPACE:
|
||||
if (!*p) return 0;
|
||||
if (*p == ' ' || *p == '\t' || *p == '\n' ||
|
||||
(p[0] == '\\' && p[1] == '\n')) {
|
||||
continue;
|
||||
}
|
||||
t = STATE_CMD;
|
||||
// fallthrough
|
||||
|
||||
case STATE_CMD:
|
||||
if (unsupported[*p & 255]) {
|
||||
UnsupportedSyntax(*p);
|
||||
}
|
||||
if (!*p || *p == ' ' || *p == '\t') {
|
||||
*q++ = 0;
|
||||
Append(0);
|
||||
return r;
|
||||
} else if (*p == '"') {
|
||||
t = STATE_QUO;
|
||||
t = STATE_QUOTED;
|
||||
} else if (*p == '\'') {
|
||||
t = STATE_STR;
|
||||
t = STATE_SINGLE;
|
||||
} else if (*p == '$') {
|
||||
t = STATE_VAR;
|
||||
var[(vari = 0)] = 0;
|
||||
} else if (*p == '\\') {
|
||||
if (!p[1]) UnsupportedSyntax(*p);
|
||||
*q++ = *++p;
|
||||
Append(*++p);
|
||||
} else if (*p == '|') {
|
||||
if (q > r) {
|
||||
*q = 0;
|
||||
Append(0);
|
||||
return r;
|
||||
} else if (p[1] == '|') {
|
||||
rc = Run();
|
||||
if (!rc) {
|
||||
_Exit(0);
|
||||
} else {
|
||||
++p;
|
||||
t = STATE_WHITESPACE;
|
||||
}
|
||||
} else {
|
||||
Pipe();
|
||||
++p;
|
||||
t = STATE_WHITESPACE;
|
||||
}
|
||||
} else if (*p == ';') {
|
||||
if (q > r) {
|
||||
Append(0);
|
||||
return r;
|
||||
} else {
|
||||
Run();
|
||||
t = STATE_WHITESPACE;
|
||||
}
|
||||
} else if (*p == '&') {
|
||||
if (q > r) {
|
||||
Append(0);
|
||||
return r;
|
||||
} else if (p[1] == '&') {
|
||||
rc = Run();
|
||||
if (!rc) {
|
||||
++p;
|
||||
t = STATE_WHITESPACE;
|
||||
} else {
|
||||
_Exit(rc);
|
||||
}
|
||||
} else {
|
||||
Async();
|
||||
t = STATE_WHITESPACE;
|
||||
}
|
||||
} else {
|
||||
*q++ = *p;
|
||||
Append(*p);
|
||||
}
|
||||
break;
|
||||
|
||||
case STATE_STR:
|
||||
if (!*p) {
|
||||
Wexit(6, "cmd: error: unterminated single string\n", 0);
|
||||
case STATE_VAR:
|
||||
if (IsVarName(*p)) {
|
||||
_unassert(vari + 1 < sizeof(var));
|
||||
var[vari++] = *p;
|
||||
var[vari] = 0;
|
||||
} else {
|
||||
// XXX: we need to find a simple elegant way to break up
|
||||
// unquoted variable expansions into multiple args.
|
||||
if ((s = GetEnv(var))) {
|
||||
if ((j = strlen(s))) {
|
||||
_unassert(q + j < argbuf + sizeof(argbuf));
|
||||
q = mempcpy(q, s, j);
|
||||
}
|
||||
}
|
||||
--p;
|
||||
t = STATE_CMD;
|
||||
}
|
||||
break;
|
||||
|
||||
case STATE_SINGLE:
|
||||
if (!*p) goto UnterminatedString;
|
||||
if (*p == '\'') {
|
||||
t = STATE_SHELL;
|
||||
t = STATE_CMD;
|
||||
} else {
|
||||
*q++ = *p;
|
||||
}
|
||||
break;
|
||||
|
||||
case STATE_QUO:
|
||||
if (!*p) {
|
||||
Wexit(6, "cmd: error: unterminated quoted string\n", 0);
|
||||
}
|
||||
UnterminatedString:
|
||||
Wexit(6, "cmd: error: unterminated string\n", 0);
|
||||
|
||||
case STATE_QUOTED:
|
||||
if (!*p) goto UnterminatedString;
|
||||
if (*p == '"') {
|
||||
t = STATE_SHELL;
|
||||
t = STATE_CMD;
|
||||
} else if (p[0] == '$') {
|
||||
t = STATE_QUOTED_VAR;
|
||||
var[(vari = 0)] = 0;
|
||||
} else if (p[0] == '\\') {
|
||||
switch ((c = *++p)) {
|
||||
case 0:
|
||||
|
@ -187,6 +529,24 @@ static char *Tokenize(void) {
|
|||
}
|
||||
break;
|
||||
|
||||
case STATE_QUOTED_VAR:
|
||||
if (!*p) goto UnterminatedString;
|
||||
if (IsVarName(*p)) {
|
||||
_unassert(vari + 1 < sizeof(var));
|
||||
var[vari++] = *p;
|
||||
var[vari] = 0;
|
||||
} else {
|
||||
if ((s = GetEnv(var))) {
|
||||
if ((j = strlen(s))) {
|
||||
_unassert(q + j < argbuf + sizeof(argbuf));
|
||||
q = mempcpy(q, s, j);
|
||||
}
|
||||
}
|
||||
--p;
|
||||
t = STATE_QUOTED;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
unreachable;
|
||||
}
|
||||
|
@ -219,13 +579,9 @@ int cocmd(int argc, char *argv[]) {
|
|||
unsupported['*'] = true;
|
||||
unsupported['('] = true;
|
||||
unsupported[')'] = true;
|
||||
unsupported['['] = true;
|
||||
unsupported[']'] = true;
|
||||
unsupported['{'] = true;
|
||||
unsupported['}'] = true;
|
||||
unsupported[';'] = true;
|
||||
unsupported['?'] = true;
|
||||
unsupported['!'] = true;
|
||||
|
||||
if (argc != 3) {
|
||||
Wexit(10, prog, ": error: wrong number of args\n", 0);
|
||||
|
@ -270,5 +626,5 @@ int cocmd(int argc, char *argv[]) {
|
|||
}
|
||||
}
|
||||
|
||||
Exec();
|
||||
Launch();
|
||||
}
|
||||
|
|
|
@ -18,11 +18,17 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/safemacros.internal.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
static char *DirName(const char *path) {
|
||||
char *dirp;
|
||||
if (!(path = strdup(path))) return 0;
|
||||
dirp = strdup(dirname(path));
|
||||
free(path);
|
||||
return dirp;
|
||||
}
|
||||
|
||||
static int MakeDirs(const char *path, unsigned mode, int e) {
|
||||
int rc;
|
||||
|
@ -34,7 +40,9 @@ static int MakeDirs(const char *path, unsigned mode, int e) {
|
|||
if (errno != ENOENT) {
|
||||
return -1;
|
||||
}
|
||||
dir = xdirname(path);
|
||||
if (!(dir = DirName(path))) {
|
||||
return -1;
|
||||
}
|
||||
if (strcmp(dir, path)) {
|
||||
rc = MakeDirs(dir, mode, e);
|
||||
} else {
|
|
@ -16,6 +16,7 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/paths.h"
|
||||
|
@ -31,14 +32,8 @@
|
|||
/**
|
||||
* Spawns subprocess and returns pipe stream.
|
||||
*
|
||||
* This embeds the cocmd.com shell interpreter which supports a limited
|
||||
* subset of the bourne shell that's significantly faster:
|
||||
*
|
||||
* - pipelines
|
||||
* - single quotes
|
||||
* - double quotes
|
||||
* - input redirection, e.g. `<path`
|
||||
* - output redirection, e.g. `>path`, `>>append`, `2>err.txt, `2>&1`
|
||||
* This embeds the Cosmopolitan Command Interpreter which provides
|
||||
* Bourne-like syntax on all platforms including Windows.
|
||||
*
|
||||
* @see pclose()
|
||||
*/
|
||||
|
@ -54,28 +49,30 @@ FILE *popen(const char *cmdline, const char *mode) {
|
|||
einval();
|
||||
return NULL;
|
||||
}
|
||||
if (pipe(pipefds) == -1) return NULL;
|
||||
fcntl(pipefds[dir], F_SETFD, FD_CLOEXEC);
|
||||
if (pipe2(pipefds, O_CLOEXEC) == -1) return NULL;
|
||||
if ((f = fdopen(pipefds[dir], mode))) {
|
||||
switch ((pid = fork())) {
|
||||
case 0:
|
||||
dup2(pipefds[!dir], !dir);
|
||||
_unassert(dup2(pipefds[!dir], !dir) == !dir);
|
||||
// we can't rely on cloexec because cocmd builtins don't execev
|
||||
if (pipefds[0] != !dir) _unassert(!close(pipefds[0]));
|
||||
if (pipefds[1] != !dir) _unassert(!close(pipefds[1]));
|
||||
_Exit(cocmd(3, (char *[]){"popen", "-c", cmdline, 0}));
|
||||
default:
|
||||
f->pid = pid;
|
||||
close(pipefds[!dir]);
|
||||
_unassert(!close(pipefds[!dir]));
|
||||
return f;
|
||||
case -1:
|
||||
e = errno;
|
||||
fclose(f);
|
||||
close(pipefds[!dir]);
|
||||
_unassert(!fclose(f));
|
||||
_unassert(!close(pipefds[!dir]));
|
||||
errno = e;
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
e = errno;
|
||||
close(pipefds[0]);
|
||||
close(pipefds[1]);
|
||||
_unassert(!close(pipefds[0]));
|
||||
_unassert(!close(pipefds[1]));
|
||||
errno = e;
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
@ -16,11 +16,14 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/sched_param.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/fmt.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/spawn.h"
|
||||
#include "libc/stdio/spawna.internal.h"
|
||||
#include "libc/str/str.h"
|
||||
|
@ -47,7 +50,7 @@ int posix_spawn(int *pid, const char *path,
|
|||
if (setpgid(0, attrp->posix_attr_pgroup)) _Exit(127);
|
||||
}
|
||||
if (attrp->posix_attr_flags & POSIX_SPAWN_SETSIGMASK) {
|
||||
sigprocmask(SIG_SETMASK, &attrp->posix_attr_sigmask, NULL);
|
||||
sigprocmask(SIG_SETMASK, &attrp->posix_attr_sigmask, 0);
|
||||
}
|
||||
if (attrp->posix_attr_flags & POSIX_SPAWN_RESETIDS) {
|
||||
setuid(getuid());
|
||||
|
@ -59,7 +62,7 @@ int posix_spawn(int *pid, const char *path,
|
|||
sigfillset(&allsigs);
|
||||
for (s = 0; sigismember(&allsigs, s); s++) {
|
||||
if (sigismember(&attrp->posix_attr_sigdefault, s)) {
|
||||
if (sigaction(s, &dfl, NULL) == -1) _Exit(127);
|
||||
if (sigaction(s, &dfl, 0) == -1) _Exit(127);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -96,15 +99,16 @@ int posix_spawn(int *pid, const char *path,
|
|||
if (attrp->posix_attr_flags & POSIX_SPAWN_SETSCHEDULER) {
|
||||
if (sched_setscheduler(0, attrp->posix_attr_schedpolicy,
|
||||
&attrp->posix_attr_schedparam) == -1) {
|
||||
_Exit(127);
|
||||
if (errno != ENOSYS) _Exit(127);
|
||||
}
|
||||
}
|
||||
if (attrp->posix_attr_flags & POSIX_SPAWN_SETSCHEDPARAM) {
|
||||
if (sched_setparam(0, &attrp->posix_attr_schedparam) == -1) {
|
||||
_Exit(127);
|
||||
if (errno != ENOSYS) _Exit(127);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!envp) envp = environ;
|
||||
execve(path, argv, envp);
|
||||
_Exit(127);
|
||||
} else {
|
||||
|
|
|
@ -86,6 +86,7 @@ int setvbuf(FILE *, char *, int, size_t);
|
|||
int pclose(FILE *);
|
||||
char *ctermid(char *);
|
||||
void perror(const char *) relegated;
|
||||
int makedirs(const char *, unsigned);
|
||||
|
||||
typedef uint64_t fpos_t;
|
||||
char *gets(char *) paramsnonnull();
|
||||
|
|
|
@ -33,14 +33,8 @@
|
|||
/**
|
||||
* Launches program with system command interpreter.
|
||||
*
|
||||
* This embeds the cocmd.com shell interpreter which supports a limited
|
||||
* subset of the bourne shell that's significantly faster:
|
||||
*
|
||||
* - pipelines
|
||||
* - single quotes
|
||||
* - double quotes
|
||||
* - input redirection, e.g. `<path`
|
||||
* - output redirection, e.g. `>path`, `>>append`, `2>err.txt, `2>&1`
|
||||
* This embeds the Cosmopolitan Command Interpreter which provides
|
||||
* Bourne-like syntax on all platforms including Windows.
|
||||
*
|
||||
* @param cmdline is an interpreted Turing-complete command
|
||||
* @return -1 if child process couldn't be created, otherwise a wait
|
||||
|
|
|
@ -78,6 +78,7 @@ wint_t towlower_l(wint_t, locale_t);
|
|||
wint_t towupper_l(wint_t, locale_t);
|
||||
int strcasecmp_l(const char *, const char *, locale_t);
|
||||
int strncasecmp_l(const char *, const char *, size_t, locale_t);
|
||||
ssize_t strfmon_l(char *, size_t, locale_t, const char *, ...);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
|
|
|
@ -151,6 +151,7 @@ wchar_t *wmempcpy(wchar_t *, const wchar_t *, size_t) memcpyesque;
|
|||
wchar_t *wmemmove(wchar_t *, const wchar_t *, size_t) memcpyesque;
|
||||
void *tinymemccpy(void *, const void *, int, size_t) memcpyesque;
|
||||
void *memmem(const void *, size_t, const void *, size_t) libcesque nosideeffect;
|
||||
ssize_t strfmon(char *, size_t, const char *, ...);
|
||||
long a64l(const char *);
|
||||
char *l64a(long);
|
||||
|
||||
|
|
|
@ -61,6 +61,7 @@ noasan static inline const char *strchr_sse(const char *s, unsigned char c) {
|
|||
* @return pointer to first instance of c or NULL if not found
|
||||
* noting that if c is NUL we return pointer to terminator
|
||||
* @asyncsignalsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
char *strchr(const char *s, int c) {
|
||||
const char *r;
|
||||
|
|
|
@ -60,6 +60,7 @@
|
|||
* @return dest
|
||||
* @see stpncpy(), memccpy()
|
||||
* @asyncsignalsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
char *strncpy(char *dest, const char *src, size_t stride) {
|
||||
size_t i;
|
||||
|
|
|
@ -38,24 +38,41 @@ COSMOPOLITAN_C_START_
|
|||
* _Exit(1);
|
||||
* EXITS(1);
|
||||
* }
|
||||
*
|
||||
* The above are shorthand for:
|
||||
*
|
||||
* TEST(my, test) {
|
||||
* SPAWN(fork);
|
||||
* // communicate with parent
|
||||
* PARENT();
|
||||
* // communicate with child
|
||||
* WAIT(exit, 0)
|
||||
* }
|
||||
*
|
||||
* These macros cause a local variable named `child` with the child pid
|
||||
* to be defined.
|
||||
*/
|
||||
|
||||
#define SPAWN(METHOD) \
|
||||
{ \
|
||||
int _pid, _failed = g_testlib_failed; \
|
||||
ASSERT_NE(-1, (_pid = METHOD())); \
|
||||
if (!_pid) {
|
||||
#define SPAWN(METHOD) \
|
||||
{ \
|
||||
int child, _failed = g_testlib_failed; \
|
||||
ASSERT_NE(-1, (child = METHOD())); \
|
||||
if (!child) {
|
||||
|
||||
#define EXITS(rc) \
|
||||
_Exit(MAX(0, MIN(255, g_testlib_failed - _failed))); \
|
||||
} \
|
||||
testlib_waitforexit(__FILE__, __LINE__, #rc, rc, _pid); \
|
||||
#define EXITS(CODE) \
|
||||
PARENT() \
|
||||
WAIT(exit, CODE)
|
||||
|
||||
#define TERMS(SIG) \
|
||||
PARENT() \
|
||||
WAIT(term, SIG)
|
||||
|
||||
#define PARENT() \
|
||||
_Exit(MAX(0, MIN(255, g_testlib_failed - _failed))); \
|
||||
}
|
||||
|
||||
#define TERMS(sig) \
|
||||
_Exit(MAX(0, MIN(255, g_testlib_failed - _failed))); \
|
||||
} \
|
||||
testlib_waitforterm(__FILE__, __LINE__, #sig, sig, _pid); \
|
||||
#define WAIT(KIND, CODE) \
|
||||
testlib_waitfor##KIND(__FILE__, __LINE__, #CODE, CODE, child); \
|
||||
}
|
||||
|
||||
void testlib_waitforexit(const char *, int, const char *, int, int);
|
||||
|
|
|
@ -52,7 +52,6 @@ void *xload(bool *, void **, const void *, size_t, size_t);
|
|||
void *xloadzd(bool *, void **, const void *, size_t, size_t, size_t, size_t,
|
||||
uint32_t);
|
||||
int rmrf(const char *);
|
||||
int makedirs(const char *, unsigned);
|
||||
char *xbasename(const char *) paramsnonnull()
|
||||
returnspointerwithnoaliases dontthrow nocallback dontdiscard returnsnonnull;
|
||||
char *xdirname(const char *) paramsnonnull()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue