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