| @@ -1,156 +1,160 @@ | | | @@ -1,156 +1,160 @@ |
1 | /* $NetBSD: atomicio.c,v 1.3 2011/07/25 03:03:10 christos Exp $ */ | | 1 | /* $NetBSD: atomicio.c,v 1.4 2011/08/01 15:55:00 christos Exp $ */ |
2 | /* $OpenBSD: atomicio.c,v 1.26 2010/09/22 22:58:51 djm Exp $ */ | | 2 | /* $OpenBSD: atomicio.c,v 1.26 2010/09/22 22:58:51 djm Exp $ */ |
3 | /* | | 3 | /* |
4 | * Copyright (c) 2006 Damien Miller. All rights reserved. | | 4 | * Copyright (c) 2006 Damien Miller. All rights reserved. |
5 | * Copyright (c) 2005 Anil Madhavapeddy. All rights reserved. | | 5 | * Copyright (c) 2005 Anil Madhavapeddy. All rights reserved. |
6 | * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved. | | 6 | * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved. |
7 | * All rights reserved. | | 7 | * All rights reserved. |
8 | * | | 8 | * |
9 | * Redistribution and use in source and binary forms, with or without | | 9 | * Redistribution and use in source and binary forms, with or without |
10 | * modification, are permitted provided that the following conditions | | 10 | * modification, are permitted provided that the following conditions |
11 | * are met: | | 11 | * are met: |
12 | * 1. Redistributions of source code must retain the above copyright | | 12 | * 1. Redistributions of source code must retain the above copyright |
13 | * notice, this list of conditions and the following disclaimer. | | 13 | * notice, this list of conditions and the following disclaimer. |
14 | * 2. Redistributions in binary form must reproduce the above copyright | | 14 | * 2. Redistributions in binary form must reproduce the above copyright |
15 | * notice, this list of conditions and the following disclaimer in the | | 15 | * notice, this list of conditions and the following disclaimer in the |
16 | * documentation and/or other materials provided with the distribution. | | 16 | * documentation and/or other materials provided with the distribution. |
17 | * | | 17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | | 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | | 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | | 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | | 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | | 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | | 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | | 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | | 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | | 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | | 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ | | 28 | */ |
29 | | | 29 | |
30 | #include "includes.h" | | 30 | #include "includes.h" |
31 | __RCSID("$NetBSD: atomicio.c,v 1.3 2011/07/25 03:03:10 christos Exp $"); | | 31 | __RCSID("$NetBSD: atomicio.c,v 1.4 2011/08/01 15:55:00 christos Exp $"); |
32 | #include <sys/param.h> | | 32 | #include <sys/param.h> |
33 | #include <sys/uio.h> | | 33 | #include <sys/uio.h> |
34 | | | 34 | |
35 | #include <errno.h> | | 35 | #include <errno.h> |
36 | #include <poll.h> | | 36 | #include <poll.h> |
37 | #include <string.h> | | 37 | #include <string.h> |
38 | #include <unistd.h> | | 38 | #include <unistd.h> |
39 | | | 39 | |
40 | #include "atomicio.h" | | 40 | #include "atomicio.h" |
41 | | | 41 | |
42 | /* | | 42 | /* |
43 | * ensure all of data on socket comes through. f==read || f==vwrite | | 43 | * ensure all of data on socket comes through. f==read || f==vwrite |
44 | */ | | 44 | */ |
45 | size_t | | 45 | size_t |
46 | atomicio6(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n, | | 46 | atomicio6(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n, |
47 | int (*cb)(void *, size_t), void *cb_arg) | | 47 | int (*cb)(void *, size_t), void *cb_arg) |
48 | { | | 48 | { |
49 | char *s = _s; | | 49 | char *s = _s; |
50 | size_t pos = 0; | | 50 | size_t pos = 0; |
51 | ssize_t res; | | 51 | ssize_t res; |
52 | struct pollfd pfd; | | 52 | struct pollfd pfd; |
53 | | | 53 | |
54 | pfd.fd = fd; | | 54 | pfd.fd = fd; |
55 | pfd.events = f == read ? POLLIN : POLLOUT; | | 55 | /* |
| | | 56 | * check for vwrite instead of read to avoid read being renamed |
| | | 57 | * by SSP issues |
| | | 58 | */ |
| | | 59 | pfd.events = f == vwrite ? POLLOUT : POLLIN; |
56 | while (n > pos) { | | 60 | while (n > pos) { |
57 | res = (f) (fd, s + pos, n - pos); | | 61 | res = (f) (fd, s + pos, n - pos); |
58 | switch (res) { | | 62 | switch (res) { |
59 | case -1: | | 63 | case -1: |
60 | if (errno == EINTR) | | 64 | if (errno == EINTR) |
61 | continue; | | 65 | continue; |
62 | if (errno == EAGAIN) { | | 66 | if (errno == EAGAIN) { |
63 | (void)poll(&pfd, 1, -1); | | 67 | (void)poll(&pfd, 1, -1); |
64 | continue; | | 68 | continue; |
65 | } | | 69 | } |
66 | return 0; | | 70 | return 0; |
67 | case 0: | | 71 | case 0: |
68 | errno = EPIPE; | | 72 | errno = EPIPE; |
69 | return pos; | | 73 | return pos; |
70 | default: | | 74 | default: |
71 | pos += (size_t)res; | | 75 | pos += (size_t)res; |
72 | if (cb != NULL && cb(cb_arg, (size_t)res) == -1) { | | 76 | if (cb != NULL && cb(cb_arg, (size_t)res) == -1) { |
73 | errno = EINTR; | | 77 | errno = EINTR; |
74 | return pos; | | 78 | return pos; |
75 | } | | 79 | } |
76 | } | | 80 | } |
77 | } | | 81 | } |
78 | return pos; | | 82 | return pos; |
79 | } | | 83 | } |
80 | | | 84 | |
81 | size_t | | 85 | size_t |
82 | atomicio(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n) | | 86 | atomicio(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n) |
83 | { | | 87 | { |
84 | return atomicio6(f, fd, _s, n, NULL, NULL); | | 88 | return atomicio6(f, fd, _s, n, NULL, NULL); |
85 | } | | 89 | } |
86 | | | 90 | |
87 | /* | | 91 | /* |
88 | * ensure all of data on socket comes through. f==readv || f==writev | | 92 | * ensure all of data on socket comes through. f==readv || f==writev |
89 | */ | | 93 | */ |
90 | size_t | | 94 | size_t |
91 | atomiciov6(ssize_t (*f) (int, const struct iovec *, int), int fd, | | 95 | atomiciov6(ssize_t (*f) (int, const struct iovec *, int), int fd, |
92 | const struct iovec *_iov, int iovcnt, | | 96 | const struct iovec *_iov, int iovcnt, |
93 | int (*cb)(void *, size_t), void *cb_arg) | | 97 | int (*cb)(void *, size_t), void *cb_arg) |
94 | { | | 98 | { |
95 | size_t pos = 0, rem; | | 99 | size_t pos = 0, rem; |
96 | ssize_t res; | | 100 | ssize_t res; |
97 | struct iovec iov_array[IOV_MAX], *iov = iov_array; | | 101 | struct iovec iov_array[IOV_MAX], *iov = iov_array; |
98 | struct pollfd pfd; | | 102 | struct pollfd pfd; |
99 | | | 103 | |
100 | if (iovcnt > IOV_MAX) { | | 104 | if (iovcnt > IOV_MAX) { |
101 | errno = EINVAL; | | 105 | errno = EINVAL; |
102 | return 0; | | 106 | return 0; |
103 | } | | 107 | } |
104 | /* Make a copy of the iov array because we may modify it below */ | | 108 | /* Make a copy of the iov array because we may modify it below */ |
105 | memcpy(iov, _iov, iovcnt * sizeof(*_iov)); | | 109 | memcpy(iov, _iov, iovcnt * sizeof(*_iov)); |
106 | | | 110 | |
107 | pfd.fd = fd; | | 111 | pfd.fd = fd; |
108 | pfd.events = f == readv ? POLLIN : POLLOUT; | | 112 | pfd.events = f == readv ? POLLIN : POLLOUT; |
109 | for (; iovcnt > 0 && iov[0].iov_len > 0;) { | | 113 | for (; iovcnt > 0 && iov[0].iov_len > 0;) { |
110 | res = (f) (fd, iov, iovcnt); | | 114 | res = (f) (fd, iov, iovcnt); |
111 | switch (res) { | | 115 | switch (res) { |
112 | case -1: | | 116 | case -1: |
113 | if (errno == EINTR) | | 117 | if (errno == EINTR) |
114 | continue; | | 118 | continue; |
115 | if (errno == EAGAIN) { | | 119 | if (errno == EAGAIN) { |
116 | (void)poll(&pfd, 1, -1); | | 120 | (void)poll(&pfd, 1, -1); |
117 | continue; | | 121 | continue; |
118 | } | | 122 | } |
119 | return 0; | | 123 | return 0; |
120 | case 0: | | 124 | case 0: |
121 | errno = EPIPE; | | 125 | errno = EPIPE; |
122 | return pos; | | 126 | return pos; |
123 | default: | | 127 | default: |
124 | rem = (size_t)res; | | 128 | rem = (size_t)res; |
125 | pos += rem; | | 129 | pos += rem; |
126 | /* skip completed iov entries */ | | 130 | /* skip completed iov entries */ |
127 | while (iovcnt > 0 && rem >= iov[0].iov_len) { | | 131 | while (iovcnt > 0 && rem >= iov[0].iov_len) { |
128 | rem -= iov[0].iov_len; | | 132 | rem -= iov[0].iov_len; |
129 | iov++; | | 133 | iov++; |
130 | iovcnt--; | | 134 | iovcnt--; |
131 | } | | 135 | } |
132 | /* This shouldn't happen... */ | | 136 | /* This shouldn't happen... */ |
133 | if (rem > 0 && (iovcnt <= 0 || rem > iov[0].iov_len)) { | | 137 | if (rem > 0 && (iovcnt <= 0 || rem > iov[0].iov_len)) { |
134 | errno = EFAULT; | | 138 | errno = EFAULT; |
135 | return 0; | | 139 | return 0; |
136 | } | | 140 | } |
137 | if (iovcnt == 0) | | 141 | if (iovcnt == 0) |
138 | break; | | 142 | break; |
139 | /* update pointer in partially complete iov */ | | 143 | /* update pointer in partially complete iov */ |
140 | iov[0].iov_base = ((char *)iov[0].iov_base) + rem; | | 144 | iov[0].iov_base = ((char *)iov[0].iov_base) + rem; |
141 | iov[0].iov_len -= rem; | | 145 | iov[0].iov_len -= rem; |
142 | } | | 146 | } |
143 | if (cb != NULL && cb(cb_arg, (size_t)res) == -1) { | | 147 | if (cb != NULL && cb(cb_arg, (size_t)res) == -1) { |
144 | errno = EINTR; | | 148 | errno = EINTR; |
145 | return pos; | | 149 | return pos; |
146 | } | | 150 | } |
147 | } | | 151 | } |
148 | return pos; | | 152 | return pos; |
149 | } | | 153 | } |
150 | | | 154 | |
151 | size_t | | 155 | size_t |
152 | atomiciov(ssize_t (*f) (int, const struct iovec *, int), int fd, | | 156 | atomiciov(ssize_t (*f) (int, const struct iovec *, int), int fd, |
153 | const struct iovec *_iov, int iovcnt) | | 157 | const struct iovec *_iov, int iovcnt) |
154 | { | | 158 | { |
155 | return atomiciov6(f, fd, _iov, iovcnt, NULL, NULL); | | 159 | return atomiciov6(f, fd, _iov, iovcnt, NULL, NULL); |
156 | } | | 160 | } |