Merge in all of libtest.
[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 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include <libtest/common.h>
38
39 #include <libmemcached/memcached.h>
40 #include <libmemcached/util.h>
41
42 using namespace libtest;
43
44 #include <cassert>
45 #include <cerrno>
46 #include <cstdio>
47 #include <cstdlib>
48 #include <cstring>
49 #include <iostream>
50 #include <signal.h>
51 #include <sys/types.h>
52 #include <sys/wait.h>
53 #include <unistd.h>
54
55 #include <libtest/server.h>
56 #include <libtest/wait.h>
57
58 #include <libtest/memcached.h>
59
60 #ifndef __INTEL_COMPILER
61 #pragma GCC diagnostic ignored "-Wold-style-cast"
62 #endif
63
64 using namespace libtest;
65
66 class Memcached : public Server
67 {
68 public:
69 Memcached(const std::string& host_arg, const in_port_t port_arg, const bool is_socket_arg) :
70 Server(host_arg, port_arg, is_socket_arg)
71 { }
72
73 pid_t get_pid(bool error_is_ok)
74 {
75 // Memcached is slow to start, so we need to do this
76 if (not pid_file().empty())
77 {
78 Wait wait(pid_file(), 0);
79
80 if (error_is_ok and not wait.successful())
81 {
82 Error << "Pidfile was not found:" << pid_file();
83 return -1;
84 }
85 }
86
87 pid_t local_pid;
88 memcached_return_t rc;
89 if (has_socket())
90 {
91 local_pid= libmemcached_util_getpid(socket().c_str(), port(), &rc);
92 }
93 else
94 {
95 local_pid= libmemcached_util_getpid(hostname().c_str(), port(), &rc);
96 }
97
98 if (error_is_ok and ((memcached_failed(rc) or local_pid < 1)))
99 {
100 Error << "libmemcached_util_getpid(" << memcached_strerror(NULL, rc) << ") pid: " << local_pid << " for:" << *this;
101 }
102
103 return local_pid;
104 }
105
106 bool ping()
107 {
108 // Memcached is slow to start, so we need to do this
109 if (not pid_file().empty())
110 {
111 Wait wait(pid_file(), 0);
112
113 if (not wait.successful())
114 {
115 Error << "Pidfile was not found:" << pid_file();
116 return -1;
117 }
118 }
119
120 memcached_return_t rc;
121 bool ret;
122 if (has_socket())
123 {
124 ret= libmemcached_util_ping(socket().c_str(), 0, &rc);
125 }
126 else
127 {
128 ret= libmemcached_util_ping(hostname().c_str(), port(), &rc);
129 }
130
131 if (memcached_failed(rc) or not ret)
132 {
133 Error << "libmemcached_util_ping(" << memcached_strerror(NULL, rc) << ")";
134 }
135 return ret;
136 }
137
138 const char *name()
139 {
140 return "memcached";
141 };
142
143 const char *executable()
144 {
145 return MEMCACHED_BINARY;
146 }
147
148 const char *pid_file_option()
149 {
150 return "-P ";
151 }
152
153 const char *socket_file_option() const
154 {
155 return "-s ";
156 }
157
158 const char *daemon_file_option()
159 {
160 return "-d";
161 }
162
163 const char *log_file_option()
164 {
165 return NULL;
166 }
167
168 const char *port_option()
169 {
170 return "-p ";
171 }
172
173 bool is_libtool()
174 {
175 return false;
176 }
177
178 // Memcached's pidfile is broken
179 bool broken_pid_file()
180 {
181 return true;
182 }
183
184 bool build(int argc, const char *argv[]);
185 };
186
187
188 #include <sstream>
189
190 bool Memcached::build(int argc, const char *argv[])
191 {
192 std::stringstream arg_buffer;
193
194 if (getuid() == 0 or geteuid() == 0)
195 {
196 arg_buffer << " -u root ";
197 }
198
199 for (int x= 1 ; x < argc ; x++)
200 {
201 arg_buffer << " " << argv[x] << " ";
202 }
203
204 set_extra_args(arg_buffer.str());
205
206 return true;
207 }
208
209 namespace libtest {
210
211 Server *build_memcached(const std::string& hostname, const in_port_t try_port)
212 {
213 return new Memcached(hostname, try_port, false);
214 }
215
216 Server *build_memcached_socket(const std::string& hostname, const in_port_t try_port)
217 {
218 return new Memcached(hostname, try_port, true);
219 }
220
221 }
222