3aba25430b20b35eabf0378dab5c3d659f45c096
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <libtest/common.h>
25 #include <libmemcached/memcached.h>
26 #include <libmemcached/util.h>
28 using namespace libtest
;
37 #include <sys/types.h>
41 #include <libtest/server.h>
42 #include <libtest/wait.h>
44 #include <libtest/memcached.h>
46 #ifndef __INTEL_COMPILER
47 #pragma GCC diagnostic ignored "-Wold-style-cast"
50 using namespace libtest
;
52 class Memcached
: public libtest::Server
54 std::string _username
;
55 std::string _password
;
58 Memcached(const std::string
& host_arg
, const in_port_t port_arg
, const bool is_socket_arg
, const std::string
& username_arg
, const std::string
& password_arg
) :
59 libtest::Server(host_arg
, port_arg
, is_socket_arg
),
60 _username(username_arg
),
61 _password(password_arg
)
64 Memcached(const std::string
& host_arg
, const in_port_t port_arg
, const bool is_socket_arg
) :
65 libtest::Server(host_arg
, port_arg
, is_socket_arg
)
70 virtual const char *sasl() const
75 const std::string
& password() const
80 const std::string
& username() const
85 pid_t
get_pid(bool error_is_ok
)
87 // Memcached is slow to start, so we need to do this
88 if (not pid_file().empty())
90 Wait
wait(pid_file(), 0);
92 if (error_is_ok
and not wait
.successful())
94 Error
<< "Pidfile was not found:" << pid_file();
100 memcached_return_t rc
;
103 if (username().empty())
105 local_pid
= libmemcached_util_getpid(socket().c_str(), 0, &rc
);
109 local_pid
= libmemcached_util_getpid2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc
);
114 if (username().empty())
116 local_pid
= libmemcached_util_getpid(hostname().c_str(), port(), &rc
);
120 local_pid
= libmemcached_util_getpid2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc
);
124 if (error_is_ok
and ((memcached_failed(rc
) or not is_pid_valid(local_pid
))))
126 Error
<< "libmemcached_util_getpid(" << memcached_strerror(NULL
, rc
) << ") pid: " << local_pid
<< " for:" << *this;
134 // Memcached is slow to start, so we need to do this
135 if (not pid_file().empty())
137 Wait
wait(pid_file(), 0);
139 if (not wait
.successful())
141 Error
<< "Pidfile was not found:" << pid_file();
146 memcached_return_t rc
;
151 if (username().empty())
153 ret
= libmemcached_util_ping(socket().c_str(), 0, &rc
);
157 ret
= libmemcached_util_ping2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc
);
162 if (username().empty())
164 ret
= libmemcached_util_ping(hostname().c_str(), port(), &rc
);
168 ret
= libmemcached_util_ping2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc
);
172 if (memcached_failed(rc
) or not ret
)
174 if (username().empty())
176 Error
<< "libmemcached_util_ping(" << hostname() << ", " << port() << ") error: " << memcached_strerror(NULL
, rc
);
180 Error
<< "libmemcached_util_ping2(" << hostname() << ", " << port() << ", " << username() << ", " << password() << ") error: " << memcached_strerror(NULL
, rc
);
192 const char *executable()
194 return MEMCACHED_BINARY
;
197 const char *pid_file_option()
202 const char *socket_file_option() const
207 const char *daemon_file_option()
212 const char *log_file_option()
217 const char *port_option()
227 bool broken_socket_cleanup()
232 // Memcached's pidfile is broken
233 bool broken_pid_file()
238 bool build(int argc
, const char *argv
[]);
241 class MemcachedSaSL
: public Memcached
244 MemcachedSaSL(const std::string
& host_arg
, const in_port_t port_arg
, const bool is_socket_arg
, const std::string
& username_arg
, const std::string
&password_arg
) :
245 Memcached(host_arg
, port_arg
, is_socket_arg
, username_arg
, password_arg
)
250 return "memcached-sasl";
253 const char *sasl() const
255 return " -S -B binary ";
258 const char *executable()
260 return MEMCACHED_SASL_BINARY
;
268 bool Memcached::build(int argc
, const char *argv
[])
270 std::stringstream arg_buffer
;
272 if (getuid() == 0 or geteuid() == 0)
274 arg_buffer
<< " -u root ";
277 arg_buffer
<< " -l 127.0.0.1 ";
278 arg_buffer
<< " -m 128 ";
279 arg_buffer
<< " -M ";
283 arg_buffer
<< sasl();
286 for (int x
= 1 ; x
< argc
; x
++)
288 arg_buffer
<< " " << argv
[x
] << " ";
291 set_extra_args(arg_buffer
.str());
298 libtest::Server
*build_memcached(const std::string
& hostname
, const in_port_t try_port
)
300 return new Memcached(hostname
, try_port
, false);
303 libtest::Server
*build_memcached_socket(const std::string
& socket_file
, const in_port_t try_port
)
305 return new Memcached(socket_file
, try_port
, true);
309 libtest::Server
*build_memcached_sasl(const std::string
& hostname
, const in_port_t try_port
, const std::string
& username
, const std::string
&password
)
311 return new MemcachedSaSL(hostname
, try_port
, false, username
, password
);
314 libtest::Server
*build_memcached_sasl_socket(const std::string
& socket_file
, const in_port_t try_port
, const std::string
& username
, const std::string
&password
)
316 return new MemcachedSaSL(socket_file
, try_port
, true, username
, password
);