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 1f562134d4f7517d8e2c8a1dd71a3a1817758844..f7d67ac3f763366edce35840078a74eb1a9aeacb 100644 (file)
@@ -215,6 +215,7 @@ bool init_control(void) {
 
 void exit_control(void) {
 #ifndef HAVE_MINGW
 
 void exit_control(void) {
 #ifndef HAVE_MINGW
+       unlink(unixsocketname);
        io_del(&unix_socket);
        close(unix_socket.fd);
 #endif
        io_del(&unix_socket);
        close(unix_socket.fd);
 #endif
index 64d25150c7cb49fd532aca7f13c20a15fd248af0..39fd1e566dfc638fc9f010c93c2a2bf4f7beaca8 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);
 
        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")) {
        /* Open sockets */
 
        if(!do_detach && getenv("LISTEN_FDS")) {
@@ -957,9 +941,7 @@ static bool setup_myself(void) {
                } while(cfg);
        }
 
                } 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;
        }
                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(!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;
 }
 
        return true;
 }
 
@@ -1050,5 +1052,7 @@ void close_network_connections(void) {
 
        devops.close();
 
 
        devops.close();
 
+       exit_control();
+
        return;
 }
        return;
 }
index 3d6db8b9c8b4b112e32347926976d396df1bd610..84036ad4795a5c0f58673293f8dd9f221538a4ca 100644 (file)
@@ -400,14 +400,7 @@ int main2(int argc, char **argv) {
        /* Setup sockets and open device. */
 
        if(!setup_network())
        /* 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 */
 
 
        /* Change process priority */
 
@@ -439,6 +432,10 @@ int main2(int argc, char **argv) {
 
        /* Start main loop. It only exits when tinc is killed. */
 
 
        /* Start main loop. It only exits when tinc is killed. */
 
+       logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready");
+
+       try_outgoing_connections();
+
        status = main_loop();
 
        /* Shutdown properly. */
        status = main_loop();
 
        /* Shutdown properly. */
@@ -446,12 +443,9 @@ int main2(int argc, char **argv) {
        if(debug_level >= DEBUG_CONNECTIONS)
                devops.dump_stats();
 
        if(debug_level >= DEBUG_CONNECTIONS)
                devops.dump_stats();
 
-       close_network_connections();
-
 end:
 end:
-       exit_control();
+       close_network_connections();
 
 
-end_nonet:
        logger(DEBUG_ALWAYS, LOG_NOTICE, "Terminating");
 
        free(priority);
        logger(DEBUG_ALWAYS, LOG_NOTICE, "Terminating");
 
        free(priority);