Make improvements

- Add rusage to redbean Lua API
- Add more redbean documentation
- Add pledge() to redbean Lua API
- Polyfill OpenBSD pledge() for Linux
- Increase PATH_MAX limit to 1024 characters
- Untrack sibling processes after fork() on Windows
This commit is contained in:
Justine Tunney 2022-04-28 09:42:36 -07:00
parent 9a6bd304a5
commit 47b3274665
212 changed files with 2251 additions and 834 deletions

View file

@ -35,8 +35,8 @@
uint64_t i;
char *oldpath;
char tmp[PATH_MAX + 1];
char pathbuf[PATH_MAX + 1];
char tmp[PATH_MAX];
char pathbuf[PATH_MAX];
char testlib_enable_tmp_setup_teardown;
void SetUp(void) {

View file

@ -43,7 +43,7 @@ TEST(getcwd, testNullBuf_allocatesResult) {
TEST(getcwd, testWindows_addsFunnyPrefix) {
if (!IsWindows()) return;
char path[PATH_MAX + 1];
char path[PATH_MAX];
ASSERT_NE(0, getcwd(path, sizeof(path)));
EXPECT_STARTSWITH("//?/", path);
}

View file

@ -23,7 +23,7 @@
#include "libc/str/str.h"
#include "libc/testlib/testlib.h"
char16_t cmdline[ARG_MAX];
char16_t cmdline[ARG_MAX / 2];
TEST(mkntcmdline, emptyArgvList_cantBeEmptyOnWindows) {
char *argv[] = {NULL};

View file

@ -20,7 +20,7 @@
#include "libc/runtime/gc.internal.h"
#include "libc/testlib/testlib.h"
char16_t envvars[ARG_MAX];
char16_t envvars[ARG_MAX / 2];
TEST(mkntenvblock, emptyList_onlyOutputsDoubleNulStringTerminator) {
char *envp[] = {NULL};

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/struct/bpf.h"
#include "libc/calls/struct/filter.h"
#include "libc/calls/struct/iovec.h"
@ -31,16 +32,6 @@
#include "libc/testlib/testlib.h"
#include "tool/net/sandbox.h"
bool __is_linux_2_6_23(void) {
int rc;
if (!IsLinux()) return false;
asm volatile("syscall"
: "=a"(rc)
: "0"(157), "D"(PR_GET_SECCOMP)
: "rcx", "r11", "memory");
return rc != -EINVAL;
}
void SetUp(void) {
if (!__is_linux_2_6_23()) {
exit(0);

View file

@ -0,0 +1,62 @@
/*-*- 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/internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/o.h"
#include "libc/testlib/testlib.h"
void SetUp(void) {
if (!__is_linux_2_6_23() && !IsOpenbsd()) {
exit(0);
}
}
TEST(pledge, default_allowsExit) {
int ws, pid;
ASSERT_NE(-1, (pid = fork()));
if (!pid) {
ASSERT_SYS(0, 0, pledge("", 0));
_Exit(0);
}
EXPECT_NE(-1, wait(&ws));
EXPECT_TRUE(WIFEXITED(ws));
EXPECT_EQ(0, WEXITSTATUS(ws));
}
TEST(pledge, stdio_forbidsOpeningPasswd) {
int ws, pid;
ASSERT_NE(-1, (pid = fork()));
if (!pid) {
ASSERT_SYS(0, 0, pledge("stdio", 0));
ASSERT_SYS(EPERM, -1, open("/etc/passwd", O_RDWR));
_Exit(0);
}
EXPECT_NE(-1, wait(&ws));
if (IsLinux()) {
EXPECT_TRUE(WIFEXITED(ws));
EXPECT_EQ(0, WEXITSTATUS(ws));
} else {
EXPECT_TRUE(WIFSIGNALED(ws));
EXPECT_EQ(SIGABRT, WTERMSIG(ws));
}
}

View file

@ -17,11 +17,16 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/sigbits.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/dce.h"
#include "libc/log/check.h"
#include "libc/macros.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/msync.h"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/consts/sig.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
@ -79,6 +84,51 @@ TEST(fork, testSharedMemory) {
EXPECT_NE(-1, munmap(privatevar, FRAMESIZE));
}
static volatile bool gotsigusr1;
static volatile bool gotsigusr2;
static void OnSigusr1(int sig) {
gotsigusr1 = true;
}
static void OnSigusr2(int sig) {
gotsigusr2 = true;
}
TEST(fork, childToChild) {
if (IsWindows()) return; // :'(
sigset_t mask, oldmask;
int ws, parent, child1, child2;
gotsigusr1 = false;
gotsigusr2 = false;
parent = getpid();
signal(SIGUSR1, OnSigusr1);
signal(SIGUSR2, OnSigusr2);
sigemptyset(&mask);
sigaddset(&mask, SIGUSR2);
sigprocmask(SIG_BLOCK, &mask, &oldmask);
ASSERT_NE(-1, (child1 = fork()));
if (!child1) {
kill(parent, SIGUSR2);
sigsuspend(0);
_Exit(!gotsigusr1);
}
sigdelset(&mask, SIGUSR2);
sigsuspend(&mask);
ASSERT_NE(-1, (child2 = fork()));
if (!child2) {
kill(child1, SIGUSR1);
_Exit(0);
}
ASSERT_NE(-1, wait(&ws));
EXPECT_TRUE(WIFEXITED(ws));
EXPECT_EQ(0, WEXITSTATUS(ws));
ASSERT_NE(-1, wait(&ws));
EXPECT_TRUE(WIFEXITED(ws));
EXPECT_EQ(0, WEXITSTATUS(ws));
sigprocmask(SIG_SETMASK, &oldmask, 0);
}
void ForkInSerial(void) {
int pid, ws;
ASSERT_NE(-1, (pid = fork()));

View file

@ -22,7 +22,7 @@
TEST(GetDosArgv, empty) {
size_t max = 4;
size_t size = ARG_MAX;
size_t size = ARG_MAX / 2;
char *buf = malloc(size * sizeof(char));
char **argv = malloc(max * sizeof(char *));
EXPECT_EQ(0, GetDosArgv(u"", buf, size, argv, max));
@ -33,7 +33,7 @@ TEST(GetDosArgv, empty) {
TEST(GetDosArgv, emptyish) {
size_t max = 4;
size_t size = ARG_MAX;
size_t size = ARG_MAX / 2;
char *buf = malloc(size * sizeof(char));
char **argv = malloc(max * sizeof(char *));
EXPECT_EQ(0, GetDosArgv(u" ", buf, size, argv, max));
@ -44,7 +44,7 @@ TEST(GetDosArgv, emptyish) {
TEST(GetDosArgv, basicUsage) {
size_t max = 4;
size_t size = ARG_MAX;
size_t size = ARG_MAX / 2;
char *buf = malloc(size * sizeof(char));
char **argv = malloc(max * sizeof(char *));
EXPECT_EQ(3, GetDosArgv(u"a\t \"b c\" d ", buf, size, argv, max));
@ -58,7 +58,7 @@ TEST(GetDosArgv, basicUsage) {
TEST(GetDosArgv, advancedUsage) {
size_t max = 4;
size_t size = ARG_MAX;
size_t size = ARG_MAX / 2;
char *buf = malloc(size * sizeof(char));
char **argv = malloc(max * sizeof(char *));
EXPECT_EQ(2, GetDosArgv(u"(╯°□°)╯︵ ┻━┻", buf, size, argv, max));
@ -71,7 +71,7 @@ TEST(GetDosArgv, advancedUsage) {
TEST(GetDosArgv, testAegeanGothicSupplementaryPlanes) {
size_t max = 4; /* these symbols are almost as old as dos */
size_t size = ARG_MAX;
size_t size = ARG_MAX / 2;
char *buf = malloc(size * sizeof(char));
char **argv = malloc(max * sizeof(char *));
EXPECT_EQ(2, GetDosArgv(u"𐄷𐄸𐄹𐄺𐄻𐄼 𐌰𐌱𐌲𐌳𐌴𐌵𐌶𐌷", buf, size, argv, max));
@ -84,7 +84,7 @@ TEST(GetDosArgv, testAegeanGothicSupplementaryPlanes) {
TEST(GetDosArgv, realWorldUsage) {
size_t max = 512;
size_t size = ARG_MAX;
size_t size = ARG_MAX / 2;
char *buf = malloc(size * sizeof(char));
char **argv = malloc(max * sizeof(char *));
EXPECT_EQ(5, GetDosArgv(u"C:\\Users\\jtunn\\printargs.com oh yes yes yes",
@ -145,7 +145,7 @@ TEST(GetDosArgv, quoteInMiddleOfArg_wontSplitArg) {
TEST(GetDosArgv, waqQuoting1) {
size_t max = 4;
size_t size = ARG_MAX;
size_t size = ARG_MAX / 2;
char *buf = malloc(size * sizeof(char));
char **argv = malloc(max * sizeof(char *));
EXPECT_EQ(2,
@ -159,7 +159,7 @@ TEST(GetDosArgv, waqQuoting1) {
TEST(GetDosArgv, waqQuoting2) {
size_t max = 4;
size_t size = ARG_MAX;
size_t size = ARG_MAX / 2;
char *buf = malloc(size * sizeof(char));
char **argv = malloc(max * sizeof(char *));
EXPECT_EQ(2, GetDosArgv(u"\"a\\\"b c\" d", buf, size, argv, max));