From 07e522cd7e223517389582a8eb647a4a6a8a5cf8 Mon Sep 17 00:00:00 2001
From: Yousong Zhou <yszhou4tech@gmail.com>
Date: Thu, 30 Apr 2015 13:53:11 +0800
Subject: [PATCH 203/210] xl2tpd-control: cleaup result file atexit().

---
 xl2tpd-control.c |   21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/xl2tpd-control.c b/xl2tpd-control.c
index 973ee87..abc0324 100644
--- a/xl2tpd-control.c
+++ b/xl2tpd-control.c
@@ -12,6 +12,7 @@
  
 #define _GNU_SOURCE
 
+#include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdarg.h>
@@ -35,6 +36,9 @@
 #define TUNNEL_REQUIRED 1
 #define TUNNEL_NOT_REQUIRED 0
 
+char result_filename[128];
+int result_fd = -1;
+
 int log_level = ERROR_LEVEL;
 
 void print_error (int level, const char *fmt, ...);
@@ -117,6 +121,14 @@ void help()
     );
 }
 
+void cleanup(void)
+{
+    /* cleaning up */
+    if (result_fd >= 0)
+	    close (result_fd);
+    unlink (result_filename);
+}
+
 int main (int argc, char *argv[])
 {
     char* control_filename = NULL;
@@ -195,11 +207,11 @@ int main (int argc, char *argv[])
     FILE* mesf = fmemopen (buf, CONTROL_PIPE_MESSAGE_SIZE, "w");
 
     /* create result pipe for reading */
-    char result_filename[128];
     snprintf (result_filename, 128, RESULT_FILENAME_FORMAT, getpid());
     unlink (result_filename);
     mkfifo (result_filename, 0600);
-    int result_fd = open (result_filename, O_RDONLY | O_NONBLOCK, 0600);
+    atexit(cleanup);
+    result_fd = open (result_filename, O_RDONLY | O_NONBLOCK, 0600);
     if (result_fd < 0)
     {
         print_error (ERROR_LEVEL,
@@ -293,11 +305,6 @@ int main (int argc, char *argv[])
     );
     printf ("%s", rbuf);
     
-    /* cleaning up */
-    
-    close (result_fd);
-    unlink (result_filename);
-    
     return command_result_code;
 }
 
-- 
1.7.10.4

