~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2010 Brian Aker
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */


#include "config.h"
#include <drizzled/gettext.h>
#include <drizzled/error.h>
#include <drizzled/query_id.h>
#include <drizzled/sql_state.h>
#include <drizzled/session.h>
#include "drizzled/internal/my_sys.h"
#include "drizzled/internal/m_string.h"
#include <algorithm>
#include <iostream>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <drizzled/module/option_map.h>

#include <sys/un.h>

#include "plugin/mysql_unix_socket_protocol/protocol.h"

#define DRIZZLE_UNIX_SOCKET_PATH "/tmp/mysql.socket"

namespace po= boost::program_options;
namespace fs= boost::filesystem;
using namespace drizzled;
using namespace std;

namespace drizzle_plugin
{
namespace mysql_unix_socket_protocol
{

static bool clobber= false;

ProtocolCounters *Protocol::mysql_unix_counters= new ProtocolCounters();

Protocol::~Protocol()
{
  fs::remove(_unix_socket_path);
}

in_port_t Protocol::getPort(void) const
{
  return 0;
}

static int init(drizzled::module::Context &context)
{  
  const module::option_map &vm= context.getOptions();

  fs::path uds_path(vm["path"].as<fs::path>());
  if (not fs::exists(uds_path))
  {
    Protocol *listen_obj= new Protocol("mysql_unix_socket_protocol",
                             true,
                             uds_path);
    context.add(listen_obj);
    context.registerVariable(new sys_var_const_string_val("path", fs::system_complete(uds_path).file_string()));
    context.registerVariable(new sys_var_bool_ptr_readonly("clobber", &clobber));
    context.registerVariable(new sys_var_uint32_t_ptr("max-connections", &Protocol::mysql_unix_counters->max_connections));
  }
  else
  {
    cerr << uds_path << _(" exists already. Do you have another Drizzle or "
                          "MySQL running? Or perhaps the file is stale and "
                          "should be removed?") << std::endl;
    return 0;
  }

  return 0;
}

bool Protocol::getFileDescriptors(std::vector<int> &fds)
{
  int unix_sock;

  if ((unix_sock= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
  {
    std::cerr << "Can't start server : UNIX Socket";
    return false;
  }

  // In case we restart and find something in our way we move it aside and
  // then attempt to remove it.
  if (clobber)
  {
    fs::path move_file(_unix_socket_path.file_string() + ".old");
    fs::rename(_unix_socket_path, move_file);
    unlink(move_file.file_string().c_str());
  }


  int arg= 1;

  (void) setsockopt(unix_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, sizeof(arg));
  unlink(_unix_socket_path.file_string().c_str());

  struct sockaddr_un servAddr;
  memset(&servAddr, 0, sizeof(servAddr));

  servAddr.sun_family= AF_UNIX;
  if (_unix_socket_path.file_string().size() > sizeof(servAddr.sun_path))
  {
    std::cerr << "Unix Socket Path length too long. Must be under "
      << sizeof(servAddr.sun_path) << " bytes." << endl;
    return false;
  }
  memcpy(servAddr.sun_path, _unix_socket_path.file_string().c_str(), min(sizeof(servAddr.sun_path)-1,_unix_socket_path.file_string().size()));

  socklen_t addrlen= sizeof(servAddr);
  if (::bind(unix_sock, reinterpret_cast<sockaddr *>(&servAddr), addrlen) < 0)
  { 
    std::cerr << "Can't start server : Bind on unix socket." << std::endl;
    std::cerr << "Do you already have another of drizzled or mysqld running on socket: " << _unix_socket_path << "?" << std::endl;
    std::cerr << "Can't start server : UNIX Socket" << std::endl;

    return false;
  }

  if (listen(unix_sock, (int) 1000) < 0)
  {
    std::cerr << "listen() on Unix socket failed with error " << errno << "\n";
  }
  else
  {
    std::cerr << "Listening on " << _unix_socket_path << "\n";
  }
  (void) unlink(_unix_socket_path.file_string().c_str());

  fds.push_back(unix_sock);

  return false;
}

plugin::Client *Protocol::getClient(int fd)
{
  int new_fd;
  new_fd= acceptTcp(fd);
  if (new_fd == -1)
    return NULL;

  return new ClientMySQLUnixSocketProtocol(new_fd, _using_mysql41_protocol, getCounters());
}

static void init_options(drizzled::module::option_context &context)
{
  context("path",
          po::value<fs::path>()->default_value(DRIZZLE_UNIX_SOCKET_PATH),
          N_("Path used for MySQL UNIX Socket Protocol."));
  context("clobber",
          N_("Clobber socket file if one is there already."));
  context("max-connections",
          po::value<uint32_t>(&Protocol::mysql_unix_counters->max_connections)->default_value(1000),
          N_("Maximum simultaneous connections."));
}

} /* namespace mysql_unix_socket_protocol */
} /* namespace drizzle_plugin */

DRIZZLE_PLUGIN(drizzle_plugin::mysql_unix_socket_protocol::init, NULL, drizzle_plugin::mysql_unix_socket_protocol::init_options);