~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/mysql_unix_socket_protocol/protocol.cc

merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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>
 
33
#include <drizzled/module/option_map.h>
 
34
 
 
35
#include <sys/un.h>
 
36
 
 
37
#include "plugin/mysql_unix_socket_protocol/protocol.h"
 
38
 
 
39
#define DRIZZLE_UNIX_SOCKET_PATH "/tmp/mysql.socket"
 
40
 
 
41
static std::string unix_socket_path(DRIZZLE_UNIX_SOCKET_PATH);
 
42
 
 
43
namespace po= boost::program_options;
 
44
using namespace drizzled;
 
45
using namespace std;
 
46
 
 
47
namespace mysql_unix_socket_protocol
 
48
{
 
49
 
 
50
Protocol::~Protocol()
 
51
{
 
52
}
 
53
 
 
54
const char* Protocol::getHost(void) const
 
55
{
 
56
  return DRIZZLE_UNIX_SOCKET_PATH;
 
57
}
 
58
 
 
59
in_port_t Protocol::getPort(void) const
 
60
{
 
61
  return 0;
 
62
}
 
63
 
 
64
static int init(drizzled::module::Context &context)
 
65
{  
 
66
  const module::option_map &vm= context.getOptions();
 
67
 
 
68
  if (vm.count("path"))
 
69
  {
 
70
    unix_socket_path.clear();
 
71
    unix_socket_path.append(vm["path"].as<string>());
 
72
  }
 
73
 
 
74
  context.add(new Protocol("mysql_unix_socket_protocol", true));
 
75
 
 
76
  return 0;
 
77
}
 
78
 
 
79
bool Protocol::getFileDescriptors(std::vector<int> &fds)
 
80
{
 
81
  struct sockaddr_un servAddr;
 
82
  socklen_t addrlen;
 
83
  int unix_sock;
 
84
 
 
85
  if ((unix_sock= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
 
86
  {
 
87
    std::cerr << "Can't start server : UNIX Socket";
 
88
    return false;
 
89
  }
 
90
 
 
91
  memset(&servAddr, 0, sizeof(servAddr));
 
92
 
 
93
  servAddr.sun_family= AF_UNIX;
 
94
  strcpy(servAddr.sun_path, unix_socket_path.c_str());
 
95
  (void) unlink(unix_socket_path.c_str());
 
96
 
 
97
  int arg= 1;
 
98
 
 
99
  (void) setsockopt(unix_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, sizeof(arg));
 
100
 
 
101
  addrlen= sizeof(servAddr);
 
102
  if (::bind(unix_sock, reinterpret_cast<sockaddr *>(&servAddr), addrlen) < 0)
 
103
  { 
 
104
    std::cerr << "Can't start server : Bind on unix socket\n";
 
105
    std::cerr << "Do you already have another of drizzled or mysqld running on socket: " << unix_socket_path << "?\n";
 
106
    std::cerr << "Can't start server : UNIX Socket";
 
107
 
 
108
    return false;
 
109
  }
 
110
 
 
111
  if (listen(unix_sock,(int) 1000) < 0)
 
112
  {
 
113
    std::cerr << "listen() on Unix socket failed with error " << errno << "\n";
 
114
  }
 
115
  else
 
116
  {
 
117
    std::cerr << "Listening on " << unix_socket_path.c_str() << "\n";
 
118
  }
 
119
 
 
120
  fds.push_back(unix_sock);
 
121
 
 
122
  return false;
 
123
}
 
124
 
 
125
 
 
126
static void init_options(drizzled::module::option_context &context)
 
127
{
 
128
  context("path",
 
129
          po::value<string>()->default_value(unix_socket_path),
 
130
          N_("Path used for MySQL UNIX Socket Protocol."));
 
131
}
 
132
 
 
133
static drizzle_sys_var* sys_variables[]= {
 
134
  NULL
 
135
};
 
136
 
 
137
} /* namespace mysql_unix_socket_protocol */
 
138
 
 
139
DRIZZLE_PLUGIN(mysql_unix_socket_protocol::init, mysql_unix_socket_protocol::sys_variables, mysql_unix_socket_protocol::init_options);