drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
listen.cc
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008 Sun Microsystems, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <config.h>
21
22#include <drizzled/errmsg_print.h>
23#include <drizzled/error.h>
24#include <drizzled/gettext.h>
25#include <drizzled/plugin/listen.h>
26#include <drizzled/plugin/listen.h>
27#include <drizzled/plugin/null_client.h>
28
29#include <sys/socket.h>
30
31#include <sys/epoll.h>
32#include <unistd.h>
33
34namespace drizzled {
35namespace plugin {
36
37static std::vector<plugin::Listen*> listen_list;
38std::vector<plugin::Listen*> listen_fd_list;
39std::vector<int> listen_fds;
40int epoll_fd= -1;
41int wakeup_pipe[2];
42
43/*
44 Sentinel stored in epoll_event.data.u32 for the wakeup pipe. Every other
45 registered descriptor stores its index into listen_fd_list so a ready event
46 can be mapped straight back to the owning protocol plugin.
47*/
48static const uint32_t WAKEUP_EVENT= UINT32_MAX;
49
50/*
51 Maximum number of ready events epoll_wait reports per call. Anything not
52 returned in a given call surfaces on the next iteration of the accept loop,
53 so this only needs to comfortably exceed the realistic number of bound
54 listening sockets. Used both to size the buffer and as the maxevents
55 argument so the two cannot drift apart.
56*/
57static const int MAX_EPOLL_EVENTS= 16;
58
59ListenVector& Listen::getListenProtocols()
60{
61 return listen_list;
62}
63
65{
66 listen_list.push_back(&listen_obj);
67 return false;
68}
69
71{
72 listen_list.erase(std::remove(listen_list.begin(), listen_list.end(), &listen_obj), listen_list.end());
73}
74
76{
77 epoll_fd= epoll_create1(EPOLL_CLOEXEC);
78 if (epoll_fd == -1)
79 {
80 sql_perror("epoll_create1()");
81 return true;
82 }
83
84 for (plugin::Listen* it : listen_list)
85 {
86 std::vector<int> fds;
87 if (it->getFileDescriptors(fds))
88 {
89 errmsg_printf(error::ERROR, _("Error getting file descriptors"));
90 return true;
91 }
92
93 for (int fd : fds)
94 {
95 epoll_event ev;
96 ev.events= EPOLLIN;
97 ev.data.u32= listen_fd_list.size();
98 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1)
99 {
100 sql_perror("epoll_ctl(EPOLL_CTL_ADD)");
101 return true;
102 }
103 listen_fd_list.push_back(it);
104 listen_fds.push_back(fd);
105 }
106 }
107
108 if (listen_fds.empty())
109 {
110 errmsg_printf(error::ERROR, _("No sockets could be bound for listening"));
111 return true;
112 }
113
114 /*
115 We need a pipe to wakeup the listening thread since some operating systems
116 are stupid. *cough* OSX *cough*
117 */
118 if (pipe(wakeup_pipe) == -1)
119 {
120 sql_perror("pipe()");
121 return true;
122 }
123
124 epoll_event ev;
125 ev.events= EPOLLIN;
126 ev.data.u32= WAKEUP_EVENT;
127 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, wakeup_pipe[0], &ev) == -1)
128 {
129 sql_perror("epoll_ctl(EPOLL_CTL_ADD)");
130 return true;
131 }
132
133 return false;
134}
135
137{
138 epoll_event events[MAX_EPOLL_EVENTS];
139
140 while (1)
141 {
142 int ready= epoll_wait(epoll_fd, events, MAX_EPOLL_EVENTS, -1);
143 if (ready == -1)
144 {
145 if (errno != EINTR)
146 {
147 sql_perror("epoll_wait()");
148 }
149 continue;
150 }
151 else if (ready == 0)
152 continue;
153
154 for (int x= 0; x < ready; x++)
155 {
156 if (not (events[x].events & EPOLLIN))
157 continue;
158
159 /* Check to see if the wakeup_pipe was written to. */
160 if (events[x].data.u32 == WAKEUP_EVENT)
161 {
162 /* Close all listening file descriptors now. */
163 for (int fd : listen_fds)
164 {
165 (void) ::shutdown(fd, SHUT_RDWR);
166 (void) close(fd);
167 }
168
169 (void) close(wakeup_pipe[0]);
170 (void) close(wakeup_pipe[1]);
171 (void) close(epoll_fd);
172 epoll_fd= -1;
173
174 return NULL;
175 }
176
177 uint32_t index= events[x].data.u32;
178 if (plugin::Client* client= listen_fd_list[index]->getClient(listen_fds[index]))
179 return client;
180 }
181 }
182}
183
188
190{
191 ssize_t ret= write(wakeup_pipe[1], "\0", 1);
192 if (ret == -1)
193 {
194 sql_perror("write()");
195 }
196 else if (ret == 0)
197 {
198 errmsg_printf(error::ERROR, _("write(1) failed for the the wakeup_pipe during shutdown"));
199 }
200 assert(ret == 1);
201}
202
203} /* namespace plugin */
204} /* namespace drizzled */
static bool addPlugin(Listen &)
Definition listen.cc:64
virtual plugin::Client * getClient(int fd)=0
static bool setup(void)
Definition listen.cc:75
static plugin::Client * serveConnections()
Definition listen.cc:136
static void shutdown()
Definition listen.cc:189
static plugin::Client * getNullClient()
Definition listen.cc:184
static void removePlugin(Listen &)
Definition listen.cc:70
TODO: Rename this file - func.h is stupid.