From 5ea9c0d700a2ff425b6c10eb20c81a4aacbf0cf1 Mon Sep 17 00:00:00 2001 From: millert Date: Sat, 20 Aug 2016 20:18:42 +0000 Subject: [PATCH] Use connect(2) + a connect_wait() function instead of connect_sync(), similar to the example in connect(2). OK tedu@ --- usr.bin/ftp/extern.h | 5 ++--- usr.bin/ftp/fetch.c | 8 +++++--- usr.bin/ftp/ftp.c | 15 ++++++++++----- usr.bin/ftp/util.c | 35 +++++++++++++---------------------- 4 files changed, 30 insertions(+), 33 deletions(-) diff --git a/usr.bin/ftp/extern.h b/usr.bin/ftp/extern.h index ea894bfe521..630facbb36b 100644 --- a/usr.bin/ftp/extern.h +++ b/usr.bin/ftp/extern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: extern.h,v 1.43 2016/08/18 16:23:06 millert Exp $ */ +/* $OpenBSD: extern.h,v 1.44 2016/08/20 20:18:42 millert Exp $ */ /* $NetBSD: extern.h,v 1.17 1997/08/18 10:20:19 lukem Exp $ */ /* @@ -62,7 +62,6 @@ */ #include -#include void abort_remote(FILE *); void abortpt(int); @@ -76,7 +75,7 @@ void cmdabort(int); void cmdscanner(int); int command(const char *, ...); int confirm(const char *, const char *); -int connect_sync(int, const struct sockaddr *, socklen_t); +int connect_wait(int); FILE *dataconn(const char *); int foregroundproc(void); int fileindir(const char *, const char *); diff --git a/usr.bin/ftp/fetch.c b/usr.bin/ftp/fetch.c index 41d17c77d96..8472b6b6f12 100644 --- a/usr.bin/ftp/fetch.c +++ b/usr.bin/ftp/fetch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fetch.c,v 1.148 2016/08/18 16:23:06 millert Exp $ */ +/* $OpenBSD: fetch.c,v 1.149 2016/08/20 20:18:42 millert Exp $ */ /* $NetBSD: fetch.c,v 1.14 1997/08/18 10:20:20 lukem Exp $ */ /*- @@ -557,8 +557,10 @@ noslash: } #endif /* !SMALL */ -again: - if (connect_sync(s, res->ai_addr, res->ai_addrlen) < 0) { + for (error = connect(s, res->ai_addr, res->ai_addrlen); + error != 0 && errno == EINTR; error = connect_wait(s)) + continue; + if (error != 0) { save_errno = errno; close(s); errno = save_errno; diff --git a/usr.bin/ftp/ftp.c b/usr.bin/ftp/ftp.c index 14f6ea45d3e..59135e8a244 100644 --- a/usr.bin/ftp/ftp.c +++ b/usr.bin/ftp/ftp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ftp.c,v 1.98 2016/08/18 16:23:06 millert Exp $ */ +/* $OpenBSD: ftp.c,v 1.99 2016/08/20 20:18:42 millert Exp $ */ /* $NetBSD: ftp.c,v 1.27 1997/08/18 10:20:23 lukem Exp $ */ /* @@ -219,8 +219,10 @@ hookup(char *host, char *port) } } #endif /* !SMALL */ - error = connect_sync(s, res->ai_addr, res->ai_addrlen); - if (error) { + for (error = connect(s, res->ai_addr, res->ai_addrlen); + error != 0 && errno == EINTR; error = connect_wait(s)) + continue; + if (error != 0) { /* this "if" clause is to prevent print warning twice */ if (verbose && res->ai_next) { if (getnameinfo(res->ai_addr, res->ai_addrlen, @@ -1514,8 +1516,11 @@ reinit: } else goto bad; - if (connect_sync(data, (struct sockaddr *)&data_addr, - data_addr.su_len) < 0) { + for (error = connect(data, (struct sockaddr *)&data_addr, + data_addr.su_len); error != 0 && errno == EINTR; + error = connect_wait(data)) + continue; + if (error != 0) { if (activefallback) { (void)close(data); data = -1; diff --git a/usr.bin/ftp/util.c b/usr.bin/ftp/util.c index f0f96ca1bcc..4da0e3be26a 100644 --- a/usr.bin/ftp/util.c +++ b/usr.bin/ftp/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.80 2016/08/18 16:23:06 millert Exp $ */ +/* $OpenBSD: util.c,v 1.81 2016/08/20 20:18:42 millert Exp $ */ /* $NetBSD: util.c,v 1.12 1997/08/18 10:20:27 lukem Exp $ */ /*- @@ -67,6 +67,7 @@ * FTP User Program -- Misc support routines */ #include +#include #include #include @@ -1070,35 +1071,25 @@ controlediting(void) #endif /* !SMALL */ /* - * Wrapper for connect(2) that restarts the syscall when - * interrupted and operates synchronously. + * Wait for an asynchronous connect(2) attempt to finish. */ int -connect_sync(int s, const struct sockaddr *name, socklen_t namelen) +connect_wait(int s) { struct pollfd pfd[1]; int error = 0; socklen_t len = sizeof(error); - if (connect(s, name, namelen) < 0) { - if (errno != EINTR) - return -1; - } - - /* An interrupted connect(2) continues asyncronously. */ pfd[0].fd = s; pfd[0].events = POLLOUT; - for (;;) { - if (poll(pfd, 1, -1) == -1) { - if (errno != EINTR) - return -1; - continue; - } - if (getsockopt(s, SOL_SOCKET, SO_ERROR, &error, &len) < 0) - return -1; - if (error != 0) - errno = error; - break; + + if (poll(pfd, 1, -1) == -1) + return -1; + if (getsockopt(s, SOL_SOCKET, SO_ERROR, &error, &len) < 0) + return -1; + if (error != 0) { + errno = error; + return -1; } - return (error ? -1 : 0); + return 0; } -- 2.20.1