From e196fa9f981da1eee2313f6817318397ea74f5d5 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Tue, 28 Aug 2018 15:28:49 +1200 Subject: Handle clients in separate threads, add mutexes --- alarmd/Makefile | 6 ++++-- alarmd/alarmd.c | 35 ++++++++++++++++++++--------------- alarmd/alarmd_lock.c | 21 +++++++++++++++++++++ alarmd/alarmd_lock.h | 3 +++ 4 files changed, 48 insertions(+), 17 deletions(-) create mode 100644 alarmd/alarmd_lock.c create mode 100644 alarmd/alarmd_lock.h diff --git a/alarmd/Makefile b/alarmd/Makefile index 9c69056..4e016ea 100644 --- a/alarmd/Makefile +++ b/alarmd/Makefile @@ -1,7 +1,9 @@ -LDFLAGS += -L../lib -lalarm -luuid +LDFLAGS += -L../lib -lalarm -luuid -lpthread CFLAGS += -I../inc all: alarmd +alarmd: alarmd.o alarmd_lock.o + clean: - rm alarmd + rm alarmd alarmd.o alarmd_lock.o diff --git a/alarmd/alarmd.c b/alarmd/alarmd.c index 9f2c254..900aa52 100644 --- a/alarmd/alarmd.c +++ b/alarmd/alarmd.c @@ -13,7 +13,9 @@ #include #include #include +#include +#include "alarmd_lock.h" #include "alarmd_proto.h" struct alarm { @@ -72,9 +74,9 @@ int handle_register(int c_sock, pid_t owner) new_alarm->is_raised = 0; new_alarm->owner = owner; - /* LOCK */ + alarmd_lock(); HASH_ADD(hh, alarms, uuid, sizeof(uuid), new_alarm); - /* UNLOCK */ + alarmd_unlock(); return 0; } @@ -82,22 +84,22 @@ int handle_deregister(int c_sock) { uuid_t uuid; struct alarm *target = NULL; - char uuid_str[37]; +// char uuid_str[37]; if (recv(c_sock, &uuid, sizeof(uuid_t), 0) < sizeof(uuid_t)) { return 1; } - uuid_unparse(uuid, uuid_str); - fprintf(stderr, "Deregistering alarm %s\n", uuid_str); +// uuid_unparse(uuid, uuid_str); +// fprintf(stderr, "Deregistering alarm %s\n", uuid_str); - /* LOCK */ + alarmd_lock(); HASH_FIND(hh, alarms, uuid, sizeof(uuid_t), target); if (target != NULL) { HASH_DEL(alarms, target); free(target); } - /* UNLOCK */ + alarmd_unlock(); return 0; } @@ -113,12 +115,12 @@ int handle_set_raised_generic(int c_sock, int shall_raise) return 1; } - /* LOCK */ + alarmd_lock(); HASH_FIND(hh, alarms, uuid, sizeof(uuid_t), target); if (target != NULL) { target->is_raised = shall_raise; } - /* UNLOCK */ + alarmd_unlock(); return 0; } @@ -139,7 +141,7 @@ int handle_query(int c_sock) struct alarm *a = NULL; struct alarm *tmp = NULL; - /* LOCK */ + alarmd_lock(); count = HASH_COUNT(alarms); if (send(c_sock, &count, sizeof(count), 0) < sizeof(count)) { @@ -162,12 +164,13 @@ int handle_query(int c_sock) perror("send"); } } - /* UNLOCK */ + alarmd_unlock(); return 0; } -void handle_client(int c_sock) +void *handle_client(void *c_sock_) { + int c_sock = *((int*)c_sock_); uint32_t packet_type = 0; ssize_t nread = 0; unsigned int cred_len = 0; @@ -185,12 +188,12 @@ void handle_client(int c_sock) nread = recv(c_sock, &packet_type, sizeof(packet_type), 0); if (nread < 0) { perror("recv"); - return; + return NULL; } else if (nread == 0) { break; } else if (nread < sizeof(packet_type)) { fprintf(stderr, "Packet type too short\n"); - return; + return NULL; } switch(packet_type) { @@ -225,6 +228,7 @@ void handle_client(int c_sock) } } close(c_sock); + return NULL; } int main(int argc, char **argv) @@ -263,7 +267,8 @@ int main(int argc, char **argv) perror("accept"); return 1; } - handle_client(c_sock); + pthread_t thread; + pthread_create(&thread, NULL, handle_client, &c_sock); } fprintf(stderr, "Shutting down\n"); close(sock); diff --git a/alarmd/alarmd_lock.c b/alarmd/alarmd_lock.c new file mode 100644 index 0000000..9770183 --- /dev/null +++ b/alarmd/alarmd_lock.c @@ -0,0 +1,21 @@ +#include + +static pthread_mutex_t alarmd_global_lock = PTHREAD_MUTEX_INITIALIZER; + + +int alarmd_lock_destroy() +{ + return pthread_mutex_destroy(&alarmd_global_lock); +} + + +int alarmd_lock() +{ + return pthread_mutex_lock(&alarmd_global_lock); +} + + +int alarmd_unlock() +{ + return pthread_mutex_unlock(&alarmd_global_lock); +} diff --git a/alarmd/alarmd_lock.h b/alarmd/alarmd_lock.h new file mode 100644 index 0000000..ea60726 --- /dev/null +++ b/alarmd/alarmd_lock.h @@ -0,0 +1,3 @@ +int alarmd_lock_destroy(); +int alarmd_lock(); +int alarmd_unlock(); -- cgit v1.1