Skip to contentSkip to conversation

xl: free libxl context, logger and lockfile using atexit handler

PATCH In review Ian Campbell 2 participants 3 messages Sep 6, 2012 – Sep 17, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xl: free libxl context, logger and lockfile using atexit handler

# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1346932936 -3600
# Node ID 844e10487b91fb4dcb9c6f8be42a9940d95a7119
# Parent 1b6912dc15f39d1455929f605bbbda1a51c06fe5
xl: free libxl context, logger and lockfile using atexit handler

xl frequently just calls exit(3), especially on error. Try to clean up some of our global state to make tools like valgrind more useful.

---

For 4.3 only.

tools/libxl/xl.c +18 -2
diff -r 1b6912dc15f3 -r 844e10487b91 tools/libxl/xl.c
@@ -191,6 +191,22 @@ void xl_ctx_alloc(void) {
     libxl_childproc_setmode(ctx, &childproc_hooks, 0);
 }
 
+static void xl_ctx_free(void)
+{
+    if (ctx) {
+        libxl_ctx_free(ctx);
+        ctx = NULL;
+    }
+    if (logger) {
+        xtl_logger_destroy((xentoollog_logger*)logger);
+        logger = NULL;
+    }
+    if (lockfile) {
+        free(lockfile);
+        lockfile = NULL;
+    }
+}
+
 int main(int argc, char **argv)
 {
     int opt = 0;
@@ -229,6 +245,8 @@ int main(int argc, char **argv)
     logger = xtl_createlogger_stdiostream(stderr, minmsglevel,  0);
     if (!logger) exit(1);
 
+    atexit(xl_ctx_free);
+
     xl_ctx_alloc();
 
     ret = libxl_read_file_contents(ctx, XL_GLOBAL_CONFIG,
@@ -274,8 +292,6 @@ int main(int argc, char **argv)
     }
 
  xit:
-    libxl_ctx_free(ctx);
-    xtl_logger_destroy((xentoollog_logger*)logger);
     return ret;
 }
Acked-by on v1 1/1
#1 lore raw

Ian Campbell writes ("[Xen-devel] [PATCH] xl: free libxl context, logger and lockfile using atexit handler"):

Quoted 1 line

xl: free libxl context, logger and lockfile using atexit handler

author Applied
#2 lore raw
Quoted 4 lines · Ian Jackson

Ian Campbell writes ("[Xen-devel] [PATCH] xl: free libxl context, logger and lockfile using atexit handler"):

Quoted 1 line

xl: free libxl context, logger and lockfile using atexit handler

Applied, thanks.