Fix order of tincd's initialization.
authorGuus Sliepen <guus@tinc-vpn.org>
Sun, 18 Aug 2013 20:35:27 +0000 (22:35 +0200)
committerGuus Sliepen <guus@tinc-vpn.org>
Sun, 18 Aug 2013 20:35:27 +0000 (22:35 +0200)
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.

src/control.c
src/net_setup.c
src/tincd.c

index 1f56213..f7d67ac 100644 (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
index 64d2515..39fd1e5 100644 (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;
 }
index 3d6db8b..84036ad 100644 (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();
 
-       close_network_connections();
-
 end:
-       exit_control();
+       close_network_connections();
 
-end_nonet:
        logger(DEBUG_ALWAYS, LOG_NOTICE, "Terminating");
 
        free(priority);