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
;
54 bool is_memcached_libtool()
56 if (MEMCACHED_BINARY
and strcmp(MEMCACHED_BINARY
, "memcached/memcached") == 0)
65 class Memcached
: public libtest::Server
67 std::string _username
;
68 std::string _password
;
71 Memcached(const std::string
& host_arg
,
72 const in_port_t port_arg
,
73 const bool is_socket_arg
,
74 const std::string
& username_arg
,
75 const std::string
& password_arg
) :
76 libtest::Server(host_arg
, port_arg
,
77 MEMCACHED_BINARY
, is_memcached_libtool(), is_socket_arg
),
78 _username(username_arg
),
79 _password(password_arg
)
82 Memcached(const std::string
& host_arg
, const in_port_t port_arg
, const bool is_socket_arg
) :
83 libtest::Server(host_arg
, port_arg
,
84 MEMCACHED_BINARY
, is_memcached_libtool(), is_socket_arg
)
89 virtual const char *sasl() const
94 const std::string
& password() const
99 const std::string
& username() const
104 pid_t
get_pid(bool error_is_ok
)
106 // Memcached is slow to start, so we need to do this
107 if (pid_file().empty() == false)
110 wait_for_pidfile() == false)
112 Error
<< "Pidfile was not found:" << pid_file();
118 memcached_return_t rc
= MEMCACHED_SUCCESS
;
121 if (socket().empty())
126 local_pid
= libmemcached_util_getpid(socket().c_str(), port(), &rc
);
130 local_pid
= libmemcached_util_getpid(hostname().c_str(), port(), &rc
);
133 if (error_is_ok
and ((memcached_failed(rc
) or not is_pid_valid(local_pid
))))
135 Error
<< "libmemcached_util_getpid(" << memcached_strerror(NULL
, rc
) << ") pid: " << local_pid
<< " for:" << *this;
143 // Memcached is slow to start, so we need to do this
144 if (pid_file().empty() == false)
146 if (wait_for_pidfile() == false)
148 Error
<< "Pidfile was not found:" << pid_file();
153 memcached_return_t rc
;
158 ret
= libmemcached_util_ping(socket().c_str(), 0, &rc
);
162 ret
= libmemcached_util_ping(hostname().c_str(), port(), &rc
);
165 if (memcached_failed(rc
) or not ret
)
167 Error
<< "libmemcached_util_ping(" << hostname() << ", " << port() << ") error: " << memcached_strerror(NULL
, rc
);
178 const char *executable()
180 return MEMCACHED_BINARY
;
185 return is_memcached_libtool();
188 virtual void pid_file_option(Application
& app
, const std::string
& arg
)
190 if (arg
.empty() == false)
192 app
.add_option("-P", arg
);
196 const char *socket_file_option() const
201 virtual void port_option(Application
& app
, in_port_t arg
)
204 snprintf(buffer
, sizeof(buffer
), "%d", int(arg
));
205 app
.add_option("-p", buffer
);
208 bool has_port_option() const
213 bool has_socket_file_option() const
218 void socket_file_option(Application
& app
, const std::string
& socket_arg
)
220 if (socket_arg
.empty() == false)
222 app
.add_option("-s", socket_arg
);
226 bool broken_socket_cleanup()
231 // Memcached's pidfile is broken
232 bool broken_pid_file()
237 bool build(size_t argc
, const char *argv
[]);
240 class MemcachedLight
: public libtest::Server
244 MemcachedLight(const std::string
& host_arg
, const in_port_t port_arg
) :
245 libtest::Server(host_arg
, port_arg
, MEMCACHED_LIGHT_BINARY
, true)
250 pid_t
get_pid(bool error_is_ok
)
252 // Memcached is slow to start, so we need to do this
253 if (pid_file().empty() == false)
255 if (error_is_ok
and wait_for_pidfile() == false)
257 Error
<< "Pidfile was not found:" << pid_file();
263 std::stringstream error_message
;
264 pid_t local_pid
= get_pid_from_file(pid_file(), error_message
);
267 if (::kill(local_pid
, 0) > 0)
273 if (error_is_ok
and ((success
or not is_pid_valid(local_pid
))))
275 Error
<< "kill(" << " pid: " << local_pid
<< " errno:" << strerror(errno
) << " for:" << *this;
283 // Memcached is slow to start, so we need to do this
284 if (not pid_file().empty())
286 if (not wait_for_pidfile())
288 Error
<< "Pidfile was not found:" << pid_file();
293 std::stringstream error_message
;
294 pid_t local_pid
= get_pid_from_file(pid_file(), error_message
);
297 if (::kill(local_pid
, 0) == 0)
308 return "memcached_light";
311 const char *executable()
313 return MEMCACHED_LIGHT_BINARY
;
316 virtual void port_option(Application
& app
, in_port_t arg
)
319 snprintf(buffer
, sizeof(buffer
), "--port=%d", int(arg
));
320 app
.add_option(buffer
);
323 bool has_port_option() const
333 void log_file_option(Application
& app
, const std::string
& arg
)
335 if (arg
.empty() == false)
337 std::string
buffer("--log-file=");
339 app
.add_option("--verbose");
340 app
.add_option(buffer
);
344 bool has_log_file_option() const
349 bool build(size_t argc
, const char *argv
[]);
352 class MemcachedSaSL
: public Memcached
355 MemcachedSaSL(const std::string
& host_arg
,
356 const in_port_t port_arg
,
357 const bool is_socket_arg
,
358 const std::string
& username_arg
,
359 const std::string
&password_arg
) :
360 Memcached(host_arg
, port_arg
, is_socket_arg
, username_arg
, password_arg
)
365 return "memcached-sasl";
368 const char *sasl() const
370 return " -S -B binary ";
373 const char *executable()
375 return MEMCACHED_SASL_BINARY
;
378 pid_t
get_pid(bool error_is_ok
)
380 // Memcached is slow to start, so we need to do this
381 if (pid_file().empty() == false)
384 wait_for_pidfile() == false)
386 Error
<< "Pidfile was not found:" << pid_file();
392 memcached_return_t rc
;
395 local_pid
= libmemcached_util_getpid2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc
);
399 local_pid
= libmemcached_util_getpid2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc
);
402 if (error_is_ok
and ((memcached_failed(rc
) or not is_pid_valid(local_pid
))))
404 Error
<< "libmemcached_util_getpid2(" << memcached_strerror(NULL
, rc
) << ") username: " << username() << " password: " << password() << " pid: " << local_pid
<< " for:" << *this;
412 // Memcached is slow to start, so we need to do this
413 if (pid_file().empty() == false)
415 if (wait_for_pidfile() == false)
417 Error
<< "Pidfile was not found:" << pid_file();
422 memcached_return_t rc
;
427 ret
= libmemcached_util_ping2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc
);
431 ret
= libmemcached_util_ping2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc
);
434 if (memcached_failed(rc
) or ret
== false)
436 Error
<< "libmemcached_util_ping2(" << hostname() << ", " << port() << ", " << username() << ", " << password() << ") error: " << memcached_strerror(NULL
, rc
);
447 bool Memcached::build(size_t argc
, const char *argv
[])
449 std::stringstream arg_buffer
;
451 if (getuid() == 0 or geteuid() == 0)
453 add_option("-u", "root");
456 add_option("-l", "localhost");
457 add_option("-m", "128");
465 for (int x
= 0 ; x
< argc
; x
++)
473 bool MemcachedLight::build(size_t argc
, const char *argv
[])
475 for (size_t x
= 0 ; x
< argc
; x
++)
485 libtest::Server
*build_memcached(const std::string
& hostname
, const in_port_t try_port
)
487 return new Memcached(hostname
, try_port
, false);
490 libtest::Server
*build_memcached_socket(const std::string
& socket_file
, const in_port_t try_port
)
492 return new Memcached(socket_file
, try_port
, true);
495 libtest::Server
*build_memcached_light(const std::string
& hostname
, const in_port_t try_port
)
497 return new MemcachedLight(hostname
, try_port
);
501 libtest::Server
*build_memcached_sasl(const std::string
& hostname
, const in_port_t try_port
, const std::string
& username
, const std::string
&password
)
503 if (username
.empty())
505 return new MemcachedSaSL(hostname
, try_port
, false, "memcached", "memcached");
508 return new MemcachedSaSL(hostname
, try_port
, false, username
, password
);
511 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
)
513 if (username
.empty())
515 return new MemcachedSaSL(socket_file
, try_port
, true, "memcached", "memcached");
518 return new MemcachedSaSL(socket_file
, try_port
, true, username
, password
);