c4e7579cf8975b33838b64641f3abb1bb56fa7d9
[m6w6/libmemcached] / libtest / server.h
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 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 #pragma once
38
39 #include <libtest/cmdline.h>
40
41 #include <cassert>
42 #include <cstdio>
43 #include <cstring>
44
45 #ifdef HAVE_NETDB_H
46 # include <netdb.h>
47 #endif
48
49 #ifdef HAVE_NETINET_IN_H
50 # include <netinet/in.h>
51 #endif
52
53 #include <string>
54 #include <unistd.h>
55 #include <vector>
56
57 namespace libtest {
58
59 struct Server {
60 private:
61 typedef std::vector< std::pair<std::string, std::string> > Options;
62
63 private:
64 uint64_t _magic;
65 bool _is_socket;
66 std::string _socket;
67 std::string _sasl;
68 std::string _pid_file;
69 std::string _log_file;
70 std::string _base_command; // executable command which include libtool, valgrind, gdb, etc
71 std::string _running; // Current string being used for system()
72
73 protected:
74 in_port_t _port;
75 std::string _hostname;
76 std::string _extra_args;
77
78 public:
79 Server(const std::string& hostname, const in_port_t port_arg,
80 const std::string& executable, const bool _is_libtool,
81 const bool is_socket_arg= false);
82
83 virtual ~Server();
84
85 virtual const char *name()= 0;
86 virtual bool is_libtool()= 0;
87
88 virtual bool has_socket_file_option() const
89 {
90 return false;
91 }
92
93 virtual void socket_file_option(Application& app, const std::string& socket_arg)
94 {
95 if (socket_arg.empty() == false)
96 {
97 std::string buffer("--socket=");
98 buffer+= socket_arg;
99 app.add_option(buffer);
100 }
101 }
102
103 virtual bool has_log_file_option() const
104 {
105 return false;
106 }
107
108 virtual void log_file_option(Application& app, const std::string& arg)
109 {
110 if (arg.empty() == false)
111 {
112 std::string buffer("--log-file=");
113 buffer+= arg;
114 app.add_option(buffer);
115 }
116 }
117
118 virtual void pid_file_option(Application& app, const std::string& arg)
119 {
120 if (arg.empty() == false)
121 {
122 std::string buffer("--pid-file=");
123 buffer+= arg;
124 app.add_option(buffer);
125 }
126 }
127
128 virtual bool has_port_option() const
129 {
130 return false;
131 }
132
133 virtual void port_option(Application& app, in_port_t arg)
134 {
135 if (arg > 0)
136 {
137 char buffer[1024];
138 snprintf(buffer, sizeof(buffer), "--port=%d", int(arg));
139 app.add_option(buffer);
140 }
141 }
142
143 virtual bool broken_socket_cleanup()
144 {
145 return false;
146 }
147
148 virtual bool broken_pid_file()
149 {
150 return false;
151 }
152
153 const std::string& pid_file() const
154 {
155 return _pid_file;
156 }
157
158 const std::string& base_command() const
159 {
160 return _base_command;
161 }
162
163 const std::string& log_file() const
164 {
165 return _log_file;
166 }
167
168 const std::string& hostname() const
169 {
170 return _hostname;
171 }
172
173 const std::string& socket() const
174 {
175 return _socket;
176 }
177
178 bool has_socket() const
179 {
180 return _is_socket;
181 }
182
183 bool cycle();
184
185 virtual bool ping()= 0;
186
187 virtual bool build(size_t argc, const char *argv[])= 0;
188
189 void add_option(const std::string&);
190 void add_option(const std::string&, const std::string&);
191
192 in_port_t port() const
193 {
194 return _port;
195 }
196
197 bool has_port() const
198 {
199 return (_port != 0);
200 }
201
202 virtual bool has_syslog() const
203 {
204 return false;
205 }
206
207 // Reset a server if another process has killed the server
208 void reset()
209 {
210 _pid_file.clear();
211 _log_file.clear();
212 }
213
214 pid_t pid() const;
215
216 bool has_pid() const;
217
218 virtual bool has_pid_file() const
219 {
220 return true;
221 }
222
223 const std::string& error()
224 {
225 return _error;
226 }
227
228 void error(std::string arg)
229 {
230 _error= arg;
231 }
232
233 virtual bool wait_for_pidfile() const;
234
235 bool check_pid(pid_t pid_arg) const
236 {
237 return (pid_arg > 1);
238 }
239
240 bool is_socket() const
241 {
242 return _hostname[0] == '/';
243 }
244
245 const std::string running() const
246 {
247 return _running;
248 }
249
250 bool check();
251
252 std::string log_and_pid();
253
254 bool kill();
255 bool start();
256 bool command(libtest::Application& app);
257
258 bool validate();
259
260 void out_of_ban_killed(bool arg)
261 {
262 out_of_ban_killed_= arg;
263 }
264
265 bool out_of_ban_killed()
266 {
267 return out_of_ban_killed_;
268 }
269
270 protected:
271 bool set_pid_file();
272 Options _options;
273 Application _app;
274
275 private:
276 bool is_helgrind() const;
277 bool is_valgrind() const;
278 bool is_debug() const;
279 bool set_log_file();
280 bool set_socket_file();
281 void reset_pid();
282 bool out_of_ban_killed_;
283 bool args(Application&);
284
285 std::string _error;
286 };
287
288 std::ostream& operator<<(std::ostream& output, const libtest::Server &arg);
289
290 } // namespace libtest
291
292