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/memcached.h>
26 #include <libmemcached/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 Server
55 Memcached(const std::string
& host_arg
, const in_port_t port_arg
, const bool is_socket_arg
) :
56 Server(host_arg
, port_arg
, is_socket_arg
)
59 pid_t
get_pid(bool error_is_ok
)
61 // Memcached is slow to start, so we need to do this
62 if (not pid_file().empty())
64 Wait
wait(pid_file(), 0);
66 if (error_is_ok
and not wait
.successful())
68 Error
<< "Pidfile was not found:" << pid_file();
74 memcached_return_t rc
;
77 local_pid
= libmemcached_util_getpid(socket().c_str(), port(), &rc
);
81 local_pid
= libmemcached_util_getpid(hostname().c_str(), port(), &rc
);
84 if (error_is_ok
and ((memcached_failed(rc
) or local_pid
< 1)))
86 Error
<< "libmemcached_util_getpid(" << memcached_strerror(NULL
, rc
) << ") pid: " << local_pid
<< " for:" << *this;
94 // Memcached is slow to start, so we need to do this
95 if (not pid_file().empty())
97 Wait
wait(pid_file(), 0);
99 if (not wait
.successful())
101 Error
<< "Pidfile was not found:" << pid_file();
106 memcached_return_t rc
;
110 ret
= libmemcached_util_ping(socket().c_str(), 0, &rc
);
114 ret
= libmemcached_util_ping(hostname().c_str(), port(), &rc
);
117 if (memcached_failed(rc
) or not ret
)
119 Error
<< "libmemcached_util_ping(" << memcached_strerror(NULL
, rc
) << ")";
129 const char *executable()
131 return MEMCACHED_BINARY
;
134 const char *pid_file_option()
139 const char *socket_file_option() const
144 const char *daemon_file_option()
149 const char *log_file_option()
154 const char *port_option()
164 // Memcached's pidfile is broken
165 bool broken_pid_file()
170 bool build(int argc
, const char *argv
[]);
176 bool Memcached::build(int argc
, const char *argv
[])
178 std::stringstream arg_buffer
;
180 if (getuid() == 0 or geteuid() == 0)
182 arg_buffer
<< " -u root ";
185 arg_buffer
<< " -l 127.0.0.1 ";
186 arg_buffer
<< " -m 128 ";
187 arg_buffer
<< " -M ";
189 for (int x
= 1 ; x
< argc
; x
++)
191 arg_buffer
<< " " << argv
[x
] << " ";
194 set_extra_args(arg_buffer
.str());
201 Server
*build_memcached(const std::string
& hostname
, const in_port_t try_port
)
203 return new Memcached(hostname
, try_port
, false);
206 Server
*build_memcached_socket(const std::string
& hostname
, const in_port_t try_port
)
208 return new Memcached(hostname
, try_port
, true);