Added detached thread handling code
Make it easier to create a detached thread, it's always the same and error-checking is quite repetitive.
This commit is contained in:
parent
8200c9668d
commit
2143adc91f
2
Makefile
2
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
|
||||
|
41
server.c
41
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);
|
||||
|
75
thread.c
Normal file
75
thread.c
Normal file
@ -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 <JohannesBauer@gmx.de>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#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;
|
||||
}
|
33
thread.h
Normal file
33
thread.h
Normal file
@ -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 <JohannesBauer@gmx.de>
|
||||
*/
|
||||
|
||||
#ifndef __THREAD_H__
|
||||
#define __THREAD_H__
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
/*************** 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
|
Loading…
Reference in New Issue
Block a user