Merge in util update
[awesomized/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 extern "C" {
46 static bool exited_successfully(int status)
47 {
48 if (WEXITSTATUS(status) == 0)
49 {
50 return true;
51 }
52
53 return true;
54 }
55 }
56
57
58 namespace libtest {
59
60 std::ostream& operator<<(std::ostream& output, const Server &arg)
61 {
62 if (arg.is_socket())
63 {
64 output << arg.hostname();
65 }
66 else
67 {
68 output << arg.hostname() << ":" << arg.port();
69 }
70
71 if (arg.has_pid())
72 {
73 output << " Pid:" << arg.pid();
74 }
75
76 if (arg.has_socket())
77 {
78 output << " Socket:" << arg.socket();
79 }
80
81 if (not arg.running().empty())
82 {
83 output << " Exec:" << arg.running();
84 }
85
86
87 return output; // for multiple << operators
88 }
89
90 void Server::nap(void)
91 {
92 #ifdef WIN32
93 sleep(1);
94 #else
95 struct timespec global_sleep_value= { 0, 50000 };
96 nanosleep(&global_sleep_value, NULL);
97 #endif
98 }
99
100 Server::Server(const std::string& host_arg, const in_port_t port_arg, bool is_socket_arg) :
101 _is_socket(is_socket_arg),
102 _pid(-1),
103 _port(port_arg),
104 _hostname(host_arg)
105 {
106 }
107
108 Server::~Server()
109 {
110 if (has_pid() and not kill(_pid))
111 {
112 Error << "Unable to kill:" << *this;
113 }
114 }
115
116 // If the server exists, kill it
117 bool Server::cycle()
118 {
119 uint32_t limit= 3;
120
121 // Try to ping, and kill the server #limit number of times
122 pid_t current_pid;
123 while (--limit and is_pid_valid(current_pid= get_pid()))
124 {
125 if (kill(current_pid))
126 {
127 Log << "Killed existing server," << *this << " with pid:" << current_pid;
128 nap();
129 continue;
130 }
131 }
132
133 // For whatever reason we could not kill it, and we reached limit
134 if (limit == 0)
135 {
136 Error << "Reached limit, could not kill server pid:" << current_pid;
137 return false;
138 }
139
140 return true;
141 }
142
143 // Grab a one off command
144 bool Server::command(std::string& command_arg)
145 {
146 rebuild_base_command();
147
148 command_arg+= _base_command;
149
150 if (args(command_arg))
151 {
152 return true;
153 }
154
155 return false;
156 }
157
158 bool Server::wait_for_pidfile() const
159 {
160 Wait wait(pid_file(), 4);
161
162 return wait.successful();
163 }
164
165 bool Server::start()
166 {
167 // If we find that we already have a pid then kill it.
168 if (has_pid() and not kill(_pid))
169 {
170 Error << "Could not kill() existing server during start() pid:" << _pid;
171 return false;
172 }
173 assert(not has_pid());
174
175 _running.clear();
176 if (not command(_running))
177 {
178 Error << "Could not build command()";
179 return false;
180 }
181
182 if (is_valgrind() or is_helgrind())
183 {
184 _running+= " &";
185 }
186
187 int ret= system(_running.c_str());
188 if (not exited_successfully(ret))
189 {
190 Error << "system() failed:" << strerror(errno);
191 _running.clear();
192 return false;
193 }
194
195 if (is_helgrind() or is_valgrind())
196 {
197 sleep(4);
198 }
199
200 if (pid_file_option() and not pid_file().empty())
201 {
202 Wait wait(pid_file(), 8);
203
204 if (not wait.successful())
205 {
206 Error << "Unable to open pidfile for: " << _running;
207 }
208 }
209
210 int count= is_helgrind() or is_valgrind() ? 20 : 5;
211 while (not ping() and --count)
212 {
213 nap();
214 }
215
216 if (count == 0)
217 {
218 // If we happen to have a pid file, lets try to kill it
219 if (pid_file_option() and not pid_file().empty())
220 {
221 kill_file(pid_file());
222 }
223 Error << "Failed to ping() server started with:" << _running;
224 _running.clear();
225 return false;
226 }
227
228 // A failing get_pid() at this point is considered an error
229 _pid= get_pid(true);
230
231 return has_pid();
232 }
233
234 void Server::reset_pid()
235 {
236 _running.clear();
237 _pid_file.clear();
238 _pid= -1;
239 }
240
241 pid_t Server::pid()
242 {
243 return _pid;
244 }
245
246 bool Server::set_socket_file()
247 {
248 char file_buffer[FILENAME_MAX];
249 file_buffer[0]= 0;
250
251 if (broken_pid_file())
252 {
253 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.socketXXXXXX", name());
254 }
255 else
256 {
257 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.socketXXXXXX", name());
258 }
259
260 int fd;
261 if ((fd= mkstemp(file_buffer)) == -1)
262 {
263 perror(file_buffer);
264 return false;
265 }
266 close(fd);
267 unlink(file_buffer);
268
269 _socket= file_buffer;
270
271 return true;
272 }
273
274 bool Server::set_pid_file()
275 {
276 char file_buffer[FILENAME_MAX];
277 file_buffer[0]= 0;
278
279 if (broken_pid_file())
280 {
281 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.pidXXXXXX", name());
282 }
283 else
284 {
285 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.pidXXXXXX", name());
286 }
287
288 int fd;
289 if ((fd= mkstemp(file_buffer)) == -1)
290 {
291 perror(file_buffer);
292 return false;
293 }
294 close(fd);
295 unlink(file_buffer);
296
297 _pid_file= file_buffer;
298
299 return true;
300 }
301
302 bool Server::set_log_file()
303 {
304 char file_buffer[FILENAME_MAX];
305 file_buffer[0]= 0;
306
307 snprintf(file_buffer, sizeof(file_buffer), "var/log/%s.logXXXXXX", name());
308 int fd;
309 if ((fd= mkstemp(file_buffer)) == -1)
310 {
311 perror(file_buffer);
312 return false;
313 }
314 close(fd);
315
316 _log_file= file_buffer;
317
318 return true;
319 }
320
321 void Server::rebuild_base_command()
322 {
323 _base_command.clear();
324 if (is_libtool())
325 {
326 _base_command+= libtool();
327 }
328
329 if (is_debug() and getenv("GDB_COMMAND"))
330 {
331 _base_command+= getenv("GDB_COMMAND");
332 _base_command+= " ";
333 }
334 else if (is_valgrind() and getenv("VALGRIND_COMMAND"))
335 {
336 _base_command+= getenv("VALGRIND_COMMAND");
337 _base_command+= " ";
338 }
339 else if (is_helgrind() and getenv("HELGRIND_COMMAND"))
340 {
341 _base_command+= getenv("HELGRIND_COMMAND");
342 _base_command+= " ";
343 }
344
345 _base_command+= executable();
346 }
347
348 void Server::set_extra_args(const std::string &arg)
349 {
350 _extra_args= arg;
351 }
352
353 bool Server::args(std::string& options)
354 {
355 std::stringstream arg_buffer;
356
357 // Set a log file if it was requested (and we can)
358 if (getenv("LIBTEST_LOG") and log_file_option())
359 {
360 if (not set_log_file())
361 {
362 return false;
363 }
364
365 arg_buffer << " " << log_file_option() << _log_file;
366 }
367
368 // Update pid_file
369 if (pid_file_option())
370 {
371 if (_pid_file.empty() and not set_pid_file())
372 {
373 return false;
374 }
375
376 arg_buffer << " " << pid_file_option() << pid_file();
377 }
378
379 assert(daemon_file_option());
380 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
381 {
382 arg_buffer << " " << daemon_file_option();
383 }
384
385 if (_is_socket and socket_file_option())
386 {
387 if (not set_socket_file())
388 {
389 return false;
390 }
391
392 arg_buffer << " " << socket_file_option() << "\"" << _socket << "\"";
393 }
394
395 assert(port_option());
396 if (port_option() and _port > 0)
397 {
398 arg_buffer << " " << port_option() << _port;
399 }
400
401 options+= arg_buffer.str();
402
403 if (not _extra_args.empty())
404 {
405 options+= _extra_args;
406 }
407
408 return true;
409 }
410
411 bool Server::is_debug() const
412 {
413 return bool(getenv("LIBTEST_MANUAL_GDB"));
414 }
415
416 bool Server::is_valgrind() const
417 {
418 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
419 }
420
421 bool Server::is_helgrind() const
422 {
423 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
424 }
425
426 bool Server::kill(pid_t pid_arg)
427 {
428 if (check_pid(pid_arg) and kill_pid(pid_arg)) // If we kill it, reset
429 {
430 if (broken_pid_file() and not pid_file().empty())
431 {
432 unlink(pid_file().c_str());
433 }
434
435 if (broken_socket_cleanup() and has_socket() and not socket().empty())
436 {
437 unlink(socket().c_str());
438 }
439
440 reset_pid();
441
442 return true;
443 }
444
445 return false;
446 }
447
448 } // namespace libtest