tools/libxc: free thread specific hypercall buffer on xc_interface_close
authorIan Campbell <ian.campbell@citrix.com>
Wed, 18 Aug 2010 16:09:25 +0000 (17:09 +0100)
committerIan Campbell <ian.campbell@citrix.com>
Wed, 18 Aug 2010 16:09:25 +0000 (17:09 +0100)
The per-thread hypercall buffer is usually cleaned up on pthread_exit
by the destructor passed to pthread_key_create. However if the calling
application is not threaded then the destructor is never called.

This frees the data for the current thread only but that is OK since
any other threads will be cleaned up by the destructor.

Changed since v1:
 * Ensure hcall_buf_pkey is initialised before use. Thanks to
   Christoph Egger for his help diagnosing this issue on NetBSD.
 * Remove redundant if (hcall_buf) from xc_clean_hcall_buf since
   _xc_clean_hcall_buf includes the same check.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
tools/libxc/xc_private.c

index befc3f965a4c0c7f0bb7d98c0b43f5dfa7f8f018..429e7b148d7325252dc5a768e4ea46183d7ec5de 100644 (file)
@@ -57,6 +57,8 @@ xc_interface *xc_interface_open(xentoollog_logger *logger,
     return 0;
 }
 
+static void xc_clean_hcall_buf(void);
+
 int xc_interface_close(xc_interface *xch)
 {
     int rc = 0;
@@ -68,6 +70,9 @@ int xc_interface_close(xc_interface *xch)
         rc = xc_interface_close_core(xch, xch->fd);
         if (rc) PERROR("Could not close hypervisor interface");
     }
+
+    xc_clean_hcall_buf();
+
     free(xch);
     return rc;
 }
@@ -180,6 +185,8 @@ void unlock_pages(void *addr, size_t len) { }
 int hcall_buf_prep(void **addr, size_t len) { return 0; }
 void hcall_buf_release(void **addr, size_t len) { }
 
+static void xc_clean_hcall_buf(void) { }
+
 #else /* !__sun__ */
 
 int lock_pages(void *addr, size_t len)
@@ -230,6 +237,13 @@ static void _xc_init_hcall_buf(void)
     pthread_key_create(&hcall_buf_pkey, _xc_clean_hcall_buf);
 }
 
+static void xc_clean_hcall_buf(void)
+{
+    pthread_once(&hcall_buf_pkey_once, _xc_init_hcall_buf);
+
+    _xc_clean_hcall_buf(pthread_getspecific(hcall_buf_pkey));
+}
+
 int hcall_buf_prep(void **addr, size_t len)
 {
     struct hcall_buf *hcall_buf;