1
/* Copyright (C) 2000 MySQL AB
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.
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.
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 */
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
25
int vio_errno(Vio *vio __attribute__((unused)))
27
return socket_errno; /* On Win32 this mapped to WSAGetLastError() */
31
size_t vio_read(Vio * vio, uchar* buf, size_t size)
34
DBUG_ENTER("vio_read");
35
DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio->sd, (long) buf,
38
/* Ensure nobody uses vio_read_buff and vio_read simultaneously */
39
DBUG_ASSERT(vio->read_end == vio->read_pos);
41
r = recv(vio->sd, buf, size,0);
43
errno=0; /* For linux */
44
r = read(vio->sd, buf, size);
49
DBUG_PRINT("vio_error", ("Got error %d during read",errno));
52
DBUG_PRINT("exit", ("%ld", (long) r));
58
Buffered read: if average read size is small it may
59
reduce number of syscalls.
62
size_t vio_read_buff(Vio *vio, uchar* buf, size_t size)
65
#define VIO_UNBUFFERED_READ_MIN_SIZE 2048
66
DBUG_ENTER("vio_read_buff");
67
DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio->sd, (long) buf,
70
if (vio->read_pos < vio->read_end)
72
rc= min((size_t) (vio->read_end - vio->read_pos), size);
73
memcpy(buf, vio->read_pos, rc);
76
Do not try to read from the socket now even if rc < size:
77
vio_read can return -1 due to an error or non-blocking mode, and
78
the safest way to handle it is to move to a separate branch.
81
else if (size < VIO_UNBUFFERED_READ_MIN_SIZE)
83
rc= vio_read(vio, (uchar*) vio->read_buffer, VIO_READ_BUFFER_SIZE);
84
if (rc != 0 && rc != (size_t) -1)
88
vio->read_pos= vio->read_buffer + size;
89
vio->read_end= vio->read_buffer + rc;
92
memcpy(buf, vio->read_buffer, rc);
96
rc= vio_read(vio, buf, size);
98
#undef VIO_UNBUFFERED_READ_MIN_SIZE
102
size_t vio_write(Vio * vio, const uchar* buf, size_t size)
105
DBUG_ENTER("vio_write");
106
DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio->sd, (long) buf,
109
r = send(vio->sd, buf, size,0);
111
r = write(vio->sd, buf, size);
114
if (r == (size_t) -1)
116
DBUG_PRINT("vio_error", ("Got error on write: %d",socket_errno));
118
#endif /* DBUG_OFF */
119
DBUG_PRINT("exit", ("%u", (uint) r));
123
int vio_blocking(Vio * vio __attribute__((unused)), my_bool set_blocking_mode,
127
DBUG_ENTER("vio_blocking");
129
*old_mode= test(!(vio->fcntl_mode & O_NONBLOCK));
130
DBUG_PRINT("enter", ("set_blocking_mode: %d old_mode: %d",
131
(int) set_blocking_mode, (int) *old_mode));
133
#if !defined(__WIN__)
134
#if !defined(NO_FCNTL_NONBLOCK)
137
int old_fcntl=vio->fcntl_mode;
138
if (set_blocking_mode)
139
vio->fcntl_mode &= ~O_NONBLOCK; /* clear bit */
141
vio->fcntl_mode |= O_NONBLOCK; /* set bit */
142
if (old_fcntl != vio->fcntl_mode)
144
r= fcntl(vio->sd, F_SETFL, vio->fcntl_mode);
147
DBUG_PRINT("info", ("fcntl failed, errno %d", errno));
148
vio->fcntl_mode= old_fcntl;
153
r= set_blocking_mode ? 0 : 1;
154
#endif /* !defined(NO_FCNTL_NONBLOCK) */
155
#else /* !defined(__WIN__) */
156
if (vio->type != VIO_TYPE_NAMEDPIPE && vio->type != VIO_TYPE_SHARED_MEMORY)
159
int old_fcntl=vio->fcntl_mode;
160
if (set_blocking_mode)
163
vio->fcntl_mode &= ~O_NONBLOCK; /* clear bit */
168
vio->fcntl_mode |= O_NONBLOCK; /* set bit */
170
if (old_fcntl != vio->fcntl_mode)
171
r = ioctlsocket(vio->sd,FIONBIO,(void*) &arg);
174
r= test(!(vio->fcntl_mode & O_NONBLOCK)) != set_blocking_mode;
175
#endif /* !defined(__WIN__) */
176
DBUG_PRINT("exit", ("%d", r));
181
vio_is_blocking(Vio * vio)
184
DBUG_ENTER("vio_is_blocking");
185
r = !(vio->fcntl_mode & O_NONBLOCK);
186
DBUG_PRINT("exit", ("%d", (int) r));
191
int vio_fastsend(Vio * vio __attribute__((unused)))
194
DBUG_ENTER("vio_fastsend");
196
#if defined(IPTOS_THROUGHPUT)
198
int tos = IPTOS_THROUGHPUT;
199
r= setsockopt(vio->sd, IPPROTO_IP, IP_TOS, (void *) &tos, sizeof(tos));
201
#endif /* IPTOS_THROUGHPUT */
210
r= setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY,
211
IF_WIN(const char*, void*) &nodelay,
217
DBUG_PRINT("warning", ("Couldn't set socket option for fast send"));
220
DBUG_PRINT("exit", ("%d", r));
224
int vio_keepalive(Vio* vio, my_bool set_keep_alive)
228
DBUG_ENTER("vio_keepalive");
229
DBUG_PRINT("enter", ("sd: %d set_keep_alive: %d", vio->sd, (int)
231
if (vio->type != VIO_TYPE_NAMEDPIPE)
235
r = setsockopt(vio->sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt,
243
vio_should_retry(Vio * vio __attribute__((unused)))
245
int en = socket_errno;
246
return (en == SOCKET_EAGAIN || en == SOCKET_EINTR ||
247
en == SOCKET_EWOULDBLOCK);
252
vio_was_interrupted(Vio *vio __attribute__((unused)))
254
int en= socket_errno;
255
return (en == SOCKET_EAGAIN || en == SOCKET_EINTR ||
256
en == SOCKET_EWOULDBLOCK || en == SOCKET_ETIMEDOUT);
260
int vio_close(Vio * vio)
263
DBUG_ENTER("vio_close");
265
if (vio->type == VIO_TYPE_NAMEDPIPE)
267
#if defined(__NT__) && defined(MYSQL_SERVER)
268
CancelIo(vio->hPipe);
269
DisconnectNamedPipe(vio->hPipe);
271
r=CloseHandle(vio->hPipe);
275
if (vio->type != VIO_CLOSED)
277
DBUG_ASSERT(vio->sd >= 0);
278
if (shutdown(vio->sd, SHUT_RDWR))
280
if (closesocket(vio->sd))
285
DBUG_PRINT("vio_error", ("close() failed, error: %d",socket_errno));
286
/* FIXME: error handling (not critical for MySQL) */
288
vio->type= VIO_CLOSED;
294
const char *vio_description(Vio * vio)
299
enum enum_vio_type vio_type(Vio* vio)
304
my_socket vio_fd(Vio* vio)
309
my_bool vio_peer_addr(Vio *vio, char *buf, uint16 *port, size_t buflen)
311
DBUG_ENTER("vio_peer_addr");
312
DBUG_PRINT("enter", ("sd: %d", vio->sd));
316
strmov(buf, "127.0.0.1");
322
char port_buf[NI_MAXSERV];
323
size_socket addrLen = sizeof(vio->remote);
324
if (getpeername(vio->sd, (struct sockaddr *) (&vio->remote),
327
DBUG_PRINT("exit", ("getpeername gave error: %d", socket_errno));
330
vio->addrLen= (int)addrLen;
332
if ((error= getnameinfo((struct sockaddr *)(&vio->remote),
335
port_buf, NI_MAXSERV, NI_NUMERICHOST|NI_NUMERICSERV)))
337
DBUG_PRINT("exit", ("getnameinfo gave error: %s",
338
gai_strerror(error)));
342
*port= (uint16)strtol(port_buf, (char **)NULL, 10);
345
A lot of users do not have IPv6 loopback resolving to localhost
346
correctly setup. Should this exist? No. If we do not do it though
347
we will be getting a lot of support questions from users who
348
have bad setups. This code should be removed by say... 2012.
351
if (!memcmp(buf, "::ffff:127.0.0.1", sizeof("::ffff:127.0.0.1")))
352
strmov(buf, "127.0.0.1");
354
DBUG_PRINT("exit", ("addr: %s", buf));
359
/* Return 0 if there is data to be read */
361
my_bool vio_poll_read(Vio *vio,uint timeout)
365
my_socket fd= vio->sd;
366
fd_set readfds, errorfds;
368
DBUG_ENTER("vio_poll");
373
FD_SET(fd, &readfds);
374
FD_SET(fd, &errorfds);
375
/* The first argument is ignored on Windows, so a conversion to int is OK */
376
if ((res= select((int) fd, &readfds, NULL, &errorfds, &tm) <= 0))
378
DBUG_RETURN(res < 0 ? 0 : 1);
380
res= FD_ISSET(fd, &readfds) || FD_ISSET(fd, &errorfds);
382
#elif defined(HAVE_POLL)
385
DBUG_ENTER("vio_poll");
389
if ((res=poll(&fds,1,(int) timeout*1000)) <= 0)
391
DBUG_RETURN(res < 0 ? 0 : 1); /* Don't return 1 on errors */
393
DBUG_RETURN(fds.revents & (POLLIN | POLLERR | POLLHUP) ? 0 : 1);
400
my_bool vio_peek_read(Vio *vio, uint *bytes)
404
if (ioctlsocket(vio->sd, FIONREAD, &len))
408
#elif FIONREAD_IN_SYS_IOCTL
410
if (ioctl(vio->sd, FIONREAD, &len) < 0)
416
ssize_t res= recv(vio->sd, &buf, sizeof(buf), MSG_PEEK);
424
void vio_timeout(Vio *vio, uint which, uint timeout)
426
#if defined(SO_SNDTIMEO) && defined(SO_RCVTIMEO)
428
DBUG_ENTER("vio_timeout");
432
/* Windows expects time in milliseconds as int */
433
int wait_timeout= (int) timeout * 1000;
435
/* POSIX specifies time as struct timeval. */
436
struct timeval wait_timeout;
437
wait_timeout.tv_sec= timeout;
438
wait_timeout.tv_usec= 0;
441
r= setsockopt(vio->sd, SOL_SOCKET, which ? SO_SNDTIMEO : SO_RCVTIMEO,
442
IF_WIN(const char*, const void*)&wait_timeout,
443
sizeof(wait_timeout));
449
DBUG_PRINT("error", ("setsockopt failed: %d, errno: %d", r, socket_errno));
455
Platforms not suporting setting of socket timeout should either use
456
thr_alarm or just run without read/write timeout(s)
463
size_t vio_read_pipe(Vio * vio, uchar* buf, size_t size)
466
DBUG_ENTER("vio_read_pipe");
467
DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio->sd, (long) buf,
470
if (!ReadFile(vio->hPipe, buf, size, &length, NULL))
473
DBUG_PRINT("exit", ("%d", length));
474
DBUG_RETURN((size_t) length);
478
size_t vio_write_pipe(Vio * vio, const uchar* buf, size_t size)
481
DBUG_ENTER("vio_write_pipe");
482
DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio->sd, (long) buf,
485
if (!WriteFile(vio->hPipe, (char*) buf, size, &length, NULL))
488
DBUG_PRINT("exit", ("%d", length));
489
DBUG_RETURN((size_t) length);
492
int vio_close_pipe(Vio * vio)
495
DBUG_ENTER("vio_close_pipe");
496
#if defined(__NT__) && defined(MYSQL_SERVER)
497
CancelIo(vio->hPipe);
498
DisconnectNamedPipe(vio->hPipe);
500
r=CloseHandle(vio->hPipe);
503
DBUG_PRINT("vio_error", ("close() failed, error: %d",GetLastError()));
504
/* FIXME: error handling (not critical for MySQL) */
506
vio->type= VIO_CLOSED;
512
void vio_ignore_timeout(Vio *vio __attribute__((unused)),
513
uint which __attribute__((unused)),
514
uint timeout __attribute__((unused)))
521
size_t vio_read_shared_memory(Vio * vio, uchar* buf, size_t size)
525
char *current_postion;
526
DBUG_ENTER("vio_read_shared_memory");
527
DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %d", vio->sd, (long) buf,
534
if (vio->shared_memory_remain == 0)
537
events[0]= vio->event_server_wrote;
538
events[1]= vio->event_conn_closed;
540
WaitForMultipleObjects can return next values:
541
WAIT_OBJECT_0+0 - event from vio->event_server_wrote
542
WAIT_OBJECT_0+1 - event from vio->event_conn_closed. We can't read
544
WAIT_ABANDONED_0 and WAIT_TIMEOUT - fail. We can't read anything
546
if (WaitForMultipleObjects(2, (HANDLE*)&events,FALSE,
547
vio->net->read_timeout*1000) != WAIT_OBJECT_0)
552
vio->shared_memory_pos = vio->handle_map;
553
vio->shared_memory_remain = uint4korr((ulong*)vio->shared_memory_pos);
554
vio->shared_memory_pos+=4;
559
if (vio->shared_memory_remain < length)
560
length = vio->shared_memory_remain;
561
if (length > remain_local)
562
length = remain_local;
564
memcpy(current_postion,vio->shared_memory_pos,length);
566
vio->shared_memory_remain-=length;
567
vio->shared_memory_pos+=length;
568
current_postion+=length;
569
remain_local-=length;
571
if (!vio->shared_memory_remain)
573
if (!SetEvent(vio->event_client_read))
576
} while (remain_local);
579
DBUG_PRINT("exit", ("%lu", (ulong) length));
584
size_t vio_write_shared_memory(Vio * vio, const uchar* buf, size_t size)
586
size_t length, remain, sz;
588
const uchar *current_postion;
589
DBUG_ENTER("vio_write_shared_memory");
590
DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %d", vio->sd, (long) buf,
594
current_postion = buf;
597
if (WaitForSingleObject(vio->event_server_read,
598
vio->net->write_timeout*1000) !=
601
DBUG_RETURN((size_t) -1);
604
sz= (remain > shared_memory_buffer_length ? shared_memory_buffer_length :
607
int4store(vio->handle_map,sz);
608
pos = vio->handle_map + 4;
609
memcpy(pos,current_postion,sz);
612
if (!SetEvent(vio->event_client_wrote))
613
DBUG_RETURN((size_t) -1);
617
DBUG_PRINT("exit", ("%lu", (ulong) length));
623
Close shared memory and DBUG_PRINT any errors that happen on closing.
624
@return Zero if all closing functions succeed, and nonzero otherwise.
626
int vio_close_shared_memory(Vio * vio)
629
DBUG_ENTER("vio_close_shared_memory");
630
if (vio->type != VIO_CLOSED)
633
Set event_conn_closed for notification of both client and server that
636
SetEvent(vio->event_conn_closed);
638
Close all handlers. UnmapViewOfFile and CloseHandle return non-zero
639
result if they are success.
641
if (UnmapViewOfFile(vio->handle_map) == 0)
644
DBUG_PRINT("vio_error", ("UnmapViewOfFile() failed"));
646
if (CloseHandle(vio->event_server_wrote) == 0)
649
DBUG_PRINT("vio_error", ("CloseHandle(vio->esw) failed"));
651
if (CloseHandle(vio->event_server_read) == 0)
654
DBUG_PRINT("vio_error", ("CloseHandle(vio->esr) failed"));
656
if (CloseHandle(vio->event_client_wrote) == 0)
659
DBUG_PRINT("vio_error", ("CloseHandle(vio->ecw) failed"));
661
if (CloseHandle(vio->event_client_read) == 0)
664
DBUG_PRINT("vio_error", ("CloseHandle(vio->ecr) failed"));
666
if (CloseHandle(vio->handle_file_map) == 0)
669
DBUG_PRINT("vio_error", ("CloseHandle(vio->hfm) failed"));
671
if (CloseHandle(vio->event_conn_closed) == 0)
674
DBUG_PRINT("vio_error", ("CloseHandle(vio->ecc) failed"));
677
vio->type= VIO_CLOSED;
679
DBUG_RETURN(error_count);
681
#endif /* HAVE_SMEM */