Merge branch 'master' of git://tinc-vpn.org/tinc into 1.1
authorGuus Sliepen <guus@tinc-vpn.org>
Mon, 26 Mar 2012 18:06:39 +0000 (19:06 +0100)
committerGuus Sliepen <guus@tinc-vpn.org>
Mon, 26 Mar 2012 18:06:39 +0000 (19:06 +0100)
Conflicts:
src/logger.c
src/net_setup.c

1  2 
configure.in
src/logger.c
src/net_setup.c
src/tincd.c

diff --cc configure.in
Simple merge
diff --cc src/logger.c
index 028cbaef804ba053e285dc4d37b7716ac2d87f51,6765cc5abeba7994b1283734714577eeb44f3bf1..cc47a4a1cbfdedeb05e30b29e9a6dcc5d1cd2ee5
@@@ -79,7 -75,7 +79,7 @@@ void reopenlogger() 
        fflush(logfile);
        FILE *newfile = fopen(logfilename, "a");
        if(!newfile) {
-               logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reopen log file %s: %s\n", logfilename, strerror(errno));
 -              logger(LOG_ERR, "Unable to reopen log file %s: %s", logfilename, strerror(errno));
++              logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reopen log file %s: %s", logfilename, strerror(errno));
                return;
        }
        fclose(logfile);
diff --cc src/net_setup.c
index c181df9472e78c07f0e1ea6efae2d27259d9d574,d3940e72b36aa698404a9e70ac1c47167d03a17a..0d21cc059cfd1669a4d5bc1e656a97bf3d352800
@@@ -639,96 -581,117 +639,149 @@@ static bool setup_myself(void) 
  
        /* Open sockets */
  
