jenkins-promote-staging-trunk-libmemcached-6
[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 _port(port_arg),
85 _hostname(host_arg),
86 _app(executable, _is_libtool)
87 {
88 }
89
90 Server::~Server()
91 {
92 }
93
94 bool Server::check()
95 {
96 _app.slurp();
97 _app.check();
98 return true;
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 (_app.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() == true)
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 (getenv("YATL_VALGRIND_SERVER"))
163 {
164 _app.use_valgrind();
165 }
166 else if (args(_app) == false)
167 {
168 Error << "Could not build command()";
169 return false;
170 }
171
172 Application::error_t ret;
173 if (Application::SUCCESS != (ret= _app.run()))
174 {
175 Error << "Application::run() " << ret;
176 return false;
177 }
178 _running= _app.print();
179
180 if (valgrind_is_caller())
181 {
182 dream(5, 50000);
183 }
184
185 if (pid_file().empty() == false)
186 {
187 Wait wait(pid_file(), 8);
188
189 if (wait.successful() == false)
190 {
191 throw libtest::fatal(LIBYATL_DEFAULT_PARAM,
192 "Unable to open pidfile for: %s",
193 _running.c_str());
194 }
195 }
196
197 uint32_t this_wait;
198 bool pinged= false;
199 {
200 uint32_t timeout= 20; // This number should be high enough for valgrind startup (which is slow)
201 uint32_t waited;
202 uint32_t retry;
203
204 for (waited= 0, retry= 1; ; retry++, waited+= this_wait)
205 {
206 if ((pinged= ping()) == true)
207 {
208 break;
209 }
210 else if (waited >= timeout)
211 {
212 break;
213 }
214
215 this_wait= retry * retry / 3 + 1;
216 libtest::dream(this_wait, 0);
217 }
218 }
219
220 if (pinged == false)
221 {
222 // If we happen to have a pid file, lets try to kill it
223 if (pid_file().empty() == false)
224 {
225 if (kill_file(pid_file()) == false)
226 {
227 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Failed to kill off server after startup occurred, when pinging failed: %s", pid_file().c_str());
228 }
229 Error << "Failed to ping(), waited:" << this_wait
230 << " server started, having pid_file. exec:" << _running
231 << " error:" << _app.stderr_result();
232 }
233 else
234 {
235 Error << "Failed to ping() server started. exec:" << _running;
236 }
237 _running.clear();
238 return false;
239 }
240
241 return has_pid();
242 }
243
244 void Server::reset_pid()
245 {
246 _running.clear();
247 _pid_file.clear();
248 }
249
250 pid_t Server::pid() const
251 {
252 return _app.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 throw 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 (false and 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())) // If we kill it, reset
397 {
398 _app.murder();
399 if (broken_pid_file() and pid_file().empty() == false)
400 {
401 unlink(pid_file().c_str());
402 }
403
404 if (broken_socket_cleanup() and has_socket() and not socket().empty())
405 {
406 unlink(socket().c_str());
407 }
408
409 reset_pid();
410
411 return true;
412 }
413
414 return false;
415 }
416
417 } // namespace libtest