1
by brian
clean slate |
1 |
/* Copyright (C) 2000 MySQL AB
|
2 |
||
3 |
This program is free software; you can redistribute it and/or modify
|
|
4 |
it under the terms of the GNU General Public License as published by
|
|
5 |
the Free Software Foundation; version 2 of the License.
|
|
6 |
||
7 |
This program is distributed in the hope that it will be useful,
|
|
8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
GNU General Public License for more details.
|
|
11 |
||
12 |
You should have received a copy of the GNU General Public License
|
|
13 |
along with this program; if not, write to the Free Software
|
|
14 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
15 |
||
16 |
/*
|
|
17 |
Note that we can't have assertion on file descriptors; The reason for
|
|
18 |
this is that during mysql shutdown, another thread can close a file
|
|
19 |
we are working on. In this case we should just return read errors from
|
|
20 |
the file descriptior.
|
|
21 |
*/
|
|
22 |
||
575.2.2
by Monty Taylor
Moved vio stuff into libdrizzle. |
23 |
#define DONT_MAP_VIO
|
24 |
#include <drizzled/global.h> |
|
25 |
#include <libdrizzle/vio.h> |
|
268
by Brian Aker
Merging up a bunch of assert() and cleanup of my_sock typedef |
26 |
#include <sys/socket.h> |
492.1.7
by Monty Taylor
Moved test() to its own file. |
27 |
#include <drizzled/util/test.h> |
575.2.2
by Monty Taylor
Moved vio stuff into libdrizzle. |
28 |
#include <string.h> |
29 |
||
30 |
#include <netinet/tcp.h> |
|
31 |
#include <sys/poll.h> |
|
32 |
||
33 |
#include <netdb.h> |
|
1
by brian
clean slate |
34 |
|
35 |
int vio_errno(Vio *vio __attribute__((unused))) |
|
36 |
{
|
|
429
by Monty Taylor
Removed SOCKET redefines. |
37 |
return errno; |
1
by brian
clean slate |
38 |
}
|
39 |
||
40 |
||
481
by Brian Aker
Remove all of uchar. |
41 |
size_t vio_read(Vio * vio, unsigned char* buf, size_t size) |
1
by brian
clean slate |
42 |
{
|
43 |
size_t r; |
|
44 |
||
45 |
/* Ensure nobody uses vio_read_buff and vio_read simultaneously */
|
|
127
by brian
Removed DBUG statements from VIO |
46 |
assert(vio->read_end == vio->read_pos); |
172
by Brian Aker
First pass of cleanup |
47 |
r= read(vio->sd, buf, size); |
48 |
||
127
by brian
Removed DBUG statements from VIO |
49 |
return r; |
1
by brian
clean slate |
50 |
}
|
51 |
||
52 |
||
53 |
/*
|
|
54 |
Buffered read: if average read size is small it may
|
|
55 |
reduce number of syscalls.
|
|
56 |
*/
|
|
57 |
||
481
by Brian Aker
Remove all of uchar. |
58 |
size_t vio_read_buff(Vio *vio, unsigned char* buf, size_t size) |
1
by brian
clean slate |
59 |
{
|
60 |
size_t rc; |
|
61 |
#define VIO_UNBUFFERED_READ_MIN_SIZE 2048
|
|
62 |
||
63 |
if (vio->read_pos < vio->read_end) |
|
64 |
{
|
|
398.1.4
by Monty Taylor
Renamed max/min. |
65 |
rc= cmin((size_t) (vio->read_end - vio->read_pos), size); |
1
by brian
clean slate |
66 |
memcpy(buf, vio->read_pos, rc); |
67 |
vio->read_pos+= rc; |
|
68 |
/*
|
|
69 |
Do not try to read from the socket now even if rc < size:
|
|
70 |
vio_read can return -1 due to an error or non-blocking mode, and
|
|
71 |
the safest way to handle it is to move to a separate branch.
|
|
72 |
*/
|
|
73 |
}
|
|
74 |
else if (size < VIO_UNBUFFERED_READ_MIN_SIZE) |
|
75 |
{
|
|
481
by Brian Aker
Remove all of uchar. |
76 |
rc= vio_read(vio, (unsigned char*) vio->read_buffer, VIO_READ_BUFFER_SIZE); |
1
by brian
clean slate |
77 |
if (rc != 0 && rc != (size_t) -1) |
78 |
{
|
|
79 |
if (rc > size) |
|
80 |
{
|
|
81 |
vio->read_pos= vio->read_buffer + size; |
|
82 |
vio->read_end= vio->read_buffer + rc; |
|
83 |
rc= size; |
|
84 |
}
|
|
85 |
memcpy(buf, vio->read_buffer, rc); |
|
86 |
}
|
|
87 |
}
|
|
88 |
else
|
|
89 |
rc= vio_read(vio, buf, size); |
|
127
by brian
Removed DBUG statements from VIO |
90 |
|
91 |
return rc; |
|
1
by brian
clean slate |
92 |
#undef VIO_UNBUFFERED_READ_MIN_SIZE
|
93 |
}
|
|
94 |
||
95 |
||
481
by Brian Aker
Remove all of uchar. |
96 |
size_t vio_write(Vio * vio, const unsigned char* buf, size_t size) |
1
by brian
clean slate |
97 |
{
|
98 |
size_t r; |
|
127
by brian
Removed DBUG statements from VIO |
99 |
|
1
by brian
clean slate |
100 |
r = write(vio->sd, buf, size); |
127
by brian
Removed DBUG statements from VIO |
101 |
|
102 |
return r; |
|
1
by brian
clean slate |
103 |
}
|
104 |
||
173
by Brian Aker
Removed thd alarm around socket, we now use timeouts. |
105 |
int vio_blocking(Vio * vio, bool set_blocking_mode, bool *old_mode) |
1
by brian
clean slate |
106 |
{
|
107 |
int r=0; |
|
108 |
||
109 |
*old_mode= test(!(vio->fcntl_mode & O_NONBLOCK)); |
|
110 |
||
111 |
if (vio->sd >= 0) |
|
112 |
{
|
|
113 |
int old_fcntl=vio->fcntl_mode; |
|
114 |
if (set_blocking_mode) |
|
115 |
vio->fcntl_mode &= ~O_NONBLOCK; /* clear bit */ |
|
116 |
else
|
|
117 |
vio->fcntl_mode |= O_NONBLOCK; /* set bit */ |
|
118 |
if (old_fcntl != vio->fcntl_mode) |
|
119 |
{
|
|
120 |
r= fcntl(vio->sd, F_SETFL, vio->fcntl_mode); |
|
121 |
if (r == -1) |
|
122 |
{
|
|
123 |
vio->fcntl_mode= old_fcntl; |
|
124 |
}
|
|
125 |
}
|
|
126 |
}
|
|
172
by Brian Aker
First pass of cleanup |
127 |
|
127
by brian
Removed DBUG statements from VIO |
128 |
return r; |
1
by brian
clean slate |
129 |
}
|
130 |
||
172
by Brian Aker
First pass of cleanup |
131 |
bool
|
1
by brian
clean slate |
132 |
vio_is_blocking(Vio * vio) |
133 |
{
|
|
172
by Brian Aker
First pass of cleanup |
134 |
bool r; |
1
by brian
clean slate |
135 |
r = !(vio->fcntl_mode & O_NONBLOCK); |
127
by brian
Removed DBUG statements from VIO |
136 |
|
137 |
return r; |
|
1
by brian
clean slate |
138 |
}
|
139 |
||
140 |
||
141 |
int vio_fastsend(Vio * vio __attribute__((unused))) |
|
142 |
{
|
|
172
by Brian Aker
First pass of cleanup |
143 |
int nodelay = 1; |
267
by Brian Aker
More error control around socket calls. |
144 |
int error; |
145 |
||
146 |
error= setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY, |
|
147 |
&nodelay, sizeof(nodelay)); |
|
148 |
if (error != 0) |
|
149 |
{
|
|
150 |
perror("setsockopt"); |
|
151 |
assert(error == 0); |
|
152 |
}
|
|
153 |
||
154 |
return error; |
|
1
by brian
clean slate |
155 |
}
|
156 |
||
172
by Brian Aker
First pass of cleanup |
157 |
int32_t vio_keepalive(Vio* vio, bool set_keep_alive) |
1
by brian
clean slate |
158 |
{
|
127
by brian
Removed DBUG statements from VIO |
159 |
int r= 0; |
172
by Brian Aker
First pass of cleanup |
160 |
uint32_t opt= 0; |
161 |
||
162 |
if (set_keep_alive) |
|
163 |
opt= 1; |
|
164 |
||
165 |
r= setsockopt(vio->sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof(opt)); |
|
267
by Brian Aker
More error control around socket calls. |
166 |
if (r != 0) |
167 |
{
|
|
168 |
perror("setsockopt"); |
|
169 |
assert(r == 0); |
|
170 |
}
|
|
127
by brian
Removed DBUG statements from VIO |
171 |
|
172 |
return r; |
|
1
by brian
clean slate |
173 |
}
|
174 |
||
175 |
||
172
by Brian Aker
First pass of cleanup |
176 |
bool
|
1
by brian
clean slate |
177 |
vio_should_retry(Vio * vio __attribute__((unused))) |
178 |
{
|
|
432
by Monty Taylor
Missed two socket_errno earlier. |
179 |
int en = errno; |
429
by Monty Taylor
Removed SOCKET redefines. |
180 |
return (en == EAGAIN || en == EINTR || |
181 |
en == EWOULDBLOCK); |
|
1
by brian
clean slate |
182 |
}
|
183 |
||
184 |
||
172
by Brian Aker
First pass of cleanup |
185 |
bool
|
1
by brian
clean slate |
186 |
vio_was_interrupted(Vio *vio __attribute__((unused))) |
187 |
{
|
|
432
by Monty Taylor
Missed two socket_errno earlier. |
188 |
int en= errno; |
429
by Monty Taylor
Removed SOCKET redefines. |
189 |
return (en == EAGAIN || en == EINTR || |
190 |
en == EWOULDBLOCK || en == ETIMEDOUT); |
|
1
by brian
clean slate |
191 |
}
|
192 |
||
193 |
||
194 |
int vio_close(Vio * vio) |
|
195 |
{
|
|
196 |
int r=0; |
|
197 |
if (vio->type != VIO_CLOSED) |
|
198 |
{
|
|
127
by brian
Removed DBUG statements from VIO |
199 |
assert(vio->sd >= 0); |
1
by brian
clean slate |
200 |
if (shutdown(vio->sd, SHUT_RDWR)) |
201 |
r= -1; |
|
293
by Brian Aker
Merge + closesocket() fix |
202 |
if (close(vio->sd)) |
1
by brian
clean slate |
203 |
r= -1; |
204 |
}
|
|
205 |
vio->type= VIO_CLOSED; |
|
206 |
vio->sd= -1; |
|
127
by brian
Removed DBUG statements from VIO |
207 |
|
208 |
return r; |
|
1
by brian
clean slate |
209 |
}
|
210 |
||
211 |
||
212 |
const char *vio_description(Vio * vio) |
|
213 |
{
|
|
214 |
return vio->desc; |
|
215 |
}
|
|
216 |
||
217 |
enum enum_vio_type vio_type(Vio* vio) |
|
218 |
{
|
|
219 |
return vio->type; |
|
220 |
}
|
|
221 |
||
268
by Brian Aker
Merging up a bunch of assert() and cleanup of my_sock typedef |
222 |
int vio_fd(Vio* vio) |
1
by brian
clean slate |
223 |
{
|
224 |
return vio->sd; |
|
225 |
}
|
|
226 |
||
172
by Brian Aker
First pass of cleanup |
227 |
bool vio_peer_addr(Vio *vio, char *buf, uint16_t *port, size_t buflen) |
1
by brian
clean slate |
228 |
{
|
172
by Brian Aker
First pass of cleanup |
229 |
int error; |
230 |
char port_buf[NI_MAXSERV]; |
|
212.5.29
by Monty Taylor
Changed to work around ubuntu 32-bit. |
231 |
socklen_t addrLen = sizeof(vio->remote); |
172
by Brian Aker
First pass of cleanup |
232 |
|
233 |
if (getpeername(vio->sd, (struct sockaddr *) (&vio->remote), |
|
234 |
&addrLen) != 0) |
|
235 |
{
|
|
236 |
return true; |
|
237 |
}
|
|
238 |
vio->addrLen= (int)addrLen; |
|
239 |
||
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
240 |
if ((error= getnameinfo((struct sockaddr *)(&vio->remote), |
172
by Brian Aker
First pass of cleanup |
241 |
addrLen, |
242 |
buf, buflen, |
|
243 |
port_buf, NI_MAXSERV, NI_NUMERICHOST|NI_NUMERICSERV))) |
|
244 |
{
|
|
245 |
return true; |
|
246 |
}
|
|
247 |
||
248 |
*port= (uint16_t)strtol(port_buf, (char **)NULL, 10); |
|
127
by brian
Removed DBUG statements from VIO |
249 |
|
250 |
return false; |
|
1
by brian
clean slate |
251 |
}
|
252 |
||
253 |
||
254 |
/* Return 0 if there is data to be read */
|
|
255 |
||
268
by Brian Aker
Merging up a bunch of assert() and cleanup of my_sock typedef |
256 |
bool vio_poll_read(Vio *vio, int32_t timeout) |
1
by brian
clean slate |
257 |
{
|
258 |
struct pollfd fds; |
|
259 |
int res; |
|
127
by brian
Removed DBUG statements from VIO |
260 |
|
1
by brian
clean slate |
261 |
fds.fd=vio->sd; |
262 |
fds.events=POLLIN; |
|
263 |
fds.revents=0; |
|
264 |
if ((res=poll(&fds,1,(int) timeout*1000)) <= 0) |
|
265 |
{
|
|
127
by brian
Removed DBUG statements from VIO |
266 |
return res < 0 ? false : true; /* Don't return 1 on errors */ |
1
by brian
clean slate |
267 |
}
|
127
by brian
Removed DBUG statements from VIO |
268 |
return (fds.revents & (POLLIN | POLLERR | POLLHUP) ? false : true); |
1
by brian
clean slate |
269 |
}
|
270 |
||
271 |
||
172
by Brian Aker
First pass of cleanup |
272 |
bool vio_peek_read(Vio *vio, uint32_t *bytes) |
1
by brian
clean slate |
273 |
{
|
274 |
char buf[1024]; |
|
275 |
ssize_t res= recv(vio->sd, &buf, sizeof(buf), MSG_PEEK); |
|
172
by Brian Aker
First pass of cleanup |
276 |
|
1
by brian
clean slate |
277 |
if (res < 0) |
163
by Brian Aker
Merge Monty's code. |
278 |
return true; |
172
by Brian Aker
First pass of cleanup |
279 |
*bytes= (uint32_t)res; |
163
by Brian Aker
Merge Monty's code. |
280 |
return false; |
1
by brian
clean slate |
281 |
}
|
282 |
||
268
by Brian Aker
Merging up a bunch of assert() and cleanup of my_sock typedef |
283 |
void vio_timeout(Vio *vio, bool is_sndtimeo, int32_t timeout) |
1
by brian
clean slate |
284 |
{
|
268
by Brian Aker
Merging up a bunch of assert() and cleanup of my_sock typedef |
285 |
int error; |
1
by brian
clean slate |
286 |
|
287 |
/* POSIX specifies time as struct timeval. */
|
|
288 |
struct timeval wait_timeout; |
|
289 |
wait_timeout.tv_sec= timeout; |
|
290 |
wait_timeout.tv_usec= 0; |
|
291 |
||
268
by Brian Aker
Merging up a bunch of assert() and cleanup of my_sock typedef |
292 |
assert(timeout >= 0 && timeout <= INT32_MAX); |
293 |
assert(vio->sd != -1); |
|
294 |
error= setsockopt(vio->sd, SOL_SOCKET, is_sndtimeo ? SO_SNDTIMEO : SO_RCVTIMEO, |
|
295 |
&wait_timeout, |
|
296 |
(socklen_t)sizeof(struct timeval)); |
|
297 |
if (error != 0) |
|
267
by Brian Aker
More error control around socket calls. |
298 |
{
|
299 |
perror("setsockopt"); |
|
268
by Brian Aker
Merging up a bunch of assert() and cleanup of my_sock typedef |
300 |
assert(error == 0); |
267
by Brian Aker
More error control around socket calls. |
301 |
}
|
1
by brian
clean slate |
302 |
}
|