]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Another batch of memory management polish.
authorAlberto Leiva Popper <ydahhrk@gmail.com>
Fri, 3 May 2019 18:31:23 +0000 (13:31 -0500)
committerAlberto Leiva Popper <ydahhrk@gmail.com>
Fri, 3 May 2019 18:31:23 +0000 (13:31 -0500)
src/common.c
src/common.h
src/rtr/rtr.c
src/updates_daemon.c

index 29a8913a782e792c1f56f2b849b655aa95235545..e210c1a1ff0cd64ea6717439786cbfc78a6fd0c4 100644 (file)
@@ -1,7 +1,6 @@
 #include "common.h"
 
 #include <errno.h>
-#include <pthread.h>
 #include <stdlib.h>
 #include "log.h"
 
@@ -64,3 +63,24 @@ rwlock_unlock(pthread_rwlock_t *lock)
                exit(error);
        }
 }
+
+int
+close_thread(pthread_t thread, char const *what)
+{
+       int error;
+
+       error = pthread_cancel(thread);
+       if (error && error != ESRCH) {
+               pr_crit("pthread_cancel() threw %d on the '%s' thread.",
+                   error, what);
+               return error;
+       }
+
+       error = pthread_join(thread, NULL);
+       if (error)
+               pr_crit("pthread_join() threw %d on the '%s' thread.",
+                   error, what);
+
+       return error;
+}
+
index a5c5c59615f6ae01e9104a2f55884c3fc888c994..469a4c853b9a201323c180f815fe7f6e77e70c7c 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef SRC_RTR_COMMON_H_
 #define SRC_RTR_COMMON_H_
 
+#include <pthread.h>
 #include <semaphore.h>
 
 /* "I think that this is not supposed to be implemented." */
@@ -31,4 +32,7 @@ int rwlock_read_lock(pthread_rwlock_t *);
 void rwlock_write_lock(pthread_rwlock_t *);
 void rwlock_unlock(pthread_rwlock_t *);
 
+/** Also boilerplate. */
+int close_thread(pthread_t thread, char const *what);
+
 #endif /* SRC_RTR_COMMON_H_ */
index 0f4b44eb7d192cdb8463f5d00417ac15abf90feb..b2cf7fdcd4645defc9cd754b52849280d99ab141 100644 (file)
@@ -302,7 +302,7 @@ init_signal_handler(void)
        return error;
 }
 
-/* Wait for threads to end gracefully */
+/* Terminates client threads as gracefully as I know how to. */
 static void
 wait_threads(void)
 {
@@ -312,10 +312,12 @@ wait_threads(void)
        while (!SLIST_EMPTY(&threads)) {
                ptr = SLIST_FIRST(&threads);
                SLIST_REMOVE_HEAD(&threads, next);
-               /* TODO interrupt then join? Is this legal? */
-               pthread_kill(ptr->tid, SIGINT);
-               pthread_join(ptr->tid, NULL);
-               free(ptr);
+               /*
+                * If the close fails, the thread might still be using the
+                * thread_param variables, so leak instead.
+                */
+               if (close_thread(ptr->tid, "Client") == 0)
+                       free(ptr);
        }
 }
 
index 4572d90ef1503bc059e89d678c5f8838fc3e50b5..b7713d50d08078fbda81ba2c2797a87e795e0402 100644 (file)
@@ -1,12 +1,12 @@
 #include "updates_daemon.h"
 
 #include <errno.h>
-#include <pthread.h>
 #include <stdbool.h>
 #include <unistd.h>
 
+#include "common.h"
 #include "config.h"
-#include "log.h" /* TODO delete me probably */
+#include "log.h"
 #include "notify.h"
 #include "object/tal.h"
 #include "rtr/db/vrps.h"
@@ -57,6 +57,6 @@ updates_daemon_start(void)
 void
 updates_daemon_destroy(void)
 {
-       pthread_cancel(thread);
-       pthread_join(thread, NULL);
+       /* Not much to do with the error code. */
+       close_thread(thread, "Validation");
 }