]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Use semaphores at clients DB, create common read lock funcs
authorpcarana <pc.moreno2099@gmail.com>
Thu, 28 Mar 2019 23:31:25 +0000 (17:31 -0600)
committerpcarana <pc.moreno2099@gmail.com>
Thu, 28 Mar 2019 23:31:25 +0000 (17:31 -0600)
src/Makefile.am
src/clients.c
src/clients.h
src/common.c [new file with mode: 0644]
src/common.h

index c3f85c269e8f4bb776429fd9ee61fc6ffea495ca..ce5019eb53216edefc50327394aecf51e7ed196f 100644 (file)
@@ -7,7 +7,7 @@ rtr_server_SOURCES = main.c
 rtr_server_SOURCES += address.c address.h
 rtr_server_SOURCES += array_list.h
 rtr_server_SOURCES += clients.c clients.h
-rtr_server_SOURCES += common.h
+rtr_server_SOURCES += common.c common.h
 rtr_server_SOURCES += configuration.c configuration.h
 rtr_server_SOURCES += csv.c csv.h
 rtr_server_SOURCES += line_file.c line_file.h
index c0fc6fce84734b8950bcd5f94a8e25fae27b5cde..7c957dd63951da01ed224b383fce0083ea47207d 100644 (file)
@@ -1,6 +1,7 @@
 #include "clients.h"
 
 #include "array_list.h"
+#include "common.h"
 
 #define SADDR_IN(addr) ((struct sockaddr_in *)addr)
 #define SADDR_IN6(addr) ((struct sockaddr_in6 *)addr)
@@ -9,6 +10,12 @@ ARRAY_LIST(clientsdb, struct client)
 
 struct clientsdb clients_db;
 
+/* Read and Write locks */
+sem_t rlock, wlock;
+
+/* Readers counter */
+unsigned int rcounter;
+
 int
 clients_db_init(void)
 {
@@ -18,13 +25,11 @@ clients_db_init(void)
        if (error)
                warnx( "Clients DB couldn't be initialized");
 
-       return error;
-}
+       sem_init(&rlock, 0, 1);
+       sem_init(&wlock, 0, 1);
+       rcounter = 0;
 
-static int
-clients_db_add_client(struct client *client)
-{
-       return clientsdb_add(&clients_db, client);
+       return error;
 }
 
 static struct client *
@@ -32,22 +37,28 @@ get_client(struct sockaddr_storage *addr)
 {
        struct client *ptr;
 
+       read_lock(&rlock, &wlock, &rcounter);
        ARRAYLIST_FOREACH(&clients_db, ptr)
                if (ptr->sin_family == addr->ss_family) {
                        if (ptr->sin_family == AF_INET) {
                                if (ptr->sin_addr.s_addr ==
                                    SADDR_IN(addr)->sin_addr.s_addr &&
-                                   ptr->sin_port == SADDR_IN(addr)->sin_port)
+                                   ptr->sin_port ==
+                                   SADDR_IN(addr)->sin_port) {
+                                       read_unlock(&rlock, &wlock, &rcounter);
                                        return ptr;
+                               }
                        } else if (ptr->sin_family == AF_INET6)
                                if (IN6_ARE_ADDR_EQUAL(
                                    ptr->sin6_addr.s6_addr32,
                                    SADDR_IN6(addr)->sin6_addr.s6_addr32) &&
                                    ptr->sin_port ==
-                                   SADDR_IN6(addr)->sin6_port)
+                                   SADDR_IN6(addr)->sin6_port) {
+                                       read_unlock(&rlock, &wlock, &rcounter);
                                        return ptr;
+                               }
                }
-
+       read_unlock(&rlock, &wlock, &rcounter);
        return NULL;
 }
 
@@ -55,6 +66,7 @@ static int
 create_client(int fd, struct sockaddr_storage *addr, u_int8_t rtr_version)
 {
        struct client client;
+       int error;
 
        client.fd = fd;
        client.sin_family = addr->ss_family;
@@ -67,7 +79,11 @@ create_client(int fd, struct sockaddr_storage *addr, u_int8_t rtr_version)
        }
        client.rtr_version = rtr_version;
 
-       return clients_db_add_client(&client);
+       sem_wait(&wlock);
+       error = clientsdb_add(&clients_db, &client);
+       sem_post(&wlock);
+
+       return error;
 }
 
 /*
@@ -102,8 +118,14 @@ update_client(int fd, struct sockaddr_storage *addr, u_int8_t rtr_version)
 size_t
 client_list(struct client **clients)
 {
+       size_t len;
+
+       read_lock(&rlock, &wlock, &rcounter);
        *clients = clients_db.array;
-       return clients_db.len;
+       len = clients_db.len;
+       read_unlock(&rlock, &wlock, &rcounter);
+
+       return len;
 }
 
 static void
@@ -115,5 +137,10 @@ client_destroy(struct client *client)
 void
 clients_db_destroy(void)
 {
+       sem_wait(&wlock);
        clientsdb_cleanup(&clients_db, client_destroy);
+       sem_post(&wlock);
+
+       sem_destroy(&wlock);
+       sem_destroy(&rlock);
 }
index 6888f32c1f9bf7a94499286b10103a15eb8a9a1a..976343e6d1e7ddaf3c42d23a6a854491d0dc014f 100644 (file)
@@ -17,6 +17,7 @@ struct client {
 int clients_db_init(void);
 int update_client(int fd, struct sockaddr_storage *, u_int8_t);
 size_t client_list(struct client **);
+
 void clients_db_destroy(void);
 
 #endif /* SRC_CLIENTS_H_ */
diff --git a/src/common.c b/src/common.c
new file mode 100644 (file)
index 0000000..b41387a
--- /dev/null
@@ -0,0 +1,26 @@
+#include "common.h"
+
+void
+read_lock(sem_t *read, sem_t *write, unsigned int *reader_count)
+{
+       sem_wait(read);
+       (*reader_count)++;
+       if (*reader_count == 1)
+               sem_wait(write);
+       sem_post(read);
+}
+
+/*
+ * MUST NOT be called without previously called 'read_lock' or done the same
+ * things that such function does.
+ */
+void
+read_unlock(sem_t *read, sem_t *write, unsigned int *reader_count)
+{
+       sem_wait(read);
+       (*reader_count)--;
+       if (*reader_count == 0) {
+               sem_post(write);
+       }
+       sem_post(read);
+}
index e5fa6cf3b20c605fa070f2fefa5d7551200d8619..042b5bd8e3aafee43501ff265153191a929780f4 100644 (file)
@@ -1,18 +1,7 @@
 #ifndef _SRC_COMMON_H_
 #define _SRC_COMMON_H_
 
-/* __BEGIN_DECLS should be used at the beginning of your declarations,
-   so that C++ compilers don't mangle their names.  Use __END_DECLS at
-   the end of C declarations. */
-#undef __BEGIN_DECLS
-#undef __END_DECLS
-#ifdef __cplusplus
-# define __BEGIN_DECLS extern "C" {
-# define __END_DECLS }
-#else
-# define __BEGIN_DECLS /* empty */
-# define __END_DECLS /* empty */
-#endif
+#include <semaphore.h>
 
 #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
 
@@ -24,4 +13,7 @@
  * does not.
  */
 
+void read_lock(sem_t *, sem_t *, unsigned int *);
+void read_unlock(sem_t *, sem_t *, unsigned int *);
+
 #endif /* _SRC_COMMON_H_ */