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_CLOSED
)
267 DBUG_ASSERT(vio
->type
== VIO_TYPE_TCPIP
||
268 vio
->type
== VIO_TYPE_SOCKET
||
269 vio
->type
== VIO_TYPE_SSL
);
271 DBUG_ASSERT(vio
->sd
>= 0);
272 if (shutdown(vio
->sd
, SHUT_RDWR
))
274 if (closesocket(vio
->sd
))
279 DBUG_PRINT("vio_error", ("close() failed, error: %d",socket_errno
));
280 /* FIXME: error handling (not critical for MySQL) */
282 vio
->type
= VIO_CLOSED
;
288 const char *vio_description(Vio
* vio
)
293 enum enum_vio_type
vio_type(Vio
* vio
)
298 my_socket
vio_fd(Vio
* vio
)
304 my_bool
vio_peer_addr(Vio
* vio
, char *buf
, uint16
*port
)
306 DBUG_ENTER("vio_peer_addr");
307 DBUG_PRINT("enter", ("sd: %d", vio
->sd
));
310 strmov(buf
,"127.0.0.1");
315 size_socket addrLen
= sizeof(vio
->remote
);
316 if (getpeername(vio
->sd
, (struct sockaddr
*) (&vio
->remote
),
319 DBUG_PRINT("exit", ("getpeername gave error: %d", socket_errno
));
322 my_inet_ntoa(vio
->remote
.sin_addr
,buf
);
323 *port
= ntohs(vio
->remote
.sin_port
);
325 DBUG_PRINT("exit", ("addr: %s", buf
));
331 Get in_addr for a TCP/IP connection
339 one must call vio_peer_addr() before calling this one
342 void vio_in_addr(Vio
*vio
, struct in_addr
*in
)
344 DBUG_ENTER("vio_in_addr");
346 bzero((char*) in
, sizeof(*in
));
348 *in
=vio
->remote
.sin_addr
;
353 /* Return 0 if there is data to be read */
355 my_bool
vio_poll_read(Vio
*vio
,uint timeout
)
362 DBUG_ENTER("vio_poll");
366 if ((res
=poll(&fds
,1,(int) timeout
*1000)) <= 0)
368 DBUG_RETURN(res
< 0 ? 0 : 1); /* Don't return 1 on errors */
370 DBUG_RETURN(fds
.revents
& POLLIN
? 0 : 1);
375 void vio_timeout(Vio
*vio
, uint which
, uint timeout
)
377 #if defined(SO_SNDTIMEO) && defined(SO_RCVTIMEO)
379 DBUG_ENTER("vio_timeout");
383 /* Windows expects time in milliseconds as int */
384 int wait_timeout
= (int) timeout
* 1000;
386 /* POSIX specifies time as struct timeval. */
387 struct timeval wait_timeout
;
388 wait_timeout
.tv_sec
= timeout
;
389 wait_timeout
.tv_usec
= 0;
392 r
= setsockopt(vio
->sd
, SOL_SOCKET
, which
? SO_SNDTIMEO
: SO_RCVTIMEO
,
393 IF_WIN(const char*, const void*)&wait_timeout
,
394 sizeof(wait_timeout
));
400 DBUG_PRINT("error", ("setsockopt failed: %d, errno: %d", r
, socket_errno
));
406 Platforms not suporting setting of socket timeout should either use
407 thr_alarm or just run without read/write timeout(s)
416 Finish pending IO on pipe. Honor wait timeout
418 static size_t pipe_complete_io(Vio
* vio
, char* buf
, size_t size
, DWORD timeout_ms
)
423 DBUG_ENTER("pipe_complete_io");
425 ret
= WaitForSingleObject(vio
->pipe_overlapped
.hEvent
, timeout_ms
);
427 WaitForSingleObjects will normally return WAIT_OBJECT_O (success, IO completed)
430 if(ret
!= WAIT_OBJECT_0
)
432 CancelIo(vio
->hPipe
);
433 DBUG_PRINT("error",("WaitForSingleObject() returned %d", ret
));
434 DBUG_RETURN((size_t)-1);
437 if (!GetOverlappedResult(vio
->hPipe
,&(vio
->pipe_overlapped
),&length
, FALSE
))
439 DBUG_PRINT("error",("GetOverlappedResult() returned last error %d",
441 DBUG_RETURN((size_t)-1);
448 size_t vio_read_pipe(Vio
* vio
, uchar
*buf
, size_t size
)
452 DBUG_ENTER("vio_read_pipe");
453 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio
->sd
, (long) buf
,
456 if (ReadFile(vio
->hPipe
, buf
, (DWORD
)size
, &bytes_read
,
457 &(vio
->pipe_overlapped
)))
463 if (GetLastError() != ERROR_IO_PENDING
)
465 DBUG_PRINT("error",("ReadFile() returned last error %d",
467 DBUG_RETURN((size_t)-1);
469 retval
= pipe_complete_io(vio
, buf
, size
,vio
->read_timeout_ms
);
472 DBUG_PRINT("exit", ("%lld", (longlong
)retval
));
477 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
, buf
, (DWORD
)size
, &bytes_written
,
486 &(vio
->pipe_overlapped
)))
488 retval
= bytes_written
;
492 if (GetLastError() != ERROR_IO_PENDING
)
494 DBUG_PRINT("vio_error",("WriteFile() returned last error %d",
496 DBUG_RETURN((size_t)-1);
498 retval
= pipe_complete_io(vio
, (char *)buf
, size
, vio
->write_timeout_ms
);
501 DBUG_PRINT("exit", ("%lld", (longlong
)retval
));
506 int vio_close_pipe(Vio
* vio
)
509 DBUG_ENTER("vio_close_pipe");
511 CloseHandle(vio
->pipe_overlapped
.hEvent
);
512 DisconnectNamedPipe(vio
->hPipe
);
513 r
= CloseHandle(vio
->hPipe
);
516 DBUG_PRINT("vio_error", ("close() failed, error: %d",GetLastError()));
517 /* FIXME: error handling (not critical for MySQL) */
519 vio
->type
= VIO_CLOSED
;
525 void vio_win32_timeout(Vio
*vio
, uint which
, uint timeout_sec
)
529 Windows is measuring timeouts in milliseconds. Check for possible int
532 if (timeout_sec
> UINT_MAX
/1000)
533 timeout_ms
= INFINITE
;
535 timeout_ms
= timeout_sec
* 1000;
537 /* which == 1 means "write", which == 0 means "read".*/
539 vio
->write_timeout_ms
= timeout_ms
;
541 vio
->read_timeout_ms
= timeout_ms
;
547 size_t vio_read_shared_memory(Vio
* vio
, uchar
* buf
, size_t size
)
551 char *current_postion
;
554 DBUG_ENTER("vio_read_shared_memory");
555 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %d", vio
->sd
, (long) buf
,
561 events
[0]= vio
->event_server_wrote
;
562 events
[1]= vio
->event_conn_closed
;
566 if (vio
->shared_memory_remain
== 0)
569 WaitForMultipleObjects can return next values:
570 WAIT_OBJECT_0+0 - event from vio->event_server_wrote
571 WAIT_OBJECT_0+1 - event from vio->event_conn_closed. We can't read
573 WAIT_ABANDONED_0 and WAIT_TIMEOUT - fail. We can't read anything
575 if (WaitForMultipleObjects(array_elements(events
), events
, FALSE
,
576 vio
->read_timeout_ms
) != WAIT_OBJECT_0
)
581 vio
->shared_memory_pos
= vio
->handle_map
;
582 vio
->shared_memory_remain
= uint4korr((ulong
*)vio
->shared_memory_pos
);
583 vio
->shared_memory_pos
+=4;
588 if (vio
->shared_memory_remain
< length
)
589 length
= vio
->shared_memory_remain
;
590 if (length
> remain_local
)
591 length
= remain_local
;
593 memcpy(current_postion
,vio
->shared_memory_pos
,length
);
595 vio
->shared_memory_remain
-=length
;
596 vio
->shared_memory_pos
+=length
;
597 current_postion
+=length
;
598 remain_local
-=length
;
600 if (!vio
->shared_memory_remain
)
602 if (!SetEvent(vio
->event_client_read
))
605 } while (remain_local
);
608 DBUG_PRINT("exit", ("%lu", (ulong
) length
));
613 size_t vio_write_shared_memory(Vio
* vio
, const uchar
* buf
, size_t size
)
615 size_t length
, remain
, sz
;
617 const uchar
*current_postion
;
620 DBUG_ENTER("vio_write_shared_memory");
621 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %d", vio
->sd
, (long) buf
,
625 current_postion
= buf
;
627 events
[0]= vio
->event_server_read
;
628 events
[1]= vio
->event_conn_closed
;
632 if (WaitForMultipleObjects(array_elements(events
), events
, FALSE
,
633 vio
->write_timeout_ms
) != WAIT_OBJECT_0
)
635 DBUG_RETURN((size_t) -1);
638 sz
= (remain
> shared_memory_buffer_length
? shared_memory_buffer_length
:
641 int4store(vio
->handle_map
,sz
);
642 pos
= vio
->handle_map
+ 4;
643 memcpy(pos
,current_postion
,sz
);
646 if (!SetEvent(vio
->event_client_wrote
))
647 DBUG_RETURN((size_t) -1);
651 DBUG_PRINT("exit", ("%lu", (ulong
) length
));
657 Close shared memory and DBUG_PRINT any errors that happen on closing.
658 @return Zero if all closing functions succeed, and nonzero otherwise.
660 int vio_close_shared_memory(Vio
* vio
)
663 DBUG_ENTER("vio_close_shared_memory");
664 if (vio
->type
!= VIO_CLOSED
)
667 Set event_conn_closed for notification of both client and server that
670 SetEvent(vio
->event_conn_closed
);
672 Close all handlers. UnmapViewOfFile and CloseHandle return non-zero
673 result if they are success.
675 if (UnmapViewOfFile(vio
->handle_map
) == 0)
678 DBUG_PRINT("vio_error", ("UnmapViewOfFile() failed"));
680 if (CloseHandle(vio
->event_server_wrote
) == 0)
683 DBUG_PRINT("vio_error", ("CloseHandle(vio->esw) failed"));
685 if (CloseHandle(vio
->event_server_read
) == 0)
688 DBUG_PRINT("vio_error", ("CloseHandle(vio->esr) failed"));
690 if (CloseHandle(vio
->event_client_wrote
) == 0)
693 DBUG_PRINT("vio_error", ("CloseHandle(vio->ecw) failed"));
695 if (CloseHandle(vio
->event_client_read
) == 0)
698 DBUG_PRINT("vio_error", ("CloseHandle(vio->ecr) failed"));
700 if (CloseHandle(vio
->handle_file_map
) == 0)
703 DBUG_PRINT("vio_error", ("CloseHandle(vio->hfm) failed"));
705 if (CloseHandle(vio
->event_conn_closed
) == 0)
708 DBUG_PRINT("vio_error", ("CloseHandle(vio->ecc) failed"));
711 vio
->type
= VIO_CLOSED
;
713 DBUG_RETURN(error_count
);
715 #endif /* HAVE_SMEM */