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 (not wait
.successful())
82 Error
<< "Pidfile was not found:" << pid_file();
87 memcached_return_t rc
;
90 _pid
= libmemcached_util_getpid(socket().c_str(), port(), &rc
);
94 _pid
= libmemcached_util_getpid(hostname().c_str(), port(), &rc
);
97 if ((memcached_failed(rc
) or _pid
< 1) and not error_is_ok
)
99 Error
<< "libmemcached_util_getpid(" << memcached_strerror(NULL
, rc
) << ") pid: " << _pid
<< " for:" << *this;
107 // Memcached is slow to start, so we need to do this
108 if (not pid_file().empty())
110 Wait
wait(pid_file(), 0);
112 if (not wait
.successful())
114 Error
<< "Pidfile was not found:" << pid_file();
119 memcached_return_t rc
;
123 ret
= libmemcached_util_ping(socket().c_str(), 0, &rc
);
127 ret
= libmemcached_util_ping(hostname().c_str(), port(), &rc
);
130 if (memcached_failed(rc
) or not ret
)
132 Error
<< "libmemcached_util_ping(" << memcached_strerror(NULL
, rc
) << ")";
142 const char *executable()
144 return MEMCACHED_BINARY
;
147 const char *pid_file_option()
152 const char *socket_file_option() const
157 const char *daemon_file_option()
162 const char *log_file_option()
167 const char *port_option()
177 // Memcached's pidfile is broken
178 bool broken_pid_file()
183 bool build(int argc
, const char *argv
[]);
189 bool Memcached::build(int argc
, const char *argv
[])
191 std::stringstream arg_buffer
;
193 if (getuid() == 0 or geteuid() == 0)
195 arg_buffer
<< " -u root ";
198 for (int x
= 1 ; x
< argc
; x
++)
200 arg_buffer
<< " " << argv
[x
] << " ";
203 set_extra_args(arg_buffer
.str());
210 Server
*build_memcached(const std::string
& hostname
, const in_port_t try_port
)
212 return new Memcached(hostname
, try_port
, false);
215 Server
*build_memcached_socket(const std::string
& hostname
, const in_port_t try_port
)
217 return new Memcached(hostname
, try_port
, true);