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-1.0/memcached.h>
26 #include <libmemcachedutil-1.0/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 (pid_file().empty() == false)
90 if (error_is_ok
and not wait_for_pidfile())
92 Error
<< "Pidfile was not found:" << pid_file();
98 memcached_return_t rc
= MEMCACHED_SUCCESS
;
101 if (socket().empty())
106 local_pid
= libmemcached_util_getpid(socket().c_str(), port(), &rc
);
110 local_pid
= libmemcached_util_getpid(hostname().c_str(), port(), &rc
);
113 if (error_is_ok
and ((memcached_failed(rc
) or not is_pid_valid(local_pid
))))
115 Error
<< "libmemcached_util_getpid(" << memcached_strerror(NULL
, rc
) << ") pid: " << local_pid
<< " for:" << *this;
123 // Memcached is slow to start, so we need to do this
124 if (not pid_file().empty())
126 if (not wait_for_pidfile())
128 Error
<< "Pidfile was not found:" << pid_file();
133 memcached_return_t rc
;
138 ret
= libmemcached_util_ping(socket().c_str(), 0, &rc
);
142 ret
= libmemcached_util_ping(hostname().c_str(), port(), &rc
);
145 if (memcached_failed(rc
) or not ret
)
147 Error
<< "libmemcached_util_ping(" << hostname() << ", " << port() << ") error: " << memcached_strerror(NULL
, rc
);
158 const char *executable()
160 return MEMCACHED_BINARY
;
163 virtual void pid_file_option(Application
& app
, const std::string
& arg
)
165 if (arg
.empty() == false)
167 app
.add_option("-P", arg
);
171 const char *socket_file_option() const
176 const char *daemon_file_option()
181 virtual void port_option(Application
& app
, in_port_t arg
)
184 snprintf(buffer
, sizeof(buffer
), "%d", int(arg
));
185 app
.add_option("-p", buffer
);
188 bool has_port_option() const
193 bool has_socket_file_option() const
198 void socket_file_option(Application
& app
, const std::string
& socket_arg
)
200 if (socket_arg
.empty() == false)
202 app
.add_option("-s", socket_arg
);
211 bool broken_socket_cleanup()
216 // Memcached's pidfile is broken
217 bool broken_pid_file()
222 bool build(size_t argc
, const char *argv
[]);
225 class MemcachedLight
: public libtest::Server
229 MemcachedLight(const std::string
& host_arg
, const in_port_t port_arg
):
230 libtest::Server(host_arg
, port_arg
)
235 pid_t
get_pid(bool error_is_ok
)
237 // Memcached is slow to start, so we need to do this
238 if (not pid_file().empty())
240 if (error_is_ok
and not wait_for_pidfile())
242 Error
<< "Pidfile was not found:" << pid_file();
248 std::stringstream error_message
;
249 pid_t local_pid
= get_pid_from_file(pid_file(), error_message
);
252 if (::kill(local_pid
, 0) > 0)
258 if (error_is_ok
and ((success
or not is_pid_valid(local_pid
))))
260 Error
<< "kill(" << " pid: " << local_pid
<< " errno:" << strerror(errno
) << " for:" << *this;
268 // Memcached is slow to start, so we need to do this
269 if (not pid_file().empty())
271 if (not wait_for_pidfile())
273 Error
<< "Pidfile was not found:" << pid_file();
278 std::stringstream error_message
;
279 pid_t local_pid
= get_pid_from_file(pid_file(), error_message
);
282 if (::kill(local_pid
, 0) == 0)
293 return "memcached_light";
296 const char *executable()
298 return MEMCACHED_LIGHT_BINARY
;
301 const char *daemon_file_option()
306 virtual void port_option(Application
& app
, in_port_t arg
)
309 snprintf(buffer
, sizeof(buffer
), "--port=%d", int(arg
));
310 app
.add_option(buffer
);
313 bool has_port_option() const
323 bool build(size_t argc
, const char *argv
[]);
326 class MemcachedSaSL
: public Memcached
329 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
) :
330 Memcached(host_arg
, port_arg
, is_socket_arg
, username_arg
, password_arg
)
335 return "memcached-sasl";
338 const char *sasl() const
340 return " -S -B binary ";
343 const char *executable()
345 return MEMCACHED_SASL_BINARY
;
348 pid_t
get_pid(bool error_is_ok
)
350 // Memcached is slow to start, so we need to do this
351 if (not pid_file().empty())
353 if (error_is_ok
and not wait_for_pidfile())
355 Error
<< "Pidfile was not found:" << pid_file();
361 memcached_return_t rc
;
364 local_pid
= libmemcached_util_getpid2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc
);
368 local_pid
= libmemcached_util_getpid2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc
);
371 if (error_is_ok
and ((memcached_failed(rc
) or not is_pid_valid(local_pid
))))
373 Error
<< "libmemcached_util_getpid2(" << memcached_strerror(NULL
, rc
) << ") username: " << username() << " password: " << password() << " pid: " << local_pid
<< " for:" << *this;
381 // Memcached is slow to start, so we need to do this
382 if (not pid_file().empty())
384 if (not wait_for_pidfile())
386 Error
<< "Pidfile was not found:" << pid_file();
391 memcached_return_t rc
;
396 ret
= libmemcached_util_ping2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc
);
400 ret
= libmemcached_util_ping2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc
);
403 if (memcached_failed(rc
) or not ret
)
405 Error
<< "libmemcached_util_ping2(" << hostname() << ", " << port() << ", " << username() << ", " << password() << ") error: " << memcached_strerror(NULL
, rc
);
416 bool Memcached::build(size_t argc
, const char *argv
[])
418 std::stringstream arg_buffer
;
420 if (getuid() == 0 or geteuid() == 0)
422 add_option("-u", "root");
425 add_option("-l", "localhost");
426 add_option("-m", "128");
434 for (int x
= 0 ; x
< argc
; x
++)
442 bool MemcachedLight::build(size_t argc
, const char *argv
[])
444 for (int x
= 0 ; x
< argc
; x
++)
454 libtest::Server
*build_memcached(const std::string
& hostname
, const in_port_t try_port
)
456 return new Memcached(hostname
, try_port
, false);
459 libtest::Server
*build_memcached_socket(const std::string
& socket_file
, const in_port_t try_port
)
461 return new Memcached(socket_file
, try_port
, true);
464 libtest::Server
*build_memcached_light(const std::string
& hostname
, const in_port_t try_port
)
466 return new MemcachedLight(hostname
, try_port
);
470 libtest::Server
*build_memcached_sasl(const std::string
& hostname
, const in_port_t try_port
, const std::string
& username
, const std::string
&password
)
472 if (username
.empty())
474 return new MemcachedSaSL(hostname
, try_port
, false, "memcached", "memcached");
477 return new MemcachedSaSL(hostname
, try_port
, false, username
, password
);
480 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
)
482 if (username
.empty())
484 return new MemcachedSaSL(socket_file
, try_port
, true, "memcached", "memcached");
487 return new MemcachedSaSL(socket_file
, try_port
, true, username
, password
);