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