Use re-entrant locks on stdio

This commit is contained in:
Justine Tunney 2022-05-22 08:13:13 -07:00
parent 4e9662cbc7
commit 1f229e4efc
78 changed files with 427 additions and 179 deletions

View file

@ -42,6 +42,7 @@
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/poll.h"
#include "libc/sysv/consts/sa.h"
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/consts/so.h"
#include "libc/sysv/consts/sock.h"
#include "libc/sysv/consts/sol.h"
@ -110,7 +111,12 @@ void OnInterrupt(int sig) {
void OnChildTerminated(int sig) {
int ws, pid;
sigset_t ss, oldss;
sigfillset(&ss);
sigdelset(&ss, SIGTERM);
sigprocmask(SIG_BLOCK, &ss, &oldss);
for (;;) {
INFOF("waitpid");
if ((pid = waitpid(-1, &ws, WNOHANG)) != -1) {
if (pid) {
if (WIFEXITED(ws)) {
@ -127,6 +133,7 @@ void OnChildTerminated(int sig) {
FATALF("waitpid failed in sigchld");
}
}
sigprocmask(SIG_SETMASK, &oldss, 0);
}
wontreturn void ShowUsage(FILE *f, int rc) {
@ -229,6 +236,7 @@ void SendExitMessage(int rc) {
msg[0 + 3] = (RUNITD_MAGIC & 0x000000ff) >> 000;
msg[4] = kRunitExit;
msg[5] = rc;
INFOF("mbedtls_ssl_write");
CHECK_EQ(sizeof(msg), mbedtls_ssl_write(&ezssl, msg, sizeof(msg)));
CHECK_EQ(0, EzTlsFlush(&ezbio, 0, 0));
}
@ -247,6 +255,7 @@ void SendOutputFragmentMessage(enum RunitCommand kind, unsigned char *buf,
msg[5 + 1] = (size & 0x00ff0000) >> 020;
msg[5 + 2] = (size & 0x0000ff00) >> 010;
msg[5 + 3] = (size & 0x000000ff) >> 000;
INFOF("mbedtls_ssl_write");
CHECK_EQ(sizeof(msg), mbedtls_ssl_write(&ezssl, msg, sizeof(msg)));
while (size) {
CHECK_NE(-1, (rc = mbedtls_ssl_write(&ezssl, buf, size)));
@ -294,6 +303,7 @@ void Recv(void *output, size_t outputsize) {
// get another fixed-size data packet from network
// pass along error conditions to caller
// pass along eof condition to zlib
INFOF("mbedtls_ssl_read");
received = mbedtls_ssl_read(&ezssl, buf, sizeof(buf));
if (received < 0) TlsDie("read failed", received);
// decompress packet completely
@ -347,12 +357,14 @@ void HandleClient(void) {
/* read request to run program */
addrsize = sizeof(addr);
INFOF("accept");
CHECK_NE(-1, (g_clifd = accept4(g_servfd, &addr, &addrsize, SOCK_CLOEXEC)));
if (fork()) {
close(g_clifd);
return;
}
EzFd(g_clifd);
INFOF("EzHandshake");
EzHandshake();
addrstr = gc(DescribeAddress(&addr));
DEBUGF("%s %s %s", gc(DescribeAddress(&g_servaddr)), "accepted", addrstr);
@ -376,6 +388,7 @@ void HandleClient(void) {
}
CHECK_NE(-1, (g_exefd = creat(g_exepath, 0700)));
LOGIFNEG1(ftruncate(g_exefd, filesize));
INFOF("xwrite");
CHECK_NE(-1, xwrite(g_exefd, exe, filesize));
LOGIFNEG1(close(g_exefd));
@ -425,6 +438,7 @@ void HandleClient(void) {
fds[0].events = POLLIN;
fds[1].fd = pipefds[0];
fds[1].events = POLLIN;
INFOF("poll");
events = poll(fds, ARRAYLEN(fds), (deadline - now) * 1000);
CHECK_NE(-1, events); // EINTR shouldn't be possible
if (fds[0].revents) {
@ -440,6 +454,7 @@ void HandleClient(void) {
LOGIFNEG1(unlink(g_exepath));
_exit(1);
}
INFOF("read");
got = read(pipefds[0], g_buf, sizeof(g_buf));
CHECK_NE(-1, got); // EINTR shouldn't be possible
if (!got) {
@ -449,6 +464,7 @@ void HandleClient(void) {
fwrite(g_buf, got, 1, stderr);
SendOutputFragmentMessage(kRunitStderr, g_buf, got);
}
INFOF("waitpid");
CHECK_NE(-1, waitpid(child, &wstatus, 0)); // EINTR shouldn't be possible
if (WIFEXITED(wstatus)) {
if (WEXITSTATUS(wstatus)) {
@ -463,6 +479,7 @@ void HandleClient(void) {
}
LOGIFNEG1(unlink(g_exepath));
SendExitMessage(exitcode);
INFOF("mbedtls_ssl_close_notify");
mbedtls_ssl_close_notify(&ezssl);
LOGIFNEG1(close(g_clifd));
_exit(0);
@ -524,7 +541,7 @@ void Daemonize(void) {
int main(int argc, char *argv[]) {
int i;
SetupPresharedKeySsl(MBEDTLS_SSL_IS_SERVER, GetRunitPsk());
/* __log_level = kLogDebug; */
__log_level = kLogInfo;
GetOpts(argc, argv);
for (i = 3; i < 16; ++i) close(i);
errno = 0;