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
24 #include <libtest/common.h>
26 #include <libmemcached-1.0/memcached.h>
27 #include <libmemcachedutil-1.0/util.h>
29 using namespace libtest
;
38 #include <sys/types.h>
42 #include <libtest/server.h>
43 #include <libtest/wait.h>
45 #include <libtest/memcached.h>
47 #ifndef __INTEL_COMPILER
48 #pragma GCC diagnostic ignored "-Wold-style-cast"
51 using namespace libtest
;
53 class Memcached
: public libtest::Server
55 std::string _username
;
56 std::string _password
;
59 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
) :
60 libtest::Server(host_arg
, port_arg
, is_socket_arg
),
61 _username(username_arg
),
62 _password(password_arg
)
65 Memcached(const std::string
& host_arg
, const in_port_t port_arg
, const bool is_socket_arg
) :
66 libtest::Server(host_arg
, port_arg
, is_socket_arg
)
71 virtual const char *sasl() const
76 const std::string
& password() const
81 const std::string
& username() const
86 pid_t
get_pid(bool error_is_ok
)
88 // Memcached is slow to start, so we need to do this
89 if (pid_file().empty() == false)
92 wait_for_pidfile() == false)
94 Error
<< "Pidfile was not found:" << pid_file();
100 memcached_return_t rc
= MEMCACHED_SUCCESS
;
103 if (socket().empty())
108 local_pid
= libmemcached_util_getpid(socket().c_str(), port(), &rc
);
112 local_pid
= libmemcached_util_getpid(hostname().c_str(), port(), &rc
);
115 if (error_is_ok
and ((memcached_failed(rc
) or not is_pid_valid(local_pid
))))
117 Error
<< "libmemcached_util_getpid(" << memcached_strerror(NULL
, rc
) << ") pid: " << local_pid
<< " for:" << *this;
125 // Memcached is slow to start, so we need to do this
126 if (not pid_file().empty())
128 if (wait_for_pidfile() == false)
130 Error
<< "Pidfile was not found:" << pid_file();
135 memcached_return_t rc
;
140 ret
= libmemcached_util_ping(socket().c_str(), 0, &rc
);
144 ret
= libmemcached_util_ping(hostname().c_str(), port(), &rc
);
147 if (memcached_failed(rc
) or not ret
)
149 Error
<< "libmemcached_util_ping(" << hostname() << ", " << port() << ") error: " << memcached_strerror(NULL
, rc
);
160 const char *executable()
162 return MEMCACHED_BINARY
;
165 virtual void pid_file_option(Application
& app
, const std::string
& arg
)
167 if (arg
.empty() == false)
169 app
.add_option("-P", arg
);
173 const char *socket_file_option() const
178 const char *daemon_file_option()
183 virtual void port_option(Application
& app
, in_port_t arg
)
186 snprintf(buffer
, sizeof(buffer
), "%d", int(arg
));
187 app
.add_option("-p", buffer
);
190 bool has_port_option() const
195 bool has_socket_file_option() const
200 void socket_file_option(Application
& app
, const std::string
& socket_arg
)
202 if (socket_arg
.empty() == false)
204 app
.add_option("-s", socket_arg
);
213 bool broken_socket_cleanup()
218 // Memcached's pidfile is broken
219 bool broken_pid_file()
224 bool build(size_t argc
, const char *argv
[]);
227 class MemcachedLight
: public libtest::Server
231 MemcachedLight(const std::string
& host_arg
, const in_port_t port_arg
):
232 libtest::Server(host_arg
, port_arg
)
237 pid_t
get_pid(bool error_is_ok
)
239 // Memcached is slow to start, so we need to do this
240 if (pid_file().empty() == false)
242 if (error_is_ok
and wait_for_pidfile() == false)
244 Error
<< "Pidfile was not found:" << pid_file();
250 std::stringstream error_message
;
251 pid_t local_pid
= get_pid_from_file(pid_file(), error_message
);
254 if (::kill(local_pid
, 0) > 0)
260 if (error_is_ok
and ((success
or not is_pid_valid(local_pid
))))
262 Error
<< "kill(" << " pid: " << local_pid
<< " errno:" << strerror(errno
) << " for:" << *this;
270 // Memcached is slow to start, so we need to do this
271 if (not pid_file().empty())
273 if (not wait_for_pidfile())
275 Error
<< "Pidfile was not found:" << pid_file();
280 std::stringstream error_message
;
281 pid_t local_pid
= get_pid_from_file(pid_file(), error_message
);
284 if (::kill(local_pid
, 0) == 0)
295 return "memcached_light";
298 const char *executable()
300 return MEMCACHED_LIGHT_BINARY
;
303 const char *daemon_file_option()
308 virtual void port_option(Application
& app
, in_port_t arg
)
311 snprintf(buffer
, sizeof(buffer
), "--port=%d", int(arg
));
312 app
.add_option(buffer
);
315 bool has_port_option() const
325 void log_file_option(Application
& app
, const std::string
& arg
)
327 if (arg
.empty() == false)
329 std::string
buffer("--log-file=");
331 app
.add_option("--verbose");
332 app
.add_option(buffer
);
336 bool has_log_file_option() const
341 bool build(size_t argc
, const char *argv
[]);
344 class MemcachedSaSL
: public Memcached
347 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
) :
348 Memcached(host_arg
, port_arg
, is_socket_arg
, username_arg
, password_arg
)
353 return "memcached-sasl";
356 const char *sasl() const
358 return " -S -B binary ";
361 const char *executable()
363 return MEMCACHED_SASL_BINARY
;
366 pid_t
get_pid(bool error_is_ok
)
368 // Memcached is slow to start, so we need to do this
369 if (pid_file().empty() == false)
372 wait_for_pidfile() == false)
374 Error
<< "Pidfile was not found:" << pid_file();
380 memcached_return_t rc
;
383 local_pid
= libmemcached_util_getpid2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc
);
387 local_pid
= libmemcached_util_getpid2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc
);
390 if (error_is_ok
and ((memcached_failed(rc
) or not is_pid_valid(local_pid
))))
392 Error
<< "libmemcached_util_getpid2(" << memcached_strerror(NULL
, rc
) << ") username: " << username() << " password: " << password() << " pid: " << local_pid
<< " for:" << *this;
400 // Memcached is slow to start, so we need to do this
401 if (not pid_file().empty())
403 if (not wait_for_pidfile())
405 Error
<< "Pidfile was not found:" << pid_file();
410 memcached_return_t rc
;
415 ret
= libmemcached_util_ping2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc
);
419 ret
= libmemcached_util_ping2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc
);
422 if (memcached_failed(rc
) or not ret
)
424 Error
<< "libmemcached_util_ping2(" << hostname() << ", " << port() << ", " << username() << ", " << password() << ") error: " << memcached_strerror(NULL
, rc
);
435 bool Memcached::build(size_t argc
, const char *argv
[])
437 std::stringstream arg_buffer
;
439 if (getuid() == 0 or geteuid() == 0)
441 add_option("-u", "root");
444 add_option("-l", "localhost");
445 add_option("-m", "128");
453 for (int x
= 0 ; x
< argc
; x
++)
461 bool MemcachedLight::build(size_t argc
, const char *argv
[])
463 for (int x
= 0 ; x
< argc
; x
++)
473 libtest::Server
*build_memcached(const std::string
& hostname
, const in_port_t try_port
)
475 return new Memcached(hostname
, try_port
, false);
478 libtest::Server
*build_memcached_socket(const std::string
& socket_file
, const in_port_t try_port
)
480 return new Memcached(socket_file
, try_port
, true);
483 libtest::Server
*build_memcached_light(const std::string
& hostname
, const in_port_t try_port
)
485 return new MemcachedLight(hostname
, try_port
);
489 libtest::Server
*build_memcached_sasl(const std::string
& hostname
, const in_port_t try_port
, const std::string
& username
, const std::string
&password
)
491 if (username
.empty())
493 return new MemcachedSaSL(hostname
, try_port
, false, "memcached", "memcached");
496 return new MemcachedSaSL(hostname
, try_port
, false, username
, password
);
499 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
)
501 if (username
.empty())
503 return new MemcachedSaSL(socket_file
, try_port
, true, "memcached", "memcached");
506 return new MemcachedSaSL(socket_file
, try_port
, true, username
, password
);