diff --git a/libc/log/oncrash.c b/libc/log/oncrash.c index 313aed3bd..3c4bbbcd2 100644 --- a/libc/log/oncrash.c +++ b/libc/log/oncrash.c @@ -232,8 +232,6 @@ relegated void __oncrash(int sig, struct siginfo *si, ucontext_t *ctx) { err = errno; if (once) _exit(119); once = true; - /* TODO(jart): Needs translation for ucontext_t and possibly siginfo_t. */ - if (IsFreebsd() || IsOpenbsd()) ctx = NULL; rip = ctx ? ctx->uc_mcontext.rip : 0; if ((gdbpid = IsDebuggerPresent(true))) { DebugBreak(); diff --git a/libc/str/getzipcdircomment.c b/libc/str/getzipcdircomment.c new file mode 100644 index 000000000..cefd8ab82 --- /dev/null +++ b/libc/str/getzipcdircomment.c @@ -0,0 +1,30 @@ +/*-*- 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 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 │ +│ 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/zip.h" + +/** + * Returns comment of zip central directory. + */ +void *GetZipCdirComment(const uint8_t *eocd) { + if (READ32LE(eocd) == kZipCdir64HdrMagic) { + return ZIP_CDIR64_COMMENT(eocd); + } else { + return ZIP_CDIR_COMMENT(eocd); + } +} diff --git a/libc/str/getzipcdircommentsize.c b/libc/str/getzipcdircommentsize.c new file mode 100644 index 000000000..6741fadac --- /dev/null +++ b/libc/str/getzipcdircommentsize.c @@ -0,0 +1,30 @@ +/*-*- 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 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 │ +│ 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/zip.h" + +/** + * Returns comment of zip central directory. + */ +uint64_t GetZipCdirCommentSize(const uint8_t *eocd) { + if (READ32LE(eocd) == kZipCdir64HdrMagic) { + return ZIP_CDIR64_COMMENTSIZE(eocd); + } else { + return ZIP_CDIR_COMMENTSIZE(eocd); + } +} diff --git a/libc/zip.h b/libc/zip.h index 4c999ecb0..cf5c0c7e0 100644 --- a/libc/zip.h +++ b/libc/zip.h @@ -184,6 +184,8 @@ bool IsZipCdir64(const uint8_t *, size_t, size_t); int GetZipCfileMode(const uint8_t *); uint64_t GetZipCdirOffset(const uint8_t *); uint64_t GetZipCdirRecords(const uint8_t *); +void *GetZipCdirComment(const uint8_t *); +uint64_t GetZipCdirCommentSize(const uint8_t *); uint64_t GetZipCfileUncompressedSize(const uint8_t *); uint64_t GetZipCfileCompressedSize(const uint8_t *); uint64_t GetZipCfileOffset(const uint8_t *); diff --git a/tool/net/demo/virtualbean.html b/tool/net/demo/virtualbean.html new file mode 100644 index 000000000..49326e0f8 --- /dev/null +++ b/tool/net/demo/virtualbean.html @@ -0,0 +1,7 @@ + + +
This page uses redbean virtual hosting. Please refer to +//redbean.justine.lol +to see the primary instance. diff --git a/tool/net/net.mk b/tool/net/net.mk index 5fafc16ba..bc720ede2 100644 --- a/tool/net/net.mk +++ b/tool/net/net.mk @@ -98,6 +98,7 @@ o/$(MODE)/tool/net/redbean-demo.com: \ tool/net/demo/redbean-form.lua \ tool/net/demo/redbean-xhr.lua \ tool/net/demo/seekable.txt \ + tool/net/demo/virtualbean.html \ tool/net/redbean.c \ net/http/parsehttprequest.c \ net/http/parseurl.c \ @@ -107,9 +108,18 @@ o/$(MODE)/tool/net/redbean-demo.com: \ @$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@ @$(COMPILE) -AMKDIR -T$@ mkdir -p o/$(MODE)/tool/net/.redbean-demo @$(COMPILE) -ADD -T$@ dd if=$@ of=o/$(MODE)/tool/net/.redbean-demo/.ape bs=64 count=11 conv=notrunc 2>/dev/null - @$(COMPILE) -AZIP -T$@ zip -qj $@ o/$(MODE)/tool/net/.redbean-demo/.ape tool/net/demo/.init.lua tool/net/demo/.reload.lua tool/net/demo/hello.lua tool/net/demo/redbean.lua tool/net/demo/404.html tool/net/favicon.ico tool/net/redbean.png tool/net/demo/redbean-form.lua tool/net/demo/redbean-xhr.lua - @$(COMPILE) -AZIP -T$@ zip -qj0 $@ tool/net/demo/seekable.txt - @$(COMPILE) -AZIP -T$@ zip -q $@ tool/net/ tool/net/demo/ tool/net/demo/index.html tool/net/demo/redbean.css tool/net/redbean.c net/http/parsehttprequest.c net/http/parseurl.c net/http/encodeurl.c test/net/http/parsehttprequest_test.c test/net/http/parseurl_test.c + @$(COMPILE) -AZIP -T$@ zip -qj $@ o/$(MODE)/tool/net/.redbean-demo/.ape tool/net/demo/.init.lua tool/net/demo/.reload.lua tool/net/demo/hello.lua + @echo "<-- check out this lua server page" | $(COMPILE) -AZIP -T$@ zip -cqj $@ tool/net/demo/redbean.lua + @$(COMPILE) -AZIP -T$@ zip -qj $@ tool/net/demo/404.html tool/net/favicon.ico tool/net/redbean.png tool/net/demo/redbean-form.lua tool/net/demo/redbean-xhr.lua + @echo Uncompressed for HTTP Range requests | $(COMPILE) -AZIP -T$@ zip -cqj0 $@ tool/net/demo/seekable.txt + @$(COMPILE) -AZIP -T$@ zip -q $@ tool/net/ tool/net/demo/ tool/net/demo/index.html tool/net/demo/redbean.css tool/net/redbean.c net/http/parsehttprequest.c net/http/parseurl.c net/http/encodeurl.c test/net/http/parsehttprequest_test.c test/net/http/parseurl_test.c + @echo "
This is a live instance of redbean: a tiny multiplatform webserver that went viral on hacker news a few months ago. since then, we've added Lua dynamic serving, which also goes as fast as 1,000,000 requests per second on a core i9 (rather than a cheap virtual machine like this). the text you're reading now is a PKZIP End Of Central Directory comment.
redbean aims to be production worthy across six operating systems, using a single executable file (this demo is hosted on FreeBSD 13). redbean has been enhanced to restore the APE header after startup. It automatically generates this listing page based on your ZIP contents. If you use redbean as an application server / web development environment, then you'll find other new and useful features like function call logging so you can get that sweet sweet microsecond scale latency." | $(COMPILE) -AZIP -T$@ zip -z $@
+ @$(COMPILE) -AMKDIR -T$@ mkdir -p o/$(MODE)/tool/net/virtualbean.justine.lol/
+ @$(COMPILE) -ACP -T$@ cp tool/net/redbean.png o/$(MODE)/tool/net/virtualbean.justine.lol/redbean.png
+ @$(COMPILE) -ACP -T$@ cp tool/net/demo/virtualbean.html o/$(MODE)/tool/net/virtualbean.justine.lol/index.html
+ @(cd o/$(MODE)/tool/net && zip -q redbean-demo.com virtualbean.justine.lol/)
+ @(cd o/$(MODE)/tool/net && echo 'Go to http://virtualbean.justine.lol' | zip -cq redbean-demo.com virtualbean.justine.lol/index.html)
+ @(cd o/$(MODE)/tool/net && zip -q redbean-demo.com virtualbean.justine.lol/redbean.png)
o/$(MODE)/tool/net/redbean-static.com: \
o/$(MODE)/tool/net/redbean-static.com.dbg \
diff --git a/tool/net/redbean.c b/tool/net/redbean.c
index c7ebcc6a7..e9b8437c5 100644
--- a/tool/net/redbean.c
+++ b/tool/net/redbean.c
@@ -1091,7 +1091,11 @@ static void GetOpts(int argc, char *argv[]) {
static void Daemonize(void) {
char ibuf[21];
int i, fd, pid;
- for (i = 0; i < 128; ++i) close(i);
+ for (i = 3; i < 128; ++i) {
+ if (i != server) {
+ close(i);
+ }
+ }
if ((pid = fork()) > 0) exit(0);
setsid();
if ((pid = fork()) > 0) _exit(0);
@@ -1105,8 +1109,8 @@ static void Daemonize(void) {
freopen("/dev/null", "r", stdin);
freopen(logpath, "a", stdout);
freopen(logpath, "a", stderr);
- LOGIFNEG1(setuid(daemonuid));
LOGIFNEG1(setgid(daemongid));
+ LOGIFNEG1(setuid(daemonuid));
}
static void ReportWorkerExit(int pid, int ws) {
@@ -1164,13 +1168,13 @@ static void AppendResourceReport(struct rusage *ru, const char *nl) {
(int)((long double)ru->ru_nvcsw / (ru->ru_nvcsw + ru->ru_nivcsw) * 100),
nl);
}
- if (ru->ru_inblock || ru->ru_oublock) {
- Append("performed %,ld read and %,ld write i/o operations%s",
- ru->ru_inblock, ru->ru_oublock, nl);
- }
if (ru->ru_msgrcv || ru->ru_msgsnd) {
- Append("received %,ld message and sent %,ld%s", ru->ru_msgrcv,
- ru->ru_msgsnd, nl);
+ Append("received %,ld message%s and sent %,ld%s", ru->ru_msgrcv,
+ ru->ru_msgrcv == 1 ? "" : "s", ru->ru_msgsnd, nl);
+ }
+ if (ru->ru_inblock || ru->ru_oublock) {
+ Append("performed %,ld read%s and %,ld write i/o operations%s",
+ ru->ru_inblock, ru->ru_inblock == 1 ? "" : "s", ru->ru_oublock, nl);
}
if (ru->ru_nsignals) {
Append("received %,ld signals%s", ru->ru_nsignals, nl);
@@ -2035,17 +2039,25 @@ static char *ServeListing(void) {
\r\n\
\r\n");
AppendLogo();
rp[0] = EscapeHtml(brand, -1, &rn[0]);
AppendData(rp[0], rn[0]);
free(rp[0]);
- Append("
\r\n"); + Append("\r\n" + "%.*s\r\n" + "
\r\n" + "\r\n" + "\r\n", + strnlen(GetZipCdirComment(cdir), GetZipCdirCommentSize(cdir)), + GetZipCdirComment(cdir)); memset(w, 0, sizeof(w)); n = GetZipCdirRecords(cdir); for (cf = GetZipCdirOffset(cdir); n--; cf += ZIP_CFILE_HDRSIZE(zmap + cf)) { @@ -2073,7 +2085,6 @@ td { padding-right: 3em; }\r\n\ ZIP_CFILE_COMMENTSIZE(zmap + cf)), &rn[3]); rp[4] = EscapeHtml(rp[0], rn[0], &rn[4]); - rp[5] = EscapeHtml(rp[3], rn[3], &rn[0]); lastmod = GetZipCfileLastModified(zmap + cf); localtime_r(&lastmod, &tm); strftime(tb, sizeof(tb), "%Y-%m-%d %H:%M:%S %Z", &tm); @@ -2083,14 +2094,13 @@ td { padding-right: 3em; }\r\n\ Append("%-*.*s %s %0*o %4s %,*ld %'s\r\n", rn[2], rp[2], w[0], rn[4], rp[4], tb, w[1], GetZipCfileMode(zmap + cf), DescribeCompressionRatio(rb, lf), - w[2], GetZipLfileUncompressedSize(zmap + lf), rp[5]); + w[2], GetZipLfileUncompressedSize(zmap + lf), rp[3]); } else { Append("%-*.*s %s %0*o %4s %,*ld %'s\r\n", w[0], rn[4], rp[4], tb, w[1], GetZipCfileMode(zmap + cf), DescribeCompressionRatio(rb, lf), w[2], - GetZipLfileUncompressedSize(zmap + lf), rp[5]); + GetZipLfileUncompressedSize(zmap + lf), rp[3]); } - free(rp[5]); free(rp[4]); free(rp[3]); free(rp[2]); @@ -2100,9 +2110,13 @@ td { padding-right: 3em; }\r\n\ free(path); } Append("