Additions to testing to better check return values/etc for servers.
[m6w6/libmemcached] / libtest / server.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 <cassert>
26 #include <cerrno>
27 #include <cstdlib>
28 #include <iostream>
29
30 #include <algorithm>
31 #include <functional>
32 #include <locale>
33
34 // trim from end
35 static inline std::string &rtrim(std::string &s)
36 {
37 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
38 return s;
39 }
40
41 #include <libtest/server.h>
42 #include <libtest/stream.h>
43 #include <libtest/killpid.h>
44
45 namespace libtest {
46
47 std::ostream& operator<<(std::ostream& output, const Server &arg)
48 {
49 if (arg.is_socket())
50 {
51 output << arg.hostname();
52 }
53 else
54 {
55 output << arg.hostname() << ":" << arg.port();
56 }
57
58 if (arg.has_pid())
59 {
60 output << " Pid:" << arg.pid();
61 }
62
63 if (arg.has_socket())
64 {
65 output << " Socket:" << arg.socket();
66 }
67
68 if (not arg.running().empty())
69 {
70 output << " Exec:" << arg.running();
71 }
72
73
74 return output; // for multiple << operators
75 }
76
77 Server::Server(const std::string& host_arg, const in_port_t port_arg, bool is_socket_arg) :
78 _is_socket(is_socket_arg),
79 _pid(-1),
80 _port(port_arg),
81 _hostname(host_arg)
82 {
83 }
84
85 Server::~Server()
86 {
87 if (has_pid() and not kill(_pid))
88 {
89 Error << "Unable to kill:" << *this;
90 }
91 }
92
93 // If the server exists, kill it
94 bool Server::cycle()
95 {
96 uint32_t limit= 3;
97
98 // Try to ping, and kill the server #limit number of times
99 pid_t current_pid;
100 while (--limit and is_pid_valid(current_pid= get_pid()))
101 {
102 if (kill(current_pid))
103 {
104 Log << "Killed existing server," << *this << " with pid:" << current_pid;
105 dream(0, 50000);
106 continue;
107 }
108 }
109
110 // For whatever reason we could not kill it, and we reached limit
111 if (limit == 0)
112 {
113 Error << "Reached limit, could not kill server pid:" << current_pid;
114 return false;
115 }
116
117 return true;
118 }
119
120 bool Server::wait_for_pidfile() const
121 {
122 Wait wait(pid_file(), 4);
123
124 return wait.successful();
125 }
126
127 bool Server::start()
128 {
129 // If we find that we already have a pid then kill it.
130 if (has_pid() and kill(_pid) == false)
131 {
132 Error << "Could not kill() existing server during start() pid:" << _pid;
133 return false;
134 }
135 assert(not has_pid());
136
137 Application app(name(), is_libtool());
138 if (args(app) == false)
139 {
140 Error << "Could not build command()";
141 return false;
142 }
143
144 if (Application::SUCCESS != app.run())
145 {
146 Error << "Application::run()";
147 return false;
148 }
149
150 if (Application::SUCCESS != app.wait())
151 {
152 Error << "Application::wait()";
153 return false;
154 }
155
156 if (is_helgrind() or is_valgrind())
157 {
158 dream(5, 50000);
159 }
160
161 if (pid_file().empty() == false)
162 {
163 Wait wait(pid_file(), 8);
164
165 if (not wait.successful())
166 {
167 Error << "Unable to open pidfile for: " << _running;
168 }
169 }
170
171 int counter= 0;
172 bool pinged= false;
173 while ((pinged= ping()) == false and
174 counter < (is_helgrind() or is_valgrind() ? 20 : 5))
175 {
176 dream(counter++, 50000);
177 }
178
179 if (pinged == false)
180 {
181 // If we happen to have a pid file, lets try to kill it
182 if (pid_file().empty() == false)
183 {
184 Error << "We are going to kill it off";
185 kill_file(pid_file());
186 }
187 Error << "Failed to ping() server started with:" << _running;
188 _running.clear();
189 return false;
190 }
191
192 // A failing get_pid() at this point is considered an error
193 _pid= get_pid(true);
194
195 return has_pid();
196 }
197
198 void Server::reset_pid()
199 {
200 _running.clear();
201 _pid_file.clear();
202 _pid= -1;
203 }
204
205 pid_t Server::pid()
206 {
207 return _pid;
208 }
209
210 void Server::add_option(const std::string& arg)
211 {
212 _options.push_back(std::make_pair(arg, std::string()));
213 }
214
215 void Server::add_option(const std::string& name, const std::string& value)
216 {
217 _options.push_back(std::make_pair(name, value));
218 }
219
220 bool Server::set_socket_file()
221 {
222 char file_buffer[FILENAME_MAX];
223 file_buffer[0]= 0;
224
225 if (broken_pid_file())
226 {
227 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.socketXXXXXX", name());
228 }
229 else
230 {
231 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.socketXXXXXX", name());
232 }
233
234 int fd;
235 if ((fd= mkstemp(file_buffer)) == -1)
236 {
237 perror(file_buffer);
238 return false;
239 }
240 close(fd);
241 unlink(file_buffer);
242
243 _socket= file_buffer;
244
245 return true;
246 }
247
248 bool Server::set_pid_file()
249 {
250 char file_buffer[FILENAME_MAX];
251 file_buffer[0]= 0;
252
253 if (broken_pid_file())
254 {
255 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.pidXXXXXX", name());
256 }
257 else
258 {
259 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.pidXXXXXX", name());
260 }
261
262 int fd;
263 if ((fd= mkstemp(file_buffer)) == -1)
264 {
265 perror(file_buffer);
266 return false;
267 }
268 close(fd);
269 unlink(file_buffer);
270
271 _pid_file= file_buffer;
272
273 return true;
274 }
275
276 bool Server::set_log_file()
277 {
278 char file_buffer[FILENAME_MAX];
279 file_buffer[0]= 0;
280
281 snprintf(file_buffer, sizeof(file_buffer), "var/log/%s.logXXXXXX", name());
282 int fd;
283 if ((fd= mkstemp(file_buffer)) == -1)
284 {
285 perror(file_buffer);
286 return false;
287 }
288 close(fd);
289
290 _log_file= file_buffer;
291
292 return true;
293 }
294
295 bool Server::args(Application& app)
296 {
297
298 // Set a log file if it was requested (and we can)
299 if (getenv("LIBTEST_LOG") and has_log_file_option())
300 {
301 if (not set_log_file())
302 {
303 return false;
304 }
305
306 log_file_option(app, _log_file);
307 }
308
309 if (getenv("LIBTEST_SYSLOG") and has_syslog())
310 {
311 app.add_option("--syslog");
312 }
313
314 // Update pid_file
315 {
316 if (_pid_file.empty() and set_pid_file() == false)
317 {
318 return false;
319 }
320
321 pid_file_option(app, pid_file());
322 }
323
324 assert(daemon_file_option());
325 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
326 {
327 app.add_option(daemon_file_option());
328 }
329
330 if (_is_socket and has_socket_file_option())
331 {
332 if (not set_socket_file())
333 {
334 return false;
335 }
336
337 socket_file_option(app, _socket);
338 }
339
340 if (has_port_option())
341 {
342 port_option(app, _port);
343 }
344
345 for (Options::const_iterator iter= _options.begin(); iter != _options.end(); iter++)
346 {
347 if ((*iter).second.empty() == false)
348 {
349 app.add_option((*iter).first, (*iter).second);
350 }
351 else
352 {
353 app.add_option((*iter).first);
354 }
355 }
356
357 return true;
358 }
359
360 bool Server::is_debug() const
361 {
362 return bool(getenv("LIBTEST_MANUAL_GDB"));
363 }
364
365 bool Server::is_valgrind() const
366 {
367 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
368 }
369
370 bool Server::is_helgrind() const
371 {
372 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
373 }
374
375 bool Server::kill(pid_t pid_arg)
376 {
377 if (check_pid(pid_arg) and kill_pid(pid_arg)) // If we kill it, reset
378 {
379 if (broken_pid_file() and pid_file().empty() == false)
380 {
381 unlink(pid_file().c_str());
382 }
383
384 if (broken_socket_cleanup() and has_socket() and not socket().empty())
385 {
386 unlink(socket().c_str());
387 }
388
389 reset_pid();
390
391 return true;
392 }
393
394 return false;
395 }
396
397 } // namespace libtest