From ed1d0878afe53032a4b63e87afd4a435015cf5de Mon Sep 17 00:00:00 2001 From: Etienne Dechamps Date: Sun, 22 Jun 2014 09:53:26 +0100 Subject: [PATCH] Make device close cleaner. --- src/bsd/device.c | 6 ++++-- src/cygwin/device.c | 7 ++++--- src/linux/device.c | 8 +++++--- src/mingw/device.c | 7 ++++--- src/multicast_device.c | 12 +++++++----- src/raw_socket_device.c | 7 ++++--- src/solaris/device.c | 8 ++++---- src/uml_device.c | 27 +++++++++++++++++---------- src/vde_device.c | 11 +++++++---- 9 files changed, 56 insertions(+), 37 deletions(-) diff --git a/src/bsd/device.c b/src/bsd/device.c index 145b79e..49508bc 100644 --- a/src/bsd/device.c +++ b/src/bsd/device.c @@ -197,9 +197,11 @@ static void close_device(void) { default: close(device_fd); } + device_fd = -1; - free(device); - free(iface); + free(device); device = NULL; + free(iface); iface = NULL; + device_info = NULL; } static bool read_packet(vpn_packet_t *packet) { diff --git a/src/cygwin/device.c b/src/cygwin/device.c index a110f7f..d522b4f 100644 --- a/src/cygwin/device.c +++ b/src/cygwin/device.c @@ -215,12 +215,13 @@ static bool setup_device(void) { static void close_device(void) { close(sp[0]); close(sp[1]); - CloseHandle(device_handle); + CloseHandle(device_handle); device_handle = INVALID_HANDLE_VALUE; kill(reader_pid, SIGKILL); - free(device); - free(iface); + free(device); device = NULL; + free(iface); iface = NULL; + device_info = NULL; } static bool read_packet(vpn_packet_t *packet) { diff --git a/src/linux/device.c b/src/linux/device.c index 6ed1622..cfd99ff 100644 --- a/src/linux/device.c +++ b/src/linux/device.c @@ -110,10 +110,12 @@ static bool setup_device(void) { static void close_device(void) { close(device_fd); + device_fd = -1; - free(type); - free(device); - free(iface); + free(type); type = NULL; + free(device); device = NULL; + free(iface); iface = NULL; + device_info = NULL; } static bool read_packet(vpn_packet_t *packet) { diff --git a/src/mingw/device.c b/src/mingw/device.c index 0642725..85c0c7b 100644 --- a/src/mingw/device.c +++ b/src/mingw/device.c @@ -213,10 +213,11 @@ static bool setup_device(void) { } static void close_device(void) { - CloseHandle(device_handle); + CloseHandle(device_handle); device_handle = INVALID_HANDLE_VALUE; - free(device); - free(iface); + free(device); device = NULL; + free(iface); iface = NULL; + device_info = NULL; } static bool read_packet(vpn_packet_t *packet) { diff --git a/src/multicast_device.c b/src/multicast_device.c index 48e1200..ba272eb 100644 --- a/src/multicast_device.c +++ b/src/multicast_device.c @@ -148,13 +148,15 @@ error: } static void close_device(void) { - close(device_fd); + close(device_fd); device_fd = -1; - free(device); - free(iface); + free(device); device = NULL; + free(iface); iface = NULL; - if(ai) - freeaddrinfo(ai); + if(ai) { + freeaddrinfo(ai); ai = NULL; + } + device_info = NULL; } static bool read_packet(vpn_packet_t *packet) { diff --git a/src/raw_socket_device.c b/src/raw_socket_device.c index 57df5b7..a8c2c86 100644 --- a/src/raw_socket_device.c +++ b/src/raw_socket_device.c @@ -83,10 +83,11 @@ static bool setup_device(void) { } static void close_device(void) { - close(device_fd); + close(device_fd); device_fd = -1; - free(device); - free(iface); + free(device); device = NULL; + free(iface); iface = NULL; + device_info = NULL; } static bool read_packet(vpn_packet_t *packet) { diff --git a/src/solaris/device.c b/src/solaris/device.c index c11f60f..a4c0d27 100644 --- a/src/solaris/device.c +++ b/src/solaris/device.c @@ -287,11 +287,11 @@ static void close_device(void) { } } - close(ip_fd); - close(device_fd); + close(ip_fd); ip_fd = -1; + close(device_fd); device_fd = -1; - free(device); - free(iface); + free(device); device = NULL; + free(iface); iface = NULL; } static bool read_packet(vpn_packet_t *packet) { diff --git a/src/uml_device.c b/src/uml_device.c index 5193fe9..f2b52cb 100644 --- a/src/uml_device.c +++ b/src/uml_device.c @@ -156,22 +156,29 @@ static bool setup_device(void) { } void close_device(void) { - if(listen_fd >= 0) - close(listen_fd); + if(listen_fd >= 0) { + close(listen_fd); listen_fd = -1; + } - if(request_fd >= 0) - close(request_fd); + if(request_fd >= 0) { + close(request_fd); request_fd = -1; + } - if(data_fd >= 0) - close(data_fd); + if(data_fd >= 0) { + close(data_fd); data_fd = -1; + } - if(write_fd >= 0) - close(write_fd); + if(write_fd >= 0) { + close(write_fd); write_fd = -1; + } unlink(device); - free(device); - if(iface) free(iface); + free(device); device = NULL; + if(iface) { + free(iface); iface = NULL; + } + device_info = NULL; } static bool read_packet(vpn_packet_t *packet) { diff --git a/src/vde_device.c b/src/vde_device.c index 1021f5d..55d66b4 100644 --- a/src/vde_device.c +++ b/src/vde_device.c @@ -82,15 +82,18 @@ static bool setup_device(void) { } static void close_device(void) { - if(conn) - plug.vde_close(conn); + if(conn) { + plug.vde_close(conn); conn = NULL; + } if(plug.dl_handle) libvdeplug_dynclose(plug); - free(device); + free(device); device = NULL; + + free(iface); iface = NULL; - free(iface); + device_info = NULL; } static bool read_packet(vpn_packet_t *packet) { -- 2.25.1