From: Guus Sliepen Date: Sun, 18 Aug 2013 20:35:27 +0000 (+0200) Subject: Fix order of tincd's initialization. X-Git-Tag: release-1.1pre9~36 X-Git-Url: https://tinc-vpn.org/git/browse?a=commitdiff_plain;h=09b0b49b98cc16f6b281e4e635c2c70234e38865;p=tinc Fix order of tincd's initialization. The order in which tinc initialized things was not completely correct. Now, it is done as follows: - Load and parse configuration files. - Create all TCP and UDP listening sockets. - Create PID file and UNIX socket. - Run the tinc-up script. - Drop privileges. - Start outgoing connections. - Run the main loop. The PID file can only be created correctly if the listening sockets have been set up ,as it includes the address and port of the first listening socket. The tinc-up script has to be run after the PID file and UNIX socket have been created so it can change their permissions if necessary. Outgoing connections should only be started right before the main loop, because this is not really part of the initialization. --- diff --git a/src/control.c b/src/control.c index 1f562134..f7d67ac3 100644 --- a/src/control.c +++ b/src/control.c @@ -215,6 +215,7 @@ bool init_control(void) { void exit_control(void) { #ifndef HAVE_MINGW + unlink(unixsocketname); io_del(&unix_socket); close(unix_socket.fd); #endif diff --git a/src/net_setup.c b/src/net_setup.c index 64d25150..39fd1e56 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -828,22 +828,6 @@ static bool setup_myself(void) { if(device_fd >= 0) io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ); - /* Run tinc-up script to further initialize the tap interface */ - char *envp[5] = {NULL}; - xasprintf(&envp[0], "NETNAME=%s", netname ? : ""); - xasprintf(&envp[1], "DEVICE=%s", device ? : ""); - xasprintf(&envp[2], "INTERFACE=%s", iface ? : ""); - xasprintf(&envp[3], "NAME=%s", myself->name); - - execute_script("tinc-up", envp); - - for(int i = 0; i < 4; i++) - free(envp[i]); - - /* Run subnet-up scripts for our own subnets */ - - subnet_update(myself, NULL, true); - /* Open sockets */ if(!do_detach && getenv("LISTEN_FDS")) { @@ -957,9 +941,7 @@ static bool setup_myself(void) { } while(cfg); } - if(listen_sockets) - logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready"); - else { + if(!listen_sockets) { logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!"); return false; } @@ -997,6 +979,26 @@ bool setup_network(void) { if(!setup_myself()) return false; + if(!init_control()) + return false; + + /* Run tinc-up script to further initialize the tap interface */ + + char *envp[5] = {NULL}; + xasprintf(&envp[0], "NETNAME=%s", netname ? : ""); + xasprintf(&envp[1], "DEVICE=%s", device ? : ""); + xasprintf(&envp[2], "INTERFACE=%s", iface ? : ""); + xasprintf(&envp[3], "NAME=%s", myself->name); + + execute_script("tinc-up", envp); + + for(int i = 0; i < 4; i++) + free(envp[i]); + + /* Run subnet-up scripts for our own subnets */ + + subnet_update(myself, NULL, true); + return true; } @@ -1050,5 +1052,7 @@ void close_network_connections(void) { devops.close(); + exit_control(); + return; } diff --git a/src/tincd.c b/src/tincd.c index 3d6db8b9..84036ad4 100644 --- a/src/tincd.c +++ b/src/tincd.c @@ -400,14 +400,7 @@ int main2(int argc, char **argv) { /* Setup sockets and open device. */ if(!setup_network()) - goto end_nonet; - - if(!init_control()) - goto end_nonet; - - /* Initiate all outgoing connections. */ - - try_outgoing_connections(); + goto end; /* Change process priority */ @@ -439,6 +432,10 @@ int main2(int argc, char **argv) { /* Start main loop. It only exits when tinc is killed. */ + logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready"); + + try_outgoing_connections(); + status = main_loop(); /* Shutdown properly. */ @@ -446,12 +443,9 @@ int main2(int argc, char **argv) { if(debug_level >= DEBUG_CONNECTIONS) devops.dump_stats(); - close_network_connections(); - end: - exit_control(); + close_network_connections(); -end_nonet: logger(DEBUG_ALWAYS, LOG_NOTICE, "Terminating"); free(priority);