mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-25 12:00:31 +00:00
Make more fixes and improvements
This change attempts to fix some report build issues. It also builds upon development work described in previous changes.
This commit is contained in:
parent
9bfa6ec06e
commit
9d61e23c80
27 changed files with 980 additions and 902 deletions
149
tool/net/echo.c
Normal file
149
tool/net/echo.c
Normal file
|
@ -0,0 +1,149 @@
|
|||
/*-*- 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 │
|
||||
│ │
|
||||
│ 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/calls/calls.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/log/check.h"
|
||||
#include "libc/rand/rand.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/sysv/consts/af.h"
|
||||
#include "libc/sysv/consts/ipproto.h"
|
||||
#include "libc/sysv/consts/sock.h"
|
||||
#include "net/http/http.h"
|
||||
|
||||
/**
|
||||
* @fileoverview tcp/udp echo servers/clients
|
||||
* use it to fill your network with junk data
|
||||
*/
|
||||
|
||||
int sock;
|
||||
char buf[1000];
|
||||
struct sockaddr_in addr = {0};
|
||||
uint32_t addrsize = sizeof(struct sockaddr_in);
|
||||
|
||||
void PrintUsage(char **argv) {
|
||||
kprintf("usage: %s udp server [ip port]%n", argv[0]);
|
||||
kprintf(" %s tcp server [ip port]%n", argv[0]);
|
||||
kprintf(" %s udp client [ip port]%n", argv[0]);
|
||||
kprintf(" %s tcp client [ip port]%n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void UdpServer(void) {
|
||||
ssize_t rc;
|
||||
struct sockaddr_in addr2;
|
||||
uint32_t addrsize2 = sizeof(struct sockaddr_in);
|
||||
CHECK_NE(-1, (sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)));
|
||||
CHECK_NE(-1, bind(sock, &addr, addrsize));
|
||||
CHECK_NE(-1, getsockname(sock, &addr2, &addrsize2));
|
||||
kprintf("udp server %hhu.%hhu.%hhu.%hhu %hu%n", addr2.sin_addr.s_addr >> 24,
|
||||
addr2.sin_addr.s_addr >> 16, addr2.sin_addr.s_addr >> 8,
|
||||
addr2.sin_addr.s_addr, addr2.sin_port);
|
||||
for (;;) {
|
||||
CHECK_NE(-1,
|
||||
(rc = recvfrom(sock, buf, sizeof(buf), 0, &addr2, &addrsize2)));
|
||||
CHECK_NE(-1, sendto(sock, buf, rc, 0, &addr2, addrsize2));
|
||||
}
|
||||
}
|
||||
|
||||
void UdpClient(void) {
|
||||
CHECK_NE(-1, (sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)));
|
||||
CHECK_NE(-1, connect(sock, &addr, addrsize));
|
||||
for (;;) {
|
||||
rngset(buf, sizeof(buf), rand64, -1);
|
||||
CHECK_NE(-1, write(sock, &addr, addrsize));
|
||||
}
|
||||
}
|
||||
|
||||
void TcpServer(void) {
|
||||
int client;
|
||||
ssize_t rc;
|
||||
struct sockaddr_in addr2;
|
||||
uint32_t addrsize2 = sizeof(struct sockaddr_in);
|
||||
CHECK_NE(-1, (sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)));
|
||||
CHECK_NE(-1, bind(sock, &addr, addrsize));
|
||||
CHECK_NE(-1, listen(sock, 10));
|
||||
CHECK_NE(-1, getsockname(sock, &addr2, &addrsize2));
|
||||
kprintf("tcp server %hhu.%hhu.%hhu.%hhu %hu%n", addr2.sin_addr.s_addr >> 24,
|
||||
addr2.sin_addr.s_addr >> 16, addr2.sin_addr.s_addr >> 8,
|
||||
addr2.sin_addr.s_addr, addr2.sin_port);
|
||||
for (;;) {
|
||||
addrsize2 = sizeof(struct sockaddr_in);
|
||||
CHECK_NE(-1, (client = accept(sock, &addr2, &addrsize2)));
|
||||
kprintf("got client %hhu.%hhu.%hhu.%hhu %hu%n", addr2.sin_addr.s_addr >> 24,
|
||||
addr2.sin_addr.s_addr >> 16, addr2.sin_addr.s_addr >> 8,
|
||||
addr2.sin_addr.s_addr, addr2.sin_port);
|
||||
for (;;) {
|
||||
CHECK_NE(-1, (rc = read(client, buf, sizeof(buf))));
|
||||
if (!rc) break;
|
||||
CHECK_NE(-1, write(client, buf, rc));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TcpClient(void) {
|
||||
CHECK_NE(-1, (sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)));
|
||||
CHECK_NE(-1, connect(sock, &addr, addrsize));
|
||||
for (;;) {
|
||||
rngset(buf, sizeof(buf), rand64, -1);
|
||||
CHECK_NE(-1, write(sock, buf, sizeof(buf)));
|
||||
CHECK_NE(-1, read(sock, buf, sizeof(buf)));
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int port = 0;
|
||||
int64_t ip = 0;
|
||||
if (argc < 3) PrintUsage(argv);
|
||||
if (argc >= 4) {
|
||||
if ((ip = ParseIp(argv[3], -1)) == -1) {
|
||||
PrintUsage(argv);
|
||||
}
|
||||
}
|
||||
if (argc >= 5) {
|
||||
port = atoi(argv[4]);
|
||||
if (port & 0xffff0000) {
|
||||
PrintUsage(argv);
|
||||
}
|
||||
}
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
addr.sin_addr.s_addr = htonl(ip);
|
||||
if (!strcmp(argv[1], "udp")) {
|
||||
if (!strcmp(argv[2], "server")) {
|
||||
UdpServer();
|
||||
} else if (!strcmp(argv[2], "client")) {
|
||||
UdpClient();
|
||||
} else {
|
||||
PrintUsage(argv);
|
||||
}
|
||||
} else if (!strcmp(argv[1], "tcp")) {
|
||||
if (!strcmp(argv[2], "server")) {
|
||||
TcpServer();
|
||||
} else if (!strcmp(argv[2], "client")) {
|
||||
TcpClient();
|
||||
} else {
|
||||
PrintUsage(argv);
|
||||
}
|
||||
} else {
|
||||
PrintUsage(argv);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -1,297 +0,0 @@
|
|||
/*-*- 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 │
|
||||
│ │
|
||||
│ 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/alg/arraylist.internal.h"
|
||||
#include "libc/bits/safemacros.internal.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/iovec.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/fmt.h"
|
||||
#include "libc/log/check.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/runtime/gc.internal.h"
|
||||
#include "libc/runtime/interruptiblecall.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/af.h"
|
||||
#include "libc/sysv/consts/exit.h"
|
||||
#include "libc/sysv/consts/ipproto.h"
|
||||
#include "libc/sysv/consts/msg.h"
|
||||
#include "libc/sysv/consts/poll.h"
|
||||
#include "libc/sysv/consts/so.h"
|
||||
#include "libc/sysv/consts/sock.h"
|
||||
#include "libc/sysv/consts/sol.h"
|
||||
#include "libc/x/x.h"
|
||||
#include "third_party/getopt/getopt.h"
|
||||
|
||||
/**
|
||||
* @fileoverview Asynchronous TCP/UDP Echo Server.
|
||||
*
|
||||
* make -j8 o/default/tool/net/echoserver.com
|
||||
* o/default/tool/net/echoserver.com udp:0.0.0.0:7 tcp:0.0.0.0:7
|
||||
*/
|
||||
|
||||
enum SocketKind {
|
||||
kSocketServer,
|
||||
kSocketClient,
|
||||
};
|
||||
|
||||
struct Message {
|
||||
struct iovec data;
|
||||
struct sockaddr_in dest;
|
||||
uint32_t destsize;
|
||||
};
|
||||
|
||||
struct Messages {
|
||||
size_t i, n;
|
||||
struct Message *p;
|
||||
};
|
||||
|
||||
struct Socket {
|
||||
int64_t fd;
|
||||
enum SocketKind kind;
|
||||
int type;
|
||||
int protocol;
|
||||
struct sockaddr_in addr;
|
||||
struct Messages egress; /* LIFO */
|
||||
};
|
||||
|
||||
struct Sockets {
|
||||
size_t i, n;
|
||||
struct Socket *p;
|
||||
};
|
||||
|
||||
struct Polls {
|
||||
size_t i, n;
|
||||
struct pollfd *p;
|
||||
};
|
||||
|
||||
struct Sockets g_sockets;
|
||||
struct Polls g_polls;
|
||||
|
||||
dontdiscard char *DescribeAddress(struct sockaddr_in *addr) {
|
||||
char ip4buf[16];
|
||||
return xasprintf("%s:%hu",
|
||||
inet_ntop(addr->sin_family, &addr->sin_addr.s_addr, ip4buf,
|
||||
sizeof(ip4buf)),
|
||||
ntohs(addr->sin_port));
|
||||
}
|
||||
|
||||
dontdiscard char *DescribeSocket(struct Socket *s) {
|
||||
return xasprintf("%s:%s", s->protocol == IPPROTO_UDP ? "udp" : "tcp",
|
||||
gc(DescribeAddress(&s->addr)));
|
||||
}
|
||||
|
||||
wontreturn void ShowUsageAndExit(bool iserror) {
|
||||
FILE *f = iserror ? stderr : stdout;
|
||||
int rc = iserror ? EXIT_FAILURE : EXIT_SUCCESS;
|
||||
fprintf(f, "%s: %s %s\n", "Usage", __argv[0], "PROTOCOL:ADDR:PORT...");
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
void GetFlags(int argc, char *argv[]) {
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "h")) != -1) {
|
||||
switch (opt) {
|
||||
case 'h':
|
||||
ShowUsageAndExit(false);
|
||||
default:
|
||||
ShowUsageAndExit(true);
|
||||
}
|
||||
}
|
||||
if (optind == argc) ShowUsageAndExit(true);
|
||||
}
|
||||
|
||||
void AddSocket(const struct Socket *s) {
|
||||
struct pollfd pfd;
|
||||
pfd.fd = s->fd;
|
||||
pfd.events = POLLIN;
|
||||
pfd.revents = 0;
|
||||
CHECK_NE(-1L, append(&g_sockets, s));
|
||||
CHECK_NE(-1L, append(&g_polls, &pfd));
|
||||
}
|
||||
|
||||
void RemoveSocket(size_t i) {
|
||||
DCHECK_LT(i, g_sockets.i);
|
||||
INFOF("removing: %s", gc(DescribeSocket(&g_sockets.p[i])));
|
||||
CHECK_NE(-1, close(g_sockets.p[i].fd));
|
||||
while (g_sockets.p[i].egress.i) {
|
||||
free(g_sockets.p[i].egress.p[g_sockets.p[i].egress.i - 1].data.iov_base);
|
||||
}
|
||||
memcpy(&g_sockets.p[i], &g_sockets.p[i + 1],
|
||||
(intptr_t)&g_sockets.p[g_sockets.i] - (intptr_t)&g_sockets.p[i + 1]);
|
||||
memcpy(&g_polls.p[i], &g_polls.p[i + 1],
|
||||
(intptr_t)&g_polls.p[g_polls.i] - (intptr_t)&g_polls.p[i + 1]);
|
||||
g_sockets.i--;
|
||||
g_polls.i--;
|
||||
}
|
||||
|
||||
void GetListeningAddressesFromCommandLine(int argc, char *argv[]) {
|
||||
int i;
|
||||
for (i = optind; i < argc; ++i) {
|
||||
struct Socket server;
|
||||
bzero(&server, sizeof(server));
|
||||
char scheme[4];
|
||||
unsigned char *ip4 = (unsigned char *)&server.addr.sin_addr.s_addr;
|
||||
uint16_t port;
|
||||
if (sscanf(argv[i], "%3s:%hhu.%hhu.%hhu.%hhu:%hu", scheme, &ip4[0], &ip4[1],
|
||||
&ip4[2], &ip4[3], &port) != 6) {
|
||||
fprintf(stderr, "error: bad ip4 uri\n");
|
||||
ShowUsageAndExit(true);
|
||||
}
|
||||
server.fd = -1;
|
||||
server.kind = kSocketServer;
|
||||
server.addr.sin_family = AF_INET;
|
||||
server.addr.sin_port = htons(port);
|
||||
if (strcasecmp(scheme, "tcp") == 0) {
|
||||
server.type = SOCK_STREAM;
|
||||
server.protocol = IPPROTO_TCP;
|
||||
} else if (strcasecmp(scheme, "udp") == 0) {
|
||||
server.type = SOCK_DGRAM;
|
||||
server.protocol = IPPROTO_UDP;
|
||||
} else {
|
||||
fprintf(stderr, "%s: %s\n", "error", "bad scheme (should be tcp or udp)");
|
||||
ShowUsageAndExit(true);
|
||||
}
|
||||
AddSocket(&server);
|
||||
}
|
||||
}
|
||||
|
||||
void BeginListeningForIncomingTraffic(void) {
|
||||
size_t i;
|
||||
for (i = 0; i < g_sockets.i; ++i) {
|
||||
int yes = 1;
|
||||
struct Socket *s = &g_sockets.p[i];
|
||||
CHECK_NE(-1L, (g_polls.p[i].fd = s->fd =
|
||||
socket(s->addr.sin_family, s->type, s->protocol)));
|
||||
CHECK_NE(-1L,
|
||||
setsockopt(s->fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes)));
|
||||
CHECK_NE(-1, bind(s->fd, &s->addr, sizeof(s->addr)));
|
||||
if (s->protocol == IPPROTO_TCP) {
|
||||
CHECK_NE(-1, listen(s->fd, 1));
|
||||
}
|
||||
uint32_t addrsize = sizeof(s->addr);
|
||||
CHECK_NE(-1, getsockname(s->fd, &s->addr, &addrsize));
|
||||
INFOF("listening on %s", gc(DescribeSocket(s)));
|
||||
}
|
||||
}
|
||||
|
||||
void AcceptConnection(size_t i) {
|
||||
struct Socket *server = &g_sockets.p[i];
|
||||
struct Socket client;
|
||||
bzero(&client, sizeof(client));
|
||||
client.kind = kSocketClient;
|
||||
client.type = server->type;
|
||||
client.protocol = server->protocol;
|
||||
uint32_t addrsize = sizeof(client.addr);
|
||||
CHECK_NE(-1L, (client.fd = accept(server->fd, &client.addr, &addrsize)));
|
||||
INFOF("%s accepted %s", gc(DescribeSocket(server)),
|
||||
gc(DescribeSocket(&client)));
|
||||
AddSocket(&client);
|
||||
}
|
||||
|
||||
bool ReceiveData(size_t i) {
|
||||
ssize_t got;
|
||||
struct Message msg;
|
||||
bool isudp = g_sockets.p[i].protocol == IPPROTO_UDP;
|
||||
bzero(&msg, sizeof(msg));
|
||||
msg.destsize = sizeof(msg.dest);
|
||||
msg.data.iov_len = PAGESIZE;
|
||||
msg.data.iov_base = xmalloc(msg.data.iov_len);
|
||||
CHECK_NE(-1L, (got = recvfrom(g_sockets.p[i].fd, msg.data.iov_base,
|
||||
msg.data.iov_len, 0, isudp ? &msg.dest : NULL,
|
||||
isudp ? &msg.destsize : NULL)));
|
||||
if (0 < got && got <= msg.data.iov_len) {
|
||||
INFOF("%s received %lu bytes from %s", gc(DescribeSocket(&g_sockets.p[i])),
|
||||
got, gc(DescribeAddress(&msg.dest)));
|
||||
msg.data.iov_base = xrealloc(msg.data.iov_base, (msg.data.iov_len = got));
|
||||
append(&g_sockets.p[i].egress, &msg);
|
||||
g_polls.p[i].events |= POLLOUT;
|
||||
return true;
|
||||
} else {
|
||||
RemoveSocket(i);
|
||||
free_s(&msg.data.iov_base);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void SendData(size_t i) {
|
||||
ssize_t sent;
|
||||
struct Socket *s = &g_sockets.p[i];
|
||||
struct Message *msg = &s->egress.p[s->egress.i - 1];
|
||||
bool isudp = s->protocol == IPPROTO_UDP;
|
||||
DCHECK(s->egress.i);
|
||||
CHECK_NE(-1L, (sent = sendto(s->fd, msg->data.iov_base, msg->data.iov_len, 0,
|
||||
isudp ? &msg->dest : NULL,
|
||||
isudp ? msg->destsize : 0)));
|
||||
INFOF("%s sent %lu bytes to %s", gc(DescribeSocket(s)), msg->data.iov_len,
|
||||
gc(DescribeAddress(&msg->dest)));
|
||||
if (!(msg->data.iov_len -= min((size_t)sent, (size_t)msg->data.iov_len))) {
|
||||
free_s(&msg->data.iov_base);
|
||||
if (!--s->egress.i) {
|
||||
g_polls.p[i].events &= ~POLLOUT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HandleSomeNetworkTraffic(void) {
|
||||
size_t i;
|
||||
int eventcount;
|
||||
CHECK_GE((eventcount = poll(g_polls.p, g_polls.i, -1)), 0);
|
||||
for (i = 0; eventcount && i < g_sockets.i; ++i) {
|
||||
if (!g_polls.p[i].revents) continue;
|
||||
--eventcount;
|
||||
if (g_polls.p[i].revents & (POLLERR | POLLHUP | POLLNVAL)) {
|
||||
CHECK_EQ(kSocketClient, g_sockets.p[i].kind);
|
||||
RemoveSocket(i);
|
||||
} else {
|
||||
if (g_polls.p[i].revents & POLLIN) {
|
||||
if (g_sockets.p[i].kind == kSocketServer &&
|
||||
g_sockets.p[i].protocol == IPPROTO_TCP) {
|
||||
AcceptConnection(i);
|
||||
} else {
|
||||
if (!ReceiveData(i)) continue;
|
||||
}
|
||||
}
|
||||
if (g_polls.p[i].revents & POLLOUT) {
|
||||
SendData(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EchoServer(void) {
|
||||
for (;;) HandleSomeNetworkTraffic();
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
STATIC_YOINK("isatty");
|
||||
GetFlags(argc, argv);
|
||||
GetListeningAddressesFromCommandLine(argc, argv);
|
||||
BeginListeningForIncomingTraffic();
|
||||
struct InterruptibleCall icall;
|
||||
bzero(&icall, sizeof(icall));
|
||||
interruptiblecall(&icall, (void *)EchoServer, 0, 0, 0, 0);
|
||||
fputc('\r', stderr);
|
||||
INFOF("%s", "shutting down...");
|
||||
size_t i;
|
||||
for (i = g_sockets.i; i; --i) RemoveSocket(i - 1);
|
||||
return 0;
|
||||
}
|
File diff suppressed because it is too large
Load diff
162
tool/net/lunix.c
162
tool/net/lunix.c
|
@ -381,23 +381,23 @@ static int LuaUnixExecve(lua_State *L) {
|
|||
|
||||
// unix.commandv(prog:str) → path:str[, errno:int]
|
||||
static int LuaUnixCommandv(lua_State *L) {
|
||||
int rc, olderr;
|
||||
const char *prog;
|
||||
int rc, olderr, pushed;
|
||||
char *pathbuf, *resolved;
|
||||
olderr = errno;
|
||||
pathbuf = xmalloc(PATH_MAX);
|
||||
prog = luaL_checkstring(L, 1);
|
||||
if ((resolved = commandv(prog, pathbuf))) {
|
||||
lua_pushstring(L, resolved);
|
||||
pushed = 1;
|
||||
if ((pathbuf = malloc(PATH_MAX))) {
|
||||
prog = luaL_checkstring(L, 1);
|
||||
if ((resolved = commandv(prog, pathbuf))) {
|
||||
lua_pushstring(L, resolved);
|
||||
free(pathbuf);
|
||||
return 1;
|
||||
} else {
|
||||
free(pathbuf);
|
||||
return ReturnErrno(L, 1, olderr);
|
||||
}
|
||||
} else {
|
||||
lua_pushnil(L);
|
||||
lua_pushinteger(L, errno);
|
||||
errno = olderr;
|
||||
pushed = 2;
|
||||
return ReturnErrno(L, 1, olderr);
|
||||
}
|
||||
free(pathbuf);
|
||||
return pushed;
|
||||
}
|
||||
|
||||
// unix.realpath(path:str) → path:str[, errno:int]
|
||||
|
@ -720,31 +720,31 @@ static int LuaUnixTruncate(lua_State *L) {
|
|||
static int LuaUnixRead(lua_State *L) {
|
||||
char *buf;
|
||||
size_t got;
|
||||
int fd, olderr, pushed;
|
||||
int fd, olderr;
|
||||
int64_t rc, bufsiz, offset;
|
||||
olderr = errno;
|
||||
fd = luaL_checkinteger(L, 1);
|
||||
bufsiz = luaL_optinteger(L, 2, BUFSIZ);
|
||||
offset = luaL_optinteger(L, 3, -1);
|
||||
bufsiz = MIN(bufsiz, 0x7ffff000);
|
||||
buf = xmalloc(bufsiz);
|
||||
if (offset == -1) {
|
||||
rc = read(fd, buf, bufsiz);
|
||||
if ((buf = malloc(bufsiz))) {
|
||||
if (offset == -1) {
|
||||
rc = read(fd, buf, bufsiz);
|
||||
} else {
|
||||
rc = pread(fd, buf, bufsiz, offset);
|
||||
}
|
||||
if (rc != -1) {
|
||||
got = rc;
|
||||
lua_pushlstring(L, buf, got);
|
||||
free(buf);
|
||||
return 1;
|
||||
} else {
|
||||
free(buf);
|
||||
return ReturnErrno(L, 1, olderr);
|
||||
}
|
||||
} else {
|
||||
rc = pread(fd, buf, bufsiz, offset);
|
||||
return ReturnErrno(L, 1, olderr);
|
||||
}
|
||||
if (rc != -1) {
|
||||
got = rc;
|
||||
lua_pushlstring(L, buf, got);
|
||||
pushed = 1;
|
||||
} else {
|
||||
lua_pushnil(L);
|
||||
lua_pushinteger(L, errno);
|
||||
errno = olderr;
|
||||
pushed = 2;
|
||||
}
|
||||
free(buf);
|
||||
return pushed;
|
||||
}
|
||||
|
||||
// unix.write(fd:int, data:str[, offset:int]) → rc:int[, errno:int]
|
||||
|
@ -773,29 +773,31 @@ static int LuaUnixStat(lua_State *L) {
|
|||
int rc, fd, olderr;
|
||||
struct UnixStat **ust, *st;
|
||||
olderr = errno;
|
||||
st = xmalloc(sizeof(struct UnixStat));
|
||||
if (lua_isinteger(L, 1)) {
|
||||
fd = luaL_checkinteger(L, 1);
|
||||
rc = fstat(fd, &st->st);
|
||||
} else if (lua_isstring(L, 1)) {
|
||||
path = luaL_checkstring(L, 1);
|
||||
rc = stat(path, &st->st);
|
||||
if ((st = malloc(sizeof(struct UnixStat)))) {
|
||||
if (lua_isinteger(L, 1)) {
|
||||
fd = luaL_checkinteger(L, 1);
|
||||
rc = fstat(fd, &st->st);
|
||||
} else if (lua_isstring(L, 1)) {
|
||||
path = luaL_checkstring(L, 1);
|
||||
rc = stat(path, &st->st);
|
||||
} else {
|
||||
free(st);
|
||||
luaL_argerror(L, 1, "not integer or string");
|
||||
unreachable;
|
||||
}
|
||||
if (rc != -1) {
|
||||
st->refs = 1;
|
||||
ust = lua_newuserdatauv(L, sizeof(st), 1);
|
||||
luaL_setmetatable(L, "UnixStat*");
|
||||
*ust = st;
|
||||
return 1;
|
||||
} else {
|
||||
free(st);
|
||||
return ReturnErrno(L, 1, olderr);
|
||||
}
|
||||
} else {
|
||||
luaL_argerror(L, 1, "not integer or string");
|
||||
unreachable;
|
||||
return ReturnErrno(L, 1, olderr);
|
||||
}
|
||||
if (rc == -1) {
|
||||
lua_pushnil(L);
|
||||
lua_pushinteger(L, errno);
|
||||
errno = olderr;
|
||||
free(st);
|
||||
return 2;
|
||||
}
|
||||
st->refs = 1;
|
||||
ust = lua_newuserdatauv(L, sizeof(st), 1);
|
||||
luaL_setmetatable(L, "UnixStat*");
|
||||
*ust = st;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// unix.opendir(path:str) → UnixDir*[, errno]
|
||||
|
@ -918,7 +920,6 @@ static int LuaUnixGetsockname(lua_State *L) {
|
|||
uint32_t addrsize;
|
||||
struct sockaddr_in sa;
|
||||
olderr = errno;
|
||||
bzero(&sa, sizeof(sa));
|
||||
addrsize = sizeof(sa);
|
||||
fd = luaL_checkinteger(L, 1);
|
||||
if (!getsockname(fd, &sa, &addrsize)) {
|
||||
|
@ -936,7 +937,6 @@ static int LuaUnixGetpeername(lua_State *L) {
|
|||
uint32_t addrsize;
|
||||
struct sockaddr_in sa;
|
||||
olderr = errno;
|
||||
bzero(&sa, sizeof(sa));
|
||||
addrsize = sizeof(sa);
|
||||
fd = luaL_checkinteger(L, 1);
|
||||
if (!getpeername(fd, &sa, &addrsize)) {
|
||||
|
@ -954,7 +954,6 @@ static int LuaUnixAccept(lua_State *L) {
|
|||
struct sockaddr_in sa;
|
||||
int clientfd, serverfd, olderr;
|
||||
olderr = errno;
|
||||
bzero(&sa, sizeof(sa));
|
||||
addrsize = sizeof(sa);
|
||||
serverfd = luaL_checkinteger(L, 1);
|
||||
clientfd = accept(serverfd, &sa, &addrsize);
|
||||
|
@ -976,32 +975,29 @@ static int LuaUnixRecvfrom(lua_State *L) {
|
|||
ssize_t rc;
|
||||
uint32_t addrsize;
|
||||
struct sockaddr_in sa;
|
||||
int fd, flags, bufsiz, olderr, pushed;
|
||||
int fd, flags, bufsiz, olderr;
|
||||
olderr = errno;
|
||||
bzero(&sa, sizeof(sa));
|
||||
addrsize = sizeof(sa);
|
||||
fd = luaL_checkinteger(L, 1);
|
||||
bufsiz = luaL_optinteger(L, 2, 1500);
|
||||
flags = luaL_optinteger(L, 3, 0);
|
||||
bufsiz = MIN(bufsiz, 0x7ffff000);
|
||||
buf = xmalloc(bufsiz);
|
||||
rc = recvfrom(fd, buf, bufsiz, flags, &sa, &addrsize);
|
||||
if (rc != -1) {
|
||||
got = rc;
|
||||
lua_pushlstring(L, buf, got);
|
||||
lua_pushinteger(L, ntohl(sa.sin_addr.s_addr));
|
||||
lua_pushinteger(L, ntohs(sa.sin_port));
|
||||
pushed = 3;
|
||||
if ((buf = malloc(bufsiz))) {
|
||||
rc = recvfrom(fd, buf, bufsiz, flags, &sa, &addrsize);
|
||||
if (rc != -1) {
|
||||
got = rc;
|
||||
lua_pushlstring(L, buf, got);
|
||||
lua_pushinteger(L, ntohl(sa.sin_addr.s_addr));
|
||||
lua_pushinteger(L, ntohs(sa.sin_port));
|
||||
free(buf);
|
||||
return 3;
|
||||
} else {
|
||||
free(buf);
|
||||
return ReturnErrno(L, 3, olderr);
|
||||
}
|
||||
} else {
|
||||
lua_pushnil(L);
|
||||
lua_pushnil(L);
|
||||
lua_pushnil(L);
|
||||
lua_pushinteger(L, errno);
|
||||
errno = olderr;
|
||||
pushed = 4;
|
||||
return ReturnErrno(L, 3, olderr);
|
||||
}
|
||||
free(buf);
|
||||
return pushed;
|
||||
}
|
||||
|
||||
// unix.recv(fd[, bufsiz[, flags]]) → data[, errno]
|
||||
|
@ -1015,20 +1011,20 @@ static int LuaUnixRecv(lua_State *L) {
|
|||
bufsiz = luaL_optinteger(L, 2, 1500);
|
||||
flags = luaL_optinteger(L, 3, 0);
|
||||
bufsiz = MIN(bufsiz, 0x7ffff000);
|
||||
buf = xmalloc(bufsiz);
|
||||
rc = recv(fd, buf, bufsiz, flags);
|
||||
if (rc != -1) {
|
||||
got = rc;
|
||||
lua_pushlstring(L, buf, got);
|
||||
pushed = 1;
|
||||
if ((buf = malloc(bufsiz))) {
|
||||
rc = recv(fd, buf, bufsiz, flags);
|
||||
if (rc != -1) {
|
||||
got = rc;
|
||||
lua_pushlstring(L, buf, got);
|
||||
free(buf);
|
||||
return 1;
|
||||
} else {
|
||||
free(buf);
|
||||
return ReturnErrno(L, 1, olderr);
|
||||
}
|
||||
} else {
|
||||
lua_pushnil(L);
|
||||
lua_pushinteger(L, errno);
|
||||
errno = olderr;
|
||||
pushed = 2;
|
||||
return ReturnErrno(L, 3, olderr);
|
||||
}
|
||||
free(buf);
|
||||
return pushed;
|
||||
}
|
||||
|
||||
// unix.send(fd, data[, flags]) → sent, errno
|
||||
|
|
|
@ -16,16 +16,18 @@ TOOL_NET_BINS = \
|
|||
|
||||
TOOL_NET_COMS = \
|
||||
o/$(MODE)/tool/net/dig.com \
|
||||
o/$(MODE)/tool/net/echoserver.com \
|
||||
o/$(MODE)/tool/net/redbean.com \
|
||||
o/$(MODE)/tool/net/redbean-demo.com \
|
||||
o/$(MODE)/tool/net/redbean-static.com \
|
||||
o/$(MODE)/tool/net/redbean-unsecure.com \
|
||||
o/$(MODE)/tool/net/redbean-original.com \
|
||||
o/$(MODE)/tool/net/redbean-assimilate.com \
|
||||
o/$(MODE)/tool/net/echoserver.com \
|
||||
o/$(MODE)/tool/net/wb.com
|
||||
|
||||
TOOL_NET_CHECKS = \
|
||||
o/$(MODE)/tool/net/net.pkg \
|
||||
$(TOOL_NET_HDRS:%=o/$(MODE)/%.ok)
|
||||
|
||||
TOOL_NET_DIRECTDEPS = \
|
||||
DSP_SCALE \
|
||||
LIBC_ALG \
|
||||
|
|
|
@ -2217,25 +2217,20 @@ static void *LoadAsset(struct Asset *a, size_t *out_size) {
|
|||
}
|
||||
}
|
||||
|
||||
static const char *GetPagerPath(char path[PATH_MAX]) {
|
||||
const char *s;
|
||||
if ((s = commandv("less", path))) return s;
|
||||
if ((s = commandv("more", path))) return s;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static wontreturn void PrintUsage(int fd, int rc) {
|
||||
size_t n;
|
||||
int pip[2];
|
||||
const char *p;
|
||||
struct Asset *a;
|
||||
char buf[PATH_MAX];
|
||||
char *args[2] = {0};
|
||||
char pathbuf[PATH_MAX];
|
||||
if (!(a = GetAssetZip("/help.txt", 9)) || !(p = LoadAsset(a, &n))) {
|
||||
fprintf(stderr, "error: /help.txt is not a zip asset\n");
|
||||
exit(1);
|
||||
}
|
||||
if (isatty(0) && isatty(1) && (args[0] = GetPagerPath(pathbuf))) {
|
||||
if (strcmp(nulltoempty(getenv("TERM")), "dumb") && isatty(0) && isatty(1) &&
|
||||
((args[0] = commandv("less", buf)) ||
|
||||
(args[0] = commandv("more", buf)))) {
|
||||
sigaction(SIGPIPE, &(struct sigaction){.sa_handler = SIG_IGN}, 0);
|
||||
close(0);
|
||||
pipe(pip);
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include "libc/calls/struct/bpf.h"
|
||||
#include "libc/calls/struct/filter.h"
|
||||
#include "libc/calls/struct/seccomp.h"
|
||||
#include "libc/sysv/consts/audit.h"
|
||||
// clang-format off
|
||||
|
||||
#define _SECCOMP_MACHINE(MAGNUM) \
|
||||
|
@ -17,7 +18,15 @@
|
|||
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, MAGNUM, 0, 1), \
|
||||
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW)
|
||||
|
||||
#define _SECCOMP_TRAP_SYSCALL(MAGNUM, DATA) \
|
||||
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, MAGNUM, 0, 1), \
|
||||
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRAP | ((DATA) & SECCOMP_RET_DATA))
|
||||
|
||||
#define _SECCOMP_TRACE_SYSCALL(MAGNUM, DATA) \
|
||||
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, MAGNUM, 0, 1), \
|
||||
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRACE | ((DATA) & SECCOMP_RET_DATA))
|
||||
|
||||
#define _SECCOMP_LOG_AND_RETURN_ERRNO(MAGNUM) \
|
||||
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ERRNO | (MAGNUM & SECCOMP_RET_DATA))
|
||||
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ERRNO | ((MAGNUM) & SECCOMP_RET_DATA))
|
||||
|
||||
#endif /* COSMOPOLITAN_TOOL_NET_SANDBOX_H_ */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue