starfix: HoR fix!!! wave+team check fix+2 bos jalan...kurang LK nya + PR:lootannya...
[st4rcore.git] / dep / mysqllite / vio / viosocket.c
blobf73b890c6975d3449b9f9eecc1602617f6eb512b
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
20 the file descriptior.
23 #include "vio_priv.h"
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)
33 size_t r;
34 DBUG_ENTER("vio_read");
35 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio->sd, (long) buf,
36 (uint) size));
38 /* Ensure nobody uses vio_read_buff and vio_read simultaneously */
39 DBUG_ASSERT(vio->read_end == vio->read_pos);
40 #ifdef __WIN__
41 r = recv(vio->sd, buf, size,0);
42 #else
43 errno=0; /* For linux */
44 r = read(vio->sd, buf, size);
45 #endif /* __WIN__ */
46 #ifndef DBUG_OFF
47 if (r == (size_t) -1)
49 DBUG_PRINT("vio_error", ("Got error %d during read",errno));
51 #endif /* DBUG_OFF */
52 DBUG_PRINT("exit", ("%ld", (long) r));
53 DBUG_RETURN(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)
64 size_t rc;
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,
68 (uint) size));
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);
74 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)
86 if (rc > size)
88 vio->read_pos= vio->read_buffer + size;
89 vio->read_end= vio->read_buffer + rc;
90 rc= size;
92 memcpy(buf, vio->read_buffer, rc);
95 else
96 rc= vio_read(vio, buf, size);
97 DBUG_RETURN(rc);
98 #undef VIO_UNBUFFERED_READ_MIN_SIZE
102 size_t vio_write(Vio * vio, const uchar* buf, size_t size)
104 size_t r;
105 DBUG_ENTER("vio_write");
106 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio->sd, (long) buf,
107 (uint) size));
108 #ifdef __WIN__
109 r = send(vio->sd, buf, size,0);
110 #else
111 r = write(vio->sd, buf, size);
112 #endif /* __WIN__ */
113 #ifndef DBUG_OFF
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));
120 DBUG_RETURN(r);
123 int vio_blocking(Vio * vio __attribute__((unused)), my_bool set_blocking_mode,
124 my_bool *old_mode)
126 int r=0;
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)
135 if (vio->sd >= 0)
137 int old_fcntl=vio->fcntl_mode;
138 if (set_blocking_mode)
139 vio->fcntl_mode &= ~O_NONBLOCK; /* clear bit */
140 else
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);
145 if (r == -1)
147 DBUG_PRINT("info", ("fcntl failed, errno %d", errno));
148 vio->fcntl_mode= old_fcntl;
152 #else
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)
158 ulong arg;
159 int old_fcntl=vio->fcntl_mode;
160 if (set_blocking_mode)
162 arg = 0;
163 vio->fcntl_mode &= ~O_NONBLOCK; /* clear bit */
165 else
167 arg = 1;
168 vio->fcntl_mode |= O_NONBLOCK; /* set bit */
170 if (old_fcntl != vio->fcntl_mode)
171 r = ioctlsocket(vio->sd,FIONBIO,(void*) &arg);
173 else
174 r= test(!(vio->fcntl_mode & O_NONBLOCK)) != set_blocking_mode;
175 #endif /* !defined(__WIN__) */
176 DBUG_PRINT("exit", ("%d", r));
177 DBUG_RETURN(r);
180 my_bool
181 vio_is_blocking(Vio * vio)
183 my_bool r;
184 DBUG_ENTER("vio_is_blocking");
185 r = !(vio->fcntl_mode & O_NONBLOCK);
186 DBUG_PRINT("exit", ("%d", (int) r));
187 DBUG_RETURN(r);
191 int vio_fastsend(Vio * vio __attribute__((unused)))
193 int r=0;
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 */
202 if (!r)
204 #ifdef __WIN__
205 BOOL nodelay= 1;
206 #else
207 int nodelay = 1;
208 #endif
210 r= setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY,
211 IF_WIN(const char*, void*) &nodelay,
212 sizeof(nodelay));
215 if (r)
217 DBUG_PRINT("warning", ("Couldn't set socket option for fast send"));
218 r= -1;
220 DBUG_PRINT("exit", ("%d", r));
221 DBUG_RETURN(r);
224 int vio_keepalive(Vio* vio, my_bool set_keep_alive)
226 int r=0;
227 uint opt = 0;
228 DBUG_ENTER("vio_keepalive");
229 DBUG_PRINT("enter", ("sd: %d set_keep_alive: %d", vio->sd, (int)
230 set_keep_alive));
231 if (vio->type != VIO_TYPE_NAMEDPIPE)
233 if (set_keep_alive)
234 opt = 1;
235 r = setsockopt(vio->sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt,
236 sizeof(opt));
238 DBUG_RETURN(r);
242 my_bool
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);
251 my_bool
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)
262 int r=0;
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))
273 r= -1;
274 if (closesocket(vio->sd))
275 r= -1;
277 if (r)
279 DBUG_PRINT("vio_error", ("close() failed, error: %d",socket_errno));
280 /* FIXME: error handling (not critical for MySQL) */
282 vio->type= VIO_CLOSED;
283 vio->sd= -1;
284 DBUG_RETURN(r);
288 const char *vio_description(Vio * vio)
290 return vio->desc;
293 enum enum_vio_type vio_type(Vio* vio)
295 return vio->type;
298 my_socket vio_fd(Vio* vio)
300 return vio->sd;
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));
308 if (vio->localhost)
310 strmov(buf,"127.0.0.1");
311 *port= 0;
313 else
315 size_socket addrLen = sizeof(vio->remote);
316 if (getpeername(vio->sd, (struct sockaddr *) (&vio->remote),
317 &addrLen) != 0)
319 DBUG_PRINT("exit", ("getpeername gave error: %d", socket_errno));
320 DBUG_RETURN(1);
322 my_inet_ntoa(vio->remote.sin_addr,buf);
323 *port= ntohs(vio->remote.sin_port);
325 DBUG_PRINT("exit", ("addr: %s", buf));
326 DBUG_RETURN(0);
331 Get in_addr for a TCP/IP connection
333 SYNOPSIS
334 vio_in_addr()
335 vio vio handle
336 in put in_addr here
338 NOTES
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");
345 if (vio->localhost)
346 bzero((char*) in, sizeof(*in));
347 else
348 *in=vio->remote.sin_addr;
349 DBUG_VOID_RETURN;
353 /* Return 0 if there is data to be read */
355 my_bool vio_poll_read(Vio *vio,uint timeout)
357 #ifndef HAVE_POLL
358 return 0;
359 #else
360 struct pollfd fds;
361 int res;
362 DBUG_ENTER("vio_poll");
363 fds.fd=vio->sd;
364 fds.events=POLLIN;
365 fds.revents=0;
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);
371 #endif
375 void vio_timeout(Vio *vio, uint which, uint timeout)
377 #if defined(SO_SNDTIMEO) && defined(SO_RCVTIMEO)
378 int r;
379 DBUG_ENTER("vio_timeout");
382 #ifdef __WIN__
383 /* Windows expects time in milliseconds as int */
384 int wait_timeout= (int) timeout * 1000;
385 #else
386 /* POSIX specifies time as struct timeval. */
387 struct timeval wait_timeout;
388 wait_timeout.tv_sec= timeout;
389 wait_timeout.tv_usec= 0;
390 #endif
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));
398 #ifndef DBUG_OFF
399 if (r != 0)
400 DBUG_PRINT("error", ("setsockopt failed: %d, errno: %d", r, socket_errno));
401 #endif
403 DBUG_VOID_RETURN;
404 #else
406 Platforms not suporting setting of socket timeout should either use
407 thr_alarm or just run without read/write timeout(s)
409 #endif
413 #ifdef __WIN__
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)
420 DWORD length;
421 DWORD ret;
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)
428 or WAIT_TIMEOUT.
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",
440 GetLastError()));
441 DBUG_RETURN((size_t)-1);
444 DBUG_RETURN(length);
448 size_t vio_read_pipe(Vio * vio, uchar *buf, size_t size)
450 DWORD bytes_read;
451 size_t retval;
452 DBUG_ENTER("vio_read_pipe");
453 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio->sd, (long) buf,
454 (uint) size));
456 if (ReadFile(vio->hPipe, buf, (DWORD)size, &bytes_read,
457 &(vio->pipe_overlapped)))
459 retval= bytes_read;
461 else
463 if (GetLastError() != ERROR_IO_PENDING)
465 DBUG_PRINT("error",("ReadFile() returned last error %d",
466 GetLastError()));
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));
473 DBUG_RETURN(retval);
477 size_t vio_write_pipe(Vio * vio, const uchar* buf, size_t size)
479 DWORD bytes_written;
480 size_t retval;
481 DBUG_ENTER("vio_write_pipe");
482 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %u", vio->sd, (long) buf,
483 (uint) size));
485 if (WriteFile(vio->hPipe, buf, (DWORD)size, &bytes_written,
486 &(vio->pipe_overlapped)))
488 retval= bytes_written;
490 else
492 if (GetLastError() != ERROR_IO_PENDING)
494 DBUG_PRINT("vio_error",("WriteFile() returned last error %d",
495 GetLastError()));
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));
502 DBUG_RETURN(retval);
506 int vio_close_pipe(Vio * vio)
508 int r;
509 DBUG_ENTER("vio_close_pipe");
511 CloseHandle(vio->pipe_overlapped.hEvent);
512 DisconnectNamedPipe(vio->hPipe);
513 r= CloseHandle(vio->hPipe);
514 if (r)
516 DBUG_PRINT("vio_error", ("close() failed, error: %d",GetLastError()));
517 /* FIXME: error handling (not critical for MySQL) */
519 vio->type= VIO_CLOSED;
520 vio->sd= -1;
521 DBUG_RETURN(r);
525 void vio_win32_timeout(Vio *vio, uint which , uint timeout_sec)
527 DWORD timeout_ms;
529 Windows is measuring timeouts in milliseconds. Check for possible int
530 overflow.
532 if (timeout_sec > UINT_MAX/1000)
533 timeout_ms= INFINITE;
534 else
535 timeout_ms= timeout_sec * 1000;
537 /* which == 1 means "write", which == 0 means "read".*/
538 if(which)
539 vio->write_timeout_ms= timeout_ms;
540 else
541 vio->read_timeout_ms= timeout_ms;
545 #ifdef HAVE_SMEM
547 size_t vio_read_shared_memory(Vio * vio, uchar* buf, size_t size)
549 size_t length;
550 size_t remain_local;
551 char *current_postion;
552 HANDLE events[2];
554 DBUG_ENTER("vio_read_shared_memory");
555 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %d", vio->sd, (long) buf,
556 size));
558 remain_local = size;
559 current_postion=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
572 anything
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)
578 DBUG_RETURN(-1);
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;
586 length = size;
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))
603 DBUG_RETURN(-1);
605 } while (remain_local);
606 length = size;
608 DBUG_PRINT("exit", ("%lu", (ulong) length));
609 DBUG_RETURN(length);
613 size_t vio_write_shared_memory(Vio * vio, const uchar* buf, size_t size)
615 size_t length, remain, sz;
616 HANDLE pos;
617 const uchar *current_postion;
618 HANDLE events[2];
620 DBUG_ENTER("vio_write_shared_memory");
621 DBUG_PRINT("enter", ("sd: %d buf: 0x%lx size: %d", vio->sd, (long) buf,
622 size));
624 remain = size;
625 current_postion = buf;
627 events[0]= vio->event_server_read;
628 events[1]= vio->event_conn_closed;
630 while (remain != 0)
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 :
639 remain);
641 int4store(vio->handle_map,sz);
642 pos = vio->handle_map + 4;
643 memcpy(pos,current_postion,sz);
644 remain-=sz;
645 current_postion+=sz;
646 if (!SetEvent(vio->event_client_wrote))
647 DBUG_RETURN((size_t) -1);
649 length = size;
651 DBUG_PRINT("exit", ("%lu", (ulong) length));
652 DBUG_RETURN(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)
662 int error_count= 0;
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
668 connection is closed
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)
677 error_count++;
678 DBUG_PRINT("vio_error", ("UnmapViewOfFile() failed"));
680 if (CloseHandle(vio->event_server_wrote) == 0)
682 error_count++;
683 DBUG_PRINT("vio_error", ("CloseHandle(vio->esw) failed"));
685 if (CloseHandle(vio->event_server_read) == 0)
687 error_count++;
688 DBUG_PRINT("vio_error", ("CloseHandle(vio->esr) failed"));
690 if (CloseHandle(vio->event_client_wrote) == 0)
692 error_count++;
693 DBUG_PRINT("vio_error", ("CloseHandle(vio->ecw) failed"));
695 if (CloseHandle(vio->event_client_read) == 0)
697 error_count++;
698 DBUG_PRINT("vio_error", ("CloseHandle(vio->ecr) failed"));
700 if (CloseHandle(vio->handle_file_map) == 0)
702 error_count++;
703 DBUG_PRINT("vio_error", ("CloseHandle(vio->hfm) failed"));
705 if (CloseHandle(vio->event_conn_closed) == 0)
707 error_count++;
708 DBUG_PRINT("vio_error", ("CloseHandle(vio->ecc) failed"));
711 vio->type= VIO_CLOSED;
712 vio->sd= -1;
713 DBUG_RETURN(error_count);
715 #endif /* HAVE_SMEM */
716 #endif /* __WIN__ */