diff options
author | Tobias Stoeckmann <tobias@cvs.openbsd.org> | 2007-09-07 14:50:45 +0000 |
---|---|---|
committer | Tobias Stoeckmann <tobias@cvs.openbsd.org> | 2007-09-07 14:50:45 +0000 |
commit | 27eb8946aec561bdd8abb9e09db43d6f4093d59a (patch) | |
tree | 3dfae515908e3aec2e4068ad7e754b070d8adae2 /usr.bin | |
parent | 7ccda80cec5fd4b4511e0ff4b470767559afc294 (diff) |
Synced atomicio implementation in nc and sendbug with ssh.
OK djm@, joris@, ray@
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/nc/atomicio.c | 29 | ||||
-rw-r--r-- | usr.bin/nc/atomicio.h | 8 | ||||
-rw-r--r-- | usr.bin/sendbug/atomicio.c | 14 |
3 files changed, 35 insertions, 16 deletions
diff --git a/usr.bin/nc/atomicio.c b/usr.bin/nc/atomicio.c index c3f26846df9..e9d98b3561e 100644 --- a/usr.bin/nc/atomicio.c +++ b/usr.bin/nc/atomicio.c @@ -1,7 +1,7 @@ -/* $OpenBSD: atomicio.c,v 1.8 2006/02/11 19:31:18 otto Exp $ */ - +/* $OpenBSD: atomicio.c,v 1.9 2007/09/07 14:50:44 tobias Exp $ */ /* - * Copyright (c) 2005 Anil Madhavapeddy. All rights served. + * Copyright (c) 2006 Damien Miller. All rights reserved. + * Copyright (c) 2005 Anil Madhavapeddy. All rights reserved. * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved. * All rights reserved. * @@ -26,32 +26,37 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include <sys/types.h> -#include <sys/uio.h> +#include <sys/param.h> + #include <errno.h> +#include <poll.h> #include <unistd.h> + #include "atomicio.h" /* * ensure all of data on socket comes through. f==read || f==vwrite */ size_t -atomicio(f, fd, _s, n) - ssize_t (*f) (int, void *, size_t); - int fd; - void *_s; - size_t n; +atomicio(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n) { char *s = _s; size_t pos = 0; ssize_t res; + struct pollfd pfd; + pfd.fd = fd; + pfd.events = f == read ? POLLIN : POLLOUT; while (n > pos) { res = (f) (fd, s + pos, n - pos); switch (res) { case -1: - if (errno == EINTR || errno == EAGAIN) + if (errno == EINTR) + continue; + if (errno == EAGAIN) { + (void)poll(&pfd, 1, -1); continue; + } return 0; case 0: errno = EPIPE; @@ -60,5 +65,5 @@ atomicio(f, fd, _s, n) pos += (size_t)res; } } - return pos; + return (pos); } diff --git a/usr.bin/nc/atomicio.h b/usr.bin/nc/atomicio.h index 551a0565da8..7bf5b25418c 100644 --- a/usr.bin/nc/atomicio.h +++ b/usr.bin/nc/atomicio.h @@ -1,6 +1,7 @@ -/* $OpenBSD: atomicio.h,v 1.1 2005/05/24 20:13:28 avsm Exp $ */ +/* $OpenBSD: atomicio.h,v 1.2 2007/09/07 14:50:44 tobias Exp $ */ /* + * Copyright (c) 2006 Damien Miller. All rights reserved. * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved. * All rights reserved. * @@ -25,9 +26,14 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#ifndef _ATOMICIO_H +#define _ATOMICIO_H + /* * Ensure all of data on socket comes through. f==read || f==vwrite */ size_t atomicio(ssize_t (*)(int, void *, size_t), int, void *, size_t); #define vwrite (ssize_t (*)(int, void *, size_t))write + +#endif /* _ATOMICIO_H */ diff --git a/usr.bin/sendbug/atomicio.c b/usr.bin/sendbug/atomicio.c index 3478ab2e9f5..f85dec49b9b 100644 --- a/usr.bin/sendbug/atomicio.c +++ b/usr.bin/sendbug/atomicio.c @@ -1,4 +1,4 @@ -/* $OpenBSD: atomicio.c,v 1.2 2007/07/31 03:44:21 ray Exp $ */ +/* $OpenBSD: atomicio.c,v 1.3 2007/09/07 14:50:44 tobias Exp $ */ /* * Copyright (c) 2006 Damien Miller. All rights reserved. * Copyright (c) 2005 Anil Madhavapeddy. All rights reserved. @@ -27,9 +27,10 @@ */ #include <sys/param.h> -#include <sys/uio.h> #include <errno.h> +#include <poll.h> +#include <unistd.h> #include "atomicio.h" @@ -42,13 +43,20 @@ atomicio(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n) char *s = _s; size_t pos = 0; ssize_t res; + struct pollfd pfd; + pfd.fd = fd; + pfd.events = f == read ? POLLIN : POLLOUT; while (n > pos) { res = (f) (fd, s + pos, n - pos); switch (res) { case -1: - if (errno == EINTR || errno == EAGAIN) + if (errno == EINTR) continue; + if (errno == EAGAIN) { + (void)poll(&pfd, 1, -1); + continue; + } return 0; case 0: errno = EPIPE; |