Add timeouts to 'tinc join'
[tinc] / src / tincctl.c
index a0a0281..3b7efd4 100644 (file)
@@ -44,6 +44,7 @@
 #include "sandbox.h"
 #include "pidfile.h"
 #include "console.h"
+#include "fs.h"
 
 #ifndef MSG_NOSIGNAL
 #define MSG_NOSIGNAL 0
@@ -494,6 +495,10 @@ bool recvline(int fd, char *line, size_t len) {
        }
 
        while(!(newline = memchr(buffer, '\n', blen))) {
+               if(!wait_socket_recv(fd)) {
+                       return false;
+               }
+
                ssize_t nrecv = recv(fd, buffer + blen, sizeof(buffer) - blen, 0);
 
                if(nrecv == -1 && sockerrno == EINTR) {
@@ -1519,7 +1524,10 @@ static int cmd_pcap(int argc, char *argv[]) {
 static void sigint_handler(int sig) {
        (void)sig;
 
-       fprintf(stderr, "\n");
+       if(write(2, "\n", 1) < 0) {
+               // nothing we can do
+       }
+
        shutdown(fd, SHUT_RDWR);
 }
 #endif
@@ -1931,16 +1939,18 @@ static int cmd_config(int argc, char *argv[]) {
        char filename[PATH_MAX];
 
        if(node) {
-               if((size_t)snprintf(filename, sizeof(filename), "%s" SLASH "%s", hosts_dir, node) >= sizeof(filename)) {
-                       fprintf(stderr, "Filename too long: %s" SLASH "%s\n", hosts_dir, node);
-                       free(node);
-                       return 1;
-               }
+               size_t wrote = (size_t)snprintf(filename, sizeof(filename), "%s" SLASH "%s", hosts_dir, node);
 
                if(node != line) {
                        free(node);
                        node = NULL;
                }
+
+               if(wrote >= sizeof(filename)) {
+                       fprintf(stderr, "Filename too long: %s" SLASH "%s\n", hosts_dir, node);
+                       return 1;
+               }
+
        } else {
                snprintf(filename, sizeof(filename), "%s", tinc_conf);
        }
@@ -2229,19 +2239,8 @@ static int cmd_init(int argc, char *argv[]) {
                return 1;
        }
 
-       if(!confbase_given && mkdir(confdir, 0755) && errno != EEXIST) {
-               fprintf(stderr, "Could not create directory %s: %s\n", confdir, strerror(errno));
-               return 1;
-       }
-
-       if(mkdir(confbase, 0777) && errno != EEXIST) {
-               fprintf(stderr, "Could not create directory %s: %s\n", confbase, strerror(errno));
-               return 1;
-       }
-
-       if(mkdir(hosts_dir, 0777) && errno != EEXIST) {
-               fprintf(stderr, "Could not create directory %s: %s\n", hosts_dir, strerror(errno));
-               return 1;
+       if(!makedirs(DIR_HOSTS | DIR_CONFBASE | DIR_CONFDIR | DIR_CACHE)) {
+               return false;
        }
 
        FILE *f = fopen(tinc_conf, "w");
@@ -2540,7 +2539,7 @@ static int cmd_export_all(int argc, char *argv[]) {
                if(first) {
                        first = false;
                } else {
-                       printf("#---------------------------------------------------------------#\n");
+                       printf("\n#---------------------------------------------------------------#\n");
                }
 
                result |= export(ent->d_name, stdout);