~skinny.moey/drizzle/tlog_no_changes_enabled

« back to all changes in this revision

Viewing changes to plugin/mysql_unix_socket_protocol/protocol.cc

  • Committer: Brian Aker
  • Date: 2010-10-28 17:12:01 UTC
  • mfrom: (1887.1.3 merge)
  • Revision ID: brian@tangent.org-20101028171201-baj6l1bnntn1s4ad
Merge in POTFILES changes.

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
  int unix_sock;
 
83
 
 
84
  if ((unix_sock= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
 
85
  {
 
86
    std::cerr << "Can't start server : UNIX Socket";
 
87
    return false;
 
88
  }
 
89
 
 
90
  memset(&servAddr, 0, sizeof(servAddr));
 
91
 
 
92
  servAddr.sun_family= AF_UNIX;
 
93
  strcpy(servAddr.sun_path, unix_socket_path.c_str());
 
94
  (void) unlink(unix_socket_path.c_str());
 
95
 
 
96
  int arg= 1;
 
97
 
 
98
  (void) setsockopt(unix_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, sizeof(arg));
 
99
 
 
100
  if (bind(unix_sock, reinterpret_cast<struct sockaddr *>(&servAddr), sizeof(servAddr)) < 0)
 
101
  { 
 
102
    std::cerr << "Can't start server : Bind on unix socket\n";
 
103
    std::cerr << "Do you already have another of drizzled or mysqld running on socket: " << "/tmp/mysql.socket" << "?\n";
 
104
    std::cerr << "Can't start server : UNIX Socket";
 
105
 
 
106
    return false;
 
107
  }
 
108
 
 
109
  if (listen(unix_sock,(int) 1000) < 0)
 
110
  {
 
111
    std::cerr << "listen() on Unix socket failed with error " << errno << "\n";
 
112
  }
 
113
  else
 
114
  {
 
115
    std::cerr << "Listening on " << unix_socket_path.c_str() << "\n";
 
116
  }
 
117
 
 
118
  fds.push_back(unix_sock);
 
119
 
 
120
  return false;
 
121
}
 
122
 
 
123
 
 
124
static void init_options(drizzled::module::option_context &context)
 
125
{
 
126
  context("path",
 
127
          po::value<string>()->default_value(unix_socket_path),
 
128
          N_("Path used for MySQL UNIX Socket Protocol."));
 
129
}
 
130
 
 
131
static drizzle_sys_var* sys_variables[]= {
 
132
  NULL
 
133
};
 
134
 
 
135
} /* namespace mysql_unix_socket_protocol */
 
136
 
 
137
DRIZZLE_PLUGIN(mysql_unix_socket_protocol::init, mysql_unix_socket_protocol::sys_variables, mysql_unix_socket_protocol::init_options);