-       listen_sockets = 0;
-       cfg = lookup_config(config_tree, "BindToAddress");
-       do {
-               get_config_string(cfg, &address);
-               if(cfg)
-                       cfg = lookup_config_next(config_tree, cfg);
-               char *port = myport;
-               if(address) {
-                       char *space = strchr(address, ' ');
-                       if(space) {
-                               *space++ = 0;
-                               port = space;
-                       }
-                       if(!strcmp(address, "*"))
-                               *address = 0;
-               }
-               hint.ai_family = addressfamily;
-               hint.ai_socktype = SOCK_STREAM;
-               hint.ai_protocol = IPPROTO_TCP;
-               hint.ai_flags = AI_PASSIVE;
+       if(!do_detach && getenv("LISTEN_FDS")) {
+               sockaddr_t sa;
+               socklen_t salen;
  
-               err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
-               free(address);
+               listen_sockets = atoi(getenv("LISTEN_FDS"));
+ #ifdef HAVE_UNSETENV
+               unsetenv("LISTEN_FDS");
+ #endif
  
-               if(err || !ai) {
-                       logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
-                                  gai_strerror(err));
+               if(listen_sockets > MAXSOCKETS) {
 -                      logger(LOG_ERR, "Too many listening sockets");
++                      logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
                        return false;
                }
  
-               for(aip = ai; aip; aip = aip->ai_next) {
-                       if(listen_sockets >= MAXSOCKETS) {
-                               logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
+               for(i = 0; i < listen_sockets; i++) {
+                       salen = sizeof sa;
+                       if(getsockname(i + 3, &sa.sa, &salen) < 0) {
 -                              logger(LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno));
++                              logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno));
                                return false;
                        }
  
-                       listen_socket[listen_sockets].tcp =
-                               setup_listen_socket((sockaddr_t *) aip->ai_addr);
-                       if(listen_socket[listen_sockets].tcp < 0)
-                               continue;
+                       listen_socket[i].tcp = i + 3;
  
-                       listen_socket[listen_sockets].udp =
-                               setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
+ #ifdef FD_CLOEXEC
+                       fcntl(i + 3, F_SETFD, FD_CLOEXEC);
+ #endif
  
-                       if(listen_socket[listen_sockets].udp < 0) {
-                               close(listen_socket[listen_sockets].tcp);
-                               continue;
-                       }
+                       listen_socket[i].udp = setup_vpn_in_socket(&sa);
+                       if(listen_socket[i].udp < 0)
+                               return false;
  
-                       event_set(&listen_socket[listen_sockets].ev_tcp,
-                                         listen_socket[listen_sockets].tcp,
-                                         EV_READ|EV_PERSIST,
-                                         handle_new_meta_connection, NULL);
-                       if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
 -                      ifdebug(CONNECTIONS) {
++                      event_set(&listen_socket[i].ev_tcp, listen_socket[i].tcp, EV_READ|EV_PERSIST, handle_new_meta_connection, NULL);
++                      if(event_add(&listen_socket[i].ev_tcp, NULL) < 0) {
 +                              logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
 +                              abort();
 +                      }
 +
-                       event_set(&listen_socket[listen_sockets].ev_udp,
-                                         listen_socket[listen_sockets].udp,
-                                         EV_READ|EV_PERSIST,
-                                         handle_incoming_vpn_data, (void *)(intptr_t)listen_sockets);
++                      event_set(&listen_socket[i].ev_udp, listen_socket[i].udp, EV_READ|EV_PERSIST, handle_incoming_vpn_data, (void *)(intptr_t)listen_sockets);
 +                      if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
 +                              logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
 +                              abort();
 +                      }
 +
 +                      if(debug_level >= DEBUG_CONNECTIONS) {
-                               hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
+                               hostname = sockaddr2hostname(&sa);
 -                              logger(LOG_NOTICE, "Listening on %s", hostname);
 +                              logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
                                free(hostname);
                        }
  
-                       memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
-                       listen_sockets++;
+                       memcpy(&listen_socket[i].sa, &sa, salen);
                }
+       } else {
+               listen_sockets = 0;
+               cfg = lookup_config(config_tree, "BindToAddress");
+               do {
+                       get_config_string(cfg, &address);
+                       if(cfg)
+                               cfg = lookup_config_next(config_tree, cfg);
+                       char *port = myport;
+                       if(address) {
+                               char *space = strchr(address, ' ');
+                               if(space) {
+                                       *space++ = 0;
+                                       port = space;
+                               }
+                               if(!strcmp(address, "*"))
+                                       *address = 0;
+                       }
+                       hint.ai_family = addressfamily;
+                       hint.ai_socktype = SOCK_STREAM;
+                       hint.ai_protocol = IPPROTO_TCP;
+                       hint.ai_flags = AI_PASSIVE;
  
-               freeaddrinfo(ai);
-       } while(cfg);
+                       err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
+                       free(address);
+                       if(err || !ai) {
 -                              logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
++                              logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
+                                          gai_strerror(err));
+                               return false;
+                       }
+                       for(aip = ai; aip; aip = aip->ai_next) {
+                               if(listen_sockets >= MAXSOCKETS) {
 -                                      logger(LOG_ERR, "Too many listening sockets");
++                                      logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
+                                       return false;
+                               }
+                               listen_socket[listen_sockets].tcp =
+                                       setup_listen_socket((sockaddr_t *) aip->ai_addr);
+                               if(listen_socket[listen_sockets].tcp < 0)
+                                       continue;
+                               listen_socket[listen_sockets].udp =
+                                       setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
 -                              if(listen_socket[listen_sockets].udp < 0)
++                              if(listen_socket[listen_sockets].udp < 0) {
++                                      close(listen_socket[listen_sockets].tcp);
+                                       continue;
++                              }
 -                              ifdebug(CONNECTIONS) {
++                              event_set(&listen_socket[listen_sockets].ev_tcp,
++                                                listen_socket[listen_sockets].tcp,
++                                                EV_READ|EV_PERSIST,
++                                                handle_new_meta_connection, NULL);
++                              if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
++                                      logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
++                                      abort();
++                              }
++
++                              event_set(&listen_socket[listen_sockets].ev_udp,
++                                                listen_socket[listen_sockets].udp,
++                                                EV_READ|EV_PERSIST,
++                                                handle_incoming_vpn_data, (void *)(intptr_t)listen_sockets);
++                              if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
++                                      logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
++                                      abort();
++                              }
++
++                              if(debug_level >= DEBUG_CONNECTIONS) {
+                                       hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
 -                                      logger(LOG_NOTICE, "Listening on %s", hostname);
++                                      logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
+                                       free(hostname);
+                               }
+                               memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
+                               listen_sockets++;
+                       }
+                       freeaddrinfo(ai);
+               } while(cfg);
+       }
  
        if(listen_sockets)
 -              logger(LOG_NOTICE, "Ready");
 +              logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready");
        else {
 -              logger(LOG_ERR, "Unable to create any listening socket!");
 +              logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
                return false;
        }
  
diff --cc src/tincd.c
index 1008f88aaec0865a5c604594f586f277a83f9f60,148e13e444ef2c7ef8188e04baa3e1a189504a55..3cbac4ba25f5daa4a29b2f0189dc8b466aff035a
@@@ -382,13 -537,14 +382,19 @@@ int main(int argc, char **argv) 
  
        openlogger("tinc", use_logfile?LOGMODE_FILE:LOGMODE_STDERR);
  
 +      if(!event_init()) {
 +              logger(DEBUG_ALWAYS, LOG_ERR, "Error initializing libevent!");
 +              return 1;
 +      }
 +
        g_argv = argv;
  
+       if(getenv("LISTEN_PID") && atoi(getenv("LISTEN_PID")) == getpid())
+               do_detach = false;
+ #ifdef HAVE_UNSETENV
+       unsetenv("LISTEN_PID");
+ #endif
        init_configuration(&config_tree);
  
        /* Slllluuuuuuurrrrp! */