* Use fork() and exec() instead of system() to set up the master SSH session, so...
authorArt Cancro <ajc@citadel.org>
Sun, 26 Apr 2009 03:46:53 +0000 (03:46 +0000)
committerArt Cancro <ajc@citadel.org>
Sun, 26 Apr 2009 03:46:53 +0000 (03:46 +0000)
* At exit, kill the master SSH session and delete the socket

citadel/ctdlmigrate.c

index 1cd12dd47f8c1e21bbf4fc9c878f8d034f55ffd9..108600db943d607eb41879517528e50318b01bc4 100644 (file)
@@ -79,6 +79,7 @@ int main(int argc, char *argv[])
        int linecount = 0;
        char spinning[4] = "-\\|/" ;
        int exitcode = 0;
+       pid_t sshpid;
        
        calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
        CtdlMakeTempFileName(socket_path, sizeof socket_path);
@@ -132,14 +133,40 @@ int main(int argc, char *argv[])
                remote_host);
        getz(remote_user);
 
-       printf("\nEstablishing an SSH connection to the source system...\n\n");
-       unlink(socket_path);
-       snprintf(cmd, sizeof cmd, "ssh -MNf -S %s %s@%s", socket_path, remote_user, remote_host);
-       cmdexit = system(cmd);
-       if (cmdexit != 0) {
-               printf("\nctdlmigrate was unable to establish an SSH connection to the\n"
-                       "source system, and cannot continue.\n\n");
-               exitcode = cmdexit;
+       sshpid = fork();
+       if (sshpid < 0)
+       {
+               printf("\n%s\n", strerror(errno));
+               exitcode = errno;
+               goto THEEND;
+       }
+       else if (sshpid == 0)
+       {
+               printf("\nEstablishing an SSH connection to the source system...\n\n");
+               unlink(socket_path);
+               snprintf(cmd, sizeof cmd, "%s@%s", remote_user, remote_host);
+               execlp("ssh", "ssh", "-MNf", "-S", socket_path, cmd, NULL);
+               cmdexit = errno;
+               printf("\n%s\n", strerror(cmdexit));
+               exit(cmdexit);          /* child process exits */
+       }
+
+       /* If we get here we are the parent process */
+       if (waitpid(sshpid, &cmdexit, 0) <= 0) {
+               exitcode = errno;
+               printf("\n%s\n", strerror(errno));
+               goto THEEND;
+       }
+
+       if (WIFSIGNALED(cmdexit)) {
+               exitcode = errno;
+               printf("\n%s\n", strerror(errno));
+               goto THEEND;
+       }
+
+       if ((WIFEXITED(cmdexit)) && (WEXITSTATUS(cmdexit) != 0)) {
+               exitcode = WEXITSTATUS(cmdexit);
+               printf("\n%s\n", strerror(errno));
                goto THEEND;
        }
 
@@ -219,9 +246,9 @@ FAIL:       pclose(source_artv);
        pclose(target_artv);
 
        // FIXME handle -h on both sides
-       // FIXME kill the master ssh session
        printf("If this program was finished we would do more.  FIXME\n");
 
-THEEND:        unlink(socket_path);
+THEEND:        kill(sshpid, SIGKILL);
+       unlink(socket_path);
        exit(exitcode);
 }