Make improvements

- Clean up sigaction() code
- Add a port scanner example
- Introduce a ParseCidr() API
- Clean up our futex abstraction code
- Fix a harmless integer overflow in ParseIp()
- Use kernel semaphores on NetBSD to make threads much faster
This commit is contained in:
Justine Tunney 2022-11-07 02:22:09 -08:00
parent 539bddce8c
commit c995838e5c
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
107 changed files with 1085 additions and 492 deletions

View file

@ -228,11 +228,13 @@ TEST(uc_sigmask, signalHandlerCanChangeSignalMaskOfTrappedThread) {
struct sigaction oldsa;
struct sigaction sa = {.sa_sigaction = OnSigMask, .sa_flags = SA_SIGINFO};
sigemptyset(&want);
ASSERT_SYS(0, 0, sigprocmask(SIG_SETMASK, &want, 0));
ASSERT_SYS(0, 0, sigprocmask(SIG_SETMASK, &want, &got));
ASSERT_FALSE(sigismember(&got, SIGUSR1));
ASSERT_SYS(0, 0, sigaction(SIGUSR1, &sa, &oldsa));
ASSERT_SYS(0, 0, raise(SIGUSR1));
ASSERT_TRUE(gotusr1);
ASSERT_SYS(0, 0, sigprocmask(SIG_SETMASK, 0, &got));
ASSERT_TRUE(sigismember(&got, SIGUSR1));
sigaddset(&want, SIGUSR1);
ASSERT_STREQ(DescribeSigset(0, &want), DescribeSigset(0, &got));
ASSERT_SYS(0, 0, sigaction(SIGUSR1, &oldsa, 0));

View file

@ -34,6 +34,7 @@ TEST_LIBC_INTRIN_DIRECTDEPS = \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
LIBC_SYSV_CALLS \
LIBC_THREAD \
LIBC_TESTLIB \
LIBC_TINYMATH \

View file

@ -22,7 +22,9 @@
#include "libc/errno.h"
#include "libc/fmt/fmt.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/kprintf.h"
#include "libc/limits.h"
#include "libc/log/log.h"
#include "libc/mem/gc.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
@ -39,6 +41,23 @@ FILE *f;
char buf[32];
char testlib_enable_tmp_setup_teardown;
void CheckForFdLeaks(void) {
int rc, i, l = 0, e = errno;
for (i = 3; i < 16; ++i) {
rc = fcntl(i, F_GETFL);
if (rc == -1) {
ASSERT_EQ(EBADF, errno);
errno = e;
} else {
kprintf("file descriptor %d leaked!\n", i);
++l;
}
}
if (l) {
__die();
}
}
TEST(popen, command) {
char foo[6];
testlib_extract("/zip/echo.com", "echo.com", 0755);
@ -46,6 +65,7 @@ TEST(popen, command) {
ASSERT_NE(NULL, fgets(foo, sizeof(foo), f));
ASSERT_STREQ("hello", foo);
ASSERT_EQ(0, pclose(f));
CheckForFdLeaks();
}
TEST(popen, semicolon) {
@ -53,6 +73,7 @@ TEST(popen, semicolon) {
ASSERT_STREQ("hello\n", fgets(buf, sizeof(buf), f));
ASSERT_STREQ("there\n", fgets(buf, sizeof(buf), f));
ASSERT_EQ(0, pclose(f));
CheckForFdLeaks();
}
TEST(popen, singleQuotes) {
@ -61,6 +82,7 @@ TEST(popen, singleQuotes) {
ASSERT_STREQ("hello $there\n", fgets(buf, sizeof(buf), f));
ASSERT_STREQ("yo\n", fgets(buf, sizeof(buf), f));
ASSERT_EQ(0, pclose(f));
CheckForFdLeaks();
}
TEST(popen, doubleQuotes) {
@ -68,6 +90,7 @@ TEST(popen, doubleQuotes) {
ASSERT_NE(NULL, (f = popen("echo -l \"$hello there\"", "r")));
ASSERT_STREQ("a b c there\n", fgets(buf, sizeof(buf), f));
ASSERT_EQ(0, pclose(f));
CheckForFdLeaks();
}
TEST(popen, quoteless) {
@ -77,6 +100,7 @@ TEST(popen, quoteless) {
ASSERT_STREQ("aa b c\n", fgets(buf, sizeof(buf), f)); // mixed feelings
ASSERT_STREQ("yo\n", fgets(buf, sizeof(buf), f));
ASSERT_EQ(0, pclose(f));
CheckForFdLeaks();
}
TEST(popen, pipe) {
@ -84,6 +108,7 @@ TEST(popen, pipe) {
ASSERT_NE(NULL, (f = popen("echo hello | toupper", "r")));
ASSERT_STREQ("HELLO\n", fgets(buf, sizeof(buf), f));
ASSERT_EQ(0, pclose(f));
CheckForFdLeaks();
}
sig_atomic_t gotsig;
@ -102,6 +127,7 @@ TEST(popen, complicated) {
ASSERT_EQ(0, pclose(f));
ASSERT_EQ(1, gotsig);
signal(SIGUSR1, SIG_DFL);
CheckForFdLeaks();
}
void *Worker(void *arg) {
@ -129,10 +155,10 @@ void *Worker(void *arg) {
}
TEST(popen, torture) {
int i, n = 8;
int i, n = 4;
pthread_t *t = _gc(malloc(sizeof(pthread_t) * n));
testlib_extract("/zip/echo.com", "echo.com", 0755);
for (i = 0; i < n; ++i) ASSERT_EQ(0, pthread_create(t + i, 0, Worker, 0));
for (i = 0; i < n; ++i) ASSERT_EQ(0, pthread_join(t[i], 0));
for (i = 3; i < 16; ++i) ASSERT_SYS(EBADF, -1, fcntl(i, F_GETFL));
CheckForFdLeaks();
}

View file

@ -170,12 +170,6 @@ TEST(sem_timedwait, threads) {
TEST(sem_timedwait, processes) {
int i, r, rc, n = 4, pshared = 1;
sem_t *sm = _mapshared(FRAMESIZE), *s[2] = {sm, sm + 1};
if (IsOpenbsd()) {
// TODO(jart): why?
ASSERT_SYS(EPERM, -1, sem_init(s[0], pshared, 0));
ASSERT_SYS(0, 0, munmap(sm, FRAMESIZE));
return;
}
ASSERT_SYS(0, 0, sem_init(s[0], pshared, 0));
ASSERT_SYS(0, 0, sem_init(s[1], pshared, 0));
for (i = 0; i < n; ++i) {