Merge in updates to io
[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 Server
53 {
54 public:
55 Memcached(const std::string& host_arg, const in_port_t port_arg, const bool is_socket_arg) :
56 Server(host_arg, port_arg, is_socket_arg)
57 { }
58
59 pid_t get_pid(bool error_is_ok)
60 {
61 // Memcached is slow to start, so we need to do this
62 if (not pid_file().empty())
63 {
64 Wait wait(pid_file(), 0);
65
66 if (error_is_ok and not wait.successful())
67 {
68 Error << "Pidfile was not found:" << pid_file();
69 return -1;
70 }
71 }
72
73 pid_t local_pid;
74 memcached_return_t rc;
75 if (has_socket())
76 {
77 local_pid= libmemcached_util_getpid(socket().c_str(), port(), &rc);
78 }
79 else
80 {
81 local_pid= libmemcached_util_getpid(hostname().c_str(), port(), &rc);
82 }
83
84 if (error_is_ok and ((memcached_failed(rc) or local_pid < 1)))
85 {
86 Error << "libmemcached_util_getpid(" << memcached_strerror(NULL, rc) << ") pid: " << local_pid << " for:" << *this;
87 }
88
89 return local_pid;
90 }
91
92 bool ping()
93 {
94 // Memcached is slow to start, so we need to do this
95 if (not pid_file().empty())
96 {
97 Wait wait(pid_file(), 0);
98
99 if (not wait.successful())
100 {
101 Error << "Pidfile was not found:" << pid_file();
102 return -1;
103 }
104 }
105
106 memcached_return_t rc;
107 bool ret;
108 if (has_socket())
109 {
110 ret= libmemcached_util_ping(socket().c_str(), 0, &rc);
111 }
112 else
113 {
114 ret= libmemcached_util_ping(hostname().c_str(), port(), &rc);
115 }
116
117 if (memcached_failed(rc) or not ret)
118 {
119 Error << "libmemcached_util_ping(" << memcached_strerror(NULL, rc) << ")";
120 }
121 return ret;
122 }
123
124 const char *name()
125 {
126 return "memcached";
127 };
128
129 const char *executable()
130 {
131 return MEMCACHED_BINARY;
132 }
133
134 const char *pid_file_option()
135 {
136 return "-P ";
137 }
138
139 const char *socket_file_option() const
140 {
141 return "-s ";
142 }
143
144 const char *daemon_file_option()
145 {
146 return "-d";
147 }
148
149 const char *log_file_option()
150 {
151 return NULL;
152 }
153
154 const char *port_option()
155 {
156 return "-p ";
157 }
158
159 bool is_libtool()
160 {
161 return false;
162 }
163
164 // Memcached's pidfile is broken
165 bool broken_pid_file()
166 {
167 return true;
168 }
169
170 bool build(int argc, const char *argv[]);
171 };
172
173
174 #include <sstream>
175
176 bool Memcached::build(int argc, const char *argv[])
177 {
178 std::stringstream arg_buffer;
179
180 if (getuid() == 0 or geteuid() == 0)
181 {
182 arg_buffer << " -u root ";
183 }
184
185 arg_buffer << " -l 127.0.0.1 ";
186 arg_buffer << " -m 128 ";
187 arg_buffer << " -M ";
188
189 for (int x= 1 ; x < argc ; x++)
190 {
191 arg_buffer << " " << argv[x] << " ";
192 }
193
194 set_extra_args(arg_buffer.str());
195
196 return true;
197 }
198
199 namespace libtest {
200
201 Server *build_memcached(const std::string& hostname, const in_port_t try_port)
202 {
203 return new Memcached(hostname, try_port, false);
204 }
205
206 Server *build_memcached_socket(const std::string& hostname, const in_port_t try_port)
207 {
208 return new Memcached(hostname, try_port, true);
209 }
210
211 }
212