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);
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;
}
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);
}