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
,
60 const in_port_t port_arg
,
61 const bool is_socket_arg
,
62 const std::string
& username_arg
,
63 const std::string
& password_arg
) :
64 libtest::Server(host_arg
, port_arg
, is_socket_arg
),
65 _username(username_arg
),
66 _password(password_arg
)
69 Memcached(const std::string
& host_arg
, const in_port_t port_arg
, const bool is_socket_arg
) :
70 libtest::Server(host_arg
, port_arg
, is_socket_arg
)
75 virtual const char *sasl() const
80 const std::string
& password() const
85 const std::string
& username() const
90 pid_t
get_pid(bool error_is_ok
)
92 // Memcached is slow to start, so we need to do this
93 if (pid_file().empty() == false)
96 wait_for_pidfile() == false)
98 Error
<< "Pidfile was not found:" << pid_file();
104 memcached_return_t rc
= MEMCACHED_SUCCESS
;
107 if (socket().empty())
112 local_pid
= libmemcached_util_getpid(socket().c_str(), port(), &rc
);
116 local_pid
= libmemcached_util_getpid(hostname().c_str(), port(), &rc
);
119 if (error_is_ok
and ((memcached_failed(rc
) or not is_pid_valid(local_pid
))))
121 Error
<< "libmemcached_util_getpid(" << memcached_strerror(NULL
, rc
) << ") pid: " << local_pid
<< " for:" << *this;
129 // Memcached is slow to start, so we need to do this
130 if (pid_file().empty() == false)
132 if (wait_for_pidfile() == false)
134 Error
<< "Pidfile was not found:" << pid_file();
139 memcached_return_t rc
;
144 ret
= libmemcached_util_ping(socket().c_str(), 0, &rc
);
148 ret
= libmemcached_util_ping(hostname().c_str(), port(), &rc
);
151 if (memcached_failed(rc
) or not ret
)
153 Error
<< "libmemcached_util_ping(" << hostname() << ", " << port() << ") error: " << memcached_strerror(NULL
, rc
);
164 const char *executable()
166 return MEMCACHED_BINARY
;
171 if (MEMCACHED_BINARY
and strcmp(MEMCACHED_BINARY
, "memcached/memcached") == 0)
179 virtual void pid_file_option(Application
& app
, const std::string
& arg
)
181 if (arg
.empty() == false)
183 app
.add_option("-P", arg
);
187 const char *socket_file_option() const
192 const char *daemon_file_option()
197 virtual void port_option(Application
& app
, in_port_t arg
)
200 snprintf(buffer
, sizeof(buffer
), "%d", int(arg
));
201 app
.add_option("-p", buffer
);
204 bool has_port_option() const
209 bool has_socket_file_option() const
214 void socket_file_option(Application
& app
, const std::string
& socket_arg
)
216 if (socket_arg
.empty() == false)
218 app
.add_option("-s", socket_arg
);
222 bool broken_socket_cleanup()
227 // Memcached's pidfile is broken
228 bool broken_pid_file()
233 bool build(size_t argc
, const char *argv
[]);
236 class MemcachedLight
: public libtest::Server
240 MemcachedLight(const std::string
& host_arg
, const in_port_t port_arg
):
241 libtest::Server(host_arg
, port_arg
)
246 pid_t
get_pid(bool error_is_ok
)
248 // Memcached is slow to start, so we need to do this
249 if (pid_file().empty() == false)
251 if (error_is_ok
and wait_for_pidfile() == false)
253 Error
<< "Pidfile was not found:" << pid_file();
259 std::stringstream error_message
;
260 pid_t local_pid
= get_pid_from_file(pid_file(), error_message
);
263 if (::kill(local_pid
, 0) > 0)
269 if (error_is_ok
and ((success
or not is_pid_valid(local_pid
))))
271 Error
<< "kill(" << " pid: " << local_pid
<< " errno:" << strerror(errno
) << " for:" << *this;
279 // Memcached is slow to start, so we need to do this
280 if (not pid_file().empty())
282 if (not wait_for_pidfile())
284 Error
<< "Pidfile was not found:" << pid_file();
289 std::stringstream error_message
;
290 pid_t local_pid
= get_pid_from_file(pid_file(), error_message
);
293 if (::kill(local_pid
, 0) == 0)
304 return "memcached_light";
307 const char *executable()
309 return MEMCACHED_LIGHT_BINARY
;
312 const char *daemon_file_option()
317 virtual void port_option(Application
& app
, in_port_t arg
)
320 snprintf(buffer
, sizeof(buffer
), "--port=%d", int(arg
));
321 app
.add_option(buffer
);
324 bool has_port_option() const
334 void log_file_option(Application
& app
, const std::string
& arg
)
336 if (arg
.empty() == false)
338 std::string
buffer("--log-file=");
340 app
.add_option("--verbose");
341 app
.add_option(buffer
);
345 bool has_log_file_option() const
350 bool build(size_t argc
, const char *argv
[]);
353 class MemcachedSaSL
: public Memcached
356 MemcachedSaSL(const std::string
& host_arg
,
357 const in_port_t port_arg
,
358 const bool is_socket_arg
,
359 const std::string
& username_arg
,
360 const std::string
&password_arg
) :
361 Memcached(host_arg
, port_arg
, is_socket_arg
, username_arg
, password_arg
)
366 return "memcached-sasl";
369 const char *sasl() const
371 return " -S -B binary ";
374 const char *executable()
376 return MEMCACHED_SASL_BINARY
;
379 pid_t
get_pid(bool error_is_ok
)
381 // Memcached is slow to start, so we need to do this
382 if (pid_file().empty() == false)
385 wait_for_pidfile() == false)
387 Error
<< "Pidfile was not found:" << pid_file();
393 memcached_return_t rc
;
396 local_pid
= libmemcached_util_getpid2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc
);
400 local_pid
= libmemcached_util_getpid2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc
);
403 if (error_is_ok
and ((memcached_failed(rc
) or not is_pid_valid(local_pid
))))
405 Error
<< "libmemcached_util_getpid2(" << memcached_strerror(NULL
, rc
) << ") username: " << username() << " password: " << password() << " pid: " << local_pid
<< " for:" << *this;
413 // Memcached is slow to start, so we need to do this
414 if (not pid_file().empty())
416 if (not wait_for_pidfile())
418 Error
<< "Pidfile was not found:" << pid_file();
423 memcached_return_t rc
;
428 ret
= libmemcached_util_ping2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc
);
432 ret
= libmemcached_util_ping2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc
);
435 if (memcached_failed(rc
) or not ret
)
437 Error
<< "libmemcached_util_ping2(" << hostname() << ", " << port() << ", " << username() << ", " << password() << ") error: " << memcached_strerror(NULL
, rc
);
448 bool Memcached::build(size_t argc
, const char *argv
[])
450 std::stringstream arg_buffer
;
452 if (getuid() == 0 or geteuid() == 0)
454 add_option("-u", "root");
457 add_option("-l", "localhost");
458 add_option("-m", "128");
466 for (int x
= 0 ; x
< argc
; x
++)
474 bool MemcachedLight::build(size_t argc
, const char *argv
[])
476 for (size_t x
= 0 ; x
< argc
; x
++)
486 libtest::Server
*build_memcached(const std::string
& hostname
, const in_port_t try_port
)
488 return new Memcached(hostname
, try_port
, false);
491 libtest::Server
*build_memcached_socket(const std::string
& socket_file
, const in_port_t try_port
)
493 return new Memcached(socket_file
, try_port
, true);
496 libtest::Server
*build_memcached_light(const std::string
& hostname
, const in_port_t try_port
)
498 return new MemcachedLight(hostname
, try_port
);
502 libtest::Server
*build_memcached_sasl(const std::string
& hostname
, const in_port_t try_port
, const std::string
& username
, const std::string
&password
)
504 if (username
.empty())
506 return new MemcachedSaSL(hostname
, try_port
, false, "memcached", "memcached");
509 return new MemcachedSaSL(hostname
, try_port
, false, username
, password
);
512 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
)
514 if (username
.empty())
516 return new MemcachedSaSL(socket_file
, try_port
, true, "memcached", "memcached");
519 return new MemcachedSaSL(socket_file
, try_port
, true, username
, password
);