Fix warnings from cppcheck.
[tinc] / src / tincctl.c
index eaf676d..4dc1ff2 100644 (file)
@@ -355,7 +355,7 @@ static FILE *ask_and_open(const char *filename, const char *what, const char *mo
 static bool ecdsa_keygen(bool ask) {
        ecdsa_t key;
        FILE *f;
-       char *filename;
+       char *pubname, *privname;
 
        fprintf(stderr, "Generating ECDSA keypair:\n");
 
@@ -365,8 +365,9 @@ static bool ecdsa_keygen(bool ask) {
        } else
                fprintf(stderr, "Done.\n");
 
-       xasprintf(&filename, "%s" SLASH "ecdsa_key.priv", confbase);
-       f = ask_and_open(filename, "private ECDSA key", "a", ask);
+       xasprintf(&privname, "%s" SLASH "ecdsa_key.priv", confbase);
+       f = ask_and_open(privname, "private ECDSA key", "a", ask);
+       free(privname);
 
        if(!f)
                return false;
@@ -379,14 +380,14 @@ static bool ecdsa_keygen(bool ask) {
        ecdsa_write_pem_private_key(&key, f);
 
        fclose(f);
-       free(filename);
 
        if(name)
-               xasprintf(&filename, "%s" SLASH "hosts" SLASH "%s", confbase, name);
+               xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
        else
-               xasprintf(&filename, "%s" SLASH "ecdsa_key.pub", confbase);
+               xasprintf(&pubname, "%s" SLASH "ecdsa_key.pub", confbase);
 
-       f = ask_and_open(filename, "public ECDSA key", "a", ask);
+       f = ask_and_open(pubname, "public ECDSA key", "a", ask);
+       free(pubname);
 
        if(!f)
                return false;
@@ -396,7 +397,6 @@ static bool ecdsa_keygen(bool ask) {
        free(pubkey);
 
        fclose(f);
-       free(filename);
 
        return true;
 }
@@ -408,7 +408,7 @@ static bool ecdsa_keygen(bool ask) {
 static bool rsa_keygen(int bits, bool ask) {
        rsa_t key;
        FILE *f;
-       char *filename;
+       char *pubname, *privname;
 
        fprintf(stderr, "Generating %d bits keys:\n", bits);
 
@@ -418,8 +418,9 @@ static bool rsa_keygen(int bits, bool ask) {
        } else
                fprintf(stderr, "Done.\n");
 
-       xasprintf(&filename, "%s" SLASH "rsa_key.priv", confbase);
-       f = ask_and_open(filename, "private RSA key", "a", ask);
+       xasprintf(&privname, "%s" SLASH "rsa_key.priv", confbase);
+       f = ask_and_open(privname, "private RSA key", "a", ask);
+       free(privname);
 
        if(!f)
                return false;
@@ -432,14 +433,14 @@ static bool rsa_keygen(int bits, bool ask) {
        rsa_write_pem_private_key(&key, f);
 
        fclose(f);
-       free(filename);
 
        if(name)
-               xasprintf(&filename, "%s" SLASH "hosts" SLASH "%s", confbase, name);
+               xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
        else
-               xasprintf(&filename, "%s" SLASH "rsa_key.pub", confbase);
+               xasprintf(&pubname, "%s" SLASH "rsa_key.pub", confbase);
 
-       f = ask_and_open(filename, "public RSA key", "a", ask);
+       f = ask_and_open(pubname, "public RSA key", "a", ask);
+       free(pubname);
 
        if(!f)
                return false;
@@ -447,7 +448,6 @@ static bool rsa_keygen(int bits, bool ask) {
        rsa_write_pem_public_key(&key, f);
 
        fclose(f);
-       free(filename);
 
        return true;
 }
@@ -687,8 +687,19 @@ static bool remove_service(void) {
 #endif
 
 static bool connect_tincd(bool verbose) {
-       if(fd >= 0)
-               return true;
+       if(fd >= 0) {
+               fd_set r;
+               FD_ZERO(&r);
+               FD_SET(fd, &r);
+               struct timeval tv = {0, 0};
+               if(select(fd + 1, &r, NULL, NULL, &tv)) {
+                       fprintf(stderr, "Previous connection to tincd lost, reconnecting.\n");
+                       close(fd);
+                       fd = -1;
+               } else {
+                       return true;
+               }
+       }
 
        FILE *f = fopen(pidfilename, "r");
        if(!f) {
@@ -751,6 +762,8 @@ static bool connect_tincd(bool verbose) {
        if(connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
                if(verbose)
                        fprintf(stderr, "Cannot connect to %s port %s: %s\n", host, port, sockstrerror(sockerrno));
+               close(fd);
+               fd = -1;
                return false;
        }
 
@@ -762,6 +775,8 @@ static bool connect_tincd(bool verbose) {
        if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %s %d", &code, data, &version) != 3 || code != 0) {
                if(verbose)
                        fprintf(stderr, "Cannot read greeting from control socket: %s\n", sockstrerror(sockerrno));
+               close(fd);
+               fd = -1;
                return false;
        }
 
@@ -770,6 +785,8 @@ static bool connect_tincd(bool verbose) {
        if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &version, &pid) != 3 || code != 4 || version != TINC_CTL_VERSION_CURRENT) {
                if(verbose)
                        fprintf(stderr, "Could not fully establish control socket connection\n");
+               close(fd);
+               fd = -1;
                return false;
        }
 
@@ -800,9 +817,10 @@ static int cmd_start(int argc, char *argv[]) {
                c = "tincd";
 
        int nargc = 0;
-       char **nargv = xmalloc_and_zero((orig_argc + argc) * sizeof *nargv);
+       char **nargv = xmalloc_and_zero((optind + argc) * sizeof *nargv);
 
-       for(int i = 0; i < orig_argc; i++)
+       nargv[nargc++] = c;
+       for(int i = 1; i < optind; i++)
                nargv[nargc++] = orig_argv[i];
        for(int i = 1; i < argc; i++)
                nargv[nargc++] = argv[i];
@@ -815,12 +833,15 @@ static int cmd_start(int argc, char *argv[]) {
        pid_t pid = fork();
        if(pid == -1) {
                fprintf(stderr, "Could not fork: %s\n", strerror(errno));
+               free(nargv);
                return 1;
        }
 
        if(!pid)
                exit(execvp(c, nargv));
        
+       free(nargv);
+
        int status = -1;
        if(waitpid(pid, &status, 0) != pid || !WIFEXITED(status) || WEXITSTATUS(status)) {
                fprintf(stderr, "Error starting %s\n", c);
@@ -835,9 +856,13 @@ static int cmd_stop(int argc, char *argv[]) {
 #ifndef HAVE_MINGW
        if(!connect_tincd(true)) {
                if(pid) {
-                       if(kill(pid, SIGTERM)) 
+                       if(kill(pid, SIGTERM)) {
+                               fprintf(stderr, "Could not send TERM signal to process with PID %u: %s\n", pid, strerror(errno));
                                return 1;
+                       }
+
                        fprintf(stderr, "Sent TERM signal to process with PID %u.\n", pid);
+                       waitpid(pid, NULL, 0);
                        return 0;
                }
 
@@ -849,6 +874,12 @@ static int cmd_stop(int argc, char *argv[]) {
                fprintf(stderr, "Could not stop tinc daemon.\n");
                return 1;
        }
+
+       // Wait for tincd to close the connection...
+       fd_set r;
+       FD_ZERO(&r);
+       FD_SET(fd, &r);
+       select(fd + 1, &r, NULL, NULL, NULL);
 #else
        if(!remove_service())
                return 1;
@@ -1198,7 +1229,7 @@ static struct {
        {"ECDSAPrivateKeyFile", VAR_SERVER},
        {"ExperimentalProtocol", VAR_SERVER},
        {"Forwarding", VAR_SERVER},
-       {"GraphDumpFile", VAR_SERVER},
+       {"GraphDumpFile", VAR_SERVER | VAR_OBSOLETE},
        {"Hostnames", VAR_SERVER},
        {"IffOneQueue", VAR_SERVER},
        {"Interface", VAR_SERVER},
@@ -1217,6 +1248,8 @@ static struct {
        {"ProcessPriority", VAR_SERVER},
        {"Proxy", VAR_SERVER},
        {"ReplayWindow", VAR_SERVER},
+       {"ScriptsExtension", VAR_SERVER},
+       {"ScriptsInterpreter", VAR_SERVER},
        {"StrictSubnets", VAR_SERVER},
        {"TunnelServer", VAR_SERVER},
        {"UDPRcvBuf", VAR_SERVER},
@@ -2020,7 +2053,6 @@ static int cmd_shell(int argc, char *argv[]) {
        char *line = NULL;
        int maxargs = argc + 16;
        char **nargv = xmalloc(maxargs * sizeof *nargv);
-       optind = argc;
 
        for(int i = 0; i < argc; i++)
                nargv[i] = argv[i];
@@ -2106,6 +2138,8 @@ static int cmd_shell(int argc, char *argv[]) {
                }
        }
 
+       free(nargv);
+
        if(tty)
                printf("\n");
        return result;