81068aef0c4c33f4b0349a2bf833059656e53e5f
[m6w6/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 <config.h>
24 #include <libtest/common.h>
25
26 #include <libmemcached-1.0/memcached.h>
27 #include <libmemcachedutil-1.0/util.h>
28
29 using namespace libtest;
30
31 #include <cassert>
32 #include <cerrno>
33 #include <cstdio>
34 #include <cstdlib>
35 #include <cstring>
36 #include <iostream>
37 #include <signal.h>
38 #include <sys/types.h>
39 #include <sys/wait.h>
40 #include <unistd.h>
41
42 #include <libtest/server.h>
43 #include <libtest/wait.h>
44
45 #include <libtest/memcached.h>
46
47 #ifndef __INTEL_COMPILER
48 #pragma GCC diagnostic ignored "-Wold-style-cast"
49 #endif
50
51 using namespace libtest;
52
53 class Memcached : public libtest::Server
54 {
55 std::string _username;
56 std::string _password;
57
58 public:
59 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) :
60 libtest::Server(host_arg, port_arg, is_socket_arg),
61 _username(username_arg),
62 _password(password_arg)
63 { }
64
65 Memcached(const std::string& host_arg, const in_port_t port_arg, const bool is_socket_arg) :
66 libtest::Server(host_arg, port_arg, is_socket_arg)
67 {
68 set_pid_file();
69 }
70
71 virtual const char *sasl() const
72 {
73 return NULL;
74 }
75
76 const std::string& password() const
77 {
78 return _password;
79 }
80
81 const std::string& username() const
82 {
83 return _username;
84 }
85
86 pid_t get_pid(bool error_is_ok)
87 {
88 // Memcached is slow to start, so we need to do this
89 if (pid_file().empty() == false)
90 {
91 if (error_is_ok and not wait_for_pidfile())
92 {
93 Error << "Pidfile was not found:" << pid_file();
94 return -1;
95 }
96 }
97
98 pid_t local_pid;
99 memcached_return_t rc= MEMCACHED_SUCCESS;
100 if (has_socket())
101 {
102 if (socket().empty())
103 {
104 return -1;
105 }
106
107 local_pid= libmemcached_util_getpid(socket().c_str(), port(), &rc);
108 }
109 else
110 {
111 local_pid= libmemcached_util_getpid(hostname().c_str(), port(), &rc);
112 }
113
114 if (error_is_ok and ((memcached_failed(rc) or not is_pid_valid(local_pid))))
115 {
116 Error << "libmemcached_util_getpid(" << memcached_strerror(NULL, rc) << ") pid: " << local_pid << " for:" << *this;
117 }
118
119 return local_pid;
120 }
121
122 bool ping()
123 {
124 // Memcached is slow to start, so we need to do this
125 if (not pid_file().empty())
126 {
127 if (wait_for_pidfile() == false)
128 {
129 Error << "Pidfile was not found:" << pid_file();
130 return -1;
131 }
132 }
133
134 memcached_return_t rc;
135 bool ret;
136
137 if (has_socket())
138 {
139 ret= libmemcached_util_ping(socket().c_str(), 0, &rc);
140 }
141 else
142 {
143 ret= libmemcached_util_ping(hostname().c_str(), port(), &rc);
144 }
145
146 if (memcached_failed(rc) or not ret)
147 {
148 Error << "libmemcached_util_ping(" << hostname() << ", " << port() << ") error: " << memcached_strerror(NULL, rc);
149 }
150
151 return ret;
152 }
153
154 const char *name()
155 {
156 return "memcached";
157 };
158
159 const char *executable()
160 {
161 return MEMCACHED_BINARY;
162 }
163
164 virtual void pid_file_option(Application& app, const std::string& arg)
165 {
166 if (arg.empty() == false)
167 {
168 app.add_option("-P", arg);
169 }
170 }
171
172 const char *socket_file_option() const
173 {
174 return "-s ";
175 }
176
177 const char *daemon_file_option()
178 {
179 return "-d";
180 }
181
182 virtual void port_option(Application& app, in_port_t arg)
183 {
184 char buffer[30];
185 snprintf(buffer, sizeof(buffer), "%d", int(arg));
186 app.add_option("-p", buffer);
187 }
188
189 bool has_port_option() const
190 {
191 return true;
192 }
193
194 bool has_socket_file_option() const
195 {
196 return has_socket();
197 }
198
199 void socket_file_option(Application& app, const std::string& socket_arg)
200 {
201 if (socket_arg.empty() == false)
202 {
203 app.add_option("-s", socket_arg);
204 }
205 }
206
207 bool is_libtool()
208 {
209 return false;
210 }
211
212 bool broken_socket_cleanup()
213 {
214 return true;
215 }
216
217 // Memcached's pidfile is broken
218 bool broken_pid_file()
219 {
220 return true;
221 }
222
223 bool build(size_t argc, const char *argv[]);
224 };
225
226 class MemcachedLight : public libtest::Server
227 {
228
229 public:
230 MemcachedLight(const std::string& host_arg, const in_port_t port_arg):
231 libtest::Server(host_arg, port_arg)
232 {
233 set_pid_file();
234 }
235
236 pid_t get_pid(bool error_is_ok)
237 {
238 // Memcached is slow to start, so we need to do this
239 if (not pid_file().empty())
240 {
241 if (error_is_ok and not wait_for_pidfile())
242 {
243 Error << "Pidfile was not found:" << pid_file();
244 return -1;
245 }
246 }
247
248 bool success= false;
249 std::stringstream error_message;
250 pid_t local_pid= get_pid_from_file(pid_file(), error_message);
251 if (local_pid > 0)
252 {
253 if (::kill(local_pid, 0) > 0)
254 {
255 success= true;
256 }
257 }
258
259 if (error_is_ok and ((success or not is_pid_valid(local_pid))))
260 {
261 Error << "kill(" << " pid: " << local_pid << " errno:" << strerror(errno) << " for:" << *this;
262 }
263
264 return local_pid;
265 }
266
267 bool ping()
268 {
269 // Memcached is slow to start, so we need to do this
270 if (not pid_file().empty())
271 {
272 if (not wait_for_pidfile())
273 {
274 Error << "Pidfile was not found:" << pid_file();
275 return false;
276 }
277 }
278
279 std::stringstream error_message;
280 pid_t local_pid= get_pid_from_file(pid_file(), error_message);
281 if (local_pid > 0)
282 {
283 if (::kill(local_pid, 0) == 0)
284 {
285 return true;
286 }
287 }
288
289 return false;
290 }
291
292 const char *name()
293 {
294 return "memcached_light";
295 };
296
297 const char *executable()
298 {
299 return MEMCACHED_LIGHT_BINARY;
300 }
301
302 const char *daemon_file_option()
303 {
304 return "--daemon";
305 }
306
307 virtual void port_option(Application& app, in_port_t arg)
308 {
309 char buffer[1024];
310 snprintf(buffer, sizeof(buffer), "--port=%d", int(arg));
311 app.add_option(buffer);
312 }
313
314 bool has_port_option() const
315 {
316 return true;
317 }
318
319 bool is_libtool()
320 {
321 return true;
322 }
323
324 void log_file_option(Application& app, const std::string& arg)
325 {
326 if (arg.empty() == false)
327 {
328 std::string buffer("--log-file=");
329 buffer+= arg;
330 app.add_option("--verbose");
331 app.add_option(buffer);
332 }
333 }
334
335 bool has_log_file_option() const
336 {
337 return true;
338 }
339
340 bool build(size_t argc, const char *argv[]);
341 };
342
343 class MemcachedSaSL : public Memcached
344 {
345 public:
346 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) :
347 Memcached(host_arg, port_arg, is_socket_arg, username_arg, password_arg)
348 { }
349
350 const char *name()
351 {
352 return "memcached-sasl";
353 };
354
355 const char *sasl() const
356 {
357 return " -S -B binary ";
358 }
359
360 const char *executable()
361 {
362 return MEMCACHED_SASL_BINARY;
363 }
364
365 pid_t get_pid(bool error_is_ok)
366 {
367 // Memcached is slow to start, so we need to do this
368 if (not pid_file().empty())
369 {
370 if (error_is_ok and not wait_for_pidfile())
371 {
372 Error << "Pidfile was not found:" << pid_file();
373 return -1;
374 }
375 }
376
377 pid_t local_pid;
378 memcached_return_t rc;
379 if (has_socket())
380 {
381 local_pid= libmemcached_util_getpid2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc);
382 }
383 else
384 {
385 local_pid= libmemcached_util_getpid2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc);
386 }
387
388 if (error_is_ok and ((memcached_failed(rc) or not is_pid_valid(local_pid))))
389 {
390 Error << "libmemcached_util_getpid2(" << memcached_strerror(NULL, rc) << ") username: " << username() << " password: " << password() << " pid: " << local_pid << " for:" << *this;
391 }
392
393 return local_pid;
394 }
395
396 bool ping()
397 {
398 // Memcached is slow to start, so we need to do this
399 if (not pid_file().empty())
400 {
401 if (not wait_for_pidfile())
402 {
403 Error << "Pidfile was not found:" << pid_file();
404 return -1;
405 }
406 }
407
408 memcached_return_t rc;
409 bool ret;
410
411 if (has_socket())
412 {
413 ret= libmemcached_util_ping2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc);
414 }
415 else
416 {
417 ret= libmemcached_util_ping2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc);
418 }
419
420 if (memcached_failed(rc) or not ret)
421 {
422 Error << "libmemcached_util_ping2(" << hostname() << ", " << port() << ", " << username() << ", " << password() << ") error: " << memcached_strerror(NULL, rc);
423 }
424
425 return ret;
426 }
427
428 };
429
430
431 #include <sstream>
432
433 bool Memcached::build(size_t argc, const char *argv[])
434 {
435 std::stringstream arg_buffer;
436
437 if (getuid() == 0 or geteuid() == 0)
438 {
439 add_option("-u", "root");
440 }
441
442 add_option("-l", "localhost");
443 add_option("-m", "128");
444 add_option("-M");
445
446 if (sasl())
447 {
448 add_option(sasl());
449 }
450
451 for (int x= 0 ; x < argc ; x++)
452 {
453 add_option(argv[x]);
454 }
455
456 return true;
457 }
458
459 bool MemcachedLight::build(size_t argc, const char *argv[])
460 {
461 for (int x= 0 ; x < argc ; x++)
462 {
463 add_option(argv[x]);
464 }
465
466 return true;
467 }
468
469 namespace libtest {
470
471 libtest::Server *build_memcached(const std::string& hostname, const in_port_t try_port)
472 {
473 return new Memcached(hostname, try_port, false);
474 }
475
476 libtest::Server *build_memcached_socket(const std::string& socket_file, const in_port_t try_port)
477 {
478 return new Memcached(socket_file, try_port, true);
479 }
480
481 libtest::Server *build_memcached_light(const std::string& hostname, const in_port_t try_port)
482 {
483 return new MemcachedLight(hostname, try_port);
484 }
485
486
487 libtest::Server *build_memcached_sasl(const std::string& hostname, const in_port_t try_port, const std::string& username, const std::string &password)
488 {
489 if (username.empty())
490 {
491 return new MemcachedSaSL(hostname, try_port, false, "memcached", "memcached");
492 }
493
494 return new MemcachedSaSL(hostname, try_port, false, username, password);
495 }
496
497 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)
498 {
499 if (username.empty())
500 {
501 return new MemcachedSaSL(socket_file, try_port, true, "memcached", "memcached");
502 }
503
504 return new MemcachedSaSL(socket_file, try_port, true, username, password);
505 }
506
507 }
508