Make improvements to redbean

The following Lua APIs have been added:

  - IsDaemon() → bool
  - ProgramPidPath(str)

The following Lua hooks have been added:

  - OnClientConnection(ip:int,port:int,serverip:int,serverport:int) → bool
  - OnProcessCreate(pid:int,ip:int,port:int,serverip:int,serverport:int)
  - OnProcessDestroy(pid:int)
  - OnServerStart()
  - OnServerStop()
  - OnWorkerStart()
  - OnWorkerStop()

redbean now does a better job at applying gzip on the fly from the local
filesystem, using a streaming chunked api with constant memory, which is
useful for doing things like serving a 4gb text file off NFS, and having
it start transmitting in milliseconds. redbean will also compute entropy
on the beginnings of files to determine if compression is profitable.

This change pays off technical debts relating to memory, such as relying
on exit() to free() allocations. That's now mostly fixed so it should be
easier now to spot memory leaks in malloc traces.

This change also fixes bugs and makes improvements to our SSL support.
Uniprocess mode failed handshakes are no longer an issue. Token Alpn is
offered so curl -v looks less weird. Hybrid SSL certificate loading is
now smarter about naming conflicts. Self-signed CA root anchors will no
longer be delivered to the client during the handshake.
This commit is contained in:
Justine Tunney 2021-07-10 15:02:03 -07:00
parent 98c674d915
commit 8c4cce043c
25 changed files with 22326 additions and 359 deletions

View file

@ -3,9 +3,12 @@
#ifndef __STRICT_ANSI__
#include "libc/assert.h"
#include "libc/bits/bits.h"
#include "libc/bits/weaken.h"
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/log/backtrace.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.internal.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#if 0
@ -674,15 +677,64 @@ extern struct MallocState g_dlmalloc[1];
/* ─────────────────────────────── Hooks ──────────────────────────────── */
#ifdef MTRACE /* TODO(jart): Add --mtrace flag for this */
void *AddressBirthAction(void *);
void *AddressDeathAction(void *);
#define ADDRESS_BIRTH_ACTION(A) AddressBirthAction(A)
#define ADDRESS_DEATH_ACTION(A) AddressDeathAction(A)
#else
#define ADDRESS_BIRTH_ACTION(A) (A)
#define ADDRESS_DEATH_ACTION(A) (A)
/*
d = {}
lines = open("log").read().split('\n')
def bad(i):
while i < len(lines):
if lines[i].startswith(('BIRTH', 'DEATH')):
break
print lines[i]
i += 1
for i, line in enumerate(lines):
i += 1
x = line.split()
if len(x) != 2: continue
if x[0] == 'DEATH':
b = int(x[1], 16)
if b in d:
if d[b] < 0:
print "OH NO", i, d[b]
else:
d[b] = -d[b]
else:
print "wut", i
elif x[0] == 'BIRTH':
b = int(x[1], 16)
if b in d:
if d[b] > 0:
print "bad malloc", i, d[b]
d[b] = i
else:
d[b] = i
for k,v in d.items():
if v > 0:
print "unfreed", v
bad(v)
*/
#define MALLOC_TRACE 0
static inline void *AddressBirthAction(void *p) {
#if MALLOC_TRACE
(dprintf)(2, "BIRTH %p\n", p);
if (weaken(PrintBacktraceUsingSymbols) && weaken(GetSymbolTable)) {
weaken(PrintBacktraceUsingSymbols)(2, __builtin_frame_address(0),
weaken(GetSymbolTable)());
}
#endif
return p;
}
static inline void *AddressDeathAction(void *p) {
#if MALLOC_TRACE
(dprintf)(2, "DEATH %p\n", p);
if (weaken(PrintBacktraceUsingSymbols) && weaken(GetSymbolTable)) {
weaken(PrintBacktraceUsingSymbols)(2, __builtin_frame_address(0),
weaken(GetSymbolTable)());
}
#endif
return p;
}
/*
PREACTION should be defined to return 0 on success, and nonzero on
@ -907,7 +959,7 @@ extern struct MallocParams g_mparams;
#else /* GNUC */
#define RTCHECK(e) (e)
#endif /* GNUC */
#else /* !IsTrustworthy() */
#else /* !IsTrustworthy() */
#define RTCHECK(e) (1)
#endif /* !IsTrustworthy() */