Move free()s at the end om main() to the proper destructor functions.

This commit is contained in:
Guus Sliepen 2009-01-09 12:36:06 +01:00
parent 67df7fb7e1
commit a39a9506cd
9 changed files with 47 additions and 18 deletions

View file

@ -140,6 +140,9 @@ void close_device(void) {
cp(); cp();
close(device_fd); close(device_fd);
free(device);
free(iface);
} }
bool read_packet(vpn_packet_t *packet) { bool read_packet(vpn_packet_t *packet) {

View file

@ -227,6 +227,9 @@ void close_device(void)
CloseHandle(device_handle); CloseHandle(device_handle);
kill(reader_pid, SIGKILL); kill(reader_pid, SIGKILL);
free(device);
free(iface);
} }
bool read_packet(vpn_packet_t *packet) bool read_packet(vpn_packet_t *packet)

View file

@ -44,8 +44,8 @@ typedef enum device_type_t {
int device_fd = -1; int device_fd = -1;
static device_type_t device_type; static device_type_t device_type;
char *device; char *device = NULL;
char *iface; char *iface = NULL;
static char ifrname[IFNAMSIZ]; static char ifrname[IFNAMSIZ];
static char *device_info; static char *device_info;
@ -93,11 +93,13 @@ bool setup_device(void)
if(!ioctl(device_fd, TUNSETIFF, &ifr)) { if(!ioctl(device_fd, TUNSETIFF, &ifr)) {
strncpy(ifrname, ifr.ifr_name, IFNAMSIZ); strncpy(ifrname, ifr.ifr_name, IFNAMSIZ);
iface = ifrname; if(iface) free(iface);
iface = xstrdup(ifrname);
} else if(!ioctl(device_fd, (('T' << 8) | 202), &ifr)) { } else if(!ioctl(device_fd, (('T' << 8) | 202), &ifr)) {
logger(LOG_WARNING, _("Old ioctl() request was needed for %s"), device); logger(LOG_WARNING, _("Old ioctl() request was needed for %s"), device);
strncpy(ifrname, ifr.ifr_name, IFNAMSIZ); strncpy(ifrname, ifr.ifr_name, IFNAMSIZ);
iface = ifrname; if(iface) free(iface);
iface = xstrdup(ifrname);
} else } else
#endif #endif
{ {
@ -105,7 +107,9 @@ bool setup_device(void)
overwrite_mac = true; overwrite_mac = true;
device_info = _("Linux ethertap device"); device_info = _("Linux ethertap device");
device_type = DEVICE_TYPE_ETHERTAP; device_type = DEVICE_TYPE_ETHERTAP;
iface = rindex(device, '/') ? rindex(device, '/') + 1 : device; if(iface)
free(iface);
iface = xstrdup(rindex(device, '/') ? rindex(device, '/') + 1 : device);
} }
logger(LOG_INFO, _("%s is a %s"), device, device_info); logger(LOG_INFO, _("%s is a %s"), device, device_info);
@ -118,6 +122,9 @@ void close_device(void)
cp(); cp();
close(device_fd); close(device_fd);
free(device);
free(iface);
} }
bool read_packet(vpn_packet_t *packet) bool read_packet(vpn_packet_t *packet)

View file

@ -313,6 +313,9 @@ void close_device(void)
cp(); cp();
CloseHandle(device_handle); CloseHandle(device_handle);
free(device);
free(iface);
} }
bool read_packet(vpn_packet_t *packet) bool read_packet(vpn_packet_t *packet)

View file

@ -608,6 +608,8 @@ void close_network_connections(void)
execute_script("tinc-down", envp); execute_script("tinc-down", envp);
if(myport) free(myport);
for(i = 0; i < 4; i++) for(i = 0; i < 4; i++)
free(envp[i]); free(envp[i]);

View file

@ -32,8 +32,8 @@
#include "xalloc.h" #include "xalloc.h"
int device_fd = -1; int device_fd = -1;
char *device; char *device = NULL;
char *iface; char *iface = NULL;
static char ifrname[IFNAMSIZ]; static char ifrname[IFNAMSIZ];
static char *device_info; static char *device_info;
@ -90,6 +90,9 @@ void close_device(void)
cp(); cp();
close(device_fd); close(device_fd);
free(device);
free(iface);
} }
bool read_packet(vpn_packet_t *packet) bool read_packet(vpn_packet_t *packet)

View file

@ -114,6 +114,9 @@ void close_device(void)
cp(); cp();
close(device_fd); close(device_fd);
free(device);
free(iface);
} }
bool read_packet(vpn_packet_t *packet) bool read_packet(vpn_packet_t *packet)

View file

@ -392,6 +392,14 @@ static void make_names(void)
} }
} }
static void free_names() {
if (identname) free(identname);
if (netname) free(netname);
if (pidfilename) free(pidfilename);
if (logfilename) free(logfilename);
if (confbase) free(confbase);
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
program_name = argv[0]; program_name = argv[0];
@ -498,11 +506,11 @@ int main2(int argc, char **argv)
/* Shutdown properly. */ /* Shutdown properly. */
close_network_connections();
ifdebug(CONNECTIONS) ifdebug(CONNECTIONS)
dump_device_stats(); dump_device_stats();
close_network_connections();
end: end:
logger(LOG_NOTICE, _("Terminating")); logger(LOG_NOTICE, _("Terminating"));
@ -510,20 +518,14 @@ end:
remove_pid(pidfilename); remove_pid(pidfilename);
#endif #endif
if (identname) free(identname);
if (netname) free(netname);
if (pidfilename) free(pidfilename);
if (logfilename) free(logfilename);
if (myport) free(myport);
if (device) free(device);
if (iface) free(iface);
if (confbase) free(confbase);
EVP_cleanup(); EVP_cleanup();
ENGINE_cleanup(); ENGINE_cleanup();
CRYPTO_cleanup_all_ex_data(); CRYPTO_cleanup_all_ex_data();
ERR_remove_state(0); ERR_remove_state(0);
ERR_free_strings(); ERR_free_strings();
exit_configuration(&config_tree);
free_names();
return status; return status;
} }

View file

@ -171,6 +171,9 @@ void close_device(void)
close(write_fd); close(write_fd);
unlink(device); unlink(device);
free(device);
if(iface) free(iface);
} }
bool read_packet(vpn_packet_t *packet) bool read_packet(vpn_packet_t *packet)