Updating for 1.0.2 release
[awesomized/libmemcached] / libtest / memcached.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
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.
11 *
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.
16 *
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
20 */
21
22
23 #include <libtest/common.h>
24
25 #include <libmemcached/memcached.h>
26 #include <libmemcached/util.h>
27
28 using namespace libtest;
29
30 #include <cassert>
31 #include <cerrno>
32 #include <cstdio>
33 #include <cstdlib>
34 #include <cstring>
35 #include <iostream>
36 #include <signal.h>
37 #include <sys/types.h>
38 #include <sys/wait.h>
39 #include <unistd.h>
40
41 #include <libtest/server.h>
42 #include <libtest/wait.h>
43
44 #include <libtest/memcached.h>
45
46 #ifndef __INTEL_COMPILER
47 #pragma GCC diagnostic ignored "-Wold-style-cast"
48 #endif
49
50 using namespace libtest;
51
52 class Memcached : public libtest::Server
53 {
54 std::string _username;
55 std::string _password;
56
57 public:
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)
62 { }
63
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)
66 {
67 set_pid_file();
68 }
69
70 virtual const char *sasl() const
71 {
72 return NULL;
73 }
74
75 const std::string& password() const
76 {
77 return _password;
78 }
79
80 const std::string& username() const
81 {
82 return _username;
83 }
84
85 pid_t get_pid(bool error_is_ok)
86 {
87 // Memcached is slow to start, so we need to do this
88 if (not pid_file().empty())
89 {
90 if (error_is_ok and not wait_for_pidfile())
91 {
92 Error << "Pidfile was not found:" << pid_file();
93 return -1;
94 }
95 }
96
97 pid_t local_pid;
98 memcached_return_t rc= MEMCACHED_SUCCESS;
99 if (has_socket())
100 {
101 local_pid= libmemcached_util_getpid(socket().c_str(), 0, &rc);
102 }
103 else
104 {
105 local_pid= libmemcached_util_getpid(hostname().c_str(), port(), &rc);
106 }
107
108 if (error_is_ok and ((memcached_failed(rc) or not is_pid_valid(local_pid))))
109 {
110 Error << "libmemcached_util_getpid(" << memcached_strerror(NULL, rc) << ") pid: " << local_pid << " for:" << *this;
111 }
112
113 return local_pid;
114 }
115
116 bool ping()
117 {
118 // Memcached is slow to start, so we need to do this
119 if (not pid_file().empty())
120 {
121 if (not wait_for_pidfile())
122 {
123 Error << "Pidfile was not found:" << pid_file();
124 return -1;
125 }
126 }
127
128 memcached_return_t rc;
129 bool ret;
130
131 if (has_socket())
132 {
133 ret= libmemcached_util_ping(socket().c_str(), 0, &rc);
134 }
135 else
136 {
137 ret= libmemcached_util_ping(hostname().c_str(), port(), &rc);
138 }
139
140 if (memcached_failed(rc) or not ret)
141 {
142 Error << "libmemcached_util_ping(" << hostname() << ", " << port() << ") error: " << memcached_strerror(NULL, rc);
143 }
144
145 return ret;
146 }
147
148 const char *name()
149 {
150 return "memcached";
151 };
152
153 const char *executable()
154 {
155 return MEMCACHED_BINARY;
156 }
157
158 const char *pid_file_option()
159 {
160 return "-P ";
161 }
162
163 const char *socket_file_option() const
164 {
165 return "-s ";
166 }
167
168 const char *daemon_file_option()
169 {
170 return "-d";
171 }
172
173 const char *log_file_option()
174 {
175 return NULL;
176 }
177
178 const char *port_option()
179 {
180 return "-p ";
181 }
182
183 bool is_libtool()
184 {
185 return false;
186 }
187
188 bool broken_socket_cleanup()
189 {
190 return true;
191 }
192
193 // Memcached's pidfile is broken
194 bool broken_pid_file()
195 {
196 return true;
197 }
198
199 bool build(int argc, const char *argv[]);
200 };
201
202 class MemcachedSaSL : public Memcached
203 {
204 public:
205 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) :
206 Memcached(host_arg, port_arg, is_socket_arg, username_arg, password_arg)
207 { }
208
209 const char *name()
210 {
211 return "memcached-sasl";
212 };
213
214 const char *sasl() const
215 {
216 return " -S -B binary ";
217 }
218
219 const char *executable()
220 {
221 return MEMCACHED_SASL_BINARY;
222 }
223
224 pid_t get_pid(bool error_is_ok)
225 {
226 // Memcached is slow to start, so we need to do this
227 if (not pid_file().empty())
228 {
229 if (error_is_ok and not wait_for_pidfile())
230 {
231 Error << "Pidfile was not found:" << pid_file();
232 return -1;
233 }
234 }
235
236 pid_t local_pid;
237 memcached_return_t rc;
238 if (has_socket())
239 {
240 local_pid= libmemcached_util_getpid2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc);
241 }
242 else
243 {
244 local_pid= libmemcached_util_getpid2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc);
245 }
246
247 if (error_is_ok and ((memcached_failed(rc) or not is_pid_valid(local_pid))))
248 {
249 Error << "libmemcached_util_getpid2(" << memcached_strerror(NULL, rc) << ") username: " << username() << " password: " << password() << " pid: " << local_pid << " for:" << *this;
250 }
251
252 return local_pid;
253 }
254
255 bool ping()
256 {
257 // Memcached is slow to start, so we need to do this
258 if (not pid_file().empty())
259 {
260 if (not wait_for_pidfile())
261 {
262 Error << "Pidfile was not found:" << pid_file();
263 return -1;
264 }
265 }
266
267 memcached_return_t rc;
268 bool ret;
269
270 if (has_socket())
271 {
272 ret= libmemcached_util_ping2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc);
273 }
274 else
275 {
276 ret= libmemcached_util_ping2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc);
277 }
278
279 if (memcached_failed(rc) or not ret)
280 {
281 Error << "libmemcached_util_ping2(" << hostname() << ", " << port() << ", " << username() << ", " << password() << ") error: " << memcached_strerror(NULL, rc);
282 }
283
284 return ret;
285 }
286
287 };
288
289
290 #include <sstream>
291
292 bool Memcached::build(int argc, const char *argv[])
293 {
294 std::stringstream arg_buffer;
295
296 if (getuid() == 0 or geteuid() == 0)
297 {
298 arg_buffer << " -u root ";
299 }
300
301 arg_buffer << " -l 127.0.0.1 ";
302 arg_buffer << " -m 128 ";
303 arg_buffer << " -M ";
304
305 if (sasl())
306 {
307 arg_buffer << sasl();
308 }
309
310 for (int x= 1 ; x < argc ; x++)
311 {
312 arg_buffer << " " << argv[x] << " ";
313 }
314
315 set_extra_args(arg_buffer.str());
316
317 return true;
318 }
319
320 namespace libtest {
321
322 libtest::Server *build_memcached(const std::string& hostname, const in_port_t try_port)
323 {
324 return new Memcached(hostname, try_port, false);
325 }
326
327 libtest::Server *build_memcached_socket(const std::string& socket_file, const in_port_t try_port)
328 {
329 return new Memcached(socket_file, try_port, true);
330 }
331
332
333 libtest::Server *build_memcached_sasl(const std::string& hostname, const in_port_t try_port, const std::string& username, const std::string &password)
334 {
335 if (username.empty())
336 {
337 return new MemcachedSaSL(hostname, try_port, false, "memcached", "memcached");
338 }
339
340 return new MemcachedSaSL(hostname, try_port, false, username, password);
341 }
342
343 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)
344 {
345 if (username.empty())
346 {
347 return new MemcachedSaSL(socket_file, try_port, true, "memcached", "memcached");
348 }
349
350 return new MemcachedSaSL(socket_file, try_port, true, username, password);
351 }
352
353 }
354