00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <config.h>
00023 #include <drizzled/gettext.h>
00024 #include <drizzled/error.h>
00025 #include <drizzled/query_id.h>
00026 #include <drizzled/session.h>
00027 #include <drizzled/internal/my_sys.h>
00028 #include <drizzled/internal/m_string.h>
00029 #include <algorithm>
00030 #include <iostream>
00031 #include <boost/program_options.hpp>
00032 #include <boost/filesystem.hpp>
00033 #include <drizzled/module/option_map.h>
00034
00035 #include <sys/stat.h>
00036
00037 #include <sys/un.h>
00038
00039 #include <plugin/mysql_unix_socket_protocol/protocol.h>
00040
00041 #define DRIZZLE_UNIX_SOCKET_PATH "/tmp/mysql.socket"
00042
00043 namespace po= boost::program_options;
00044 namespace fs= boost::filesystem;
00045 using namespace drizzled;
00046 using namespace std;
00047
00048 namespace drizzle_plugin
00049 {
00050 namespace mysql_unix_socket_protocol
00051 {
00052
00053 static bool clobber= false;
00054
00055 ProtocolCounters *Protocol::mysql_unix_counters= new ProtocolCounters();
00056
00057 Protocol::~Protocol()
00058 {
00059 fs::remove(_unix_socket_path);
00060 }
00061
00062 in_port_t Protocol::getPort(void) const
00063 {
00064 return 0;
00065 }
00066
00067 static int init(drizzled::module::Context &context)
00068 {
00069 const module::option_map &vm= context.getOptions();
00070
00071 fs::path uds_path(vm["path"].as<fs::path>());
00072 if (not fs::exists(uds_path))
00073 {
00074 Protocol *listen_obj= new Protocol("mysql_unix_socket_protocol",
00075 true,
00076 uds_path);
00077 listen_obj->addCountersToTable();
00078 context.add(listen_obj);
00079 context.registerVariable(new sys_var_const_string_val("path", fs::system_complete(uds_path).file_string()));
00080 context.registerVariable(new sys_var_bool_ptr_readonly("clobber", &clobber));
00081 context.registerVariable(new sys_var_uint32_t_ptr("max-connections", &Protocol::mysql_unix_counters->max_connections));
00082 }
00083 else
00084 {
00085 cerr << uds_path << _(" exists already. Do you have another Drizzle or "
00086 "MySQL running? Or perhaps the file is stale and "
00087 "should be removed?") << std::endl;
00088 return 0;
00089 }
00090
00091 return 0;
00092 }
00093
00094 bool Protocol::getFileDescriptors(std::vector<int> &fds)
00095 {
00096 int unix_sock;
00097
00098 if ((unix_sock= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
00099 {
00100 std::cerr << "Can't start server : UNIX Socket";
00101 return false;
00102 }
00103
00104
00105
00106 if (clobber)
00107 {
00108 fs::path move_file(_unix_socket_path.file_string() + ".old");
00109 fs::rename(_unix_socket_path, move_file);
00110 unlink(move_file.file_string().c_str());
00111 }
00112
00113
00114 int arg= 1;
00115
00116 (void) setsockopt(unix_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, sizeof(arg));
00117 unlink(_unix_socket_path.file_string().c_str());
00118
00119 struct sockaddr_un servAddr;
00120 memset(&servAddr, 0, sizeof(servAddr));
00121
00122 servAddr.sun_family= AF_UNIX;
00123 if (_unix_socket_path.file_string().size() > sizeof(servAddr.sun_path))
00124 {
00125 std::cerr << "Unix Socket Path length too long. Must be under "
00126 << sizeof(servAddr.sun_path) << " bytes." << endl;
00127 return false;
00128 }
00129 memcpy(servAddr.sun_path, _unix_socket_path.file_string().c_str(), min(sizeof(servAddr.sun_path)-1,_unix_socket_path.file_string().size()));
00130
00131 socklen_t addrlen= sizeof(servAddr);
00132 if (::bind(unix_sock, reinterpret_cast<sockaddr *>(&servAddr), addrlen) < 0)
00133 {
00134 std::cerr << "Can't start server : Bind on unix socket." << std::endl;
00135 std::cerr << "Do you already have another of drizzled or mysqld running on socket: " << _unix_socket_path << "?" << std::endl;
00136 std::cerr << "Can't start server : UNIX Socket" << std::endl;
00137
00138 return false;
00139 }
00140
00141 if (listen(unix_sock, (int) 1000) < 0)
00142 {
00143 std::cerr << "listen() on Unix socket failed with error " << errno << "\n";
00144 }
00145 else
00146 {
00147 errmsg_printf(error::INFO, _("Listening on %s"), _unix_socket_path.file_string().c_str());
00148 }
00149
00150 fds.push_back(unix_sock);
00151
00152 chmod(_unix_socket_path.file_string().c_str(),0777);
00153
00154 return false;
00155 }
00156
00157 plugin::Client *Protocol::getClient(int fd)
00158 {
00159 int new_fd;
00160 new_fd= acceptTcp(fd);
00161 if (new_fd == -1)
00162 return NULL;
00163
00164 return new ClientMySQLUnixSocketProtocol(new_fd, _using_mysql41_protocol, getCounters());
00165 }
00166
00167 static void init_options(drizzled::module::option_context &context)
00168 {
00169 context("path",
00170 po::value<fs::path>()->default_value(DRIZZLE_UNIX_SOCKET_PATH),
00171 _("Path used for MySQL UNIX Socket Protocol."));
00172 context("clobber",
00173 _("Clobber socket file if one is there already."));
00174 context("max-connections",
00175 po::value<uint32_t>(&Protocol::mysql_unix_counters->max_connections)->default_value(1000),
00176 _("Maximum simultaneous connections."));
00177 }
00178
00179 }
00180 }
00181
00182 DRIZZLE_PLUGIN(drizzle_plugin::mysql_unix_socket_protocol::init, NULL, drizzle_plugin::mysql_unix_socket_protocol::init_options);