23 #include <drizzled/gettext.h>
24 #include <drizzled/error.h>
25 #include <drizzled/session.h>
26 #include <drizzled/internal/my_sys.h>
27 #include <drizzled/internal/m_string.h>
30 #include <boost/program_options.hpp>
31 #include <boost/filesystem.hpp>
38 #include <plugin/mysql_unix_socket_protocol/protocol.h>
40 #define DRIZZLE_UNIX_SOCKET_PATH "/var/run/drizzle/mysql.socket"
42 namespace po= boost::program_options;
43 namespace fs= boost::filesystem;
44 using namespace drizzled;
47 namespace drizzle_plugin {
48 namespace mysql_unix_socket_protocol {
50 static bool clobber=
false;
52 ProtocolCounters Protocol::mysql_unix_counters;
56 fs::remove(_unix_socket_path);
59 in_port_t Protocol::getPort()
const
66 char at_exit_socket_file[1024 * 4]= { 0 };
68 static void remove_socket_file(
void)
70 if (at_exit_socket_file[0])
72 if (unlink(at_exit_socket_file) == -1)
74 std::cerr <<
"Could not remove socket: " << at_exit_socket_file <<
"(" << strerror(errno) <<
")" << std::endl;
77 at_exit_socket_file[0]= 0;
86 fs::path uds_path(vm[
"path"].as<fs::path>());
87 if (not fs::exists(uds_path))
89 Protocol *listen_obj=
new Protocol(
"mysql_unix_socket_protocol", uds_path);
90 listen_obj->addCountersToTable();
91 context.add(listen_obj);
94 context.registerVariable(
new sys_var_uint32_t_ptr(
"max-connections", &Protocol::mysql_unix_counters.max_connections));
95 snprintf(at_exit_socket_file,
sizeof(at_exit_socket_file),
"%s", uds_path.file_string().c_str());
96 atexit(remove_socket_file);
100 cerr << uds_path << _(
" exists already. Do you have another Drizzle or "
101 "MySQL running? Or perhaps the file is stale and "
102 "should be removed?") << std::endl;
109 bool Protocol::getFileDescriptors(std::vector<int> &fds)
111 int unix_sock= socket(AF_UNIX, SOCK_STREAM, 0);
114 std::cerr <<
"Can't start server : UNIX Socket";
122 fs::path move_file(_unix_socket_path.file_string() +
".old");
123 fs::rename(_unix_socket_path, move_file);
124 unlink(move_file.file_string().c_str());
130 (void) setsockopt(unix_sock, SOL_SOCKET, SO_REUSEADDR, (
char*)&arg,
sizeof(arg));
131 unlink(_unix_socket_path.file_string().c_str());
133 sockaddr_un servAddr;
134 memset(&servAddr, 0,
sizeof(servAddr));
136 servAddr.sun_family= AF_UNIX;
137 if (_unix_socket_path.file_string().size() >
sizeof(servAddr.sun_path))
139 std::cerr <<
"Unix Socket Path length too long. Must be under "
140 <<
sizeof(servAddr.sun_path) <<
" bytes." << endl;
143 memcpy(servAddr.sun_path, _unix_socket_path.file_string().c_str(), min(
sizeof(servAddr.sun_path)-1,_unix_socket_path.file_string().size()));
145 socklen_t addrlen=
sizeof(servAddr);
146 if (::bind(unix_sock, reinterpret_cast<sockaddr *>(&servAddr), addrlen) < 0)
148 std::cerr <<
"Can't start server : Bind on unix socket." << std::endl;
149 std::cerr <<
"Do you already have another of drizzled or mysqld running on socket: " << _unix_socket_path <<
"?" << std::endl;
150 std::cerr <<
"Can't start server : UNIX Socket" << std::endl;
155 if (listen(unix_sock, (
int) 1000) < 0)
157 std::cerr <<
"listen() on Unix socket failed with error " << errno <<
"\n";
161 errmsg_printf(error::INFO, _(
"Listening on %s"), _unix_socket_path.file_string().c_str());
164 fds.push_back(unix_sock);
166 chmod(_unix_socket_path.file_string().c_str(),0777);
173 int new_fd= acceptTcp(fd);
180 po::value<fs::path>()->default_value(DRIZZLE_UNIX_SOCKET_PATH),
181 _(
"Path used for MySQL UNIX Socket Protocol."));
183 _(
"Clobber socket file if one is there already."));
184 context(
"max-connections",
185 po::value<uint32_t>(&Protocol::mysql_unix_counters.max_connections)->default_value(1000),
186 _(
"Maximum simultaneous connections."));
192 DRIZZLE_DECLARE_PLUGIN
195 "mysql_unix_socket_protocol",
198 N_(
"MySQL Unix socket protocol"),
200 drizzle_plugin::mysql_unix_socket_protocol::init,
202 drizzle_plugin::mysql_unix_socket_protocol::init_options,
204 DRIZZLE_DECLARE_PLUGIN_END;
An Proxy Wrapper around boost::program_options::variables_map.