aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2018-08-28 15:28:49 +1200
committerDavid Phillips <david@sighup.nz>2018-08-28 15:28:49 +1200
commite196fa9f981da1eee2313f6817318397ea74f5d5 (patch)
tree0f78c90342b08d596043294afc64fe51764124f8
parent8edfe990e819dcc1a951e9bf73c338a015cb6a6a (diff)
downloadalarmd-e196fa9f981da1eee2313f6817318397ea74f5d5.tar.xz
Handle clients in separate threads, add mutexes
-rw-r--r--alarmd/Makefile6
-rw-r--r--alarmd/alarmd.c35
-rw-r--r--alarmd/alarmd_lock.c21
-rw-r--r--alarmd/alarmd_lock.h3
4 files changed, 48 insertions, 17 deletions
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 <uuid/uuid.h>
#include <sys/un.h>
#include <uthash.h>
+#include <pthread.h>
+#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 <pthread.h>
+
+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();