Drizzled Public API Documentation

protocol.cc

00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2010 Brian Aker
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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/un.h>
00036 
00037 #include <plugin/mysql_unix_socket_protocol/protocol.h>
00038 
00039 #define DRIZZLE_UNIX_SOCKET_PATH "/tmp/mysql.socket"
00040 
00041 namespace po= boost::program_options;
00042 namespace fs= boost::filesystem;
00043 using namespace drizzled;
00044 using namespace std;
00045 
00046 namespace drizzle_plugin
00047 {
00048 namespace mysql_unix_socket_protocol
00049 {
00050 
00051 static bool clobber= false;
00052 
00053 ProtocolCounters *Protocol::mysql_unix_counters= new ProtocolCounters();
00054 
00055 Protocol::~Protocol()
00056 {
00057   fs::remove(_unix_socket_path);
00058 }
00059 
00060 in_port_t Protocol::getPort(void) const
00061 {
00062   return 0;
00063 }
00064 
00065 static int init(drizzled::module::Context &context)
00066 {  
00067   const module::option_map &vm= context.getOptions();
00068 
00069   fs::path uds_path(vm["path"].as<fs::path>());
00070   if (not fs::exists(uds_path))
00071   {
00072     Protocol *listen_obj= new Protocol("mysql_unix_socket_protocol",
00073                              true,
00074                              uds_path);
00075     listen_obj->addCountersToTable();
00076     context.add(listen_obj);
00077     context.registerVariable(new sys_var_const_string_val("path", fs::system_complete(uds_path).file_string()));
00078     context.registerVariable(new sys_var_bool_ptr_readonly("clobber", &clobber));
00079     context.registerVariable(new sys_var_uint32_t_ptr("max-connections", &Protocol::mysql_unix_counters->max_connections));
00080   }
00081   else
00082   {
00083     cerr << uds_path << _(" exists already. Do you have another Drizzle or "
00084                           "MySQL running? Or perhaps the file is stale and "
00085                           "should be removed?") << std::endl;
00086     return 0;
00087   }
00088 
00089   return 0;
00090 }
00091 
00092 bool Protocol::getFileDescriptors(std::vector<int> &fds)
00093 {
00094   int unix_sock;
00095 
00096   if ((unix_sock= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
00097   {
00098     std::cerr << "Can't start server : UNIX Socket";
00099     return false;
00100   }
00101 
00102   // In case we restart and find something in our way we move it aside and
00103   // then attempt to remove it.
00104   if (clobber)
00105   {
00106     fs::path move_file(_unix_socket_path.file_string() + ".old");
00107     fs::rename(_unix_socket_path, move_file);
00108     unlink(move_file.file_string().c_str());
00109   }
00110 
00111 
00112   int arg= 1;
00113 
00114   (void) setsockopt(unix_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, sizeof(arg));
00115   unlink(_unix_socket_path.file_string().c_str());
00116 
00117   struct sockaddr_un servAddr;
00118   memset(&servAddr, 0, sizeof(servAddr));
00119 
00120   servAddr.sun_family= AF_UNIX;
00121   if (_unix_socket_path.file_string().size() > sizeof(servAddr.sun_path))
00122   {
00123     std::cerr << "Unix Socket Path length too long. Must be under "
00124       << sizeof(servAddr.sun_path) << " bytes." << endl;
00125     return false;
00126   }
00127   memcpy(servAddr.sun_path, _unix_socket_path.file_string().c_str(), min(sizeof(servAddr.sun_path)-1,_unix_socket_path.file_string().size()));
00128 
00129   socklen_t addrlen= sizeof(servAddr);
00130   if (::bind(unix_sock, reinterpret_cast<sockaddr *>(&servAddr), addrlen) < 0)
00131   { 
00132     std::cerr << "Can't start server : Bind on unix socket." << std::endl;
00133     std::cerr << "Do you already have another of drizzled or mysqld running on socket: " << _unix_socket_path << "?" << std::endl;
00134     std::cerr << "Can't start server : UNIX Socket" << std::endl;
00135 
00136     return false;
00137   }
00138 
00139   if (listen(unix_sock, (int) 1000) < 0)
00140   {
00141     std::cerr << "listen() on Unix socket failed with error " << errno << "\n";
00142   }
00143   else
00144   {
00145     errmsg_printf(error::INFO, _("Listening on %s"), _unix_socket_path.file_string().c_str());
00146   }
00147 
00148   fds.push_back(unix_sock);
00149 
00150   return false;
00151 }
00152 
00153 plugin::Client *Protocol::getClient(int fd)
00154 {
00155   int new_fd;
00156   new_fd= acceptTcp(fd);
00157   if (new_fd == -1)
00158     return NULL;
00159 
00160   return new ClientMySQLUnixSocketProtocol(new_fd, _using_mysql41_protocol, getCounters());
00161 }
00162 
00163 static void init_options(drizzled::module::option_context &context)
00164 {
00165   context("path",
00166           po::value<fs::path>()->default_value(DRIZZLE_UNIX_SOCKET_PATH),
00167           _("Path used for MySQL UNIX Socket Protocol."));
00168   context("clobber",
00169           _("Clobber socket file if one is there already."));
00170   context("max-connections",
00171           po::value<uint32_t>(&Protocol::mysql_unix_counters->max_connections)->default_value(1000),
00172           _("Maximum simultaneous connections."));
00173 }
00174 
00175 } /* namespace mysql_unix_socket_protocol */
00176 } /* namespace drizzle_plugin */
00177 
00178 DRIZZLE_PLUGIN(drizzle_plugin::mysql_unix_socket_protocol::init, NULL, drizzle_plugin::mysql_unix_socket_protocol::init_options);