Merge in updates for sasl.
[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 <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 Wait wait(pid_file(), 0);
91
92 if (error_is_ok and not wait.successful())
93 {
94 Error << "Pidfile was not found:" << pid_file();
95 return -1;
96 }
97 }
98
99 pid_t local_pid;
100 memcached_return_t rc;
101 if (has_socket())
102 {
103 local_pid= libmemcached_util_getpid(socket().c_str(), 0, &rc);
104 }
105 else
106 {
107 local_pid= libmemcached_util_getpid(hostname().c_str(), port(), &rc);
108 }
109
110 if (error_is_ok and ((memcached_failed(rc) or not is_pid_valid(local_pid))))
111 {
112 Error << "libmemcached_util_getpid(" << memcached_strerror(NULL, rc) << ") pid: " << local_pid << " for:" << *this;
113 }
114
115 return local_pid;
116 }
117
118 bool ping()
119 {
120 // Memcached is slow to start, so we need to do this
121 if (not pid_file().empty())
122 {
123 Wait wait(pid_file(), 0);
124
125 if (not wait.successful())
126 {
127 Error << "Pidfile was not found:" << pid_file();
128 return -1;
129 }
130 }
131
132 memcached_return_t rc;
133 bool ret;
134
135 if (has_socket())
136 {
137 ret= libmemcached_util_ping(socket().c_str(), 0, &rc);
138 }
139 else
140 {
141 ret= libmemcached_util_ping(hostname().c_str(), port(), &rc);
142 }
143
144 if (memcached_failed(rc) or not ret)
145 {
146 Error << "libmemcached_util_ping(" << hostname() << ", " << port() << ") error: " << memcached_strerror(NULL, rc);
147 }
148
149 return ret;
150 }
151
152 const char *name()
153 {
154 return "memcached";
155 };
156
157 const char *executable()
158 {
159 return MEMCACHED_BINARY;
160 }
161
162 const char *pid_file_option()
163 {
164 return "-P ";
165 }
166
167 const char *socket_file_option() const
168 {
169 return "-s ";
170 }
171
172 const char *daemon_file_option()
173 {
174 return "-d";
175 }
176
177 const char *log_file_option()
178 {
179 return NULL;
180 }
181
182 const char *port_option()
183 {
184 return "-p ";
185 }
186
187 bool is_libtool()
188 {
189 return false;
190 }
191
192 bool broken_socket_cleanup()
193 {
194 return true;
195 }
196
197 // Memcached's pidfile is broken
198 bool broken_pid_file()
199 {
200 return true;
201 }
202
203 bool build(int argc, const char *argv[]);
204 };
205
206 class MemcachedSaSL : public Memcached
207 {
208 public:
209 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) :
210 Memcached(host_arg, port_arg, is_socket_arg, username_arg, password_arg)
211 { }
212
213 const char *name()
214 {
215 return "memcached-sasl";
216 };
217
218 const char *sasl() const
219 {
220 return " -S -B binary ";
221 }
222
223 const char *executable()
224 {
225 return MEMCACHED_SASL_BINARY;
226 }
227
228 pid_t get_pid(bool error_is_ok)
229 {
230 // Memcached is slow to start, so we need to do this
231 if (not pid_file().empty())
232 {
233 Wait wait(pid_file(), 0);
234
235 if (error_is_ok and not wait.successful())
236 {
237 Error << "Pidfile was not found:" << pid_file();
238 return -1;
239 }
240 }
241
242 pid_t local_pid;
243 memcached_return_t rc;
244 if (has_socket())
245 {
246 local_pid= libmemcached_util_getpid2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc);
247 }
248 else
249 {
250 local_pid= libmemcached_util_getpid2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc);
251 }
252
253 if (error_is_ok and ((memcached_failed(rc) or not is_pid_valid(local_pid))))
254 {
255 Error << "libmemcached_util_getpid2(" << memcached_strerror(NULL, rc) << ") username: " << username() << " password: " << password() << " pid: " << local_pid << " for:" << *this;
256 }
257
258 return local_pid;
259 }
260
261 bool ping()
262 {
263 // Memcached is slow to start, so we need to do this
264 if (not pid_file().empty())
265 {
266 Wait wait(pid_file(), 0);
267
268 if (not wait.successful())
269 {
270 Error << "Pidfile was not found:" << pid_file();
271 return -1;
272 }
273 }
274
275 memcached_return_t rc;
276 bool ret;
277
278 if (has_socket())
279 {
280 ret= libmemcached_util_ping2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc);
281 }
282 else
283 {
284 ret= libmemcached_util_ping2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc);
285 }
286
287 if (memcached_failed(rc) or not ret)
288 {
289 Error << "libmemcached_util_ping2(" << hostname() << ", " << port() << ", " << username() << ", " << password() << ") error: " << memcached_strerror(NULL, rc);
290 }
291
292 return ret;
293 }
294
295 };
296
297
298 #include <sstream>
299
300 bool Memcached::build(int argc, const char *argv[])
301 {
302 std::stringstream arg_buffer;
303
304 if (getuid() == 0 or geteuid() == 0)
305 {
306 arg_buffer << " -u root ";
307 }
308
309 arg_buffer << " -l 127.0.0.1 ";
310 arg_buffer << " -m 128 ";
311 arg_buffer << " -M ";
312
313 if (sasl())
314 {
315 arg_buffer << sasl();
316 }
317
318 for (int x= 1 ; x < argc ; x++)
319 {
320 arg_buffer << " " << argv[x] << " ";
321 }
322
323 set_extra_args(arg_buffer.str());
324
325 return true;
326 }
327
328 namespace libtest {
329
330 libtest::Server *build_memcached(const std::string& hostname, const in_port_t try_port)
331 {
332 return new Memcached(hostname, try_port, false);
333 }
334
335 libtest::Server *build_memcached_socket(const std::string& socket_file, const in_port_t try_port)
336 {
337 return new Memcached(socket_file, try_port, true);
338 }
339
340
341 libtest::Server *build_memcached_sasl(const std::string& hostname, const in_port_t try_port, const std::string& username, const std::string &password)
342 {
343 if (username.empty())
344 {
345 return new MemcachedSaSL(hostname, try_port, false, "memcached", "memcached");
346 }
347
348 return new MemcachedSaSL(hostname, try_port, false, username, password);
349 }
350
351 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)
352 {
353 if (username.empty())
354 {
355 return new MemcachedSaSL(socket_file, try_port, true, "memcached", "memcached");
356 }
357
358 return new MemcachedSaSL(socket_file, try_port, true, username, password);
359 }
360
361 }
362