Fix bugs and make improvements

- Get clone() working on FreeBSD
- Increase some Python build quotas
- Add more atomic builtins to chibicc
- Fix ASAN poisoning of alloca() memory
- Make MODE= mandatory link path tinier
- Improve the examples folder a little bit
- Start working on some more resource limits
- Make the linenoise auto-complete UI as good as GNU readline
- Update compile.com, avoiding AVX codegen on non-AVX systems
- Make sure empty path to syscalls like opendir raises ENOENT
- Correctly polyfill ENOENT vs. ENOTDIR on the New Technology
- Port bestline's paredit features to //third_party/linenoise
- Remove workarounds for RHEL 5.0 bugs that were fixed in 5.1
This commit is contained in:
Justine Tunney 2022-04-20 09:56:53 -07:00
parent c3fb624647
commit ae638c0850
181 changed files with 2994 additions and 1367 deletions

View file

@ -32,7 +32,31 @@ local function main()
-- steal client from redbean
fd = GetClientFd()
rc = unix.fork()
rc, errno = unix.fork()
if errno then
SetStatus(400)
SetHeader('Content-Type', 'text/html; charset=utf-8')
Write('<!doctype html>\r\n')
Write('<title>redbean unix module</title>\r\n')
Write('<h1>\r\n')
Write('<img style="vertical-align:middle" src="data:image/png;base64,\r\n')
Write(EncodeBase64(LoadAsset('/redbean.png')))
Write('">\r\n')
Write('redbean unix demo\r\n')
Write(string.format('<span style="color:red">&nbsp;%s</span>\r\n', unix.strerrno(errno)))
Write('</h1>\r\n')
Write([[
<p>
Your redbean doesn't have the ability to fork. Most likely
because you enabled sandboxing and fork() isn't in your bpf
policy.
</p>
]])
Write('</h1>\r\n')
return
end
if rc ~= 0 then
unix.close(fd)
return

View file

@ -1539,9 +1539,13 @@ UNIX MODULE
unix.sigaction(unix.SIGALRM, MyOnSigAlrm, unix.SA_RESETHAND)
unix.setitimer(unix.ITIMER_REAL, 0, 0, 1, 0)
unix.strerrno(errno:int) → str
Turns `errno` code into its symbolic name, e.g. `"EINTR"`.
unix.strerror(errno:int) → str
Turns `errno` code into a string describing the error.
Turns `errno` code into a longer string describing the error.
unix.strsignal(sig:int) → str
@ -1776,10 +1780,10 @@ UNIX MODULE
in a supplied path *existed* but wasn't a directory. For example,
if you try to `open("foo/bar")` and `foo` is a regular file, then
`ENOTDIR` will be returned. Raised by open(), access(), chdir(),
chmod(), chown(), chroot(), execve(), fcntl(), futimesat(),
inotify_add_watch(), link(), mkdir(), mknod(), opendir(),
readlink(), rename(), rmdir(), stat(), symlink(), sysctl(),
truncate(), unlink(), utimensat(), bind().
chroot(), execve(), link(), mkdir(), mknod(), opendir(),
readlink(), rename(), rmdir(), stat(), symlink(), truncate(),
unlink(), utimensat(), bind(), chmod(), chown(), fcntl(),
futimesat(), inotify_add_watch().
- `EISDIR`: Is a a directory. Raised by copy_file_range(),
execve(), open(), read(), rename(), truncate(), unlink().

View file

