From d4a484db1684cce15966bb722644416f90253ea7 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Thu, 30 Apr 2015 13:53:11 +0800 Subject: [PATCH 5/7] xl2tpd-control: cleaup result file atexit(). --- xl2tpd-control.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) --- a/xl2tpd-control.c +++ b/xl2tpd-control.c @@ -35,6 +35,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 +120,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 +206,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 +304,6 @@ int main (int argc, char *argv[]) ); printf ("%s", rbuf); - /* cleaning up */ - - close (result_fd); - unlink (result_filename); - return command_result_code; }