mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / vio / viosocket.c
blobc969a14bd345714f5a6664bbe8c5687685e47391
1 /*
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
7 the License.
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
23 the file descriptior.
26 #ifdef __WIN__
27 #include <winsock2.h>
28 #include <MSWSock.h>
29 #pragma comment(lib, "ws2_32.lib")
30 #endif
31 #include "vio_priv.h"
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)
43 size_t r;
44 DBUG_ENTER("vio_read");
45 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio->sd, (long) buf,
46 (uint) size));
48 /* Ensure nobody uses vio_read_buff and vio_read simultaneously */
49 DBUG_ASSERT(vio->read_end == vio->read_pos);
50 #ifdef __WIN__
51 r = recv(vio->sd, buf, size,0);
52 #else
53 errno=0; /* For linux */
54 r = read(vio->sd, buf, size);
55 #endif /* __WIN__ */
56 #ifndef DBUG_OFF
57 if (r == (size_t) -1)
59 DBUG_PRINT("vio_error", ("Got error %d during read",errno));
61 #endif /* DBUG_OFF */
62 DBUG_PRINT("exit", ("%ld", (long) r));
63 DBUG_RETURN(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)
74 size_t rc;
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,
78 (uint) size));
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);
84 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)
96 if (rc > size)
98 vio->read_pos= vio->read_buffer + size;
99 vio->read_end= vio->read_buffer + rc;
100 rc= size;
102 memcpy(buf, vio->read_buffer, rc);
105 else
106 rc= vio_read(vio, buf, size);
107 DBUG_RETURN(rc);
108 #undef VIO_UNBUFFERED_READ_MIN_SIZE
112 size_t vio_write(Vio * vio, const uchar* buf, size_t size)
114 size_t r;
115 DBUG_ENTER("vio_write");
116 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio->sd, (long) buf,
117 (uint) size));
118 #ifdef __WIN__
119 r = send(vio->sd, buf, size,0);
120 #else
121 r = write(vio->sd, buf, size);
122 #endif /* __WIN__ */
123 #ifndef DBUG_OFF
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));
130 DBUG_RETURN(r);
133 int vio_blocking(Vio * vio __attribute__((unused)), my_bool set_blocking_mode,
134 my_bool *old_mode)
136 int r=0;
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)
145 if (vio->sd >= 0)
147 int old_fcntl=vio->fcntl_mode;
148 if (set_blocking_mode)
149 vio->fcntl_mode &= ~O_NONBLOCK; /* clear bit */
150 else
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);
155 if (r == -1)
157 DBUG_PRINT("info", ("fcntl failed, errno %d", errno));
158 vio->fcntl_mode= old_fcntl;
162 #else
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)
168 ulong arg;
169 int old_fcntl=vio->fcntl_mode;
170 if (set_blocking_mode)
172 arg = 0;
173 vio->fcntl_mode &= ~O_NONBLOCK; /* clear bit */
175 else
177 arg = 1;
178 vio->fcntl_mode |= O_NONBLOCK; /* set bit */
180 if (old_fcntl != vio->fcntl_mode)
181 r = ioctlsocket(vio->sd,FIONBIO,(void*) &arg);
183 else
184 r= test(!(vio->fcntl_mode & O_NONBLOCK)) != set_blocking_mode;
185 #endif /* !defined(__WIN__) */
186 DBUG_PRINT("exit", ("%d", r));
187 DBUG_RETURN(r);
190 my_bool
191 vio_is_blocking(Vio * vio)
193 my_bool r;
194 DBUG_ENTER("vio_is_blocking");
195 r = !(vio->fcntl_mode & O_NONBLOCK);
196 DBUG_PRINT("exit", ("%d", (int) r));
197 DBUG_RETURN(r);
201 int vio_fastsend(Vio * vio __attribute__((unused)))
203 int r=0;
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 */
212 if (!r)
214 #ifdef __WIN__
215 BOOL nodelay= 1;
216 #else
217 int nodelay = 1;
218 #endif
220 r= setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY,
221 IF_WIN(const char*, void*) &nodelay,
222 sizeof(nodelay));
225 if (r)
227 DBUG_PRINT("warning", ("Couldn't set socket option for fast send"));
228 r= -1;
230 DBUG_PRINT("exit", ("%d", r));
231 DBUG_RETURN(r);
234 int vio_keepalive(Vio* vio, my_bool set_keep_alive)
236 int r=0;
237 uint opt = 0;
238 DBUG_ENTER("vio_keepalive");
239 DBUG_PRINT("enter", ("sd: %d set_keep_alive: %d", vio->sd, (int)
240 set_keep_alive));
241 if (vio->type != VIO_TYPE_NAMEDPIPE)
243 if (set_keep_alive)
244 opt = 1;
245 r = setsockopt(vio->sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt,
246 sizeof(opt));
248 DBUG_RETURN(r);
252 my_bool
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);
261 my_bool
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)
273 int result;
275 #ifdef __WIN__
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);
286 #endif
288 /* Non instrumented code */
289 #ifdef __WIN__
290 if (DisconnectEx)
291 result= (DisconnectEx(mysql_socket, (LPOVERLAPPED) NULL,
292 (DWORD) 0, (DWORD) 0) == TRUE) ? 0 : -1;
293 else
294 #endif
295 result= shutdown(mysql_socket, how);
297 return result;
301 int vio_close(Vio * vio)
303 int r=0;
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))
314 r= -1;
315 if (closesocket(vio->sd))
316 r= -1;
318 if (r)
320 DBUG_PRINT("vio_error", ("close() failed, error: %d",socket_errno));
321 /* FIXME: error handling (not critical for MySQL) */
323 vio->type= VIO_CLOSED;
324 vio->sd= -1;
325 DBUG_RETURN(r);
329 const char *vio_description(Vio * vio)
331 return vio->desc;
334 enum enum_vio_type vio_type(Vio* vio)
336 return vio->type;
339 my_socket vio_fd(Vio* vio)
341 return vio->sd;
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));
349 if (vio->localhost)
351 strmov(buf,"127.0.0.1");
352 *port= 0;
354 else
356 size_socket addrLen = sizeof(vio->remote);
357 if (getpeername(vio->sd, (struct sockaddr *) (&vio->remote),
358 &addrLen) != 0)
360 DBUG_PRINT("exit", ("getpeername gave error: %d", socket_errno));
361 DBUG_RETURN(1);
363 my_inet_ntoa(vio->remote.sin_addr,buf);
364 *port= ntohs(vio->remote.sin_port);
366 DBUG_PRINT("exit", ("addr: %s", buf));
367 DBUG_RETURN(0);
372 Get in_addr for a TCP/IP connection
374 SYNOPSIS
375 vio_in_addr()
376 vio vio handle
377 in put in_addr here
379 NOTES
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");
386 if (vio->localhost)
387 bzero((char*) in, sizeof(*in));
388 else
389 *in=vio->remote.sin_addr;
390 DBUG_VOID_RETURN;
394 /* Return 0 if there is data to be read */
396 my_bool vio_poll_read(Vio *vio,uint timeout)
398 #ifndef HAVE_POLL
399 return 0;
400 #else
401 struct pollfd fds;
402 int res;
403 DBUG_ENTER("vio_poll");
404 fds.fd=vio->sd;
405 fds.events=POLLIN;
406 fds.revents=0;
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);
412 #endif
416 void vio_timeout(Vio *vio, uint which, uint timeout)
418 #if defined(SO_SNDTIMEO) && defined(SO_RCVTIMEO)
419 int r;
420 DBUG_ENTER("vio_timeout");
423 #ifdef __WIN__
424 /* Windows expects time in milliseconds as int */
425 int wait_timeout= (int) timeout * 1000;
426 #else
427 /* POSIX specifies time as struct timeval. */
428 struct timeval wait_timeout;
429 wait_timeout.tv_sec= timeout;
430 wait_timeout.tv_usec= 0;
431 #endif
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));
439 #ifndef DBUG_OFF
440 if (r != 0)
441 DBUG_PRINT("error", ("setsockopt failed: %d, errno: %d", r, socket_errno));
442 #endif
444 DBUG_VOID_RETURN;
445 #else
447 Platforms not suporting setting of socket timeout should either use
448 thr_alarm or just run without read/write timeout(s)
450 #endif
454 #ifdef __WIN__
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)
461 DWORD length;
462 DWORD ret;
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)
469 or WAIT_TIMEOUT.
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",
481 GetLastError()));
482 DBUG_RETURN((size_t)-1);
485 DBUG_RETURN(length);
489 size_t vio_read_pipe(Vio * vio, uchar *buf, size_t size)
491 DWORD bytes_read;
492 size_t retval;
493 DBUG_ENTER("vio_read_pipe");
494 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio->sd, (long) buf,
495 (uint) size));
497 if (ReadFile(vio->hPipe, buf, (DWORD)size, &bytes_read,
498 &(vio->pipe_overlapped)))
500 retval= bytes_read;
502 else
504 if (GetLastError() != ERROR_IO_PENDING)
506 DBUG_PRINT("error",("ReadFile() returned last error %d",
507 GetLastError()));
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));
514 DBUG_RETURN(retval);
518 size_t vio_write_pipe(Vio * vio, const uchar* buf, size_t size)
520 DWORD bytes_written;
521 size_t retval;
522 DBUG_ENTER("vio_write_pipe");
523 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio->sd, (long) buf,
524 (uint) size));
526 if (WriteFile(vio->hPipe, buf, (DWORD)size, &bytes_written,
527 &(vio->pipe_overlapped)))
529 retval= bytes_written;
531 else
533 if (GetLastError() != ERROR_IO_PENDING)
535 DBUG_PRINT("vio_error",("WriteFile() returned last error %d",
536 GetLastError()));
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));
543 DBUG_RETURN(retval);
547 int vio_close_pipe(Vio * vio)
549 int r;
550 DBUG_ENTER("vio_close_pipe");
552 CloseHandle(vio->pipe_overlapped.hEvent);
553 DisconnectNamedPipe(vio->hPipe);
554 r= CloseHandle(vio->hPipe);
555 if (r)
557 DBUG_PRINT("vio_error", ("close() failed, error: %d",GetLastError()));
558 /* FIXME: error handling (not critical for MySQL) */
560 vio->type= VIO_CLOSED;
561 vio->sd= -1;
562 DBUG_RETURN(r);
566 void vio_win32_timeout(Vio *vio, uint which , uint timeout_sec)
568 DWORD timeout_ms;
570 Windows is measuring timeouts in milliseconds. Check for possible int
571 overflow.
573 if (timeout_sec > UINT_MAX/1000)
574 timeout_ms= INFINITE;
575 else
576 timeout_ms= timeout_sec * 1000;
578 /* which == 1 means "write", which == 0 means "read".*/
579 if(which)
580 vio->write_timeout_ms= timeout_ms;
581 else
582 vio->read_timeout_ms= timeout_ms;
586 #ifdef HAVE_SMEM
588 size_t vio_read_shared_memory(Vio * vio, uchar* buf, size_t size)
590 size_t length;
591 size_t remain_local;
592 char *current_position;
593 HANDLE events[2];
595 DBUG_ENTER("vio_read_shared_memory");
596 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %d", vio->sd, (long) buf,
597 size));
599 remain_local = size;
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
613 anything
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)
619 DBUG_RETURN(-1);
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;
627 length = size;
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))
644 DBUG_RETURN(-1);
646 } while (remain_local);
647 length = size;
649 DBUG_PRINT("exit", ("%lu", (ulong) length));
650 DBUG_RETURN(length);
654 size_t vio_write_shared_memory(Vio * vio, const uchar* buf, size_t size)
656 size_t length, remain, sz;
657 HANDLE pos;
658 const uchar *current_position;
659 HANDLE events[2];
661 DBUG_ENTER("vio_write_shared_memory");
662 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %d", vio->sd, (long) buf,
663 size));
665 remain = size;
666 current_position = buf;
668 events[0]= vio->event_server_read;
669 events[1]= vio->event_conn_closed;
671 while (remain != 0)
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 :
680 remain);
682 int4store(vio->handle_map,sz);
683 pos = vio->handle_map + 4;
684 memcpy(pos,current_position,sz);
685 remain-=sz;
686 current_position+=sz;
687 if (!SetEvent(vio->event_client_wrote))
688 DBUG_RETURN((size_t) -1);
690 length = size;
692 DBUG_PRINT("exit", ("%lu", (ulong) length));
693 DBUG_RETURN(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)
703 int error_count= 0;
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
709 connection is closed
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)
718 error_count++;
719 DBUG_PRINT("vio_error", ("UnmapViewOfFile() failed"));
721 if (CloseHandle(vio->event_server_wrote) == 0)
723 error_count++;
724 DBUG_PRINT("vio_error", ("CloseHandle(vio->esw) failed"));
726 if (CloseHandle(vio->event_server_read) == 0)
728 error_count++;
729 DBUG_PRINT("vio_error", ("CloseHandle(vio->esr) failed"));
731 if (CloseHandle(vio->event_client_wrote) == 0)
733 error_count++;
734 DBUG_PRINT("vio_error", ("CloseHandle(vio->ecw) failed"));
736 if (CloseHandle(vio->event_client_read) == 0)
738 error_count++;
739 DBUG_PRINT("vio_error", ("CloseHandle(vio->ecr) failed"));
741 if (CloseHandle(vio->handle_file_map) == 0)
743 error_count++;
744 DBUG_PRINT("vio_error", ("CloseHandle(vio->hfm) failed"));
746 if (CloseHandle(vio->event_conn_closed) == 0)
748 error_count++;
749 DBUG_PRINT("vio_error", ("CloseHandle(vio->ecc) failed"));
752 vio->type= VIO_CLOSED;
753 vio->sd= -1;
754 DBUG_RETURN(error_count);
756 #endif /* HAVE_SMEM */
757 #endif /* __WIN__ */