From 2143adc91fb955c41657a215f916e631b8ba3de5 Mon Sep 17 00:00:00 2001 From: Johannes Bauer Date: Wed, 23 Oct 2019 19:47:26 +0200 Subject: [PATCH] Added detached thread handling code Make it easier to create a detached thread, it's always the same and error-checking is quite repetitive. --- Makefile | 2 +- server.c | 41 ++++++------------------------- thread.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ thread.h | 33 +++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 34 deletions(-) create mode 100644 thread.c create mode 100644 thread.h diff --git a/Makefile b/Makefile index d7a2cbd..bd5a664 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ PYPGMOPTS := ../Python/pypgmopts/pypgmopts LDFLAGS := `pkg-config --libs openssl` -OBJS := luksrku.o editor.o util.o log.o keydb.o file_encryption.o uuid.o argparse_edit.o pgmopts.o openssl.o server.o argparse_server.o +OBJS := luksrku.o editor.o util.o log.o keydb.o file_encryption.o uuid.o argparse_edit.o pgmopts.o openssl.o server.o argparse_server.o thread.o parsers: $(PYPGMOPTS) -n edit parsers/parser_edit.py diff --git a/server.c b/server.c index 6d713a2..22a41f6 100644 --- a/server.c +++ b/server.c @@ -43,6 +43,7 @@ #include "luks.h" #include "pgmopts.h" #include "uuid.h" +#include "thread.h" static int create_tcp_server_socket(int port) { int s; @@ -376,7 +377,7 @@ static int psk_server_callback(SSL *ssl, const unsigned char *identity, size_t i return 1; } -static void *client_handler_thread(void *vctx) { +static void client_handler_thread(void *vctx) { struct client_ctx_t *client = (struct client_ctx_t*)vctx; SSL *ssl = SSL_new(client->gctx->ctx); @@ -410,8 +411,6 @@ static void *client_handler_thread(void *vctx) { SSL_free(ssl); shutdown(client->fd, SHUT_RDWR); close(client->fd); - free(client); - return NULL; } bool keyserver_start(const struct pgmopts_server_t *opts) { @@ -455,41 +454,17 @@ bool keyserver_start(const struct pgmopts_server_t *opts) { } /* Client has connected, fire up client thread. */ - struct client_ctx_t *client_ctx = calloc(1, sizeof(struct client_ctx_t)); - if (!client_ctx) { - log_libc(LLVL_FATAL, "Unable to malloc(3) client ctx"); - close(tcp_sock); - free_generic_tls_context(&gctx); - return false; - } - client_ctx->gctx = &gctx; - client_ctx->keydb = keydb; - client_ctx->fd = client; - - pthread_t thread; - pthread_attr_t attrs; - if (pthread_attr_init(&attrs)) { + struct client_ctx_t client_ctx = { + .gctx = &gctx, + .keydb = keydb, + .fd = client, + }; + if (!pthread_create_detached_thread(client_handler_thread, &client_ctx, sizeof(client_ctx))) { log_libc(LLVL_FATAL, "Unable to pthread_attr_init(3)"); close(tcp_sock); free_generic_tls_context(&gctx); return false; } - if (pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED)) { - log_libc(LLVL_FATAL, "Unable to pthread_attr_setdetachstate(3)"); - close(tcp_sock); - free_generic_tls_context(&gctx); - return false; - } - if (pthread_create(&thread, &attrs, client_handler_thread, client_ctx)) { - log_libc(LLVL_FATAL, "Unable to pthread_create(3) a client thread"); - close(tcp_sock); - free_generic_tls_context(&gctx); - return false; - - } - - - } free_generic_tls_context(&gctx); diff --git a/thread.c b/thread.c new file mode 100644 index 0000000..a7ccd4f --- /dev/null +++ b/thread.c @@ -0,0 +1,75 @@ +/* + luksrku - Tool to remotely unlock LUKS disks using TLS. + Copyright (C) 2016-2016 Johannes Bauer + + This file is part of luksrku. + + luksrku is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; this program is ONLY licensed under + version 3 of the License, later versions are explicitly excluded. + + luksrku is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with luksrku; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Johannes Bauer +*/ + +#include +#include +#include +#include +#include +#include + +#include "thread.h" +#include "log.h" + +struct pthread_trampoline_data_t { + void (*thread_function)(void *ctx); + uint8_t ctx[]; +}; + +static void* pthread_trampoline(void *vctx) { + struct pthread_trampoline_data_t *tdata = (struct pthread_trampoline_data_t*)vctx; + tdata->thread_function(tdata->ctx); + free(tdata); + return NULL; +} + +bool pthread_create_detached_thread(void (*thread_function)(void *ctx), const void *ctx, unsigned int ctx_length) { + struct pthread_trampoline_data_t *tdata = calloc(1, sizeof(struct pthread_trampoline_data_t) + ctx_length); + if (!tdata) { + log_libc(LLVL_FATAL, "Failed to allocate trampoline data using calloc(3)"); + return false; + } + tdata->thread_function = thread_function; + memcpy(tdata->ctx, ctx, ctx_length); + + pthread_attr_t attrs; + if (pthread_attr_init(&attrs)) { + log_libc(LLVL_FATAL, "Unable to pthread_attr_init(3)"); + free(tdata); + return false; + } + + if (pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED)) { + log_libc(LLVL_FATAL, "Unable to pthread_attr_setdetachstate(3)"); + free(tdata); + return false; + } + + pthread_t thread; + if (pthread_create(&thread, &attrs, pthread_trampoline, tdata)) { + log_libc(LLVL_FATAL, "Unable to pthread_create(3) a client thread"); + free(tdata); + return false; + } + return true; +} diff --git a/thread.h b/thread.h new file mode 100644 index 0000000..d962996 --- /dev/null +++ b/thread.h @@ -0,0 +1,33 @@ +/* + luksrku - Tool to remotely unlock LUKS disks using TLS. + Copyright (C) 2016-2016 Johannes Bauer + + This file is part of luksrku. + + luksrku is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; this program is ONLY licensed under + version 3 of the License, later versions are explicitly excluded. + + luksrku is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with luksrku; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Johannes Bauer +*/ + +#ifndef __THREAD_H__ +#define __THREAD_H__ + +#include + +/*************** AUTO GENERATED SECTION FOLLOWS ***************/ +bool pthread_create_detached_thread(void (*thread_function)(void *ctx), const void *ctx, unsigned int ctx_length); +/*************** AUTO GENERATED SECTION ENDS ***************/ + +#endif