2 Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; version 2 of
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 Note that we can't have assertion on file descriptors; The reason for
21 this is that during mysql shutdown, another thread can close a file
22 we are working on. In this case we should just return read errors from
29 #pragma comment(lib, "ws2_32.lib")
35 int vio_errno(Vio
*vio
__attribute__((unused
)))
37 return socket_errno
; /* On Win32 this mapped to WSAGetLastError() */
41 size_t vio_read(Vio
* vio
, uchar
* buf
, size_t size
)
44 DBUG_ENTER("vio_read");
45 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio
->sd
, (long) buf
,
48 /* Ensure nobody uses vio_read_buff and vio_read simultaneously */
49 DBUG_ASSERT(vio
->read_end
== vio
->read_pos
);
51 r
= recv(vio
->sd
, buf
, size
,0);
53 errno
=0; /* For linux */
54 r
= read(vio
->sd
, buf
, size
);
59 DBUG_PRINT("vio_error", ("Got error %d during read",errno
));
62 DBUG_PRINT("exit", ("%ld", (long) r
));
68 Buffered read: if average read size is small it may
69 reduce number of syscalls.
72 size_t vio_read_buff(Vio
*vio
, uchar
* buf
, size_t size
)
75 #define VIO_UNBUFFERED_READ_MIN_SIZE 2048
76 DBUG_ENTER("vio_read_buff");
77 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio
->sd
, (long) buf
,
80 if (vio
->read_pos
< vio
->read_end
)
82 rc
= min((size_t) (vio
->read_end
- vio
->read_pos
), size
);
83 memcpy(buf
, vio
->read_pos
, rc
);
86 Do not try to read from the socket now even if rc < size:
87 vio_read can return -1 due to an error or non-blocking mode, and
88 the safest way to handle it is to move to a separate branch.
91 else if (size
< VIO_UNBUFFERED_READ_MIN_SIZE
)
93 rc
= vio_read(vio
, (uchar
*) vio
->read_buffer
, VIO_READ_BUFFER_SIZE
);
94 if (rc
!= 0 && rc
!= (size_t) -1)
98 vio
->read_pos
= vio
->read_buffer
+ size
;
99 vio
->read_end
= vio
->read_buffer
+ rc
;
102 memcpy(buf
, vio
->read_buffer
, rc
);
106 rc
= vio_read(vio
, buf
, size
);
108 #undef VIO_UNBUFFERED_READ_MIN_SIZE
112 size_t vio_write(Vio
* vio
, const uchar
* buf
, size_t size
)
115 DBUG_ENTER("vio_write");
116 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio
->sd
, (long) buf
,
119 r
= send(vio
->sd
, buf
, size
,0);
121 r
= write(vio
->sd
, buf
, size
);
124 if (r
== (size_t) -1)
126 DBUG_PRINT("vio_error", ("Got error on write: %d",socket_errno
));
128 #endif /* DBUG_OFF */
129 DBUG_PRINT("exit", ("%u", (uint
) r
));
133 int vio_blocking(Vio
* vio
__attribute__((unused
)), my_bool set_blocking_mode
,
137 DBUG_ENTER("vio_blocking");
139 *old_mode
= test(!(vio
->fcntl_mode
& O_NONBLOCK
));
140 DBUG_PRINT("enter", ("set_blocking_mode: %d old_mode: %d",
141 (int) set_blocking_mode
, (int) *old_mode
));
143 #if !defined(__WIN__)
144 #if !defined(NO_FCNTL_NONBLOCK)
147 int old_fcntl
=vio
->fcntl_mode
;
148 if (set_blocking_mode
)
149 vio
->fcntl_mode
&= ~O_NONBLOCK
; /* clear bit */
151 vio
->fcntl_mode
|= O_NONBLOCK
; /* set bit */
152 if (old_fcntl
!= vio
->fcntl_mode
)
154 r
= fcntl(vio
->sd
, F_SETFL
, vio
->fcntl_mode
);
157 DBUG_PRINT("info", ("fcntl failed, errno %d", errno
));
158 vio
->fcntl_mode
= old_fcntl
;
163 r
= set_blocking_mode
? 0 : 1;
164 #endif /* !defined(NO_FCNTL_NONBLOCK) */
165 #else /* !defined(__WIN__) */
166 if (vio
->type
!= VIO_TYPE_NAMEDPIPE
&& vio
->type
!= VIO_TYPE_SHARED_MEMORY
)
169 int old_fcntl
=vio
->fcntl_mode
;
170 if (set_blocking_mode
)
173 vio
->fcntl_mode
&= ~O_NONBLOCK
; /* clear bit */
178 vio
->fcntl_mode
|= O_NONBLOCK
; /* set bit */
180 if (old_fcntl
!= vio
->fcntl_mode
)
181 r
= ioctlsocket(vio
->sd
,FIONBIO
,(void*) &arg
);
184 r
= test(!(vio
->fcntl_mode
& O_NONBLOCK
)) != set_blocking_mode
;
185 #endif /* !defined(__WIN__) */
186 DBUG_PRINT("exit", ("%d", r
));
191 vio_is_blocking(Vio
* vio
)
194 DBUG_ENTER("vio_is_blocking");
195 r
= !(vio
->fcntl_mode
& O_NONBLOCK
);
196 DBUG_PRINT("exit", ("%d", (int) r
));
201 int vio_fastsend(Vio
* vio
__attribute__((unused
)))
204 DBUG_ENTER("vio_fastsend");
206 #if defined(IPTOS_THROUGHPUT)
208 int tos
= IPTOS_THROUGHPUT
;
209 r
= setsockopt(vio
->sd
, IPPROTO_IP
, IP_TOS
, (void *) &tos
, sizeof(tos
));
211 #endif /* IPTOS_THROUGHPUT */
220 r
= setsockopt(vio
->sd
, IPPROTO_TCP
, TCP_NODELAY
,
221 IF_WIN(const char*, void*) &nodelay
,
227 DBUG_PRINT("warning", ("Couldn't set socket option for fast send"));
230 DBUG_PRINT("exit", ("%d", r
));
234 int vio_keepalive(Vio
* vio
, my_bool set_keep_alive
)
238 DBUG_ENTER("vio_keepalive");
239 DBUG_PRINT("enter", ("sd: %d set_keep_alive: %d", vio
->sd
, (int)
241 if (vio
->type
!= VIO_TYPE_NAMEDPIPE
)
245 r
= setsockopt(vio
->sd
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &opt
,
253 vio_should_retry(Vio
* vio
__attribute__((unused
)))
255 int en
= socket_errno
;
256 return (en
== SOCKET_EAGAIN
|| en
== SOCKET_EINTR
||
257 en
== SOCKET_EWOULDBLOCK
);
262 vio_was_interrupted(Vio
*vio
__attribute__((unused
)))
264 int en
= socket_errno
;
265 return (en
== SOCKET_EAGAIN
|| en
== SOCKET_EINTR
||
266 en
== SOCKET_EWOULDBLOCK
|| en
== SOCKET_ETIMEDOUT
);
271 mysql_socket_shutdown(my_socket mysql_socket
, int how
)
276 static LPFN_DISCONNECTEX DisconnectEx
= NULL
;
277 if (DisconnectEx
== NULL
)
279 DWORD dwBytesReturned
;
280 GUID guidDisconnectEx
= WSAID_DISCONNECTEX
;
281 WSAIoctl(mysql_socket
, SIO_GET_EXTENSION_FUNCTION_POINTER
,
282 &guidDisconnectEx
, sizeof(GUID
),
283 &DisconnectEx
, sizeof(DisconnectEx
),
284 &dwBytesReturned
, NULL
, NULL
);
288 /* Non instrumented code */
291 result
= (DisconnectEx(mysql_socket
, (LPOVERLAPPED
) NULL
,
292 (DWORD
) 0, (DWORD
) 0) == TRUE
) ? 0 : -1;
295 result
= shutdown(mysql_socket
, how
);
301 int vio_close(Vio
* vio
)
304 DBUG_ENTER("vio_close");
306 if (vio
->type
!= VIO_CLOSED
)
308 DBUG_ASSERT(vio
->type
== VIO_TYPE_TCPIP
||
309 vio
->type
== VIO_TYPE_SOCKET
||
310 vio
->type
== VIO_TYPE_SSL
);
312 DBUG_ASSERT(vio
->sd
>= 0);
313 if (mysql_socket_shutdown(vio
->sd
, SHUT_RDWR
))
315 if (closesocket(vio
->sd
))
320 DBUG_PRINT("vio_error", ("close() failed, error: %d",socket_errno
));
321 /* FIXME: error handling (not critical for MySQL) */
323 vio
->type
= VIO_CLOSED
;
329 const char *vio_description(Vio
* vio
)
334 enum enum_vio_type
vio_type(Vio
* vio
)
339 my_socket
vio_fd(Vio
* vio
)
345 my_bool
vio_peer_addr(Vio
* vio
, char *buf
, uint16
*port
)
347 DBUG_ENTER("vio_peer_addr");
348 DBUG_PRINT("enter", ("sd: %d", vio
->sd
));
351 strmov(buf
,"127.0.0.1");
356 size_socket addrLen
= sizeof(vio
->remote
);
357 if (getpeername(vio
->sd
, (struct sockaddr
*) (&vio
->remote
),
360 DBUG_PRINT("exit", ("getpeername gave error: %d", socket_errno
));
363 my_inet_ntoa(vio
->remote
.sin_addr
,buf
);
364 *port
= ntohs(vio
->remote
.sin_port
);
366 DBUG_PRINT("exit", ("addr: %s", buf
));
372 Get in_addr for a TCP/IP connection
380 one must call vio_peer_addr() before calling this one
383 void vio_in_addr(Vio
*vio
, struct in_addr
*in
)
385 DBUG_ENTER("vio_in_addr");
387 bzero((char*) in
, sizeof(*in
));
389 *in
=vio
->remote
.sin_addr
;
394 /* Return 0 if there is data to be read */
396 my_bool
vio_poll_read(Vio
*vio
,uint timeout
)
403 DBUG_ENTER("vio_poll");
407 if ((res
=poll(&fds
,1,(int) timeout
*1000)) <= 0)
409 DBUG_RETURN(res
< 0 ? 0 : 1); /* Don't return 1 on errors */
411 DBUG_RETURN(fds
.revents
& POLLIN
? 0 : 1);
416 void vio_timeout(Vio
*vio
, uint which
, uint timeout
)
418 #if defined(SO_SNDTIMEO) && defined(SO_RCVTIMEO)
420 DBUG_ENTER("vio_timeout");
424 /* Windows expects time in milliseconds as int */
425 int wait_timeout
= (int) timeout
* 1000;
427 /* POSIX specifies time as struct timeval. */
428 struct timeval wait_timeout
;
429 wait_timeout
.tv_sec
= timeout
;
430 wait_timeout
.tv_usec
= 0;
433 r
= setsockopt(vio
->sd
, SOL_SOCKET
, which
? SO_SNDTIMEO
: SO_RCVTIMEO
,
434 IF_WIN(const char*, const void*)&wait_timeout
,
435 sizeof(wait_timeout
));
441 DBUG_PRINT("error", ("setsockopt failed: %d, errno: %d", r
, socket_errno
));
447 Platforms not suporting setting of socket timeout should either use
448 thr_alarm or just run without read/write timeout(s)
457 Finish pending IO on pipe. Honor wait timeout
459 static size_t pipe_complete_io(Vio
* vio
, char* buf
, size_t size
, DWORD timeout_ms
)
464 DBUG_ENTER("pipe_complete_io");
466 ret
= WaitForSingleObject(vio
->pipe_overlapped
.hEvent
, timeout_ms
);
468 WaitForSingleObjects will normally return WAIT_OBJECT_O (success, IO completed)
471 if(ret
!= WAIT_OBJECT_0
)
473 CancelIo(vio
->hPipe
);
474 DBUG_PRINT("error",("WaitForSingleObject() returned %d", ret
));
475 DBUG_RETURN((size_t)-1);
478 if (!GetOverlappedResult(vio
->hPipe
,&(vio
->pipe_overlapped
),&length
, FALSE
))
480 DBUG_PRINT("error",("GetOverlappedResult() returned last error %d",
482 DBUG_RETURN((size_t)-1);
489 size_t vio_read_pipe(Vio
* vio
, uchar
*buf
, size_t size
)
493 DBUG_ENTER("vio_read_pipe");
494 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio
->sd
, (long) buf
,
497 if (ReadFile(vio
->hPipe
, buf
, (DWORD
)size
, &bytes_read
,
498 &(vio
->pipe_overlapped
)))
504 if (GetLastError() != ERROR_IO_PENDING
)
506 DBUG_PRINT("error",("ReadFile() returned last error %d",
508 DBUG_RETURN((size_t)-1);
510 retval
= pipe_complete_io(vio
, buf
, size
,vio
->read_timeout_ms
);
513 DBUG_PRINT("exit", ("%lld", (longlong
)retval
));
518 size_t vio_write_pipe(Vio
* vio
, const uchar
* buf
, size_t size
)
522 DBUG_ENTER("vio_write_pipe");
523 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio
->sd
, (long) buf
,
526 if (WriteFile(vio
->hPipe
, buf
, (DWORD
)size
, &bytes_written
,
527 &(vio
->pipe_overlapped
)))
529 retval
= bytes_written
;
533 if (GetLastError() != ERROR_IO_PENDING
)
535 DBUG_PRINT("vio_error",("WriteFile() returned last error %d",
537 DBUG_RETURN((size_t)-1);
539 retval
= pipe_complete_io(vio
, (char *)buf
, size
, vio
->write_timeout_ms
);
542 DBUG_PRINT("exit", ("%lld", (longlong
)retval
));
547 int vio_close_pipe(Vio
* vio
)
550 DBUG_ENTER("vio_close_pipe");
552 CloseHandle(vio
->pipe_overlapped
.hEvent
);
553 DisconnectNamedPipe(vio
->hPipe
);
554 r
= CloseHandle(vio
->hPipe
);
557 DBUG_PRINT("vio_error", ("close() failed, error: %d",GetLastError()));
558 /* FIXME: error handling (not critical for MySQL) */
560 vio
->type
= VIO_CLOSED
;
566 void vio_win32_timeout(Vio
*vio
, uint which
, uint timeout_sec
)
570 Windows is measuring timeouts in milliseconds. Check for possible int
573 if (timeout_sec
> UINT_MAX
/1000)
574 timeout_ms
= INFINITE
;
576 timeout_ms
= timeout_sec
* 1000;
578 /* which == 1 means "write", which == 0 means "read".*/
580 vio
->write_timeout_ms
= timeout_ms
;
582 vio
->read_timeout_ms
= timeout_ms
;
588 size_t vio_read_shared_memory(Vio
* vio
, uchar
* buf
, size_t size
)
592 char *current_position
;
595 DBUG_ENTER("vio_read_shared_memory");
596 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %d", vio
->sd
, (long) buf
,
600 current_position
=buf
;
602 events
[0]= vio
->event_server_wrote
;
603 events
[1]= vio
->event_conn_closed
;
607 if (vio
->shared_memory_remain
== 0)
610 WaitForMultipleObjects can return next values:
611 WAIT_OBJECT_0+0 - event from vio->event_server_wrote
612 WAIT_OBJECT_0+1 - event from vio->event_conn_closed. We can't read
614 WAIT_ABANDONED_0 and WAIT_TIMEOUT - fail. We can't read anything
616 if (WaitForMultipleObjects(array_elements(events
), events
, FALSE
,
617 vio
->read_timeout_ms
) != WAIT_OBJECT_0
)
622 vio
->shared_memory_pos
= vio
->handle_map
;
623 vio
->shared_memory_remain
= uint4korr((ulong
*)vio
->shared_memory_pos
);
624 vio
->shared_memory_pos
+=4;
629 if (vio
->shared_memory_remain
< length
)
630 length
= vio
->shared_memory_remain
;
631 if (length
> remain_local
)
632 length
= remain_local
;
634 memcpy(current_position
,vio
->shared_memory_pos
,length
);
636 vio
->shared_memory_remain
-=length
;
637 vio
->shared_memory_pos
+=length
;
638 current_position
+=length
;
639 remain_local
-=length
;
641 if (!vio
->shared_memory_remain
)
643 if (!SetEvent(vio
->event_client_read
))
646 } while (remain_local
);
649 DBUG_PRINT("exit", ("%lu", (ulong
) length
));
654 size_t vio_write_shared_memory(Vio
* vio
, const uchar
* buf
, size_t size
)
656 size_t length
, remain
, sz
;
658 const uchar
*current_position
;
661 DBUG_ENTER("vio_write_shared_memory");
662 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %d", vio
->sd
, (long) buf
,
666 current_position
= buf
;
668 events
[0]= vio
->event_server_read
;
669 events
[1]= vio
->event_conn_closed
;
673 if (WaitForMultipleObjects(array_elements(events
), events
, FALSE
,
674 vio
->write_timeout_ms
) != WAIT_OBJECT_0
)
676 DBUG_RETURN((size_t) -1);
679 sz
= (remain
> shared_memory_buffer_length
? shared_memory_buffer_length
:
682 int4store(vio
->handle_map
,sz
);
683 pos
= vio
->handle_map
+ 4;
684 memcpy(pos
,current_position
,sz
);
686 current_position
+=sz
;
687 if (!SetEvent(vio
->event_client_wrote
))
688 DBUG_RETURN((size_t) -1);
692 DBUG_PRINT("exit", ("%lu", (ulong
) length
));
698 Close shared memory and DBUG_PRINT any errors that happen on closing.
699 @return Zero if all closing functions succeed, and nonzero otherwise.
701 int vio_close_shared_memory(Vio
* vio
)
704 DBUG_ENTER("vio_close_shared_memory");
705 if (vio
->type
!= VIO_CLOSED
)
708 Set event_conn_closed for notification of both client and server that
711 SetEvent(vio
->event_conn_closed
);
713 Close all handlers. UnmapViewOfFile and CloseHandle return non-zero
714 result if they are success.
716 if (UnmapViewOfFile(vio
->handle_map
) == 0)
719 DBUG_PRINT("vio_error", ("UnmapViewOfFile() failed"));
721 if (CloseHandle(vio
->event_server_wrote
) == 0)
724 DBUG_PRINT("vio_error", ("CloseHandle(vio->esw) failed"));
726 if (CloseHandle(vio
->event_server_read
) == 0)
729 DBUG_PRINT("vio_error", ("CloseHandle(vio->esr) failed"));
731 if (CloseHandle(vio
->event_client_wrote
) == 0)
734 DBUG_PRINT("vio_error", ("CloseHandle(vio->ecw) failed"));
736 if (CloseHandle(vio
->event_client_read
) == 0)
739 DBUG_PRINT("vio_error", ("CloseHandle(vio->ecr) failed"));
741 if (CloseHandle(vio
->handle_file_map
) == 0)
744 DBUG_PRINT("vio_error", ("CloseHandle(vio->hfm) failed"));
746 if (CloseHandle(vio
->event_conn_closed
) == 0)
749 DBUG_PRINT("vio_error", ("CloseHandle(vio->ecc) failed"));
752 vio
->type
= VIO_CLOSED
;
754 DBUG_RETURN(error_count
);
756 #endif /* HAVE_SMEM */