From adced03b24272f31f5d7dc8764c8ca7fc7821c7e Mon Sep 17 00:00:00 2001 From: Test_User Date: Tue, 18 Jun 2024 15:32:54 -0400 Subject: Some fixes --- Makefile | 38 +++++++++++++++++++------------------- mutex.h | 2 +- networks/plaintext_buffered.c | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 34dd19a..c2bce49 100644 --- a/Makefile +++ b/Makefile @@ -61,7 +61,7 @@ ifneq ($(PLAINTEXT_CLIENT),$(LAST_PLAINTEXT_CLIENT)) rebuild = 1 endif else -PLAINTEXT_CLIENT = $(LAST_PLAINTEXT_CLIENT) +PLAINTEXT_CLIENT := $(LAST_PLAINTEXT_CLIENT) endif ifneq ($(PLAINTEXT_SERVER),) @@ -69,7 +69,7 @@ ifneq ($(PLAINTEXT_SERVER),$(LAST_PLAINTEXT_SERVER)) rebuild = 1 endif else -PLAINTEXT_SERVER = $(LAST_PLAINTEXT_SERVER) +PLAINTEXT_SERVER := $(LAST_PLAINTEXT_SERVER) endif ifneq ($(GNUTLS_CLIENT),) @@ -77,7 +77,7 @@ ifneq ($(GNUTLS_CLIENT),$(LAST_GNUTLS_CLIENT)) rebuild = 1 endif else -GNUTLS_CLIENT = $(LAST_GNUTLS_CLIENT) +GNUTLS_CLIENT := $(LAST_GNUTLS_CLIENT) endif ifneq ($(GNUTLS_SERVER),) @@ -85,7 +85,7 @@ ifneq ($(GNUTLS_SERVER),$(LAST_GNUTLS_SERVER)) rebuild = 1 endif else -GNUTLS_SERVER = $(LAST_GNUTLS_SERVER) +GNUTLS_SERVER := $(LAST_GNUTLS_SERVER) endif ifneq ($(OPENSSL_CLIENT),) @@ -93,7 +93,7 @@ ifneq ($(OPENSSL_CLIENT),$(LAST_OPENSSL_CLIENT)) rebuild = 1 endif else -OPENSSL_CLIENT = $(LAST_OPENSSL_CLIENT) +OPENSSL_CLIENT := $(LAST_OPENSSL_CLIENT) endif ifneq ($(OPENSSL_SERVER),) @@ -101,15 +101,15 @@ ifneq ($(OPENSSL_SERVER),$(LAST_OPENSSL_SERVER)) rebuild = 1 endif else -OPENSSL_SERVER = $(LAST_OPENSSL_SERVER) +OPENSSL_SERVER := $(LAST_OPENSSL_SERVER) endif -ifneq ($(PLAINTEXT_CLIENT),) -ifneq ($(PLAINTEXT_CLIENT),$(LAST_PLAINTEXT_CLIENT)) +ifneq ($(PLAINTEXT_BUFFERED_CLIENT),) +ifneq ($(PLAINTEXT_BUFFERED_CLIENT),$(LAST_PLAINTEXT_BUFFERED_CLIENT)) rebuild = 1 endif else -PLAINTEXT_CLIENT = $(LAST_PLAINTEXT_CLIENT) +PLAINTEXT_BUFFERED_CLIENT := $(LAST_PLAINTEXT_BUFFERED_CLIENT) endif ifneq ($(PLAINTEXT_BUFFERED_SERVER),) @@ -117,7 +117,7 @@ ifneq ($(PLAINTEXT_BUFFERED_SERVER),$(LAST_PLAINTEXT_BUFFERED_SERVER)) rebuild = 1 endif else -PLAINTEXT_BUFFERED_SERVER = $(LAST_PLAINTEXT_BUFFERED_SERVER) +PLAINTEXT_BUFFERED_SERVER := $(LAST_PLAINTEXT_BUFFERED_SERVER) endif ifneq ($(INSPIRCD2_PROTOCOL),) @@ -125,7 +125,7 @@ ifneq ($(INSPIRCD2_PROTOCOL),$(LAST_INSPIRCD2_PROTOCOL)) rebuild = 1 endif else -INSPIRCD2_PROTOCOL = $(LAST_INSPIRCD2_PROTOCOL) +INSPIRCD2_PROTOCOL := $(LAST_INSPIRCD2_PROTOCOL) endif ifneq ($(INSPIRCD3_PROTOCOL),) @@ -133,7 +133,7 @@ ifneq ($(INSPIRCD3_PROTOCOL),$(LAST_INSPIRCD3_PROTOCOL)) rebuild = 1 endif else -INSPIRCD3_PROTOCOL = $(LAST_INSPIRCD3_PROTOCOL) +INSPIRCD3_PROTOCOL := $(LAST_INSPIRCD3_PROTOCOL) endif ifneq ($(HAXSERV_PSEUDOCLIENT),) @@ -141,7 +141,7 @@ ifneq ($(HAXSERV_PSEUDOCLIENT),$(LAST_HAXSERV_PSEUDOCLIENT)) rebuild = 1 endif else -HAXSERV_PSEUDOCLIENT = $(LAST_HAXSERV_PSEUDOCLIENT) +HAXSERV_PSEUDOCLIENT := $(LAST_HAXSERV_PSEUDOCLIENT) endif ifneq ($(SAFE_STACK),) @@ -149,7 +149,7 @@ ifneq ($(SAFE_STACK),$(LAST_SAFE_STACK)) rebuild = 1 endif else -SAFE_STACK = $(LAST_SAFE_STACK) +SAFE_STACK := $(LAST_SAFE_STACK) endif ifneq ($(ORIGINAL_CFLAGS),) @@ -157,8 +157,8 @@ ifneq ($(ORIGINAL_CFLAGS),$(LAST_CFLAGS)) rebuild = 1 endif else -ORIGINAL_CFLAGS = $(LAST_CFLAGS) -CFLAGS = $(LAST_CFLAGS) +ORIGINAL_CFLAGS := $(LAST_CFLAGS) +CFLAGS := $(LAST_CFLAGS) endif ifneq ($(CC),) @@ -166,7 +166,7 @@ ifneq ($(CC),$(LAST_CC)) rebuild = 1 endif else -CC = $(LAST_CC) +CC := $(LAST_CC) endif ifneq ($(FUTEX),) @@ -174,7 +174,7 @@ ifneq ($(FUTEX),$(LAST_FUTEX)) rebuild = 1 endif else -FUXEX = $(LAST_FUTEX) +FUTEX := $(LAST_FUTEX) endif ifeq ($(rebuild),1) @@ -334,7 +334,7 @@ DEPS = $(shell $(CC) $(CFLAGS) -M -MT $(1).$(2) $(1).c | sed 's_\\$$__') .makeop .PHONY: all clean all: HaxIRCd $(SOFILES) -HaxIRCd: main.c +HaxIRCd: main.c .makeopts Makefile $(CC) main.c -o HaxIRCd HaxIRCd.so: $(OFILES) .makeopts Makefile diff --git a/mutex.h b/mutex.h index cab4018..56f8a42 100644 --- a/mutex.h +++ b/mutex.h @@ -43,7 +43,7 @@ inline int mutex_init(uint32_t *futex) { inline void mutex_lock(uint32_t *futex) { uint32_t val; - while (val = __sync_lock_test_and_set(futex, 1)) + while ((val = __sync_lock_test_and_set(futex, 1))) syscall(SYS_futex, futex, FUTEX_WAIT, val, 0, 0, 0); } diff --git a/networks/plaintext_buffered.c b/networks/plaintext_buffered.c index 97a6672..2fc1e3a 100644 --- a/networks/plaintext_buffered.c +++ b/networks/plaintext_buffered.c @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -44,6 +45,9 @@ struct plaintext_buffered_handle { MUTEX_TYPE mutex; +#ifdef USE_FUTEX + MUTEX_TYPE release_read; +#endif int fd; char valid; char close; @@ -60,7 +64,22 @@ void * plaintext_buffered_send_thread(void *handle) { while (1) { size_t len; len = info->buffer_len; + + if (!info->valid) + goto plaintext_buffered_send_thread_error_unlock; + +#ifdef USE_FUTEX + info->release_read = (len == 0); +#endif + mutex_unlock(&(info->mutex)); + +#ifdef USE_FUTEX + mutex_lock(&(info->release_read)); + if (len == 0) + continue; +#endif + if (read_buffer_index + len > PLAINTEXT_BUFFERED_LEN) len = PLAINTEXT_BUFFERED_LEN - read_buffer_index; @@ -80,6 +99,8 @@ void * plaintext_buffered_send_thread(void *handle) { info->buffer_len -= (size_t)res; } + plaintext_buffered_send_thread_error_unlock: + mutex_unlock(&(info->mutex)); plaintext_buffered_send_thread_error: // TODO: Sane solution that works with ptread mutexes while (1) { @@ -118,6 +139,9 @@ int plaintext_buffered_send(void *handle, struct string msg) { if (plaintext_handle->write_buffer_index >= PLAINTEXT_BUFFERED_LEN) plaintext_handle->write_buffer_index = 0; plaintext_handle->buffer_len += len; +#ifdef USE_FUTEX + mutex_unlock(&(plaintext_handle->release_read)); +#endif mutex_unlock(&(plaintext_handle->mutex)); msg.len -= len; } @@ -196,6 +220,10 @@ int plaintext_buffered_connect(void **handle, struct string address, struct stri if (mutex_init(&(plaintext_handle->mutex)) != 0) goto plaintext_buffered_connect_free_buffer; +#ifdef USE_FUTEX + plaintext_handle->release_read = 0; +#endif + pthread_t trash; if (pthread_create(&trash, &pthread_attr, plaintext_buffered_send_thread, plaintext_handle) != 0) goto plaintext_buffered_connect_destroy_mutex; @@ -252,6 +280,10 @@ int plaintext_buffered_accept(int listen_fd, void **handle, struct string *addr) if (mutex_init(&(plaintext_handle->mutex)) != 0) goto plaintext_buffered_accept_free_buffer; +#ifdef USE_FUTEX + plaintext_handle->release_read = 0; +#endif + pthread_t trash; if (pthread_create(&trash, &pthread_attr, plaintext_buffered_send_thread, plaintext_handle) != 0) goto plaintext_buffered_accept_destroy_mutex; @@ -276,6 +308,9 @@ void plaintext_buffered_shutdown(void *handle) { struct plaintext_buffered_handle *plaintext_handle = handle; mutex_lock(&(plaintext_handle->mutex)); plaintext_handle->valid = 0; +#ifdef USE_FUTEX + mutex_unlock(&(plaintext_handle->release_read)); +#endif mutex_unlock(&(plaintext_handle->mutex)); shutdown(plaintext_handle->fd, SHUT_RDWR); } @@ -283,6 +318,10 @@ void plaintext_buffered_shutdown(void *handle) { void plaintext_buffered_close(int fd, void *handle) { struct plaintext_buffered_handle *plaintext_handle = handle; mutex_lock(&(plaintext_handle->mutex)); + plaintext_handle->valid = 0; +#ifdef USE_FUTEX + mutex_unlock(&(plaintext_handle->release_read)); +#endif plaintext_handle->close = 1; mutex_unlock(&(plaintext_handle->mutex)); } -- cgit v1.2.3