Update for testig memcached_light
[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 if (kill_file(pid_file()) == false)
190 {
191 fatal_message("Failed to kill off server after startup occurred, when pinging failed");
192 }
193 Error << "Failed to ping() server started, having pid_file. exec:" << _running;
194 }
195 else
196 {
197 Error << "Failed to ping() server started. exec:" << _running;
198 }
199 _running.clear();
200 return false;
201 }
202
203 // A failing get_pid() at this point is considered an error
204 _pid= get_pid(true);
205
206 return has_pid();
207 }
208
209 void Server::reset_pid()
210 {
211 _running.clear();
212 _pid_file.clear();
213 _pid= -1;
214 }
215
216 pid_t Server::pid()
217 {
218 return _pid;
219 }
220
221 void Server::add_option(const std::string& arg)
222 {
223 _options.push_back(std::make_pair(arg, std::string()));
224 }
225
226 void Server::add_option(const std::string& name, const std::string& value)
227 {
228 _options.push_back(std::make_pair(name, value));
229 }
230
231 bool Server::set_socket_file()
232 {
233 char file_buffer[FILENAME_MAX];
234 file_buffer[0]= 0;
235
236 if (broken_pid_file())
237 {
238 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.socketXXXXXX", name());
239 }
240 else
241 {
242 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.socketXXXXXX", name());
243 }
244
245 int fd;
246 if ((fd= mkstemp(file_buffer)) == -1)
247 {
248 perror(file_buffer);
249 return false;
250 }
251 close(fd);
252 unlink(file_buffer);
253
254 _socket= file_buffer;
255
256 return true;
257 }
258
259 bool Server::set_pid_file()
260 {
261 char file_buffer[FILENAME_MAX];
262 file_buffer[0]= 0;
263
264 if (broken_pid_file())
265 {
266 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.pidXXXXXX", name());
267 }
268 else
269 {
270 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.pidXXXXXX", name());
271 }
272
273 int fd;
274 if ((fd= mkstemp(file_buffer)) == -1)
275 {
276 perror(file_buffer);
277 return false;
278 }
279 close(fd);
280 unlink(file_buffer);
281
282 _pid_file= file_buffer;
283
284 return true;
285 }
286
287 bool Server::set_log_file()
288 {
289 char file_buffer[FILENAME_MAX];
290 file_buffer[0]= 0;
291
292 snprintf(file_buffer, sizeof(file_buffer), "var/log/%s.logXXXXXX", name());
293 int fd;
294 if ((fd= mkstemp(file_buffer)) == -1)
295 {
296 perror(file_buffer);
297 return false;
298 }
299 close(fd);
300
301 _log_file= file_buffer;
302
303 return true;
304 }
305
306 bool Server::args(Application& app)
307 {
308
309 // Set a log file if it was requested (and we can)
310 if (getenv("LIBTEST_LOG") and has_log_file_option())
311 {
312 if (not set_log_file())
313 {
314 return false;
315 }
316
317 log_file_option(app, _log_file);
318 }
319
320 if (getenv("LIBTEST_SYSLOG") and has_syslog())
321 {
322 app.add_option("--syslog");
323 }
324
325 // Update pid_file
326 {
327 if (_pid_file.empty() and set_pid_file() == false)
328 {
329 return false;
330 }
331
332 pid_file_option(app, pid_file());
333 }
334
335 assert(daemon_file_option());
336 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
337 {
338 app.add_option(daemon_file_option());
339 }
340
341 if (has_socket_file_option())
342 {
343 if (set_socket_file() == false)
344 {
345 return false;
346 }
347
348 socket_file_option(app, _socket);
349 }
350
351 if (has_port_option())
352 {
353 port_option(app, _port);
354 }
355
356 for (Options::const_iterator iter= _options.begin(); iter != _options.end(); iter++)
357 {
358 if ((*iter).second.empty() == false)
359 {
360 app.add_option((*iter).first, (*iter).second);
361 }
362 else
363 {
364 app.add_option((*iter).first);
365 }
366 }
367
368 return true;
369 }
370
371 bool Server::is_debug() const
372 {
373 return bool(getenv("LIBTEST_MANUAL_GDB"));
374 }
375
376 bool Server::is_valgrind() const
377 {
378 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
379 }
380
381 bool Server::is_helgrind() const
382 {
383 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
384 }
385
386 bool Server::kill(pid_t pid_arg)
387 {
388 if (check_pid(pid_arg) and kill_pid(pid_arg)) // If we kill it, reset
389 {
390 if (broken_pid_file() and pid_file().empty() == false)
391 {
392 unlink(pid_file().c_str());
393 }
394
395 if (broken_socket_cleanup() and has_socket() and not socket().empty())
396 {
397 unlink(socket().c_str());
398 }
399
400 reset_pid();
401
402 return true;
403 }
404
405 return false;
406 }
407
408 } // namespace libtest