diff options
author | Damien Miller <djm@cvs.openbsd.org> | 2004-05-19 12:17:34 +0000 |
---|---|---|
committer | Damien Miller <djm@cvs.openbsd.org> | 2004-05-19 12:17:34 +0000 |
commit | d3dd12ac3cf2eb1a8016764837f8b0ea12fdcae1 (patch) | |
tree | 7d1a1b35487e98a0b03606f451829afcbdfc140e /usr.bin/ssh/sftp.c | |
parent | 6139f3618c4d02f89c6bed396c66964f873848f2 (diff) |
gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@
Diffstat (limited to 'usr.bin/ssh/sftp.c')
-rw-r--r-- | usr.bin/ssh/sftp.c | 64 |
1 files changed, 47 insertions, 17 deletions
diff --git a/usr.bin/ssh/sftp.c b/usr.bin/ssh/sftp.c index a2cf19314b1..443d3ffc79a 100644 --- a/usr.bin/ssh/sftp.c +++ b/usr.bin/ssh/sftp.c @@ -16,7 +16,7 @@ #include "includes.h" -RCSID("$OpenBSD: sftp.c,v 1.45 2004/03/03 09:31:20 djm Exp $"); +RCSID("$OpenBSD: sftp.c,v 1.46 2004/05/19 12:17:33 djm Exp $"); #include <glob.h> @@ -48,6 +48,9 @@ static pid_t sshpid = -1; /* This is set to 0 if the progressmeter is not desired. */ int showprogress = 1; +/* SIGINT received during command processing */ +volatile sig_atomic_t interrupted = 0; + int remote_glob(struct sftp_conn *, const char *, int, int (*)(const char *, int), glob_t *); /* proto for sftp-glob.c */ @@ -127,6 +130,24 @@ static const struct CMD cmds[] = { int interactive_loop(int fd_in, int fd_out, char *file1, char *file2); static void +killchild(int signo) +{ + if (sshpid > 1) + kill(sshpid, SIGTERM); + + _exit(1); +} + +static void +cmd_interrupt(int signo) +{ + const char msg[] = "\rInterrupt \n"; + + write(STDERR_FILENO, msg, sizeof(msg) - 1); + interrupted = 1; +} + +static void help(void) { printf("Available commands:\n"); @@ -461,7 +482,7 @@ process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag) goto out; } - for (i = 0; g.gl_pathv[i]; i++) { + for (i = 0; g.gl_pathv[i] && !interrupted; i++) { if (infer_path(g.gl_pathv[i], &tmp)) { err = -1; goto out; @@ -530,7 +551,7 @@ process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag) goto out; } - for (i = 0; g.gl_pathv[i]; i++) { + for (i = 0; g.gl_pathv[i] && !interrupted; i++) { if (!is_reg(g.gl_pathv[i])) { error("skipping non-regular file %s", g.gl_pathv[i]); @@ -617,7 +638,7 @@ do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag) qsort(d, n, sizeof(*d), sdirent_comp); - for (n = 0; d[n] != NULL; n++) { + for (n = 0; d[n] != NULL && !interrupted; n++) { char *tmp, *fname; tmp = path_append(path, d[n]->filename); @@ -669,6 +690,9 @@ do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path, return (-1); } + if (interrupted) + goto out; + /* * If the glob returns a single match, which is the same as the * input glob, and it is a directory, then just list its contents @@ -702,7 +726,7 @@ do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path, colspace = width / columns; } - for (i = 0; g.gl_pathv[i]; i++) { + for (i = 0; g.gl_pathv[i] && !interrupted; i++) { char *fname; fname = path_strip(g.gl_pathv[i], strip_path); @@ -739,6 +763,7 @@ do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path, if (!(lflag & LONG_VIEW) && (c != 1)) printf("\n"); + out: if (g.gl_pathc) globfree(&g); @@ -948,7 +973,7 @@ parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd, case I_RM: path1 = make_absolute(path1, *pwd); remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); - for (i = 0; g.gl_pathv[i]; i++) { + for (i = 0; g.gl_pathv[i] && !interrupted; i++) { printf("Removing %s\n", g.gl_pathv[i]); err = do_rm(conn, g.gl_pathv[i]); if (err != 0 && err_abort) @@ -1037,7 +1062,7 @@ parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd, a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; a.perm = n_arg; remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); - for (i = 0; g.gl_pathv[i]; i++) { + for (i = 0; g.gl_pathv[i] && !interrupted; i++) { printf("Changing mode on %s\n", g.gl_pathv[i]); err = do_setstat(conn, g.gl_pathv[i], &a); if (err != 0 && err_abort) @@ -1048,7 +1073,7 @@ parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd, case I_CHGRP: path1 = make_absolute(path1, *pwd); remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); - for (i = 0; g.gl_pathv[i]; i++) { + for (i = 0; g.gl_pathv[i] && !interrupted; i++) { if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) { if (err != 0 && err_abort) break; @@ -1171,6 +1196,8 @@ interactive_loop(int fd_in, int fd_out, char *file1, char *file2) for (;;) { char *cp; + signal(SIGINT, SIG_IGN); + printf("sftp> "); /* XXX: use libedit */ @@ -1186,6 +1213,10 @@ interactive_loop(int fd_in, int fd_out, char *file1, char *file2) if (cp) *cp = '\0'; + /* Handle user interrupts gracefully during commands */ + interrupted = 0; + signal(SIGINT, cmd_interrupt); + err = parse_dispatch_command(conn, cmd, &pwd, batchmode); if (err != 0) break; @@ -1197,15 +1228,6 @@ interactive_loop(int fd_in, int fd_out, char *file1, char *file2) } static void -killchild(int signo) -{ - if (sshpid > 1) - kill(sshpid, signo); - - _exit(1); -} - -static void connect_to_server(char *path, char **args, int *in, int *out) { int c_in, c_out; @@ -1240,6 +1262,14 @@ connect_to_server(char *path, char **args, int *in, int *out) close(*out); close(c_in); close(c_out); + + /* + * The underlying ssh is in the same process group, so we must + * ignore SIGINT if we want to gracefully abort commands, + * otherwise the signal will make it to the ssh process and + * kill it too + */ + signal(SIGINT, SIG_IGN); execv(path, args); fprintf(stderr, "exec: %s: %s\n", path, strerror(errno)); exit(1); |