~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
22
#include "config.h"
23
#include <drizzled/gettext.h>
24
#include <drizzled/error.h>
25
#include <drizzled/query_id.h>
26
#include <drizzled/sql_state.h>
27
#include <drizzled/session.h>
28
#include "drizzled/internal/my_sys.h"
29
#include "drizzled/internal/m_string.h"
30
#include <algorithm>
31
#include <iostream>
32
#include <boost/program_options.hpp>
1945.1.4 by Monty Taylor
Fixes the issues with socket
33
#include <boost/filesystem.hpp>
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
34
#include <drizzled/module/option_map.h>
35
36
#include <sys/un.h>
37
38
#include "plugin/mysql_unix_socket_protocol/protocol.h"
39
40
#define DRIZZLE_UNIX_SOCKET_PATH "/tmp/mysql.socket"
41
42
namespace po= boost::program_options;
1945.1.4 by Monty Taylor
Fixes the issues with socket
43
namespace fs= boost::filesystem;
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
44
using namespace drizzled;
45
using namespace std;
46
47
namespace mysql_unix_socket_protocol
48
{
49
1945.1.7 by Monty Taylor
Added clobber option which will make drizzle delete/overwrite an existing
50
static bool clobber= false;
51
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
52
Protocol::~Protocol()
53
{
1945.1.4 by Monty Taylor
Fixes the issues with socket
54
  fs::remove(unix_socket_path);
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
55
}
56
57
const char* Protocol::getHost(void) const
58
{
1945.1.4 by Monty Taylor
Fixes the issues with socket
59
  return unix_socket_path.file_string().c_str();
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
60
}
61
62
in_port_t Protocol::getPort(void) const
63
{
1888 by Brian Aker
Merge in POTFILES changes.
64
  return 0;
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
65
}
66
67
static int init(drizzled::module::Context &context)
68
{  
69
  const module::option_map &vm= context.getOptions();
70
1945.1.4 by Monty Taylor
Fixes the issues with socket
71
  fs::path uds_path(vm["path"].as<fs::path>());
72
  if (not fs::exists(uds_path))
73
  {
74
    context.add(new Protocol("mysql_unix_socket_protocol",
75
                             true,
76
                             uds_path));
1945.1.5 by Monty Taylor
zomg. I was passing in a reference to a temporary. Jeeze.
77
    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
78
    context.registerVariable(new sys_var_bool_ptr_readonly("clobber", &clobber));
1945.1.4 by Monty Taylor
Fixes the issues with socket
79
  }
80
  else
81
  {
82
    cerr << uds_path << _(" exists already. Do you have another Drizzle or "
83
                          "MySQL running? Or perhaps the file is stale and "
84
                          "should be removed?") << std::endl;
1945.1.6 by Monty Taylor
Make it non-fatal.
85
    return 0;
1945.1.4 by Monty Taylor
Fixes the issues with socket
86
  }
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
87
88
  return 0;
89
}
90
91
bool Protocol::getFileDescriptors(std::vector<int> &fds)
92
{
93
  int unix_sock;
94
95
  if ((unix_sock= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
96
  {
97
    std::cerr << "Can't start server : UNIX Socket";
98
    return false;
99
  }
100
1945.1.1 by Brian Aker
We move the unix socket file aside, and then start up. Any clients already
101
  // In case we restart and find something in our way we move it aside and
102
  // then attempt to remove it.
1945.1.7 by Monty Taylor
Added clobber option which will make drizzle delete/overwrite an existing
103
  if (clobber)
1945.1.1 by Brian Aker
We move the unix socket file aside, and then start up. Any clients already
104
  {
1945.1.4 by Monty Taylor
Fixes the issues with socket
105
    fs::path move_file(unix_socket_path.file_string() + ".old");
106
    fs::rename(unix_socket_path, move_file);
107
    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
108
  }
109
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
110
111
  int arg= 1;
112
113
  (void) setsockopt(unix_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, sizeof(arg));
1945.1.4 by Monty Taylor
Fixes the issues with socket
114
  unlink(unix_socket_path.file_string().c_str());
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
115
1945.1.8 by Monty Taylor
We were overrunning the buffer everywhere, but glibc on ubuntu was
116
  struct sockaddr_un servAddr;
117
  memset(&servAddr, 0, sizeof(servAddr));
118
119
  servAddr.sun_family= AF_UNIX;
120
  if (unix_socket_path.file_string().size() > sizeof(servAddr.sun_path))
121
  {
122
    std::cerr << "Unix Socket Path length too long. Must be under "
123
      << sizeof(servAddr.sun_path) << " bytes." << endl;
124
    return false;
125
  }
126
  memcpy(servAddr.sun_path, unix_socket_path.file_string().c_str(), sizeof(servAddr.sun_path)-1);
127
128
  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
129
  if (::bind(unix_sock, reinterpret_cast<sockaddr *>(&servAddr), addrlen) < 0)
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
130
  { 
1945.1.4 by Monty Taylor
Fixes the issues with socket
131
    std::cerr << "Can't start server : Bind on unix socket." << std::endl;
132
    std::cerr << "Do you already have another of drizzled or mysqld running on socket: " << unix_socket_path << "?" << std::endl;
133
    std::cerr << "Can't start server : UNIX Socket" << std::endl;
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
134
135
    return false;
136
  }
137
1945.1.4 by Monty Taylor
Fixes the issues with socket
138
  if (listen(unix_sock, (int) 1000) < 0)
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
139
  {
140
    std::cerr << "listen() on Unix socket failed with error " << errno << "\n";
141
  }
142
  else
143
  {
1945.1.4 by Monty Taylor
Fixes the issues with socket
144
    std::cerr << "Listening on " << unix_socket_path << "\n";
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
145
  }
1945.1.7 by Monty Taylor
Added clobber option which will make drizzle delete/overwrite an existing
146
  (void) unlink(unix_socket_path.file_string().c_str());
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
147
148
  fds.push_back(unix_sock);
149
150
  return false;
151
}
152
153
154
static void init_options(drizzled::module::option_context &context)
155
{
156
  context("path",
1945.1.4 by Monty Taylor
Fixes the issues with socket
157
          po::value<fs::path>()->default_value(DRIZZLE_UNIX_SOCKET_PATH),
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
158
          N_("Path used for MySQL UNIX Socket Protocol."));
1945.1.7 by Monty Taylor
Added clobber option which will make drizzle delete/overwrite an existing
159
  context("clobber",
160
          N_("Clobber socket file if one is there already."));
161
1887.1.1 by Brian Aker
Adding uninux socket protocol for real.
162
}
163
164
} /* namespace mysql_unix_socket_protocol */
165
1945.1.4 by Monty Taylor
Fixes the issues with socket
166
DRIZZLE_PLUGIN(mysql_unix_socket_protocol::init, NULL, mysql_unix_socket_protocol::init_options);