1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
/* Copyright (C) 2000 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/*
Note that we can't have assertion on file descriptors; The reason for
this is that during mysql shutdown, another thread can close a file
we are working on. In this case we should just return read errors from
the file descriptior.
*/
#include <config.h>
#include "vio.h"
#include <string.h>
#include <drizzled/util/test.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <sys/poll.h>
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <algorithm>
#include <cstdlib>
#include <cassert>
#include <cstdio>
#include <fcntl.h>
using namespace std;
namespace drizzle_plugin {
Vio::Vio(int nsd) :
sd(nsd)
{
/*
We call fcntl() to set the flags and then immediately read them back
to make sure that we and the system are in agreement on the state of
things.
An example of why we need to do this is FreeBSD (and apparently some
other BSD-derived systems, like Mac OS X), where the system sometimes
reports that the socket is set for non-blocking when it really will
block.
*/
fcntl(sd, F_SETFL, 0);
fcntl_mode= fcntl(sd, F_GETFL);
}
Vio::~Vio()
{
close();
}
int Vio::close()
{
int r= 0;
if (sd != -1)
{
assert(sd >= 0);
if (shutdown(sd, SHUT_RDWR))
r= -1;
if (::close(sd))
r= -1;
sd= -1;
}
return r;
}
size_t Vio::read(unsigned char* buf, size_t size)
{
return ::read(sd, buf, size);
}
size_t Vio::write(const unsigned char* buf, size_t size)
{
return ::write(sd, buf, size);
}
int Vio::blocking(bool set_blocking_mode, bool *old_mode)
{
int r=0;
// make sure ptr is not NULL:
if (NULL != old_mode)
*old_mode= drizzled::test(!(fcntl_mode & O_NONBLOCK));
if (sd >= 0)
{
int old_fcntl=fcntl_mode;
if (set_blocking_mode)
fcntl_mode &= ~O_NONBLOCK; /* clear bit */
else
fcntl_mode |= O_NONBLOCK; /* set bit */
if (old_fcntl != fcntl_mode)
{
r= fcntl(sd, F_SETFL, fcntl_mode);
if (r == -1)
{
fcntl_mode= old_fcntl;
}
}
}
return r;
}
int Vio::fastsend()
{
int nodelay = 1;
int error= setsockopt(sd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay));
if (error)
perror("setsockopt");
return error;
}
int32_t Vio::keepalive(bool set_keep_alive)
{
uint32_t opt= set_keep_alive;
int r= setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof(opt));
if (r)
{
perror("setsockopt");
assert(false);
}
return r;
}
bool Vio::should_retry() const
{
int en = errno;
return en == EAGAIN || en == EINTR || en == EWOULDBLOCK;
}
bool Vio::was_interrupted() const
{
int en= errno;
return en == EAGAIN || en == EINTR || en == EWOULDBLOCK || en == ETIMEDOUT;
}
bool Vio::peer_addr(char *buf, size_t buflen, uint16_t& port) const
{
char port_buf[NI_MAXSERV];
sockaddr_storage remote;
socklen_t al = sizeof(remote);
if (getpeername(sd, (sockaddr *) (&remote), &al) != 0)
return true;
if (getnameinfo((struct sockaddr *)(&remote), al, buf, buflen, port_buf, NI_MAXSERV, NI_NUMERICHOST|NI_NUMERICSERV))
{
return true;
}
port= (uint16_t)strtol(port_buf, (char **)NULL, 10);
return false;
}
void Vio::timeout(bool is_sndtimeo, int32_t t)
{
timeval wait_timeout;
wait_timeout.tv_sec= t;
wait_timeout.tv_usec= 0;
assert(t >= 0 && t <= INT32_MAX);
assert(sd != -1);
int error= setsockopt(sd, SOL_SOCKET, is_sndtimeo ? SO_SNDTIMEO : SO_RCVTIMEO, &wait_timeout,
(socklen_t)sizeof(struct timeval));
if (error == -1 && errno != ENOPROTOOPT)
{
perror("setsockopt");
assert(error == 0);
}
}
int Vio::get_errno() const
{
return errno;
}
int Vio::get_fd() const
{
return sd;
}
} /* namespace drizzle_plugin */
|