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 "/tmp/mysql.socket"
42namespace po= boost::program_options;
43namespace fs= boost::filesystem;
48namespace mysql_unix_socket_protocol {
50static bool clobber=
false;
52ProtocolCounters Protocol::mysql_unix_counters;
56 fs::remove(_unix_socket_path);
66 char at_exit_socket_file[PATH_MAX]= { 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;
82static int init(drizzled::module::Context &context)
84 const module::option_map &vm= context.getOptions();
86 fs::path uds_path(vm[
"path"].as<fs::path>());
87 if (not fs::exists(uds_path))
90 listen_obj->addCountersToTable();
91 context.add(listen_obj);
92 context.registerVariable(
new sys_var_const_string_val(
"path", fs::system_complete(uds_path).
string()));
93 context.registerVariable(
new sys_var_bool_ptr_readonly(
"clobber", &clobber));
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.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;
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.string() +
".old");
123 fs::rename(_unix_socket_path, move_file);
124 unlink(move_file.string().c_str());
130 (void) setsockopt(unix_sock, SOL_SOCKET, SO_REUSEADDR, (
char*)&arg,
sizeof(arg));
131 unlink(_unix_socket_path.string().c_str());
133 sockaddr_un servAddr;
134 memset(&servAddr, 0,
sizeof(servAddr));
136 servAddr.sun_family= AF_UNIX;
137 if (_unix_socket_path.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.string().c_str(), min(
sizeof(servAddr.sun_path)-1,_unix_socket_path.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.string().c_str());
164 fds.push_back(unix_sock);
166 chmod(_unix_socket_path.string().c_str(),0777);
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."));
192DRIZZLE_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,
204DRIZZLE_DECLARE_PLUGIN_END;
bool getFileDescriptors(std::vector< int > &fds)
drizzled::plugin::Client * getClient(int fd)
in_port_t getPort(void) const
TODO: Rename this file - func.h is stupid.
An Proxy Wrapper around boost::program_options::variables_map.