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