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 |
||
35 |
/* Ensure nobody uses vio_read_buff and vio_read simultaneously */
|
|
127
by brian
Removed DBUG statements from VIO |
36 |
assert(vio->read_end == vio->read_pos); |
172
by Brian Aker
First pass of cleanup |
37 |
r= read(vio->sd, buf, size); |
38 |
||
127
by brian
Removed DBUG statements from VIO |
39 |
return r; |
1
by brian
clean slate |
40 |
}
|
41 |
||
42 |
||
43 |
/*
|
|
44 |
Buffered read: if average read size is small it may
|
|
45 |
reduce number of syscalls.
|
|
46 |
*/
|
|
47 |
||
48 |
size_t vio_read_buff(Vio *vio, uchar* buf, size_t size) |
|
49 |
{
|
|
50 |
size_t rc; |
|
51 |
#define VIO_UNBUFFERED_READ_MIN_SIZE 2048
|
|
52 |
||
53 |
if (vio->read_pos < vio->read_end) |
|
54 |
{
|
|
55 |
rc= min((size_t) (vio->read_end - vio->read_pos), size); |
|
56 |
memcpy(buf, vio->read_pos, rc); |
|
57 |
vio->read_pos+= rc; |
|
58 |
/*
|
|
59 |
Do not try to read from the socket now even if rc < size:
|
|
60 |
vio_read can return -1 due to an error or non-blocking mode, and
|
|
61 |
the safest way to handle it is to move to a separate branch.
|
|
62 |
*/
|
|
63 |
}
|
|
64 |
else if (size < VIO_UNBUFFERED_READ_MIN_SIZE) |
|
65 |
{
|
|
66 |
rc= vio_read(vio, (uchar*) vio->read_buffer, VIO_READ_BUFFER_SIZE); |
|
67 |
if (rc != 0 && rc != (size_t) -1) |
|
68 |
{
|
|
69 |
if (rc > size) |
|
70 |
{
|
|
71 |
vio->read_pos= vio->read_buffer + size; |
|
72 |
vio->read_end= vio->read_buffer + rc; |
|
73 |
rc= size; |
|
74 |
}
|
|
75 |
memcpy(buf, vio->read_buffer, rc); |
|
76 |
}
|
|
77 |
}
|
|
78 |
else
|
|
79 |
rc= vio_read(vio, buf, size); |
|
127
by brian
Removed DBUG statements from VIO |
80 |
|
81 |
return rc; |
|
1
by brian
clean slate |
82 |
#undef VIO_UNBUFFERED_READ_MIN_SIZE
|
83 |
}
|
|
84 |
||
85 |
||
86 |
size_t vio_write(Vio * vio, const uchar* buf, size_t size) |
|
87 |
{
|
|
88 |
size_t r; |
|
127
by brian
Removed DBUG statements from VIO |
89 |
|
1
by brian
clean slate |
90 |
r = write(vio->sd, buf, size); |
127
by brian
Removed DBUG statements from VIO |
91 |
|
92 |
return r; |
|
1
by brian
clean slate |
93 |
}
|
94 |
||
173
by Brian Aker
Removed thd alarm around socket, we now use timeouts. |
95 |
int vio_blocking(Vio * vio, bool set_blocking_mode, bool *old_mode) |
1
by brian
clean slate |
96 |
{
|
97 |
int r=0; |
|
98 |
||
99 |
*old_mode= test(!(vio->fcntl_mode & O_NONBLOCK)); |
|
100 |
||
101 |
if (vio->sd >= 0) |
|
102 |
{
|
|
103 |
int old_fcntl=vio->fcntl_mode; |
|
104 |
if (set_blocking_mode) |
|
105 |
vio->fcntl_mode &= ~O_NONBLOCK; /* clear bit */ |
|
106 |
else
|
|
107 |
vio->fcntl_mode |= O_NONBLOCK; /* set bit */ |
|
108 |
if (old_fcntl != vio->fcntl_mode) |
|
109 |
{
|
|
110 |
r= fcntl(vio->sd, F_SETFL, vio->fcntl_mode); |
|
111 |
if (r == -1) |
|
112 |
{
|
|
113 |
vio->fcntl_mode= old_fcntl; |
|
114 |
}
|
|
115 |
}
|
|
116 |
}
|
|
172
by Brian Aker
First pass of cleanup |
117 |
|
127
by brian
Removed DBUG statements from VIO |
118 |
return r; |
1
by brian
clean slate |
119 |
}
|
120 |
||
172
by Brian Aker
First pass of cleanup |
121 |
bool
|
1
by brian
clean slate |
122 |
vio_is_blocking(Vio * vio) |
123 |
{
|
|
172
by Brian Aker
First pass of cleanup |
124 |
bool r; |
1
by brian
clean slate |
125 |
r = !(vio->fcntl_mode & O_NONBLOCK); |
127
by brian
Removed DBUG statements from VIO |
126 |
|
127 |
return r; |
|
1
by brian
clean slate |
128 |
}
|
129 |
||
130 |
||
131 |
int vio_fastsend(Vio * vio __attribute__((unused))) |
|
132 |
{
|
|
172
by Brian Aker
First pass of cleanup |
133 |
int nodelay = 1; |
134 |
int r; |
|
135 |
||
136 |
r= setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY, |
|
137 |
&nodelay, sizeof(nodelay)); |
|
138 |
assert(r == 0); |
|
127
by brian
Removed DBUG statements from VIO |
139 |
|
140 |
return r; |
|
1
by brian
clean slate |
141 |
}
|
142 |
||
172
by Brian Aker
First pass of cleanup |
143 |
int32_t vio_keepalive(Vio* vio, bool set_keep_alive) |
1
by brian
clean slate |
144 |
{
|
127
by brian
Removed DBUG statements from VIO |
145 |
int r= 0; |
172
by Brian Aker
First pass of cleanup |
146 |
uint32_t opt= 0; |
147 |
||
148 |
if (set_keep_alive) |
|
149 |
opt= 1; |
|
150 |
||
151 |
r= setsockopt(vio->sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof(opt)); |
|
127
by brian
Removed DBUG statements from VIO |
152 |
|
153 |
return r; |
|
1
by brian
clean slate |
154 |
}
|
155 |
||
156 |
||
172
by Brian Aker
First pass of cleanup |
157 |
bool
|
1
by brian
clean slate |
158 |
vio_should_retry(Vio * vio __attribute__((unused))) |
159 |
{
|
|
160 |
int en = socket_errno; |
|
161 |
return (en == SOCKET_EAGAIN || en == SOCKET_EINTR || |
|
162 |
en == SOCKET_EWOULDBLOCK); |
|
163 |
}
|
|
164 |
||
165 |
||
172
by Brian Aker
First pass of cleanup |
166 |
bool
|
1
by brian
clean slate |
167 |
vio_was_interrupted(Vio *vio __attribute__((unused))) |
168 |
{
|
|
169 |
int en= socket_errno; |
|
170 |
return (en == SOCKET_EAGAIN || en == SOCKET_EINTR || |
|
171 |
en == SOCKET_EWOULDBLOCK || en == SOCKET_ETIMEDOUT); |
|
172 |
}
|
|
173 |
||
174 |
||
175 |
int vio_close(Vio * vio) |
|
176 |
{
|
|
177 |
int r=0; |
|
178 |
if (vio->type != VIO_CLOSED) |
|
179 |
{
|
|
127
by brian
Removed DBUG statements from VIO |
180 |
assert(vio->sd >= 0); |
1
by brian
clean slate |
181 |
if (shutdown(vio->sd, SHUT_RDWR)) |
182 |
r= -1; |
|
183 |
if (closesocket(vio->sd)) |
|
184 |
r= -1; |
|
185 |
}
|
|
186 |
vio->type= VIO_CLOSED; |
|
187 |
vio->sd= -1; |
|
127
by brian
Removed DBUG statements from VIO |
188 |
|
189 |
return r; |
|
1
by brian
clean slate |
190 |
}
|
191 |
||
192 |
||
193 |
const char *vio_description(Vio * vio) |
|
194 |
{
|
|
195 |
return vio->desc; |
|
196 |
}
|
|
197 |
||
198 |
enum enum_vio_type vio_type(Vio* vio) |
|
199 |
{
|
|
200 |
return vio->type; |
|
201 |
}
|
|
202 |
||
203 |
my_socket vio_fd(Vio* vio) |
|
204 |
{
|
|
205 |
return vio->sd; |
|
206 |
}
|
|
207 |
||
172
by Brian Aker
First pass of cleanup |
208 |
bool vio_peer_addr(Vio *vio, char *buf, uint16_t *port, size_t buflen) |
1
by brian
clean slate |
209 |
{
|
172
by Brian Aker
First pass of cleanup |
210 |
int error; |
211 |
char port_buf[NI_MAXSERV]; |
|
212.5.29
by Monty Taylor
Changed to work around ubuntu 32-bit. |
212 |
socklen_t addrLen = sizeof(vio->remote); |
172
by Brian Aker
First pass of cleanup |
213 |
|
214 |
if (getpeername(vio->sd, (struct sockaddr *) (&vio->remote), |
|
215 |
&addrLen) != 0) |
|
216 |
{
|
|
217 |
return true; |
|
218 |
}
|
|
219 |
vio->addrLen= (int)addrLen; |
|
220 |
||
221 |
if ((error= getnameinfo((struct sockaddr *)(&vio->remote), |
|
222 |
addrLen, |
|
223 |
buf, buflen, |
|
224 |
port_buf, NI_MAXSERV, NI_NUMERICHOST|NI_NUMERICSERV))) |
|
225 |
{
|
|
226 |
return true; |
|
227 |
}
|
|
228 |
||
229 |
*port= (uint16_t)strtol(port_buf, (char **)NULL, 10); |
|
127
by brian
Removed DBUG statements from VIO |
230 |
|
231 |
return false; |
|
1
by brian
clean slate |
232 |
}
|
233 |
||
234 |
||
235 |
/* Return 0 if there is data to be read */
|
|
236 |
||
172
by Brian Aker
First pass of cleanup |
237 |
bool vio_poll_read(Vio *vio,uint timeout) |
1
by brian
clean slate |
238 |
{
|
239 |
struct pollfd fds; |
|
240 |
int res; |
|
127
by brian
Removed DBUG statements from VIO |
241 |
|
1
by brian
clean slate |
242 |
fds.fd=vio->sd; |
243 |
fds.events=POLLIN; |
|
244 |
fds.revents=0; |
|
245 |
if ((res=poll(&fds,1,(int) timeout*1000)) <= 0) |
|
246 |
{
|
|
127
by brian
Removed DBUG statements from VIO |
247 |
return res < 0 ? false : true; /* Don't return 1 on errors */ |
1
by brian
clean slate |
248 |
}
|
127
by brian
Removed DBUG statements from VIO |
249 |
return (fds.revents & (POLLIN | POLLERR | POLLHUP) ? false : true); |
1
by brian
clean slate |
250 |
}
|
251 |
||
252 |
||
172
by Brian Aker
First pass of cleanup |
253 |
bool vio_peek_read(Vio *vio, uint32_t *bytes) |
1
by brian
clean slate |
254 |
{
|
255 |
char buf[1024]; |
|
256 |
ssize_t res= recv(vio->sd, &buf, sizeof(buf), MSG_PEEK); |
|
172
by Brian Aker
First pass of cleanup |
257 |
|
1
by brian
clean slate |
258 |
if (res < 0) |
163
by Brian Aker
Merge Monty's code. |
259 |
return true; |
172
by Brian Aker
First pass of cleanup |
260 |
*bytes= (uint32_t)res; |
163
by Brian Aker
Merge Monty's code. |
261 |
return false; |
1
by brian
clean slate |
262 |
}
|
263 |
||
264 |
void vio_timeout(Vio *vio, uint which, uint timeout) |
|
265 |
{
|
|
266 |
int r; |
|
267 |
||
268 |
/* POSIX specifies time as struct timeval. */
|
|
269 |
struct timeval wait_timeout; |
|
270 |
wait_timeout.tv_sec= timeout; |
|
271 |
wait_timeout.tv_usec= 0; |
|
272 |
||
273 |
r= setsockopt(vio->sd, SOL_SOCKET, which ? SO_SNDTIMEO : SO_RCVTIMEO, |
|
274 |
IF_WIN(const char*, const void*)&wait_timeout, |
|
275 |
sizeof(wait_timeout)); |
|
172
by Brian Aker
First pass of cleanup |
276 |
assert(r == 0); |
1
by brian
clean slate |
277 |
}
|