]> granicus.if.org Git - libnl/commitdiff
Free associated caches when freeing cache manager
authorThomas Graf <tgr@deb.localdomain>
Tue, 5 Feb 2008 11:35:41 +0000 (12:35 +0100)
committerThomas Graf <tgr@deb.localdomain>
Tue, 5 Feb 2008 11:35:41 +0000 (12:35 +0100)
Caches allocated by the cache manager must be freed again when the cache
manager itself is freed. However, the netlink socket is allocated
indepdently so it should not be freed.

lib/cache_mngr.c
tests/test-cache-mngr.c

index a144b92d5487a0a381dec2253a60f404d78afc16..dffba90f339ffb90d7d4f422528d97d9423b4b8c 100644 (file)
@@ -365,20 +365,24 @@ int nl_cache_mngr_data_ready(struct nl_cache_mngr *mngr)
 }
 
 /**
- * Free cache manager
- * @arg mngr           Cache manager
+ * Free cache manager and all caches.
+ * @arg mngr           Cache manager.
  *
  * Release all resources after usage of a cache manager.
  */
 void nl_cache_mngr_free(struct nl_cache_mngr *mngr)
 {
+       int i;
+
        if (!mngr)
                return;
 
-       if (mngr->cm_handle) {
+       if (mngr->cm_handle)
                nl_close(mngr->cm_handle);
-               nl_handle_destroy(mngr->cm_handle);
-       }
+
+       for (i = 0; i < mngr->cm_nassocs; i++)
+               if (mngr->cm_assocs[i].ca_cache)
+                       nl_cache_free(mngr->cm_assocs[i].ca_cache);
 
        free(mngr->cm_assocs);
        free(mngr);
index 4d70e312a0ef5bbbfeb8069cc97065bc8a36163f..9a3da2d71fc8e22dea291f2064af1a70374ca46a 100644 (file)
@@ -1,4 +1,7 @@
 #include "../src/utils.h"
+#include <signal.h>
+
+static int quit = 0;
 
 static void change_cb(struct nl_cache *cache, struct nl_object *obj,
                      int action)
@@ -18,12 +21,19 @@ static void change_cb(struct nl_cache *cache, struct nl_object *obj,
        nl_object_dump(obj, &dp);
 }
 
+static void sigint(int arg)
+{
+       quit = 1;
+}
+
 int main(int argc, char *argv[])
 {
        struct nl_cache_mngr *mngr;
        struct nl_cache *lc, *nc, *ac, *rc;
        struct nl_handle *handle;
 
+       signal(SIGINT, sigint);
+
        nltool_init(argc, argv);
 
        handle = nltool_alloc_handle();
@@ -58,9 +68,9 @@ int main(int argc, char *argv[])
                return -1;
        }
 
-       for (;;) {
+       while (!quit) {
                int err = nl_cache_mngr_poll(mngr, 5000);
-               if (err < 0) {
+               if (err < 0 && err != -EINTR) {
                        nl_perror("nl_cache_mngr_poll()");
                        return -1;
                }
@@ -68,6 +78,7 @@ int main(int argc, char *argv[])
        }
 
        nl_cache_mngr_free(mngr);
+       nl_handle_destroy(handle);
 
        return 0;
 }