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