1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <libtest/common.h>
39 #include <libmemcached/memcached.h>
40 #include <libmemcached/util.h>
42 using namespace libtest
;
51 #include <sys/types.h>
55 #include <libtest/server.h>
56 #include <libtest/wait.h>
58 #include <libtest/memcached.h>
60 #ifndef __INTEL_COMPILER
61 #pragma GCC diagnostic ignored "-Wold-style-cast"
64 using namespace libtest
;
66 class Memcached
: public Server
69 Memcached(const std::string
& host_arg
, const in_port_t port_arg
, const bool is_socket_arg
) :
70 Server(host_arg
, port_arg
, is_socket_arg
)
73 pid_t
get_pid(bool error_is_ok
)
75 // Memcached is slow to start, so we need to do this
76 if (not pid_file().empty())
78 Wait
wait(pid_file(), 0);
80 if (error_is_ok
and not wait
.successful())
82 Error
<< "Pidfile was not found:" << pid_file();
88 memcached_return_t rc
;
91 local_pid
= libmemcached_util_getpid(socket().c_str(), port(), &rc
);
95 local_pid
= libmemcached_util_getpid(hostname().c_str(), port(), &rc
);
98 if (error_is_ok
and ((memcached_failed(rc
) or local_pid
< 1)))
100 Error
<< "libmemcached_util_getpid(" << memcached_strerror(NULL
, rc
) << ") pid: " << local_pid
<< " for:" << *this;
108 // Memcached is slow to start, so we need to do this
109 if (not pid_file().empty())
111 Wait
wait(pid_file(), 0);
113 if (not wait
.successful())
115 Error
<< "Pidfile was not found:" << pid_file();
120 memcached_return_t rc
;
124 ret
= libmemcached_util_ping(socket().c_str(), 0, &rc
);
128 ret
= libmemcached_util_ping(hostname().c_str(), port(), &rc
);
131 if (memcached_failed(rc
) or not ret
)
133 Error
<< "libmemcached_util_ping(" << memcached_strerror(NULL
, rc
) << ")";
143 const char *executable()
145 return MEMCACHED_BINARY
;
148 const char *pid_file_option()
153 const char *socket_file_option() const
158 const char *daemon_file_option()
163 const char *log_file_option()
168 const char *port_option()
178 // Memcached's pidfile is broken
179 bool broken_pid_file()
184 bool build(int argc
, const char *argv
[]);
190 bool Memcached::build(int argc
, const char *argv
[])
192 std::stringstream arg_buffer
;
194 if (getuid() == 0 or geteuid() == 0)
196 arg_buffer
<< " -u root ";
199 for (int x
= 1 ; x
< argc
; x
++)
201 arg_buffer
<< " " << argv
[x
] << " ";
204 set_extra_args(arg_buffer
.str());
211 Server
*build_memcached(const std::string
& hostname
, const in_port_t try_port
)
213 return new Memcached(hostname
, try_port
, false);
216 Server
*build_memcached_socket(const std::string
& hostname
, const in_port_t try_port
)
218 return new Memcached(hostname
, try_port
, true);