jenkins-promote-staging-trunk-libmemcached-1
[m6w6/libmemcached] / libtest / cmdline.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 #include <libtest/common.h>
23
24 using namespace libtest;
25
26 #include <cstdlib>
27 #include <cstring>
28 #include <fcntl.h>
29 #include <fstream>
30 #include <memory>
31 #include <spawn.h>
32 #include <sstream>
33 #include <string>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36
37 extern "C" {
38 static int exited_successfully(int status)
39 {
40 if (status == 0)
41 {
42 return EXIT_SUCCESS;
43 }
44
45 if (WIFEXITED(status) == true)
46 {
47 return WEXITSTATUS(status);
48 }
49 else if (WIFSIGNALED(status) == true)
50 {
51 return WTERMSIG(status);
52 }
53
54 return EXIT_FAILURE;
55 }
56 }
57
58 namespace {
59
60 std::string print_argv(char * * & built_argv, const size_t& argc)
61 {
62 std::stringstream arg_buffer;
63
64 for (size_t x= 0; x < argc; x++)
65 {
66 arg_buffer << built_argv[x] << " ";
67 }
68
69 return arg_buffer.str();
70 }
71
72 std::string print_argv(char** argv)
73 {
74 std::stringstream arg_buffer;
75
76 for (char** ptr= argv; *ptr; ptr++)
77 {
78 arg_buffer << *ptr << " ";
79 }
80
81 return arg_buffer.str();
82 }
83
84 static Application::error_t int_to_error_t(int arg)
85 {
86 switch (arg)
87 {
88 case 127:
89 return Application::INVALID;
90
91 case 0:
92 return Application::SUCCESS;
93
94 default:
95 case 1:
96 return Application::FAILURE;
97 }
98 }
99 }
100
101 namespace libtest {
102
103 Application::Application(const std::string& arg, const bool _use_libtool_arg) :
104 _use_libtool(_use_libtool_arg),
105 _use_valgrind(false),
106 _use_gdb(false),
107 _argc(0),
108 _exectuble(arg),
109 built_argv(NULL),
110 _pid(-1)
111 {
112 if (_use_libtool)
113 {
114 if (libtool() == NULL)
115 {
116 throw "libtool requested, but know libtool was found";
117 }
118 }
119
120 // Find just the name of the application with no path
121 {
122 size_t found= arg.find_last_of("/\\");
123 if (found)
124 {
125 _exectuble_name= arg.substr(found +1);
126 }
127 else
128 {
129 _exectuble_name= arg;
130 }
131 }
132
133 if (_use_libtool and getenv("PWD"))
134 {
135 _exectuble_with_path+= getenv("PWD");
136 _exectuble_with_path+= "/";
137 }
138 _exectuble_with_path+= _exectuble;
139 }
140
141 Application::~Application()
142 {
143 delete_argv();
144 }
145
146 Application::error_t Application::run(const char *args[])
147 {
148 stdin_fd.reset();
149 stdout_fd.reset();
150 stderr_fd.reset();
151 _stdout_buffer.clear();
152 _stderr_buffer.clear();
153
154 posix_spawn_file_actions_t file_actions;
155 posix_spawn_file_actions_init(&file_actions);
156
157 stdin_fd.dup_for_spawn(Application::Pipe::READ, file_actions, STDIN_FILENO);
158 stdout_fd.dup_for_spawn(Application::Pipe::WRITE, file_actions, STDOUT_FILENO);
159 stderr_fd.dup_for_spawn(Application::Pipe::WRITE, file_actions, STDERR_FILENO);
160
161 create_argv(args);
162
163 int spawn_ret;
164 if (_use_gdb)
165 {
166 std::string gdb_run_file= create_tmpfile(_exectuble_name);
167 std::fstream file_stream;
168 file_stream.open(gdb_run_file.c_str(), std::fstream::out | std::fstream::trunc);
169
170 _gdb_filename= create_tmpfile(_exectuble_name);
171 file_stream
172 << "set logging redirect on" << std::endl
173 << "set logging file " << _gdb_filename << std::endl
174 << "set logging overwrite on" << std::endl
175 << "set logging on" << std::endl
176 << "set environment LIBTEST_IN_GDB=1" << std::endl
177 << "run " << arguments() << std::endl
178 << "thread apply all bt" << std::endl
179 << "quit" << std::endl;
180
181 fatal_assert(file_stream.good());
182 file_stream.close();
183
184 if (_use_libtool)
185 {
186 // libtool --mode=execute gdb -f -x binary
187 char *argv[]= {
188 const_cast<char *>(libtool()),
189 const_cast<char *>("--mode=execute"),
190 const_cast<char *>("gdb"),
191 const_cast<char *>("-batch"),
192 const_cast<char *>("-f"),
193 const_cast<char *>("-x"),
194 const_cast<char *>(gdb_run_file.c_str()),
195 const_cast<char *>(_exectuble_with_path.c_str()),
196 0};
197
198 spawn_ret= posix_spawnp(&_pid, libtool(), &file_actions, NULL, argv, NULL);
199 }
200 else
201 {
202 // gdb binary
203 char *argv[]= {
204 const_cast<char *>("gdb"),
205 const_cast<char *>("-batch"),
206 const_cast<char *>("-f"),
207 const_cast<char *>("-x"),
208 const_cast<char *>(gdb_run_file.c_str()),
209 const_cast<char *>(_exectuble_with_path.c_str()),
210 0};
211 spawn_ret= posix_spawnp(&_pid, "gdb", &file_actions, NULL, argv, NULL);
212 }
213 }
214 else
215 {
216 if (_use_libtool)
217 {
218 spawn_ret= posix_spawn(&_pid, built_argv[0], &file_actions, NULL, built_argv, NULL);
219 }
220 else
221 {
222 spawn_ret= posix_spawnp(&_pid, built_argv[0], &file_actions, NULL, built_argv, NULL);
223 }
224 }
225
226 posix_spawn_file_actions_destroy(&file_actions);
227
228 stdin_fd.close(Application::Pipe::READ);
229 stdout_fd.close(Application::Pipe::WRITE);
230 stderr_fd.close(Application::Pipe::WRITE);
231
232 if (spawn_ret)
233 {
234 return Application::INVALID;
235 }
236
237 return Application::SUCCESS;
238 }
239
240 Application::error_t Application::wait()
241 {
242 if (_pid == -1)
243 {
244 Error << "wait() got an invalid pid_t";
245 return Application::INVALID;
246 }
247
248 {
249 ssize_t read_length;
250 char buffer[1024]= { 0 };
251 bool bail= false;
252 while (((read_length= ::read(stdout_fd.fd()[0], buffer, sizeof(buffer))) != 0) or bail)
253 {
254 if (read_length == -1)
255 {
256 switch(errno)
257 {
258 case EAGAIN:
259 continue;
260
261 default:
262 Error << strerror(errno);
263 bail= true;
264 }
265 }
266 _stdout_buffer.reserve(read_length +1);
267 for (size_t x= 0; x < read_length; x++)
268 {
269 _stdout_buffer.push_back(buffer[x]);
270 }
271 // @todo Suck up all output code here
272 }
273 }
274
275 {
276 ssize_t read_length;
277 char buffer[1024]= { 0 };
278 bool bail= false;
279 while (((read_length= ::read(stderr_fd.fd()[0], buffer, sizeof(buffer))) != 0) or bail)
280 {
281 if (read_length == -1)
282 {
283 switch(errno)
284 {
285 case EAGAIN:
286 continue;
287
288 default:
289 Error << strerror(errno);
290 bail= true;
291 }
292 }
293 _stderr_buffer.reserve(read_length +1);
294 for (size_t x= 0; x < read_length; x++)
295 {
296 _stderr_buffer.push_back(buffer[x]);
297 }
298 // @todo Suck up all errput code here
299 }
300 }
301
302 error_t exit_code= FAILURE;
303 {
304 int status= 0;
305 pid_t waited_pid;
306 if ((waited_pid= waitpid(_pid, &status, 0)) == -1)
307 {
308 Error << "Error occured while waitpid(" << strerror(errno) << ") on pid " << int(_pid);
309 }
310 else
311 {
312 if (waited_pid != _pid)
313 {
314 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Pid mismatch, %d != %d", int(waited_pid), int(_pid));
315 }
316 exit_code= int_to_error_t(exited_successfully(status));
317 }
318 }
319
320 #if 0
321 if (exit_code == Application::INVALID)
322 {
323 Error << print_argv(built_argv, _argc);
324 }
325 #endif
326
327 return exit_code;
328 }
329
330 void Application::add_option(const std::string& arg)
331 {
332 _options.push_back(std::make_pair(arg, std::string()));
333 }
334
335 void Application::add_option(const std::string& name, const std::string& value)
336 {
337 _options.push_back(std::make_pair(name, value));
338 }
339
340 Application::Pipe::Pipe()
341 {
342 _fd[0]= -1;
343 _fd[1]= -1;
344 _open[0]= false;
345 _open[1]= false;
346 }
347
348 void Application::Pipe::reset()
349 {
350 close(READ);
351 close(WRITE);
352
353 int ret;
354 if ((ret= pipe(_fd)) < 0)
355 {
356 throw strerror(ret);
357 }
358 _open[0]= true;
359 _open[1]= true;
360
361 {
362 ret= fcntl(_fd[0], F_GETFL, 0);
363 if (ret == -1)
364 {
365 Error << "fcntl(F_GETFL) " << strerror(ret);
366 throw strerror(ret);
367 }
368
369 ret= fcntl(_fd[0], F_SETFL, ret | O_NONBLOCK);
370 if (ret == -1)
371 {
372 Error << "fcntl(F_SETFL) " << strerror(ret);
373 throw strerror(ret);
374 }
375 }
376 }
377
378 Application::Pipe::~Pipe()
379 {
380 close(READ);
381 close(WRITE);
382 }
383
384 void Application::Pipe::dup_for_spawn(const close_t& arg, posix_spawn_file_actions_t& file_actions, const int newfildes)
385 {
386 int type= int(arg);
387
388 int ret;
389 if ((ret= posix_spawn_file_actions_adddup2(&file_actions, _fd[type], newfildes )) < 0)
390 {
391 Error << "posix_spawn_file_actions_adddup2(" << strerror(ret) << ")";
392 throw strerror(ret);
393 }
394
395 if ((ret= posix_spawn_file_actions_addclose(&file_actions, _fd[type])) < 0)
396 {
397 Error << "posix_spawn_file_actions_adddup2(" << strerror(ret) << ")";
398 throw strerror(ret);
399 }
400 }
401
402 void Application::Pipe::close(const close_t& arg)
403 {
404 int type= int(arg);
405
406 if (_open[type])
407 {
408 int ret;
409 if ((ret= ::close(_fd[type])) < 0)
410 {
411 Error << "close(" << strerror(ret) << ")";
412 }
413 _open[type]= false;
414 }
415 }
416
417 void Application::create_argv(const char *args[])
418 {
419 _argc= 2 +_use_libtool ? 2 : 0; // +1 for the command, +2 for libtool/mode=execute, +1 for the NULL
420
421 if (_use_libtool)
422 {
423 _argc+= 2; // +2 for libtool --mode=execute
424 }
425
426 /*
427 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
428 */
429 if (_use_valgrind)
430 {
431 _argc+= 7;
432 }
433 else if (_use_gdb) // gdb
434 {
435 _argc+= 1;
436 }
437
438 for (Options::const_iterator iter= _options.begin(); iter != _options.end(); iter++)
439 {
440 _argc++;
441 if ((*iter).second.empty() == false)
442 {
443 _argc++;
444 }
445 }
446
447 if (args)
448 {
449 for (const char **ptr= args; *ptr; ++ptr)
450 {
451 _argc++;
452 }
453 }
454
455 delete_argv();
456 built_argv= new char * [_argc];
457
458 size_t x= 0;
459 if (_use_libtool)
460 {
461 assert(libtool());
462 built_argv[x++]= strdup(libtool());
463 built_argv[x++]= strdup("--mode=execute");
464 }
465
466 if (_use_valgrind)
467 {
468 /*
469 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
470 */
471 built_argv[x++]= strdup("valgrind");
472 built_argv[x++]= strdup("--error-exitcode=1");
473 built_argv[x++]= strdup("--leak-check=yes");
474 built_argv[x++]= strdup("--show-reachable=yes");
475 built_argv[x++]= strdup("--track-fds=yes");
476 built_argv[x++]= strdup("--malloc-fill=A5");
477 built_argv[x++]= strdup("--free-fill=DE");
478 }
479 else if (_use_gdb)
480 {
481 built_argv[x++]= strdup("gdb");
482 }
483
484 built_argv[x++]= strdup(_exectuble_with_path.c_str());
485
486 for (Options::const_iterator iter= _options.begin(); iter != _options.end(); iter++)
487 {
488 built_argv[x++]= strdup((*iter).first.c_str());
489 if ((*iter).second.empty() == false)
490 {
491 built_argv[x++]= strdup((*iter).second.c_str());
492 }
493 }
494
495 if (args)
496 {
497 for (const char **ptr= args; *ptr; ++ptr)
498 {
499 built_argv[x++]= strdup(*ptr);
500 }
501 }
502 built_argv[_argc -1]= NULL;
503 }
504
505 std::string Application::print()
506 {
507 return print_argv(built_argv, _argc);
508 }
509
510 std::string Application::arguments()
511 {
512 std::stringstream arg_buffer;
513
514 for (size_t x= 1 + _use_libtool ? 2 : 0;
515 x < _argc and built_argv[x];
516 x++)
517 {
518 arg_buffer << built_argv[x] << " ";
519 }
520
521 return arg_buffer.str();
522 }
523
524 void Application::delete_argv()
525 {
526 if (built_argv == NULL)
527 {
528 return;
529 }
530
531 for (size_t x= 0; x < _argc; x++)
532 {
533 if (built_argv[x])
534 {
535 ::free(built_argv[x]);
536 }
537 }
538 delete[] built_argv;
539 built_argv= NULL;
540 _argc= 0;
541 }
542
543
544 int exec_cmdline(const std::string& command, const char *args[], bool use_libtool)
545 {
546 Application app(command, use_libtool);
547
548 Application::error_t ret= app.run(args);
549
550 if (ret != Application::SUCCESS)
551 {
552 return int(ret);
553 }
554
555 return int(app.wait());
556 }
557
558 const char *gearmand_binary()
559 {
560 return GEARMAND_BINARY;
561 }
562
563 } // namespace exec_cmdline