- djm@cvs.openbsd.org 2004/05/19 12:17:33

[sftp-client.c sftp.c]
     gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
     waiting for a command; ok markus@
This commit is contained in:
Darren Tucker 2004-05-24 10:12:19 +10:00
parent efec7c23b1
commit cdf547afe4
3 changed files with 83 additions and 25 deletions

View File

@ -1,3 +1,10 @@
20040524
- (dtucker) OpenBSD CVS Sync
- djm@cvs.openbsd.org 2004/05/19 12:17:33
[sftp-client.c sftp.c]
gracefully abort transfers on receipt of SIGINT, also ignore SIGINT while
waiting for a command; ok markus@
20040523 20040523
- (djm) [sshd_config] Explain consequences of UsePAM=yes a little better in - (djm) [sshd_config] Explain consequences of UsePAM=yes a little better in
sshd_config; ok dtucker@ sshd_config; ok dtucker@
@ -1126,4 +1133,4 @@
- (djm) Trim deprecated options from INSTALL. Mention UsePAM - (djm) Trim deprecated options from INSTALL. Mention UsePAM
- (djm) Fix quote handling in sftp; Patch from admorten AT umich.edu - (djm) Fix quote handling in sftp; Patch from admorten AT umich.edu
$Id: ChangeLog,v 1.3358 2004/05/23 06:22:27 mouring Exp $ $Id: ChangeLog,v 1.3359 2004/05/24 00:12:19 dtucker Exp $

View File

@ -20,7 +20,7 @@
/* XXX: copy between two remote sites */ /* XXX: copy between two remote sites */
#include "includes.h" #include "includes.h"
RCSID("$OpenBSD: sftp-client.c,v 1.48 2004/03/30 12:41:56 djm Exp $"); RCSID("$OpenBSD: sftp-client.c,v 1.49 2004/05/19 12:17:33 djm Exp $");
#include "openbsd-compat/sys-queue.h" #include "openbsd-compat/sys-queue.h"
@ -36,6 +36,7 @@ RCSID("$OpenBSD: sftp-client.c,v 1.48 2004/03/30 12:41:56 djm Exp $");
#include "sftp-common.h" #include "sftp-common.h"
#include "sftp-client.h" #include "sftp-client.h"
extern volatile sig_atomic_t interrupted;
extern int showprogress; extern int showprogress;
/* Minimum amount of data to read at at time */ /* Minimum amount of data to read at at time */
@ -330,7 +331,7 @@ do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
(*dir)[0] = NULL; (*dir)[0] = NULL;
} }
for (;;) { for (; !interrupted;) {
int count; int count;
id = expected_id = conn->msg_id++; id = expected_id = conn->msg_id++;
@ -407,6 +408,13 @@ do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
do_close(conn, handle, handle_len); do_close(conn, handle, handle_len);
xfree(handle); xfree(handle);
/* Don't return partial matches on interrupt */
if (interrupted && dir != NULL && *dir != NULL) {
free_sftp_dirents(*dir);
*dir = xmalloc(sizeof(**dir));
**dir = NULL;
}
return(0); return(0);
} }
@ -812,6 +820,16 @@ do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
char *data; char *data;
u_int len; u_int len;
/*
* Simulate EOF on interrupt: stop sending new requests and
* allow outstanding requests to drain gracefully
*/
if (interrupted) {
if (num_req == 0) /* If we haven't started yet... */
break;
max_req = 0;
}
/* Send some more requests */ /* Send some more requests */
while (num_req < max_req) { while (num_req < max_req) {
debug3("Request range %llu -> %llu (%d/%d)", debug3("Request range %llu -> %llu (%d/%d)",
@ -899,8 +917,7 @@ do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
(unsigned long long)offset, (unsigned long long)offset,
num_req); num_req);
max_req = 1; max_req = 1;
} } else if (max_req <= conn->num_requests) {
else if (max_req < conn->num_requests + 1) {
++max_req; ++max_req;
} }
} }
@ -1036,10 +1053,14 @@ do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
int len; int len;
/* /*
* Can't use atomicio here because it returns 0 on EOF, thus losing * Can't use atomicio here because it returns 0 on EOF,
* the last block of the file * thus losing the last block of the file.
* Simulate an EOF on interrupt, allowing ACKs from the
* server to drain.
*/ */
do if (interrupted)
len = 0;
else do
len = read(local_fd, data, conn->transfer_buflen); len = read(local_fd, data, conn->transfer_buflen);
while ((len == -1) && (errno == EINTR || errno == EAGAIN)); while ((len == -1) && (errno == EINTR || errno == EAGAIN));

64
sftp.c
View File

