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