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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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
#include <drizzled/util/test.h>
26
#include <sys/socket.h>
28
#include <sys/types.h>
29
#include <netinet/tcp.h>
30
#include <netinet/in.h>
43
namespace drizzle_plugin
56
We call fcntl() to set the flags and then immediately read them back
57
to make sure that we and the system are in agreement on the state of
60
An example of why we need to do this is FreeBSD (and apparently some
61
other BSD-derived systems, like Mac OS X), where the system sometimes
62
reports that the socket is set for non-blocking when it really will
65
fcntl(sd, F_SETFL, 0);
66
fcntl_mode= fcntl(sd, F_GETFL);
81
if (shutdown(sd, SHUT_RDWR))
92
size_t Vio::read(unsigned char* buf, size_t size)
96
/* Ensure nobody uses vio_read_buff and vio_read simultaneously */
97
assert(read_end == read_pos);
98
r= ::read(sd, buf, size);
103
size_t Vio::write(const unsigned char* buf, size_t size)
107
r = ::write(sd, buf, size);
112
int Vio::blocking(bool set_blocking_mode, bool *old_mode)
116
// make sure ptr is not NULL:
117
if (NULL != old_mode)
118
*old_mode= drizzled::test(!(fcntl_mode & O_NONBLOCK));
122
int old_fcntl=fcntl_mode;
123
if (set_blocking_mode)
124
fcntl_mode &= ~O_NONBLOCK; /* clear bit */
126
fcntl_mode |= O_NONBLOCK; /* set bit */
127
if (old_fcntl != fcntl_mode)
129
r= fcntl(sd, F_SETFL, fcntl_mode);
132
fcntl_mode= old_fcntl;
145
error= setsockopt(sd, IPPROTO_TCP, TCP_NODELAY,
146
&nodelay, sizeof(nodelay));
149
perror("setsockopt");
155
int32_t Vio::keepalive(bool set_keep_alive)
163
r= setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof(opt));
166
perror("setsockopt");
173
bool Vio::should_retry() const
176
return (en == EAGAIN || en == EINTR ||
180
bool Vio::was_interrupted() const
183
return (en == EAGAIN || en == EINTR ||
184
en == EWOULDBLOCK || en == ETIMEDOUT);
187
bool Vio::peer_addr(char *buf, uint16_t *port, size_t buflen) const
190
char port_buf[NI_MAXSERV];
191
socklen_t al = sizeof(remote);
193
if (getpeername(sd, (struct sockaddr *) (&remote),
199
if ((error= getnameinfo((struct sockaddr *)(&remote),
202
port_buf, NI_MAXSERV, NI_NUMERICHOST|NI_NUMERICSERV)))
207
*port= (uint16_t)strtol(port_buf, (char **)NULL, 10);
212
void Vio::timeout(bool is_sndtimeo, int32_t t)
216
/* POSIX specifies time as struct timeval. */
217
struct timeval wait_timeout;
218
wait_timeout.tv_sec= t;
219
wait_timeout.tv_usec= 0;
221
assert(t >= 0 && t <= INT32_MAX);
223
error= setsockopt(sd, SOL_SOCKET, is_sndtimeo ? SO_SNDTIMEO : SO_RCVTIMEO,
225
(socklen_t)sizeof(struct timeval));
226
if (error == -1 && errno != ENOPROTOOPT)
228
perror("setsockopt");
233
int Vio::get_errno() const
238
int Vio::get_fd() const
244
char *Vio::get_read_pos() const
249
char *Vio::get_read_end() const
254
} /* namespace drizzle_plugin */