2020-06-15 14:18:57 +00:00
|
|
|
/*-*- 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 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2023-08-18 16:34:14 +00:00
|
|
|
#include "libc/intrin/getenv.internal.h"
|
2022-10-17 18:02:04 +00:00
|
|
|
#include "libc/intrin/kmalloc.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2022-03-18 09:33:37 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/mem/internal.h"
|
|
|
|
#include "libc/runtime/runtime.h"
|
|
|
|
|
2021-10-15 02:36:49 +00:00
|
|
|
#define ToUpper(c) ((c) >= 'a' && (c) <= 'z' ? (c) - 'a' + 'A' : (c))
|
|
|
|
|
2022-10-17 18:02:04 +00:00
|
|
|
static char **expected;
|
2022-03-18 09:33:37 +00:00
|
|
|
static size_t capacity;
|
2021-02-24 12:00:38 +00:00
|
|
|
|
2022-03-18 09:33:37 +00:00
|
|
|
static size_t GetEnvironLen(char **env) {
|
|
|
|
char **p = env;
|
|
|
|
while (*p) ++p;
|
|
|
|
return p - env;
|
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.
2021-07-10 22:02:03 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 18:02:04 +00:00
|
|
|
static char **GrowEnviron(char **a) {
|
2022-03-18 09:33:37 +00:00
|
|
|
size_t n, c;
|
2022-10-17 18:02:04 +00:00
|
|
|
char **b, **p;
|
|
|
|
if (!a) a = environ;
|
2022-08-17 21:15:23 +00:00
|
|
|
n = a ? GetEnvironLen(a) : 0;
|
2022-03-18 09:33:37 +00:00
|
|
|
c = MAX(16ul, n) << 1;
|
2022-10-17 18:02:04 +00:00
|
|
|
if ((b = kmalloc(c * sizeof(char *)))) {
|
|
|
|
if (a) {
|
|
|
|
for (p = b; *a;) {
|
|
|
|
*p++ = *a++;
|
|
|
|
}
|
2022-08-17 21:15:23 +00:00
|
|
|
}
|
2022-10-17 18:02:04 +00:00
|
|
|
environ = b;
|
|
|
|
expected = b;
|
|
|
|
capacity = c;
|
|
|
|
return b;
|
|
|
|
} else {
|
|
|
|
return 0;
|
2022-03-18 09:33:37 +00:00
|
|
|
}
|
2021-10-14 00:27:13 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 09:27:49 +00:00
|
|
|
int PutEnvImpl(char *s, bool overwrite) {
|
2022-10-17 18:02:04 +00:00
|
|
|
char **p;
|
|
|
|
struct Env e;
|
|
|
|
if (!(p = environ)) {
|
|
|
|
if (!(p = GrowEnviron(0))) {
|
|
|
|
return -1;
|
2021-10-15 02:36:49 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-18 16:34:14 +00:00
|
|
|
e = __getenv(p, s);
|
2022-10-17 18:02:04 +00:00
|
|
|
if (e.s && !overwrite) {
|
|
|
|
return 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-10-17 18:02:04 +00:00
|
|
|
if (e.s) {
|
|
|
|
p[e.i] = s;
|
|
|
|
return 0;
|
2021-10-14 00:27:13 +00:00
|
|
|
}
|
2022-10-17 18:02:04 +00:00
|
|
|
if (p != expected) {
|
|
|
|
capacity = e.i;
|
|
|
|
}
|
|
|
|
if (e.i + 1 >= capacity) {
|
|
|
|
if (!(p = GrowEnviron(p))) {
|
|
|
|
return -1;
|
|
|
|
}
|
2022-03-18 09:33:37 +00:00
|
|
|
}
|
2022-10-17 18:02:04 +00:00
|
|
|
p[e.i + 1] = 0;
|
|
|
|
p[e.i] = s;
|
|
|
|
return 0;
|
2022-03-18 09:33:37 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
/**
|
|
|
|
* Emplaces environment key=value.
|
2022-03-18 09:33:37 +00:00
|
|
|
*
|
2022-10-17 18:02:04 +00:00
|
|
|
* @param s should be a string that looks like `"name=value"` and it'll
|
|
|
|
* become part of the environment; changes to its memory will change
|
|
|
|
* the environment too
|
|
|
|
* @return 0 on success, or non-zero w/ errno on error
|
|
|
|
* @raise ENOMEM if we require more vespene gas
|
2020-06-15 14:18:57 +00:00
|
|
|
* @see setenv(), getenv()
|
2023-07-30 01:44:15 +00:00
|
|
|
* @threadunsafe
|
2020-06-15 14:18:57 +00:00
|
|
|
*/
|
2022-03-18 09:33:37 +00:00
|
|
|
int putenv(char *s) {
|
|
|
|
int rc;
|
2022-10-17 18:02:04 +00:00
|
|
|
rc = PutEnvImpl(s, true);
|
|
|
|
STRACE("putenv(%#s) → %d% m", s, rc);
|
2022-03-18 09:33:37 +00:00
|
|
|
return rc;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|