@ -16,7 +16,7 @@
#include "includes.h" #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 "buffer.h" #include "buffer.h"
#include "xmalloc.h" #include "xmalloc.h"
@ -46,6 +46,9 @@ static pid_t sshpid = -1;
/* This is set to 0 if the progressmeter is not desired. */ /* This is set to 0 if the progressmeter is not desired. */
int showprogress = 1; int showprogress = 1;
/* SIGINT received during command processing */
volatile sig_atomic_t interrupted = 0;
int remote_glob(struct sftp_conn *, const char *, int, int remote_glob(struct sftp_conn *, const char *, int,
int (*)(const char *, int), glob_t *); /* proto for sftp-glob.c */ int (*)(const char *, int), glob_t *); /* proto for sftp-glob.c */
@ -130,6 +133,24 @@ static const struct CMD cmds[] = {
int interactive_loop(int fd_in, int fd_out, char *file1, char *file2); 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 static void
help(void) help(void)
{ {
@ -465,7 +486,7 @@ process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
goto out; 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)) { if (infer_path(g.gl_pathv[i], &tmp)) {
err = -1; err = -1;
goto out; goto out;
@ -534,7 +555,7 @@ process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
goto out; 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])) { if (!is_reg(g.gl_pathv[i])) {
error("skipping non-regular file %s", error("skipping non-regular file %s",
g.gl_pathv[i]); g.gl_pathv[i]);
@ -621,7 +642,7 @@ do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
qsort(d, n, sizeof(*d), sdirent_comp); 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; char *tmp, *fname;
tmp = path_append(path, d[n]->filename); tmp = path_append(path, d[n]->filename);
@ -673,6 +694,9 @@ do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
return (-1); return (-1);
} }
if (interrupted)
goto out;
/* /*
* If the glob returns a single match, which is the same as the * 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 * input glob, and it is a directory, then just list its contents
@ -706,7 +730,7 @@ do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
colspace = width / columns; colspace = width / columns;
} }
for (i = 0; g.gl_pathv[i]; i++) { for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
char *fname; char *fname;
fname = path_strip(g.gl_pathv[i], strip_path); fname = path_strip(g.gl_pathv[i], strip_path);
@ -743,6 +767,7 @@ do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
if (!(lflag & LONG_VIEW) && (c != 1)) if (!(lflag & LONG_VIEW) && (c != 1))
printf("\n"); printf("\n");
out:
if (g.gl_pathc) if (g.gl_pathc)
globfree(&g); globfree(&g);
@ -952,7 +977,7 @@ parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
case I_RM: case I_RM:
path1 = make_absolute(path1, *pwd); path1 = make_absolute(path1, *pwd);
remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); 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]); printf("Removing %s\n", g.gl_pathv[i]);
err = do_rm(conn, g.gl_pathv[i]); err = do_rm(conn, g.gl_pathv[i]);
if (err != 0 && err_abort) if (err != 0 && err_abort)
@ -1041,7 +1066,7 @@ parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
a.perm = n_arg; a.perm = n_arg;
remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); 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]); printf("Changing mode on %s\n", g.gl_pathv[i]);
err = do_setstat(conn, g.gl_pathv[i], &a); err = do_setstat(conn, g.gl_pathv[i], &a);
if (err != 0 && err_abort) if (err != 0 && err_abort)
@ -1052,7 +1077,7 @@ parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
case I_CHGRP: case I_CHGRP:
path1 = make_absolute(path1, *pwd); path1 = make_absolute(path1, *pwd);
remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); 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 (!(aa = do_stat(conn, g.gl_pathv[i], 0))) {
if (err != 0 && err_abort) if (err != 0 && err_abort)
break; break;
@ -1180,6 +1205,8 @@ interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
for (;;) { for (;;) {
char *cp; char *cp;
signal(SIGINT, SIG_IGN);
printf("sftp> "); printf("sftp> ");
/* XXX: use libedit */ /* XXX: use libedit */
@ -1195,6 +1222,10 @@ interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
if (cp) if (cp)
*cp = '\0'; *cp = '\0';
/* Handle user interrupts gracefully during commands */
interrupted = 0;
signal(SIGINT, cmd_interrupt);
err = parse_dispatch_command(conn, cmd, &pwd, batchmode); err = parse_dispatch_command(conn, cmd, &pwd, batchmode);
if (err != 0) if (err != 0)
break; break;
@ -1205,15 +1236,6 @@ interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
return (err >= 0 ? 0 : -1); return (err >= 0 ? 0 : -1);
} }
static void
killchild(int signo)
{
if (sshpid > 1)
kill(sshpid, signo);
_exit(1);
}
static void static void
connect_to_server(char *path, char **args, int *in, int *out) connect_to_server(char *path, char **args, int *in, int *out)
{ {
@ -1249,6 +1271,14 @@ connect_to_server(char *path, char **args, int *in, int *out)
close(*out); close(*out);
close(c_in); close(c_in);
close(c_out); 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); execv(path, args);
fprintf(stderr, "exec: %s: %s\n", path, strerror(errno)); fprintf(stderr, "exec: %s: %s\n", path, strerror(errno));
exit(1); exit(1);