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.
This commit is contained in:
Guus Sliepen 2013-08-18 22:35:27 +02:00
parent 8f84244458
commit 09b0b49b98
3 changed files with 30 additions and 31 deletions

View file

@ -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

View file

@ -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;
}

View file

@ -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();
end:
close_network_connections();
end:
exit_control();
end_nonet:
logger(DEBUG_ALWAYS, LOG_NOTICE, "Terminating");
free(priority);