[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v5 21/21] tools: Clean up vchan-socket-proxy socket



To avoid socket files lingering in /run/xen, have vchan-socket-proxy
clean up the sockets it creates.  Use a signal handler as well as atexit
to handle both means of termination.

Signed-off-by: Jason Andryuk <jandryuk@xxxxxxxxx>
---
 tools/libvchan/vchan-socket-proxy.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/tools/libvchan/vchan-socket-proxy.c 
b/tools/libvchan/vchan-socket-proxy.c
index 13700c5d67..0fb42964b5 100644
--- a/tools/libvchan/vchan-socket-proxy.c
+++ b/tools/libvchan/vchan-socket-proxy.c
@@ -33,6 +33,7 @@
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <signal.h>
 #include <string.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -88,6 +89,22 @@ char outbuf[BUFSIZE];
 int insiz = 0;
 int outsiz = 0;
 int verbose = 0;
+char *cleanup_socket;
+
+static void cleanup(void)
+{
+    if (cleanup_socket) {
+        unlink(cleanup_socket);
+        free(cleanup_socket);
+        cleanup_socket = NULL;
+    }
+}
+
+static void cleanup_exit(int signum)
+{
+    cleanup();
+    exit(0);
+}
 
 static void vchan_wr(struct libxenvchan *ctrl) {
     int ret;
@@ -394,6 +411,9 @@ int main(int argc, char **argv)
     vchan_path = argv[optind+1];
     socket_path = argv[optind+2];
 
+    signal(SIGHUP, cleanup_exit);
+    signal(SIGTERM, cleanup_exit);
+
     if (is_server) {
         ctrl = libxenvchan_server_init(NULL, domid, vchan_path, 0, 0);
         if (!ctrl) {
@@ -410,6 +430,8 @@ int main(int argc, char **argv)
                 perror("listen socket");
                 return 1;
             }
+            cleanup_socket = strdup(socket_path);
+            atexit(cleanup);
         }
     }
 
-- 
2.20.1




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.