39
40
#define DRIZZLE_UNIX_SOCKET_PATH "/tmp/mysql.socket"
41
static std::string unix_socket_path(DRIZZLE_UNIX_SOCKET_PATH);
43
42
namespace po= boost::program_options;
43
namespace fs= boost::filesystem;
44
44
using namespace drizzled;
45
45
using namespace std;
47
47
namespace mysql_unix_socket_protocol
50
static bool clobber= false;
50
52
Protocol::~Protocol()
54
fs::remove(unix_socket_path);
54
57
const char* Protocol::getHost(void) const
56
return DRIZZLE_UNIX_SOCKET_PATH;
59
return unix_socket_path.file_string().c_str();
59
62
in_port_t Protocol::getPort(void) const
66
69
const module::option_map &vm= context.getOptions();
70
unix_socket_path.clear();
71
unix_socket_path.append(vm["path"].as<string>());
74
context.add(new Protocol("mysql_unix_socket_protocol", true));
71
fs::path uds_path(vm["path"].as<fs::path>());
72
if (not fs::exists(uds_path))
74
context.add(new Protocol("mysql_unix_socket_protocol",
77
context.registerVariable(new sys_var_const_string_val("path", fs::system_complete(uds_path).file_string()));
78
context.registerVariable(new sys_var_bool_ptr_readonly("clobber", &clobber));
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;
79
91
bool Protocol::getFileDescriptors(std::vector<int> &fds)
81
struct sockaddr_un servAddr;
85
95
if ((unix_sock= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
101
// In case we restart and find something in our way we move it aside and
102
// then attempt to remove it.
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());
113
(void) setsockopt(unix_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, sizeof(arg));
114
unlink(unix_socket_path.file_string().c_str());
116
struct sockaddr_un servAddr;
91
117
memset(&servAddr, 0, sizeof(servAddr));
93
119
servAddr.sun_family= AF_UNIX;
94
strcpy(servAddr.sun_path, unix_socket_path.c_str());
95
(void) unlink(unix_socket_path.c_str());
99
(void) setsockopt(unix_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, sizeof(arg));
101
addrlen= sizeof(servAddr);
120
if (unix_socket_path.file_string().size() > sizeof(servAddr.sun_path))
122
std::cerr << "Unix Socket Path length too long. Must be under "
123
<< sizeof(servAddr.sun_path) << " bytes." << endl;
126
memcpy(servAddr.sun_path, unix_socket_path.file_string().c_str(), sizeof(servAddr.sun_path)-1);
128
socklen_t addrlen= sizeof(servAddr);
102
129
if (::bind(unix_sock, reinterpret_cast<sockaddr *>(&servAddr), addrlen) < 0)
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";
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;
111
if (listen(unix_sock,(int) 1000) < 0)
138
if (listen(unix_sock, (int) 1000) < 0)
113
140
std::cerr << "listen() on Unix socket failed with error " << errno << "\n";
117
std::cerr << "Listening on " << unix_socket_path.c_str() << "\n";
144
std::cerr << "Listening on " << unix_socket_path << "\n";
146
(void) unlink(unix_socket_path.file_string().c_str());
120
148
fds.push_back(unix_sock);
126
154
static void init_options(drizzled::module::option_context &context)
129
po::value<string>()->default_value(unix_socket_path),
157
po::value<fs::path>()->default_value(DRIZZLE_UNIX_SOCKET_PATH),
130
158
N_("Path used for MySQL UNIX Socket Protocol."));
160
N_("Clobber socket file if one is there already."));
133
static drizzle_sys_var* sys_variables[]= {
137
164
} /* namespace mysql_unix_socket_protocol */
139
DRIZZLE_PLUGIN(mysql_unix_socket_protocol::init, mysql_unix_socket_protocol::sys_variables, mysql_unix_socket_protocol::init_options);
166
DRIZZLE_PLUGIN(mysql_unix_socket_protocol::init, NULL, mysql_unix_socket_protocol::init_options);