diff options
author | Damien Miller <djm@mindrot.org> | 2006-04-23 12:06:20 +1000 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2006-04-23 12:06:20 +1000 |
commit | 6aa139c41ff22f9c91a934a73013265ea0a64afc (patch) | |
tree | 7df9e369f188628f3331b768752d0e918b7c7c8c | |
parent | 499a0d5ada82acbf8a5c5d496dbf0b4570dde1af (diff) |
- djm@cvs.openbsd.org 2006/04/16 00:52:55
[atomicio.c atomicio.h]
introduce atomiciov() function that wraps readv/writev to retry
interrupted transfers like atomicio() does for read/write;
feedback deraadt@ dtucker@ stevesk@ ok deraadt@
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | atomicio.c | 55 | ||||
-rw-r--r-- | atomicio.h | 18 |
3 files changed, 77 insertions, 3 deletions
@@ -26,6 +26,11 @@ | |||
26 | channels code can use to propsectivly check whether an incremental | 26 | channels code can use to propsectivly check whether an incremental |
27 | allocation will succeed. bz #1131, debugged with the assistance of | 27 | allocation will succeed. bz #1131, debugged with the assistance of |
28 | cove AT wildpackets.com; ok dtucker@ deraadt@ | 28 | cove AT wildpackets.com; ok dtucker@ deraadt@ |
29 | - djm@cvs.openbsd.org 2006/04/16 00:52:55 | ||
30 | [atomicio.c atomicio.h] | ||
31 | introduce atomiciov() function that wraps readv/writev to retry | ||
32 | interrupted transfers like atomicio() does for read/write; | ||
33 | feedback deraadt@ dtucker@ stevesk@ ok deraadt@ | ||
29 | 34 | ||
30 | 20060421 | 35 | 20060421 |
31 | - (djm) [Makefile.in configure.ac session.c sshpty.c] | 36 | - (djm) [Makefile.in configure.ac session.c sshpty.c] |
@@ -4537,4 +4542,4 @@ | |||
4537 | - (djm) Trim deprecated options from INSTALL. Mention UsePAM | 4542 | - (djm) Trim deprecated options from INSTALL. Mention UsePAM |
4538 | - (djm) Fix quote handling in sftp; Patch from admorten AT umich.edu | 4543 | - (djm) Fix quote handling in sftp; Patch from admorten AT umich.edu |
4539 | 4544 | ||
4540 | $Id: ChangeLog,v 1.4307 2006/04/23 02:06:03 djm Exp $ | 4545 | $Id: ChangeLog,v 1.4308 2006/04/23 02:06:20 djm Exp $ |
diff --git a/atomicio.c b/atomicio.c index 996beff2f..de5363aa3 100644 --- a/atomicio.c +++ b/atomicio.c | |||
@@ -1,5 +1,6 @@ | |||
1 | /* $OpenBSD: atomicio.c,v 1.17 2006/04/01 05:51:34 djm Exp $ */ | 1 | /* $OpenBSD: atomicio.c,v 1.18 2006/04/16 00:52:55 djm Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2006 Damien Miller. All rights reserved. | ||
3 | * Copyright (c) 2005 Anil Madhavapeddy. All rights reserved. | 4 | * Copyright (c) 2005 Anil Madhavapeddy. All rights reserved. |
4 | * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved. | 5 | * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved. |
5 | * All rights reserved. | 6 | * All rights reserved. |
@@ -59,3 +60,55 @@ atomicio(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n) | |||
59 | } | 60 | } |
60 | return (pos); | 61 | return (pos); |
61 | } | 62 | } |
63 | |||
64 | /* | ||
65 | * ensure all of data on socket comes through. f==readv || f==writev | ||
66 | */ | ||
67 | size_t | ||
68 | atomiciov(ssize_t (*f) (int, const struct iovec *, int), int fd, | ||
69 | const struct iovec *_iov, int iovcnt) | ||
70 | { | ||
71 | size_t pos = 0, rem; | ||
72 | ssize_t res; | ||
73 | struct iovec iov_array[IOV_MAX], *iov = iov_array; | ||
74 | |||
75 | if (iovcnt > IOV_MAX) { | ||
76 | errno = EINVAL; | ||
77 | return 0; | ||
78 | } | ||
79 | /* Make a copy of the iov array because we may modify it below */ | ||
80 | memcpy(iov, _iov, iovcnt * sizeof(*_iov)); | ||
81 | |||
82 | for (; iovcnt > 0 && iov[0].iov_len > 0;) { | ||
83 | res = (f) (fd, iov, iovcnt); | ||
84 | switch (res) { | ||
85 | case -1: | ||
86 | if (errno == EINTR || errno == EAGAIN) | ||
87 | continue; | ||
88 | return 0; | ||
89 | case 0: | ||
90 | errno = EPIPE; | ||
91 | return pos; | ||
92 | default: | ||
93 | rem = (size_t)res; | ||
94 | pos += rem; | ||
95 | /* skip completed iov entries */ | ||
96 | while (iovcnt > 0 && rem >= iov[0].iov_len) { | ||
97 | rem -= iov[0].iov_len; | ||
98 | iov++; | ||
99 | iovcnt--; | ||
100 | } | ||
101 | /* This shouldn't happen... */ | ||
102 | if (rem > iov[0].iov_len || (rem > 0 && iovcnt <= 0)) { | ||
103 | errno = EFAULT; | ||
104 | return 0; | ||
105 | } | ||
106 | if (iovcnt == 0) | ||
107 | break; | ||
108 | /* update pointer in partially complete iov */ | ||
109 | iov[0].iov_base = ((char *)iov[0].iov_base) + rem; | ||
110 | iov[0].iov_len -= rem; | ||
111 | } | ||
112 | } | ||
113 | return pos; | ||
114 | } | ||
diff --git a/atomicio.h b/atomicio.h index ddb63ee85..03bf065e2 100644 --- a/atomicio.h +++ b/atomicio.h | |||
@@ -1,6 +1,7 @@ | |||
1 | /* $OpenBSD: atomicio.h,v 1.7 2006/03/25 22:22:42 djm Exp $ */ | 1 | /* $OpenBSD: atomicio.h,v 1.8 2006/04/16 00:52:55 djm Exp $ */ |
2 | 2 | ||
3 | /* | 3 | /* |
4 | * Copyright (c) 2006 Damien Miller. All rights reserved. | ||
4 | * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved. | 5 | * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved. |
5 | * All rights reserved. | 6 | * All rights reserved. |
6 | * | 7 | * |
@@ -25,9 +26,24 @@ | |||
25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | */ | 27 | */ |
27 | 28 | ||
29 | #ifndef _ATOMICIO_H | ||
30 | #define _ATOMICIO_H | ||
31 | |||
32 | #include <sys/types.h> | ||
33 | #include <unistd.h> | ||
34 | #include <sys/uio.h> | ||
35 | |||
28 | /* | 36 | /* |
29 | * Ensure all of data on socket comes through. f==read || f==vwrite | 37 | * Ensure all of data on socket comes through. f==read || f==vwrite |
30 | */ | 38 | */ |
31 | size_t atomicio(ssize_t (*)(int, void *, size_t), int, void *, size_t); | 39 | size_t atomicio(ssize_t (*)(int, void *, size_t), int, void *, size_t); |
32 | 40 | ||
33 | #define vwrite (ssize_t (*)(int, void *, size_t))write | 41 | #define vwrite (ssize_t (*)(int, void *, size_t))write |
42 | |||
43 | /* | ||
44 | * ensure all of data on socket comes through. f==readv || f==writev | ||
45 | */ | ||
46 | size_t atomiciov(ssize_t (*)(int, const struct iovec *, int), | ||
47 | int, const struct iovec *, int); | ||
48 | |||
49 | #endif /* _ATOMICIO_H */ | ||