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 |
||
23 |
#include "vio_priv.h" |
|
24 |
||
25 |
int vio_errno(Vio *vio __attribute__((unused))) |
|
26 |
{
|
|
27 |
return socket_errno; /* On Win32 this mapped to WSAGetLastError() */ |
|
28 |
}
|
|
29 |
||
30 |
||
31 |
size_t vio_read(Vio * vio, uchar* buf, size_t size) |
|
32 |
{
|
|
33 |
size_t r; |
|
34 |
DBUG_ENTER("vio_read"); |
|
35 |
DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio->sd, (long) buf, |
|
36 |
(uint) size)); |
|
37 |
||
38 |
/* Ensure nobody uses vio_read_buff and vio_read simultaneously */
|
|
39 |
DBUG_ASSERT(vio->read_end == vio->read_pos); |
|
40 |
errno=0; /* For linux */ |
|
41 |
r = read(vio->sd, buf, size); |
|
42 |
#ifndef DBUG_OFF
|
|
43 |
if (r == (size_t) -1) |
|
44 |
{
|
|
45 |
DBUG_PRINT("vio_error", ("Got error %d during read",errno)); |
|
46 |
}
|
|
47 |
#endif /* DBUG_OFF */ |
|
48 |
DBUG_PRINT("exit", ("%ld", (long) r)); |
|
49 |
DBUG_RETURN(r); |
|
50 |
}
|
|
51 |
||
52 |
||
53 |
/*
|
|
54 |
Buffered read: if average read size is small it may
|
|
55 |
reduce number of syscalls.
|
|
56 |
*/
|
|
57 |
||
58 |
size_t vio_read_buff(Vio *vio, uchar* buf, size_t size) |
|
59 |
{
|
|
60 |
size_t rc; |
|
61 |
#define VIO_UNBUFFERED_READ_MIN_SIZE 2048
|
|
62 |
DBUG_ENTER("vio_read_buff"); |
|
63 |
DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio->sd, (long) buf, |
|
64 |
(uint) size)); |
|
65 |
||
66 |
if (vio->read_pos < vio->read_end) |
|
67 |
{
|
|
68 |
rc= min((size_t) (vio->read_end - vio->read_pos), size); |
|
69 |
memcpy(buf, vio->read_pos, rc); |
|
70 |
vio->read_pos+= rc; |
|
71 |
/*
|
|
72 |
Do not try to read from the socket now even if rc < size:
|
|
73 |
vio_read can return -1 due to an error or non-blocking mode, and
|
|
74 |
the safest way to handle it is to move to a separate branch.
|
|
75 |
*/
|
|
76 |
}
|
|
77 |
else if (size < VIO_UNBUFFERED_READ_MIN_SIZE) |
|
78 |
{
|
|
79 |
rc= vio_read(vio, (uchar*) vio->read_buffer, VIO_READ_BUFFER_SIZE); |
|
80 |
if (rc != 0 && rc != (size_t) -1) |
|
81 |
{
|
|
82 |
if (rc > size) |
|
83 |
{
|
|
84 |
vio->read_pos= vio->read_buffer + size; |
|
85 |
vio->read_end= vio->read_buffer + rc; |
|
86 |
rc= size; |
|
87 |
}
|
|
88 |
memcpy(buf, vio->read_buffer, rc); |
|
89 |
}
|
|
90 |
}
|
|
91 |
else
|
|
92 |
rc= vio_read(vio, buf, size); |
|
93 |
DBUG_RETURN(rc); |
|
94 |
#undef VIO_UNBUFFERED_READ_MIN_SIZE
|
|
95 |
}
|
|
96 |
||
97 |
||
98 |
size_t vio_write(Vio * vio, const uchar* buf, size_t size) |
|
99 |
{
|
|
100 |
size_t r; |
|
101 |
DBUG_ENTER("vio_write"); |
|
102 |
DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio->sd, (long) buf, |
|
103 |
(uint) size)); |
|
104 |
r = write(vio->sd, buf, size); |
|
105 |
#ifndef DBUG_OFF
|
|
106 |
if (r == (size_t) -1) |
|
107 |
{
|
|
108 |
DBUG_PRINT("vio_error", ("Got error on write: %d",socket_errno)); |
|
109 |
}
|
|
110 |
#endif /* DBUG_OFF */ |
|
111 |
DBUG_PRINT("exit", ("%u", (uint) r)); |
|
112 |
DBUG_RETURN(r); |
|
113 |
}
|
|
114 |
||
115 |
int vio_blocking(Vio * vio __attribute__((unused)), my_bool set_blocking_mode, |
|
116 |
my_bool *old_mode) |
|
117 |
{
|
|
118 |
int r=0; |
|
119 |
DBUG_ENTER("vio_blocking"); |
|
120 |
||
121 |
*old_mode= test(!(vio->fcntl_mode & O_NONBLOCK)); |
|
122 |
DBUG_PRINT("enter", ("set_blocking_mode: %d old_mode: %d", |
|
123 |
(int) set_blocking_mode, (int) *old_mode)); |
|
124 |
||
125 |
#if !defined(NO_FCNTL_NONBLOCK)
|
|
126 |
if (vio->sd >= 0) |
|
127 |
{
|
|
128 |
int old_fcntl=vio->fcntl_mode; |
|
129 |
if (set_blocking_mode) |
|
130 |
vio->fcntl_mode &= ~O_NONBLOCK; /* clear bit */ |
|
131 |
else
|
|
132 |
vio->fcntl_mode |= O_NONBLOCK; /* set bit */ |
|
133 |
if (old_fcntl != vio->fcntl_mode) |
|
134 |
{
|
|
135 |
r= fcntl(vio->sd, F_SETFL, vio->fcntl_mode); |
|
136 |
if (r == -1) |
|
137 |
{
|
|
138 |
DBUG_PRINT("info", ("fcntl failed, errno %d", errno)); |
|
139 |
vio->fcntl_mode= old_fcntl; |
|
140 |
}
|
|
141 |
}
|
|
142 |
}
|
|
143 |
#else
|
|
144 |
r= set_blocking_mode ? 0 : 1; |
|
145 |
#endif /* !defined(NO_FCNTL_NONBLOCK) */ |
|
146 |
DBUG_PRINT("exit", ("%d", r)); |
|
147 |
DBUG_RETURN(r); |
|
148 |
}
|
|
149 |
||
150 |
my_bool
|
|
151 |
vio_is_blocking(Vio * vio) |
|
152 |
{
|
|
153 |
my_bool r; |
|
154 |
DBUG_ENTER("vio_is_blocking"); |
|
155 |
r = !(vio->fcntl_mode & O_NONBLOCK); |
|
156 |
DBUG_PRINT("exit", ("%d", (int) r)); |
|
157 |
DBUG_RETURN(r); |
|
158 |
}
|
|
159 |
||
160 |
||
161 |
int vio_fastsend(Vio * vio __attribute__((unused))) |
|
162 |
{
|
|
163 |
int r=0; |
|
164 |
DBUG_ENTER("vio_fastsend"); |
|
165 |
||
166 |
#if defined(IPTOS_THROUGHPUT)
|
|
167 |
{
|
|
168 |
int tos = IPTOS_THROUGHPUT; |
|
169 |
r= setsockopt(vio->sd, IPPROTO_IP, IP_TOS, (void *) &tos, sizeof(tos)); |
|
170 |
}
|
|
171 |
#endif /* IPTOS_THROUGHPUT */ |
|
172 |
if (!r) |
|
173 |
{
|
|
174 |
int nodelay = 1; |
|
175 |
||
176 |
r= setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY, |
|
177 |
IF_WIN(const char*, void*) &nodelay, |
|
178 |
sizeof(nodelay)); |
|
179 |
||
180 |
}
|
|
181 |
if (r) |
|
182 |
{
|
|
183 |
DBUG_PRINT("warning", ("Couldn't set socket option for fast send")); |
|
184 |
r= -1; |
|
185 |
}
|
|
186 |
DBUG_PRINT("exit", ("%d", r)); |
|
187 |
DBUG_RETURN(r); |
|
188 |
}
|
|
189 |
||
190 |
int vio_keepalive(Vio* vio, my_bool set_keep_alive) |
|
191 |
{
|
|
192 |
int r=0; |
|
193 |
uint opt = 0; |
|
194 |
DBUG_ENTER("vio_keepalive"); |
|
195 |
DBUG_PRINT("enter", ("sd: %d set_keep_alive: %d", vio->sd, (int) |
|
196 |
set_keep_alive)); |
|
197 |
if (vio->type != VIO_TYPE_NAMEDPIPE) |
|
198 |
{
|
|
199 |
if (set_keep_alive) |
|
200 |
opt = 1; |
|
201 |
r = setsockopt(vio->sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, |
|
202 |
sizeof(opt)); |
|
203 |
}
|
|
204 |
DBUG_RETURN(r); |
|
205 |
}
|
|
206 |
||
207 |
||
208 |
my_bool
|
|
209 |
vio_should_retry(Vio * vio __attribute__((unused))) |
|
210 |
{
|
|
211 |
int en = socket_errno; |
|
212 |
return (en == SOCKET_EAGAIN || en == SOCKET_EINTR || |
|
213 |
en == SOCKET_EWOULDBLOCK); |
|
214 |
}
|
|
215 |
||
216 |
||
217 |
my_bool
|
|
218 |
vio_was_interrupted(Vio *vio __attribute__((unused))) |
|
219 |
{
|
|
220 |
int en= socket_errno; |
|
221 |
return (en == SOCKET_EAGAIN || en == SOCKET_EINTR || |
|
222 |
en == SOCKET_EWOULDBLOCK || en == SOCKET_ETIMEDOUT); |
|
223 |
}
|
|
224 |
||
225 |
||
226 |
int vio_close(Vio * vio) |
|
227 |
{
|
|
228 |
int r=0; |
|
229 |
DBUG_ENTER("vio_close"); |
|
230 |
if (vio->type != VIO_CLOSED) |
|
231 |
{
|
|
232 |
DBUG_ASSERT(vio->sd >= 0); |
|
233 |
if (shutdown(vio->sd, SHUT_RDWR)) |
|
234 |
r= -1; |
|
235 |
if (closesocket(vio->sd)) |
|
236 |
r= -1; |
|
237 |
}
|
|
238 |
if (r) |
|
239 |
{
|
|
240 |
DBUG_PRINT("vio_error", ("close() failed, error: %d",socket_errno)); |
|
241 |
/* FIXME: error handling (not critical for MySQL) */
|
|
242 |
}
|
|
243 |
vio->type= VIO_CLOSED; |
|
244 |
vio->sd= -1; |
|
245 |
DBUG_RETURN(r); |
|
246 |
}
|
|
247 |
||
248 |
||
249 |
const char *vio_description(Vio * vio) |
|
250 |
{
|
|
251 |
return vio->desc; |
|
252 |
}
|
|
253 |
||
254 |
enum enum_vio_type vio_type(Vio* vio) |
|
255 |
{
|
|
256 |
return vio->type; |
|
257 |
}
|
|
258 |
||
259 |
my_socket vio_fd(Vio* vio) |
|
260 |
{
|
|
261 |
return vio->sd; |
|
262 |
}
|
|
263 |
||
264 |
my_bool vio_peer_addr(Vio *vio, char *buf, uint16 *port, size_t buflen) |
|
265 |
{
|
|
266 |
DBUG_ENTER("vio_peer_addr"); |
|
267 |
DBUG_PRINT("enter", ("sd: %d", vio->sd)); |
|
268 |
||
269 |
if (vio->localhost) |
|
270 |
{
|
|
271 |
strmov(buf, "127.0.0.1"); |
|
272 |
*port= 0; |
|
273 |
}
|
|
274 |
else
|
|
275 |
{
|
|
276 |
int error; |
|
277 |
char port_buf[NI_MAXSERV]; |
|
278 |
size_socket addrLen = sizeof(vio->remote); |
|
279 |
if (getpeername(vio->sd, (struct sockaddr *) (&vio->remote), |
|
280 |
&addrLen) != 0) |
|
281 |
{
|
|
282 |
DBUG_PRINT("exit", ("getpeername gave error: %d", socket_errno)); |
|
283 |
DBUG_RETURN(1); |
|
284 |
}
|
|
285 |
vio->addrLen= (int)addrLen; |
|
286 |
||
287 |
if ((error= getnameinfo((struct sockaddr *)(&vio->remote), |
|
288 |
addrLen, |
|
289 |
buf, buflen, |
|
290 |
port_buf, NI_MAXSERV, NI_NUMERICHOST|NI_NUMERICSERV))) |
|
291 |
{
|
|
292 |
DBUG_PRINT("exit", ("getnameinfo gave error: %s", |
|
293 |
gai_strerror(error))); |
|
294 |
DBUG_RETURN(1); |
|
295 |
}
|
|
296 |
||
297 |
*port= (uint16)strtol(port_buf, (char **)NULL, 10); |
|
298 |
||
299 |
/*
|
|
300 |
A lot of users do not have IPv6 loopback resolving to localhost
|
|
301 |
correctly setup. Should this exist? No. If we do not do it though
|
|
302 |
we will be getting a lot of support questions from users who
|
|
303 |
have bad setups. This code should be removed by say... 2012.
|
|
304 |
-Brian
|
|
305 |
*/
|
|
306 |
if (!memcmp(buf, "::ffff:127.0.0.1", sizeof("::ffff:127.0.0.1"))) |
|
307 |
strmov(buf, "127.0.0.1"); |
|
308 |
}
|
|
309 |
DBUG_PRINT("exit", ("addr: %s", buf)); |
|
310 |
DBUG_RETURN(0); |
|
311 |
}
|
|
312 |
||
313 |
||
314 |
/* Return 0 if there is data to be read */
|
|
315 |
||
316 |
my_bool vio_poll_read(Vio *vio,uint timeout) |
|
317 |
{
|
|
116
by Brian Aker
Reworking more of VIO |
318 |
#if defined(HAVE_POLL)
|
1
by brian
clean slate |
319 |
struct pollfd fds; |
320 |
int res; |
|
321 |
DBUG_ENTER("vio_poll"); |
|
322 |
fds.fd=vio->sd; |
|
323 |
fds.events=POLLIN; |
|
324 |
fds.revents=0; |
|
325 |
if ((res=poll(&fds,1,(int) timeout*1000)) <= 0) |
|
326 |
{
|
|
327 |
DBUG_RETURN(res < 0 ? 0 : 1); /* Don't return 1 on errors */ |
|
328 |
}
|
|
329 |
DBUG_RETURN(fds.revents & (POLLIN | POLLERR | POLLHUP) ? 0 : 1); |
|
330 |
#else
|
|
331 |
return 0; |
|
332 |
#endif
|
|
333 |
}
|
|
334 |
||
335 |
||
336 |
my_bool vio_peek_read(Vio *vio, uint *bytes) |
|
337 |
{
|
|
116
by Brian Aker
Reworking more of VIO |
338 |
#if FIONREAD_IN_SYS_IOCTL
|
1
by brian
clean slate |
339 |
int len; |
340 |
if (ioctl(vio->sd, FIONREAD, &len) < 0) |
|
341 |
return TRUE; |
|
342 |
*bytes= len; |
|
343 |
return FALSE; |
|
344 |
#else
|
|
345 |
char buf[1024]; |
|
346 |
ssize_t res= recv(vio->sd, &buf, sizeof(buf), MSG_PEEK); |
|
347 |
if (res < 0) |
|
348 |
return TRUE; |
|
349 |
*bytes= res; |
|
350 |
return FALSE; |
|
351 |
#endif
|
|
352 |
}
|
|
353 |
||
354 |
void vio_timeout(Vio *vio, uint which, uint timeout) |
|
355 |
{
|
|
356 |
#if defined(SO_SNDTIMEO) && defined(SO_RCVTIMEO)
|
|
357 |
int r; |
|
358 |
DBUG_ENTER("vio_timeout"); |
|
359 |
||
360 |
{
|
|
361 |
/* POSIX specifies time as struct timeval. */
|
|
362 |
struct timeval wait_timeout; |
|
363 |
wait_timeout.tv_sec= timeout; |
|
364 |
wait_timeout.tv_usec= 0; |
|
365 |
||
366 |
r= setsockopt(vio->sd, SOL_SOCKET, which ? SO_SNDTIMEO : SO_RCVTIMEO, |
|
367 |
IF_WIN(const char*, const void*)&wait_timeout, |
|
368 |
sizeof(wait_timeout)); |
|
369 |
||
370 |
}
|
|
371 |
||
372 |
#ifndef DBUG_OFF
|
|
373 |
if (r != 0) |
|
374 |
DBUG_PRINT("error", ("setsockopt failed: %d, errno: %d", r, socket_errno)); |
|
375 |
#endif
|
|
376 |
||
377 |
DBUG_VOID_RETURN; |
|
378 |
#else
|
|
379 |
/*
|
|
380 |
Platforms not suporting setting of socket timeout should either use
|
|
381 |
thr_alarm or just run without read/write timeout(s)
|
|
382 |
*/
|
|
383 |
#endif
|
|
384 |
}
|