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

@ -24,7 +24,7 @@
struct FindComBinary {
bool once;
const char *res;
char buf[PATH_MAX + 1];
char buf[PATH_MAX];
};
static struct FindComBinary g_findcombinary;

View file

@ -18,6 +18,7 @@
*/
#include "libc/bits/bits.h"
#include "libc/calls/calls.h"
#include "libc/macros.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/str/str.h"
@ -30,7 +31,7 @@
const char *FindDebugBinary(void) {
static bool once;
static char *res;
static char buf[PATH_MAX + 1];
static char buf[PATH_MAX];
char *p;
size_t n;
if (!once) {
@ -40,12 +41,12 @@ const char *FindDebugBinary(void) {
if (n > 4 && READ32LE(p + n - 4) == READ32LE(".dbg")) {
res = p;
} else if (n > 4 && READ32LE(p + n - 4) == READ32LE(".com") &&
n + 4 <= PATH_MAX) {
n + 4 < ARRAYLEN(buf)) {
mempcpy(mempcpy(buf, p, n), ".dbg", 5);
if (fileexists(buf)) {
res = buf;
}
} else if (n + 8 <= PATH_MAX) {
} else if (n + 8 < ARRAYLEN(buf)) {
mempcpy(mempcpy(buf, p, n), ".com.dbg", 9);
if (fileexists(buf)) {
res = buf;

View file

@ -212,9 +212,18 @@ textwindows void WinMainForked(void) {
// since the handles closed on fork
if (weaken(ForkNtStdinWorker)) weaken(ForkNtStdinWorker)();
struct Fds *fds = VEIL("r", &g_fds);
fds->__init_p[0].handle = GetStdHandle(kNtStdInputHandle);
fds->__init_p[1].handle = GetStdHandle(kNtStdOutputHandle);
fds->__init_p[2].handle = GetStdHandle(kNtStdErrorHandle);
fds->p[0].handle = fds->__init_p[0].handle = GetStdHandle(kNtStdInputHandle);
fds->p[1].handle = fds->__init_p[1].handle = GetStdHandle(kNtStdOutputHandle);
fds->p[2].handle = fds->__init_p[2].handle = GetStdHandle(kNtStdErrorHandle);
// untrack the forked children of the parent since we marked the
// CreateProcess() process handle below as non-inheritable
for (i = 0; i < fds->n; ++i) {
if (fds->p[i].kind == kFdProcess) {
fds->p[i].kind = 0;
fds->f = MIN(i, fds->f);
}
}
// restore the crash reporting stuff
if (weaken(__wincrash_nt)) {
@ -269,9 +278,8 @@ textwindows int sys_fork_nt(void) {
args = args2;
}
#endif
if (ntspawn(GetProgramExecutableName(), args, environ, forkvar,
&kNtIsInheritable, NULL, true, 0, NULL, &startinfo,
&procinfo) != -1) {
if (ntspawn(GetProgramExecutableName(), args, environ, forkvar, 0, 0,
true, 0, 0, &startinfo, &procinfo) != -1) {
CloseHandle(procinfo.hThread);
ok = WriteAll(writer, jb, sizeof(jb)) &&
WriteAll(writer, &_mmi.i, sizeof(_mmi.i)) &&

33
libc/runtime/getargmax.c Normal file
View file

@ -0,0 +1,33 @@
/*-*- 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/dce.h"
#include "libc/runtime/runtime.h"
/**
* Returns `ARG_MAX` for host platform.
*/
int __arg_max(void) {
if (IsWindows()) return 32767;
if (IsLinux()) return 128 * 1024;
if (IsNetbsd()) return 256 * 1024;
if (IsFreebsd()) return 512 * 1024;
if (IsOpenbsd()) return 512 * 1024;
if (IsXnu()) return 1024 * 1024;
return ARG_MAX;
}

View file

@ -24,7 +24,7 @@
#define ADDR(x) ((char *)((int64_t)((uint64_t)(x) << 32) >> 16))
privileged int sys_mprotect_nt(void *addr, size_t size, int prot) {
textwindows int sys_mprotect_nt(void *addr, size_t size, int prot) {
int rc = 0;
unsigned i;
uint32_t op;

View file

@ -30,8 +30,8 @@
void __paginate(int fd, const char *s) {
int tfd, pid;
char *args[3] = {0};
char tmppath[PATH_MAX + 1];
char progpath[PATH_MAX + 1];
char tmppath[PATH_MAX];
char progpath[PATH_MAX];
if (strcmp(nulltoempty(getenv("TERM")), "dumb") && isatty(0) && isatty(1) &&
((args[0] = commandv("less", progpath, sizeof(progpath))) ||
(args[0] = commandv("more", progpath, sizeof(progpath))))) {

View file

@ -140,10 +140,13 @@ textstartup void __printargs(const char *prologue) {
uintptr_t *auxp;
struct utsname uts;
struct termios termios;
char path[PATH_MAX + 1];
int e, x, st, ft, flags;
struct pollfd pfds[128];
struct AuxiliaryValue *auxinfo;
union {
char path[PATH_MAX];
struct pollfd pfds[128];
} u;
st = __strace, __strace = 0;
ft = g_ftrace, g_ftrace = 0;
e = errno;
@ -240,15 +243,15 @@ textstartup void __printargs(const char *prologue) {
PRINT("");
PRINT("FILE DESCRIPTORS");
for (i = 0; i < ARRAYLEN(pfds); ++i) {
pfds[i].fd = i;
pfds[i].events = POLLIN;
for (i = 0; i < ARRAYLEN(u.pfds); ++i) {
u.pfds[i].fd = i;
u.pfds[i].events = POLLIN;
}
if ((n = poll(pfds, ARRAYLEN(pfds), 0)) != -1) {
for (i = 0; i < ARRAYLEN(pfds); ++i) {
if (i && (pfds[i].revents & POLLNVAL)) continue;
if ((n = poll(u.pfds, ARRAYLEN(u.pfds), 0)) != -1) {
for (i = 0; i < ARRAYLEN(u.pfds); ++i) {
if (i && (u.pfds[i].revents & POLLNVAL)) continue;
PRINT(" ☼ %d (revents=%#hx fcntl(F_GETFL)=%#x isatty()=%hhhd)", i,
pfds[i].revents, fcntl(i, F_GETFL), isatty(i));
u.pfds[i].revents, fcntl(i, F_GETFL), isatty(i));
}
} else {
PRINT(" poll() returned %d %m", n);
@ -298,8 +301,8 @@ textstartup void __printargs(const char *prologue) {
if (*__auxv) {
for (auxp = __auxv; *auxp; auxp += 2) {
if ((auxinfo = DescribeAuxv(auxp[0]))) {
ksnprintf(path, sizeof(path), auxinfo->fmt, auxp[1]);
PRINT(" ☼ %16s[%4ld] = %s", auxinfo->name, auxp[0], path);
ksnprintf(u.path, sizeof(u.path), auxinfo->fmt, auxp[1]);
PRINT(" ☼ %16s[%4ld] = %s", auxinfo->name, auxp[0], u.path);
} else {
PRINT(" ☼ %16s[%4ld] = %014p", "unknown", auxp[0], auxp[1]);
}
@ -326,7 +329,7 @@ textstartup void __printargs(const char *prologue) {
PRINT(" ☼ %s = %#s", "kNtWindowsDirectory", kNtWindowsDirectory);
PRINT(" ☼ %s = %#s", "GetProgramExecutableName", GetProgramExecutableName());
PRINT(" ☼ %s = %#s", "GetInterpreterExecutableName",
GetInterpreterExecutableName(path, sizeof(path)));
GetInterpreterExecutableName(u.path, sizeof(u.path)));
PRINT(" ☼ %s = %p", "RSP", __builtin_frame_address(0));
PRINT(" ☼ %s = %p", "GetStackAddr()", GetStackAddr(0));
PRINT(" ☼ %s = %p", "GetStaticStackAddr(0)", GetStaticStackAddr(0));

View file

@ -104,6 +104,7 @@ char *GetProgramExecutableName(void);
char *GetInterpreterExecutableName(char *, size_t);
void __printargs(const char *);
void __paginate(int, const char *);
int __arg_max(void);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -22,6 +22,7 @@
#include "libc/macros.internal.h"
#include "libc/runtime/clktck.h"
#include "libc/runtime/sysconf.h"
#include "libc/sysv/consts/limits.h"
#include "libc/sysv/consts/rlim.h"
#include "libc/sysv/consts/rlimit.h"
@ -42,7 +43,7 @@ long sysconf(int name) {
int n;
switch (name) {
case _SC_ARG_MAX:
return ARG_MAX;
return _ARG_MAX;
case _SC_CHILD_MAX:
return GetResourceLimit(RLIMIT_NPROC);
case _SC_CLK_TCK:

View file

@ -84,8 +84,8 @@ struct WinArgs {
char *argv[4096];
char *envp[4092];
intptr_t auxv[2][2];
char argblock[ARG_MAX];
char envblock[ARG_MAX];
char argblock[ARG_MAX / 2];
char envblock[ARG_MAX / 2];
};
extern uint32_t __winmainpid;