~drizzle-trunk/drizzle/development

1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2010 Brian Aker
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
20
21
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
22
#include <config.h>
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
23
#include <drizzled/gettext.h>
24
#include <drizzled/error.h>
25
#include <drizzled/session.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
26
#include <drizzled/internal/my_sys.h>
27
#include <drizzled/internal/m_string.h>
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
28
#include <algorithm>
29
#include <iostream>
30
#include <boost/program_options.hpp>
1945.1.4 by Monty Taylor
Fixes the issues with socket
31
#include <boost/filesystem.hpp>
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
32
#include <drizzled/module/option_map.h>
33
34
#include <sys/un.h>
35
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
36
#include <plugin/mysql_unix_socket_protocol/protocol.h>
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
37
38
#define DRIZZLE_UNIX_SOCKET_PATH "/tmp/mysql.socket"
39
40
namespace po= boost::program_options;
1945.1.4 by Monty Taylor
Fixes the issues with socket
41
namespace fs= boost::filesystem;
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
42
using namespace drizzled;
43
using namespace std;
44
2318.3.3 by Olaf van der Spek
Refactor
45
namespace drizzle_plugin {
46
namespace mysql_unix_socket_protocol {
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
47
1945.1.7 by Monty Taylor
Added clobber option which will make drizzle delete/overwrite an existing
48
static bool clobber= false;
49
2318.3.3 by Olaf van der Spek
Refactor
50
ProtocolCounters Protocol::mysql_unix_counters;
1953.2.5 by Andrew Hutchings
Make max-connections unique per-protocol
51
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
52
Protocol::~Protocol()
53
{
1964.2.9 by Monty Taylor
All protocol stuff except for the buffer_length. WTF?
54
  fs::remove(_unix_socket_path);
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
55
}
56
2318.2.19 by Olaf van der Spek
Merge trunk
57
in_port_t Protocol::getPort() const
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
58
{
1888 by Brian Aker
Merge in POTFILES changes.
59
  return 0;
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
60
}
61
62
static int init(drizzled::module::Context &context)
63
{  
64
  const module::option_map &vm= context.getOptions();
65
1945.1.4 by Monty Taylor
Fixes the issues with socket
66
  fs::path uds_path(vm["path"].as<fs::path>());
67
  if (not fs::exists(uds_path))
68
  {
2318.2.40 by Olaf van der Spek
Refactor
69
    Protocol *listen_obj= new Protocol("mysql_unix_socket_protocol", uds_path);
2131.7.1 by Andrew Hutchings
Add protocol counters table
70
    listen_obj->addCountersToTable();
1953.2.6 by Andrew Hutchings
Completely redo max-connections so it is independant per-protocol. Also make counters independant
71
    context.add(listen_obj);
1945.1.5 by Monty Taylor
zomg. I was passing in a reference to a temporary. Jeeze.
72
    context.registerVariable(new sys_var_const_string_val("path", fs::system_complete(uds_path).file_string()));
1945.1.7 by Monty Taylor
Added clobber option which will make drizzle delete/overwrite an existing
73
    context.registerVariable(new sys_var_bool_ptr_readonly("clobber", &clobber));
2318.3.3 by Olaf van der Spek
Refactor
74
    context.registerVariable(new sys_var_uint32_t_ptr("max-connections", &Protocol::mysql_unix_counters.max_connections));
1945.1.4 by Monty Taylor
Fixes the issues with socket
75
  }
76
  else
77
  {
78
    cerr << uds_path << _(" exists already. Do you have another Drizzle or "
79
                          "MySQL running? Or perhaps the file is stale and "
80
                          "should be removed?") << std::endl;
1945.1.6 by Monty Taylor
Make it non-fatal.
81
    return 0;
1945.1.4 by Monty Taylor
Fixes the issues with socket
82
  }
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
83
84
  return 0;
85
}
86
87
bool Protocol::getFileDescriptors(std::vector<int> &fds)
88
{
2318.3.3 by Olaf van der Spek
Refactor
89
  int unix_sock= socket(AF_UNIX, SOCK_STREAM, 0);
90
  if (unix_sock < 0)
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
91
  {
92
    std::cerr << "Can't start server : UNIX Socket";
93
    return false;
94
  }
95
1945.1.1 by Brian Aker
We move the unix socket file aside, and then start up. Any clients already
96
  // In case we restart and find something in our way we move it aside and
97
  // then attempt to remove it.
1945.1.7 by Monty Taylor
Added clobber option which will make drizzle delete/overwrite an existing
98
  if (clobber)
1945.1.1 by Brian Aker
We move the unix socket file aside, and then start up. Any clients already
99
  {
1964.2.9 by Monty Taylor
All protocol stuff except for the buffer_length. WTF?
100
    fs::path move_file(_unix_socket_path.file_string() + ".old");
101
    fs::rename(_unix_socket_path, move_file);
1945.1.4 by Monty Taylor
Fixes the issues with socket
102
    unlink(move_file.file_string().c_str());
1945.1.1 by Brian Aker
We move the unix socket file aside, and then start up. Any clients already
103
  }
104
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
105
106
  int arg= 1;
107
108
  (void) setsockopt(unix_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, sizeof(arg));
1964.2.9 by Monty Taylor
All protocol stuff except for the buffer_length. WTF?
109
  unlink(_unix_socket_path.file_string().c_str());
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
110
2318.3.3 by Olaf van der Spek
Refactor
111
  sockaddr_un servAddr;
1945.1.8 by Monty Taylor
We were overrunning the buffer everywhere, but glibc on ubuntu was
112
  memset(&servAddr, 0, sizeof(servAddr));
113
114
  servAddr.sun_family= AF_UNIX;
1964.2.9 by Monty Taylor
All protocol stuff except for the buffer_length. WTF?
115
  if (_unix_socket_path.file_string().size() > sizeof(servAddr.sun_path))
1945.1.8 by Monty Taylor
We were overrunning the buffer everywhere, but glibc on ubuntu was
116
  {
117
    std::cerr << "Unix Socket Path length too long. Must be under "
118
      << sizeof(servAddr.sun_path) << " bytes." << endl;
119
    return false;
120
  }
1964.2.9 by Monty Taylor
All protocol stuff except for the buffer_length. WTF?
121
  memcpy(servAddr.sun_path, _unix_socket_path.file_string().c_str(), min(sizeof(servAddr.sun_path)-1,_unix_socket_path.file_string().size()));
1945.1.8 by Monty Taylor
We were overrunning the buffer everywhere, but glibc on ubuntu was
122
123
  socklen_t addrlen= sizeof(servAddr);
1891.1.1 by Monty Taylor
Fixed an issue that pops up when compiling with -std=c++0x turned on. Bind
124
  if (::bind(unix_sock, reinterpret_cast<sockaddr *>(&servAddr), addrlen) < 0)
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
125
  { 
1945.1.4 by Monty Taylor
Fixes the issues with socket
126
    std::cerr << "Can't start server : Bind on unix socket." << std::endl;
1964.2.9 by Monty Taylor
All protocol stuff except for the buffer_length. WTF?
127
    std::cerr << "Do you already have another of drizzled or mysqld running on socket: " << _unix_socket_path << "?" << std::endl;
1945.1.4 by Monty Taylor
Fixes the issues with socket
128
    std::cerr << "Can't start server : UNIX Socket" << std::endl;
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
129
130
    return false;
131
  }
132
1945.1.4 by Monty Taylor
Fixes the issues with socket
133
  if (listen(unix_sock, (int) 1000) < 0)
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
134
  {
135
    std::cerr << "listen() on Unix socket failed with error " << errno << "\n";
136
  }
137
  else
138
  {
2215.3.2 by Andrew Hutchings
Make mysql_protocol Listening message INFO level.
139
    errmsg_printf(error::INFO, _("Listening on %s"), _unix_socket_path.file_string().c_str());
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
140
  }
141
142
  fds.push_back(unix_sock);
143
144
  return false;
145
}
146
1960.2.6 by Andrew Hutchings
Add ip address limitation for admin connections
147
plugin::Client *Protocol::getClient(int fd)
148
{
2318.3.3 by Olaf van der Spek
Refactor
149
  int new_fd= acceptTcp(fd);
2318.2.39 by Olaf van der Spek
Refactor
150
  return new_fd == -1 ? NULL : new ClientMySQLProtocol(new_fd, getCounters());
1960.2.6 by Andrew Hutchings
Add ip address limitation for admin connections
151
}
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
152
153
static void init_options(drizzled::module::option_context &context)
154
{
155
  context("path",
1945.1.4 by Monty Taylor
Fixes the issues with socket
156
          po::value<fs::path>()->default_value(DRIZZLE_UNIX_SOCKET_PATH),
2068.4.1 by Andrew Hutchings
Fix intl domain
157
          _("Path used for MySQL UNIX Socket Protocol."));
1945.1.7 by Monty Taylor
Added clobber option which will make drizzle delete/overwrite an existing
158
  context("clobber",
2068.4.1 by Andrew Hutchings
Fix intl domain
159
          _("Clobber socket file if one is there already."));
1953.2.3 by Andrew Hutchings
Add UNIX socket protocol variable
160
  context("max-connections",
2318.3.3 by Olaf van der Spek
Refactor
161
          po::value<uint32_t>(&Protocol::mysql_unix_counters.max_connections)->default_value(1000),
2068.4.1 by Andrew Hutchings
Fix intl domain
162
          _("Maximum simultaneous connections."));
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
163
}
164
165
} /* namespace mysql_unix_socket_protocol */
1964.2.9 by Monty Taylor
All protocol stuff except for the buffer_length. WTF?
166
} /* namespace drizzle_plugin */
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
167
1964.2.9 by Monty Taylor
All protocol stuff except for the buffer_length. WTF?
168
DRIZZLE_PLUGIN(drizzle_plugin::mysql_unix_socket_protocol::init, NULL, drizzle_plugin::mysql_unix_socket_protocol::init_options);