@ -536,6 +536,16 @@ static int LuaUnixSetpgid(lua_State *L) {
return ReturnRc(L, rc, olderr);
}
// unix.setpgrp() → pgid:int[, errno:int]
static int LuaUnixSetpgrp(lua_State *L) {
int rc, pid, pgrp, olderr;
olderr = errno;
pid = luaL_checkinteger(L, 1);
pgrp = luaL_checkinteger(L, 2);
rc = setpgrp();
return ReturnRc(L, rc, olderr);
}
// unix.setsid() → sid:int[, errno:int]
static int LuaUnixSetsid(lua_State *L) {
int rc, olderr;
@ -1231,6 +1241,12 @@ static int LuaUnixStrerror(lua_State *L) {
return 1;
}
// unix.strerrno(errno) → str
static int LuaUnixStrerrno(lua_State *L) {
lua_pushstring(L, strerror_short(luaL_checkinteger(L, 1)));
return 1;
}
// unix.strsignal(sig) → str
static int LuaUnixStrsignal(lua_State *L) {
lua_pushstring(L, strsignal(luaL_checkinteger(L, 1)));
@ -1506,6 +1522,7 @@ static const luaL_Reg kLuaUnix[] = {
{"getpgrp", LuaUnixGetpgrp}, // get process group id
{"getpgid", LuaUnixGetpgid}, // get process group id of pid
{"setpgid", LuaUnixSetpgid}, // set process group id for pid
{"setpgrp", LuaUnixSetpgrp}, // sets process group id
{"getsid", LuaUnixGetsid}, // get session id of pid
{"setsid", LuaUnixSetsid}, // create a new session id
{"getpid", LuaUnixGetpid}, // get id of this process
@ -1533,6 +1550,7 @@ static const luaL_Reg kLuaUnix[] = {
{"sigsuspend", LuaUnixSigsuspend}, // wait for signal
{"setitimer", LuaUnixSetitimer}, // set alarm clock
{"strerror", LuaUnixStrerror}, // turn errno into string
{"strerrno", LuaUnixStrerrno}, // turn errno into string
{"strsignal", LuaUnixStrsignal}, // turn signal into string
{0}, //
};
@ -1571,6 +1589,7 @@ int LuaUnix(lua_State *L) {
LuaSetIntField(L, "O_EXCL", O_EXCL); //
LuaSetIntField(L, "O_TRUNC", O_TRUNC); //
LuaSetIntField(L, "O_CLOEXEC", O_CLOEXEC); //
LuaSetIntField(L, "O_DIRECT", O_DIRECT); // no-op on xnu/openbsd
LuaSetIntField(L, "O_APPEND", O_APPEND); // weird on nt
LuaSetIntField(L, "O_TMPFILE", O_TMPFILE); // linux, windows
LuaSetIntField(L, "O_NOFOLLOW", O_NOFOLLOW); // unix
@ -1616,6 +1635,7 @@ int LuaUnix(lua_State *L) {
// rlimit() resources
LuaSetIntField(L, "RLIMIT_AS", RLIMIT_AS);
LuaSetIntField(L, "RLIMIT_RSS", RLIMIT_RSS);
LuaSetIntField(L, "RLIMIT_CPU", RLIMIT_CPU);
LuaSetIntField(L, "RLIMIT_FSIZE", RLIMIT_FSIZE);
LuaSetIntField(L, "RLIMIT_NPROC", RLIMIT_NPROC);

View file

@ -176,8 +176,8 @@ STATIC_YOINK("zip_uri_support");
#define VERSION 0x010500
#define HEARTBEAT 5000 /*ms*/
#define HASH_LOAD_FACTOR /* 1. / */ 4
#define read(F, P, N) readv(F, &(struct iovec){P, N}, 1)
#define write(F, P, N) writev(F, &(struct iovec){P, N}, 1)
#define READ(F, P, N) readv(F, &(struct iovec){P, N}, 1)
#define WRITE(F, P, N) writev(F, &(struct iovec){P, N}, 1)
#define LockInc(P) asm volatile("lock incq\t%0" : "=m"(*(P)))
#define AppendCrlf(P) mempcpy(P, "\r\n", 2)
#define HasHeader(H) (!!msg.headers[H].a)
@ -1029,7 +1029,7 @@ static void Daemonize(void) {
umask(0);
if (pidpath) {
fd = open(pidpath, O_CREAT | O_WRONLY, 0644);
write(fd, ibuf, uint64toarray_radix10(getpid(), ibuf));
WRITE(fd, ibuf, uint64toarray_radix10(getpid(), ibuf));
close(fd);
}
if (!logpath) ProgramLogPath("/dev/null");
@ -1253,7 +1253,7 @@ static ssize_t ReadAll(int fd, char *p, size_t n) {
ssize_t rc;
size_t i, got;
for (i = 0; i < n;) {
rc = read(fd, p + i, n - i);
rc = READ(fd, p + i, n - i);
if (rc != -1) {
got = rc;
i += got;
@ -2727,7 +2727,7 @@ static void LaunchBrowser(const char *path) {
sigprocmask(SIG_BLOCK, &chldmask, &savemask);
CHECK_NE(-1, (pid = fork()));
if (!pid) {
setpgid(getpid(), getpid()); // ctrl-c'ing redbean shouldn't kill browser
setpgrp(); // ctrl-c'ing redbean shouldn't kill browser
sigaction(SIGINT, &saveint, 0);
sigaction(SIGQUIT, &savequit, 0);
sigprocmask(SIG_SETMASK, &savemask, 0);
@ -3396,13 +3396,10 @@ static void StoreAsset(char *path, size_t pathlen, char *data, size_t datalen,
uint16_t gflags, iattrs, mtime, mdate, dosmode, method, disk;
size_t oldcdirsize, oldcdiroffset, records, cdiroffset, cdirsize, complen,
uselen;
if (IsOpenbsd() || IsNetbsd() || IsWindows()) {
DIEF("(cfg) StoreAsset() not available on Windows/NetBSD/OpenBSD yet");
}
INFOF("Storing asset %`'s", path);
disk = gflags = iattrs = 0;
if (_isutf8(path, pathlen)) gflags |= kZipGflagUtf8;
if (_istext(data, datalen)) iattrs |= kZipIattrText;
@ -3870,7 +3867,7 @@ static int LuaFetch(lua_State *L) {
LuaThrowTlsError(L, "write", ret);
unreachable;
}
} else if (write(sock, request, requestlen) != requestlen) {
} else if (WRITE(sock, request, requestlen) != requestlen) {
close(sock);
luaL_error(L, "write error: %s", strerror(errno));
unreachable;
@ -3904,7 +3901,7 @@ static int LuaFetch(lua_State *L) {
unreachable;
}
}
} else if ((rc = read(sock, inbuf.p + inbuf.n, inbuf.c - inbuf.n)) == -1) {
} else if ((rc = READ(sock, inbuf.p + inbuf.n, inbuf.c - inbuf.n)) == -1) {
close(sock);
free(inbuf.p);
DestroyHttpMessage(&msg);
@ -6863,7 +6860,7 @@ static void RestoreApe(void) {
if (endswith(zpath, ".com.dbg")) return;
if ((a = GetAssetZip("/.ape", 5)) && (p = LoadAsset(a, &n))) {
close(zfd);
if ((zfd = OpenExecutable()) == -1 || write(zfd, p, n) == -1)
if ((zfd = OpenExecutable()) == -1 || WRITE(zfd, p, n) == -1)
WARNF("(srvr) can't restore .ape");
free(p);
} else {
@ -7197,7 +7194,7 @@ void RedBean(int argc, char *argv[]) {
// ctrl-c isn't propagating as expected when running redbean
// underneath strace.com :|
if (!IsWindows()) {
setpgid(getpid(), getpid());
setpgrp();
}
if (logpath) {
close(2);