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:
Justine Tunney 2022-10-11 21:06:27 -07:00
parent e7329b7cba
commit b41f91c658
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
80 changed files with 1370 additions and 344 deletions

View file

@ -21,9 +21,10 @@
#include "libc/errno.h"
#include "libc/fmt/fmt.h"
#include "libc/log/check.h"
#include "libc/mem/mem.h"
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/o.h"
#include "libc/testlib/testlib.h"

View file

@ -19,9 +19,12 @@
#include "libc/calls/calls.h"
#include "libc/calls/struct/cpuset.h"
#include "libc/dce.h"
#include "libc/fmt/conv.h"
#include "libc/intrin/popcnt.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/sysconf.h"
#include "libc/stdio/spawn.h"
#include "libc/testlib/subprocess.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/thread.h"
#include "libc/thread/thread2.h"
@ -44,7 +47,7 @@ TEST(sched_getaffinity, firstOnly) {
}
TEST(sched_getaffinity, secondOnly) {
if (GetCpuCount() < 2) return;
if (_getcpucount() < 2) return;
cpu_set_t x, y;
CPU_ZERO(&x);
CPU_SET(1, &x);
@ -55,6 +58,48 @@ TEST(sched_getaffinity, secondOnly) {
EXPECT_TRUE(CPU_ISSET(1, &y));
}
TEST(sched_setaffinity, isInheritedAcrossFork) {
cpu_set_t x, y;
CPU_ZERO(&x);
CPU_SET(0, &x);
ASSERT_SYS(0, 0, sched_setaffinity(0, sizeof(x), &x));
SPAWN(fork);
ASSERT_SYS(0, 0, sched_getaffinity(0, sizeof(y), &y));
EXPECT_EQ(1, CPU_COUNT(&y));
EXPECT_TRUE(CPU_ISSET(0, &y));
EXPECT_FALSE(CPU_ISSET(1, &y));
EXITS(0);
}
__attribute__((__constructor__)) static void init(void) {
cpu_set_t y;
switch (atoi(nulltoempty(getenv("THE_DOGE")))) {
case 42:
ASSERT_SYS(0, 0, sched_getaffinity(0, sizeof(y), &y));
EXPECT_EQ(1, CPU_COUNT(&y));
EXPECT_TRUE(CPU_ISSET(0, &y));
EXPECT_FALSE(CPU_ISSET(1, &y));
exit(42);
default:
break;
}
}
TEST(sched_setaffinity, isInheritedAcrossExecve) {
cpu_set_t x, y;
CPU_ZERO(&x);
CPU_SET(0, &x);
ASSERT_SYS(0, 0, sched_setaffinity(0, sizeof(x), &x));
int rc, ws, pid;
char *prog = GetProgramExecutableName();
char *args[] = {program_invocation_name, NULL};
char *envs[] = {"THE_DOGE=42", NULL};
EXPECT_EQ(0, posix_spawn(&pid, prog, NULL, NULL, args, envs));
EXPECT_NE(-1, waitpid(pid, &ws, 0));
EXPECT_TRUE(WIFEXITED(ws));
EXPECT_EQ(42, WEXITSTATUS(ws));
}
TEST(sched_getaffinity, getpid) {
cpu_set_t x, y;
CPU_ZERO(&x);

View file

@ -22,9 +22,10 @@
#include "libc/calls/struct/stat.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/nt/files.h"
#include "libc/mem/gc.internal.h"
#include "libc/nt/files.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/nr.h"

View file

@ -558,6 +558,20 @@ TEST(strtoul, testoverflow) {
ASSERT_STREQ("", e);
}
TEST(strtol, invalidHex_consistentWithBsd) {
char *c = 0;
long x = strtol("0xz", &c, 16);
ASSERT_EQ(0, x);
ASSERT_STREQ("z", c);
}
TEST(strtol, invalidHex_consistentWithBsd2) {
char *c = 0;
long x = strtol("0xez", &c, 16);
ASSERT_EQ(0xe, x);
ASSERT_STREQ("z", c);
}
BENCH(atoi, bench) {
EZBENCH2("atoi 10⁸", donothing, EXPROPRIATE(atoi(VEIL("r", "100000000"))));
EZBENCH2("strtol 10⁸", donothing,

View file

@ -162,7 +162,7 @@ void *Worker(void *arg) {
}
BENCH(malloc, torture) {
int i, n = GetCpuCount() * 2;
int i, n = _getcpucount() * 2;
pthread_t *t = _gc(malloc(sizeof(pthread_t) * n));
if (!n) return;
printf("\nmalloc torture test w/ %d threads and %d iterations\n", n,

View file

@ -0,0 +1,56 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/struct/sigset.h"
#include "libc/dce.h"
#include "libc/intrin/kprintf.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/o.h"
#include "libc/testlib/subprocess.h"
#include "libc/testlib/testlib.h"
#include "libc/time/time.h"
#include "libc/x/x.h"
char testlib_enable_tmp_setup_teardown;
TEST(daemon, test) {
int dirfd;
char buf[512];
SPAWN(fork);
ASSERT_SYS(0, 3, open(".", O_RDONLY | O_DIRECTORY));
ASSERT_SYS(0, 0, daemon(false, false));
ASSERT_SYS(0, 4, openat(3, "ok", O_WRONLY | O_CREAT | O_TRUNC, 0644));
ASSERT_NE(NULL, getcwd(buf, sizeof(buf)));
ASSERT_SYS(0, 0, write(4, buf, strlen(buf)));
ASSERT_SYS(0, 0, close(4));
ASSERT_SYS(0, 0, close(3));
EXITS(0);
for (int i = 0; i < 13; ++i) {
bzero(buf, 512);
open("ok", O_RDONLY);
read(3, buf, 511);
close(3);
if (!strcmp(IsWindows() ? "/C/" : "/", buf)) {
return;
}
usleep(1000L << i);
}
ASSERT_TRUE(false);
}

View file

@ -0,0 +1,27 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/runtime/runtime.h"
#include "libc/testlib/subprocess.h"
#include "libc/testlib/testlib.h"
TEST(omg, test) {
SPAWN(fork);
EXITS(0);
}

View file

@ -69,10 +69,16 @@ o/$(MODE)/test/libc/sock/sendrecvmsg_test.com.runs \
o/$(MODE)/test/libc/sock/nointernet_test.com.runs: \
private .PLEDGE = stdio rpath wpath cpath fattr proc inet recvfd sendfd
o/$(MODE)/test/libc/sock/socket_test.com.runs: .INTERNET = 1 # todo: ipv6 filtering
o/$(MODE)/test/libc/sock/socket_test.com.runs: \
private .INTERNET = 1 # todo: ipv6 filtering
o/$(MODE)/test/libc/sock/socket_test.com.runs: \
private .PLEDGE = stdio rpath wpath cpath fattr proc inet
o/$(MODE)/test/libc/sock/recvmsg_test.com.runs: \
private .INTERNET = 1 # need to bind to 0.0.0.0
o/$(MODE)/test/libc/sock/recvmsg_test.com.runs: \
private .PLEDGE = stdio rpath wpath cpath fattr proc inet recvfd sendfd
o/$(MODE)/test/libc/sock/shutdown_test.com.runs: \
private .PLEDGE = stdio rpath wpath cpath fattr proc inet

View file

@ -16,12 +16,12 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/mem/mem.h"
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/spawn.h"
#include "libc/thread/thread.h"
#include "libc/x/x.h"
#define DIR \
"a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/A/B/C/D/E/F/G/H/I/J/K/" \

View file

@ -0,0 +1,95 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/dce.h"
#include "libc/fmt/fmt.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/sig.h"
#include "libc/testlib/testlib.h"
FILE *f;
char buf[32];
char testlib_enable_tmp_setup_teardown;
TEST(popen, command) {
char foo[6];
testlib_extract("/zip/echo.com", "echo.com", 0755);
ASSERT_NE(NULL, (f = popen("./echo.com hello", "r")));
ASSERT_NE(NULL, fgets(foo, sizeof(foo), f));
ASSERT_STREQ("hello", foo);
ASSERT_EQ(0, pclose(f));
}
TEST(popen, semicolon) {
ASSERT_NE(NULL, (f = popen("echo hello;echo there", "r")));
ASSERT_STREQ("hello\n", fgets(buf, sizeof(buf), f));
ASSERT_STREQ("there\n", fgets(buf, sizeof(buf), f));
ASSERT_EQ(0, pclose(f));
}
TEST(popen, singleQuotes) {
setenv("there", "a b c", true);
ASSERT_NE(NULL, (f = popen("echo -l 'hello $there' yo", "r")));
ASSERT_STREQ("hello $there\n", fgets(buf, sizeof(buf), f));
ASSERT_STREQ("yo\n", fgets(buf, sizeof(buf), f));
ASSERT_EQ(0, pclose(f));
}
TEST(popen, doubleQuotes) {
setenv("hello", "a b c", true);
ASSERT_NE(NULL, (f = popen("echo -l \"$hello there\"", "r")));
ASSERT_STREQ("a b c there\n", fgets(buf, sizeof(buf), f));
ASSERT_EQ(0, pclose(f));
}
TEST(popen, quoteless) {
setenv("there", "a b c", true);
ASSERT_NE(NULL, (f = popen("echo -l hello a$there yo", "r")));
ASSERT_STREQ("hello\n", fgets(buf, sizeof(buf), f));
ASSERT_STREQ("aa b c\n", fgets(buf, sizeof(buf), f)); // mixed feelings
ASSERT_STREQ("yo\n", fgets(buf, sizeof(buf), f));
ASSERT_EQ(0, pclose(f));
}
TEST(popen, pipe) {
setenv("there", "a b c", true);
ASSERT_NE(NULL, (f = popen("echo hello | toupper", "r")));
ASSERT_STREQ("HELLO\n", fgets(buf, sizeof(buf), f));
ASSERT_EQ(0, pclose(f));
}
sig_atomic_t gotsig;
void OnSig(int sig) {
gotsig = 1;
}
TEST(popen, complicated) {
if (IsWindows()) return; // windows treats sigusr1 as terminate
char cmd[64];
signal(SIGUSR1, OnSig);
sprintf(cmd, "read a ; test \"x$a\" = xhello && kill -USR1 %d", getpid());
ASSERT_NE(NULL, (f = popen(cmd, "w")));
ASSERT_GE(fputs("hello", f), 0);
ASSERT_EQ(0, pclose(f));
ASSERT_EQ(1, gotsig);
signal(SIGUSR1, SIG_DFL);
}

View file

@ -29,20 +29,18 @@
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
void SetUpOnce(void) {
__enable_threads();
}
char testlib_enable_tmp_setup_teardown;
__attribute__((__constructor__)) static void init(void) {
if (atoi(nulltoempty(getenv("THE_DOGE"))) == 42) {
exit(42);
switch (atoi(nulltoempty(getenv("THE_DOGE")))) {
case 42:
exit(42);
default:
break;
}
}
TEST(spawn, test) {
if (atoi(nulltoempty(getenv("THE_DOGE"))) == 42) {
exit(42);
}
int rc, ws, pid;
char *prog = GetProgramExecutableName();
char *args[] = {program_invocation_name, NULL};
@ -53,6 +51,25 @@ TEST(spawn, test) {
EXPECT_EQ(42, WEXITSTATUS(ws));
}
TEST(spawn, pipe) {
char buf[10];
int p[2], pid, status;
const char *pn = "./echo.com";
posix_spawn_file_actions_t fa;
testlib_extract("/zip/echo.com", "echo.com", 0755);
ASSERT_SYS(0, 0, pipe(p));
ASSERT_EQ(0, posix_spawn_file_actions_init(&fa));
ASSERT_EQ(0, posix_spawn_file_actions_addclose(&fa, p[0]));
ASSERT_EQ(0, posix_spawn_file_actions_adddup2(&fa, p[1], 1));
ASSERT_EQ(0, posix_spawn_file_actions_addclose(&fa, p[1]));
ASSERT_EQ(0, posix_spawnp(&pid, pn, &fa, 0, (char *[]){pn, "hello", 0}, 0));
ASSERT_SYS(0, 0, close(p[1]));
ASSERT_SYS(0, pid, waitpid(pid, &status, 0));
ASSERT_SYS(0, 6, read(p[0], buf, sizeof(buf)));
ASSERT_SYS(0, 0, close(p[0]));
ASSERT_EQ(0, posix_spawn_file_actions_destroy(&fa));
}
/*
* BEST LINUX FORK+EXEC+EXIT+WAIT PERFORMANCE
* The fastest it can go with fork() is 40µs

View file

@ -29,27 +29,79 @@
char testlib_enable_tmp_setup_teardown;
TEST(system, testStdoutRedirect) {
int ws;
testlib_extract("/zip/echo.com", "echo.com", 0755);
TEST(system, haveShell) {
ASSERT_TRUE(system(0));
ws = system("./echo.com hello >hello.txt");
ASSERT_TRUE(WIFEXITED(ws));
ASSERT_EQ(0, WEXITSTATUS(ws));
}
TEST(system, echo) {
ASSERT_EQ(0, system("echo hello >\"hello there.txt\""));
EXPECT_STREQ("hello\n", _gc(xslurp("hello there.txt", 0)));
}
TEST(system, exit) {
ASSERT_EQ(123, WEXITSTATUS(system("exit 123")));
}
TEST(system, testStdoutRedirect) {
testlib_extract("/zip/echo.com", "echo.com", 0755);
ASSERT_EQ(0, system("./echo.com hello >hello.txt"));
EXPECT_STREQ("hello\n", _gc(xslurp("hello.txt", 0)));
}
TEST(system, testStdoutRedirect_withSpacesInFilename) {
int ws;
testlib_extract("/zip/echo.com", "echo.com", 0755);
ASSERT_TRUE(system(0));
ws = system("./echo.com hello >\"hello there.txt\"");
ASSERT_TRUE(WIFEXITED(ws));
ASSERT_EQ(0, WEXITSTATUS(ws));
ASSERT_EQ(0, system("./echo.com hello >\"hello there.txt\""));
EXPECT_STREQ("hello\n", _gc(xslurp("hello there.txt", 0)));
}
BENCH(system, bench) {
testlib_extract("/zip/echo.com", "echo.com", 0755);
EZBENCH2("system", donothing, system("./echo.com hi >/dev/null"));
EZBENCH2("system cmd", donothing, system("./echo.com hi >/dev/null"));
EZBENCH2("cocmd echo", donothing, system("echo hi >/dev/null"));
EZBENCH2("cocmd exit", donothing, system("exit"));
}
TEST(system, and) {
ASSERT_EQ(1, WEXITSTATUS(system("false && false")));
ASSERT_EQ(1, WEXITSTATUS(system("true&& false")));
ASSERT_EQ(1, WEXITSTATUS(system("false &&true")));
ASSERT_EQ(0, WEXITSTATUS(system("true&&true")));
}
TEST(system, or) {
ASSERT_EQ(1, WEXITSTATUS(system("false || false")));
ASSERT_EQ(0, WEXITSTATUS(system("true|| false")));
ASSERT_EQ(0, WEXITSTATUS(system("false ||true")));
ASSERT_EQ(0, WEXITSTATUS(system("true||true")));
}
TEST(system, async1) {
ASSERT_EQ(123, WEXITSTATUS(system("exit 123 & "
"wait $!")));
}
TEST(system, async4) {
ASSERT_EQ(0, WEXITSTATUS(system("echo a >a & "
"echo b >b & "
"echo c >c & "
"echo d >d & "
"wait")));
ASSERT_TRUE(fileexists("a"));
ASSERT_TRUE(fileexists("b"));
ASSERT_TRUE(fileexists("c"));
ASSERT_TRUE(fileexists("d"));
}
TEST(system, equals) {
setenv("var", "wand", true);
ASSERT_EQ(0, WEXITSTATUS(system("test a = a")));
ASSERT_EQ(1, WEXITSTATUS(system("test a = b")));
ASSERT_EQ(0, WEXITSTATUS(system("test x$var = xwand")));
ASSERT_EQ(0, WEXITSTATUS(system("[ a = a ]")));
ASSERT_EQ(1, WEXITSTATUS(system("[ a = b ]")));
}
TEST(system, notequals) {
ASSERT_EQ(1, WEXITSTATUS(system("test a != a")));
ASSERT_EQ(0, WEXITSTATUS(system("test a != b")));
}

View file

@ -74,6 +74,26 @@ o/$(MODE)/test/libc/stdio/system_test.com.dbg: \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
o/$(MODE)/test/libc/stdio/popen_test.com.dbg: \
$(TEST_LIBC_STDIO_DEPS) \
o/$(MODE)/test/libc/stdio/popen_test.o \
o/$(MODE)/test/libc/stdio/stdio.pkg \
o/$(MODE)/tool/build/echo.com.zip.o \
$(LIBC_TESTMAIN) \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
o/$(MODE)/test/libc/stdio/spawn_test.com.dbg: \
$(TEST_LIBC_STDIO_DEPS) \
o/$(MODE)/test/libc/stdio/spawn_test.o \
o/$(MODE)/test/libc/stdio/stdio.pkg \
o/$(MODE)/tool/build/echo.com.zip.o \
$(LIBC_TESTMAIN) \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
$(TEST_LIBC_STDIO_OBJS): private \
DEFAULT_CCFLAGS += \
-fno-builtin