126
144
r= fcntl(vio->sd, F_SETFL, vio->fcntl_mode);
147
DBUG_PRINT("info", ("fcntl failed, errno %d", errno));
129
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));
138
drizzleclient_vio_is_blocking(Vio * vio)
181
vio_is_blocking(Vio * vio)
184
DBUG_ENTER("vio_is_blocking");
141
185
r = !(vio->fcntl_mode & O_NONBLOCK);
147
int drizzleclient_vio_fastsend(Vio * vio)
153
error= setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY,
154
&nodelay, sizeof(nodelay));
157
perror("setsockopt");
164
int32_t drizzleclient_vio_keepalive(Vio* vio, bool set_keep_alive)
172
r= setsockopt(vio->sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof(opt));
175
perror("setsockopt");
184
drizzleclient_vio_should_retry(Vio * vio)
188
return (en == EAGAIN || en == EINTR ||
194
drizzleclient_vio_was_interrupted(Vio *vio)
198
return (en == EAGAIN || en == EINTR ||
199
en == EWOULDBLOCK || en == ETIMEDOUT);
203
int drizzleclient_vio_close(Vio * vio)
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);
206
275
if (vio->type != VIO_CLOSED)
208
assert(vio->sd >= 0);
277
DBUG_ASSERT(vio->sd >= 0);
209
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) */
214
288
vio->type= VIO_CLOSED;
221
const char *drizzleclient_vio_description(Vio * vio)
294
const char *vio_description(Vio * vio)
223
296
return vio->desc;
226
enum enum_vio_type drizzleclient_vio_type(Vio* vio)
299
enum enum_vio_type vio_type(Vio* vio)
228
301
return vio->type;
231
int drizzleclient_vio_fd(Vio* vio)
304
my_socket vio_fd(Vio* vio)
236
bool drizzleclient_vio_peer_addr(Vio *vio, char *buf, uint16_t *port, size_t buflen)
309
my_bool vio_peer_addr(Vio *vio, char *buf, uint16 *port, size_t buflen)
239
char port_buf[NI_MAXSERV];
240
socklen_t addrLen = sizeof(vio->remote);
242
if (getpeername(vio->sd, (struct sockaddr *) (&vio->remote),
247
vio->addrLen= (int)addrLen;
249
if ((error= getnameinfo((struct sockaddr *)(&vio->remote),
252
port_buf, NI_MAXSERV, NI_NUMERICHOST|NI_NUMERICSERV)))
257
*port= (uint16_t)strtol(port_buf, (char **)NULL, 10);
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));
263
359
/* Return 0 if there is data to be read */
265
bool drizzleclient_vio_poll_read(Vio *vio, int32_t timeout)
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)
267
383
struct pollfd fds;
385
DBUG_ENTER("vio_poll");
271
387
fds.events=POLLIN;
273
389
if ((res=poll(&fds,1,(int) timeout*1000)) <= 0)
275
return res < 0 ? false : true; /* Don't return 1 on errors */
391
DBUG_RETURN(res < 0 ? 0 : 1); /* Don't return 1 on errors */
277
return (fds.revents & (POLLIN | POLLERR | POLLHUP) ? false : true);
393
DBUG_RETURN(fds.revents & (POLLIN | POLLERR | POLLHUP) ? 0 : 1);
281
bool drizzleclient_vio_peek_read(Vio *vio, uint32_t *bytes)
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)
284
416
ssize_t res= recv(vio->sd, &buf, sizeof(buf), MSG_PEEK);
288
*bytes= (uint32_t)res;
292
void drizzleclient_vio_timeout(Vio *vio, bool is_sndtimeo, int32_t timeout)
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;
296
435
/* POSIX specifies time as struct timeval. */
297
436
struct timeval wait_timeout;
298
437
wait_timeout.tv_sec= timeout;
299
438
wait_timeout.tv_usec= 0;
301
assert(timeout >= 0 && timeout <= INT32_MAX);
302
assert(vio->sd != -1);
303
error= setsockopt(vio->sd, SOL_SOCKET, is_sndtimeo ? SO_SNDTIMEO : SO_RCVTIMEO,
305
(socklen_t)sizeof(struct timeval));
306
if (error == -1 && errno != ENOPROTOOPT)
308
perror("setsockopt");
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 */