Merging bzr://gaz.tangent.org/libmemcached/build/ to Build branch
[awesomized/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 <config.h>
23 #include <libtest/common.h>
24
25 using namespace libtest;
26
27 #include <cstdlib>
28 #include <cstring>
29 #include <cerrno>
30 #include <fcntl.h>
31 #include <fstream>
32 #include <memory>
33 #include <poll.h>
34 #include <spawn.h>
35 #include <sstream>
36 #include <string>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39
40 extern "C" {
41 static int exited_successfully(int status)
42 {
43 if (status == 0)
44 {
45 return EXIT_SUCCESS;
46 }
47
48 if (WIFEXITED(status) == true)
49 {
50 return WEXITSTATUS(status);
51 }
52 else if (WIFSIGNALED(status) == true)
53 {
54 return WTERMSIG(status);
55 }
56
57 return EXIT_FAILURE;
58 }
59 }
60
61 namespace {
62
63 std::string print_argv(char * * & built_argv, const size_t& argc)
64 {
65 std::stringstream arg_buffer;
66
67 for (size_t x= 0; x < argc; x++)
68 {
69 arg_buffer << built_argv[x] << " ";
70 }
71
72 return arg_buffer.str();
73 }
74
75 std::string print_argv(char** argv)
76 {
77 std::stringstream arg_buffer;
78
79 for (char** ptr= argv; *ptr; ptr++)
80 {
81 arg_buffer << *ptr << " ";
82 }
83
84 return arg_buffer.str();
85 }
86
87 static Application::error_t int_to_error_t(int arg)
88 {
89 switch (arg)
90 {
91 case 127:
92 return Application::INVALID;
93
94 case 0:
95 return Application::SUCCESS;
96
97 default:
98 case 1:
99 return Application::FAILURE;
100 }
101 }
102 }
103
104 namespace libtest {
105
106 Application::Application(const std::string& arg, const bool _use_libtool_arg) :
107 _use_libtool(_use_libtool_arg),
108 _use_valgrind(false),
109 _use_gdb(false),
110 _argc(0),
111 _exectuble(arg),
112 stdin_fd(STDIN_FILENO),
113 stdout_fd(STDOUT_FILENO),
114 stderr_fd(STDERR_FILENO),
115 built_argv(NULL),
116 _pid(-1)
117 {
118 if (_use_libtool)
119 {
120 if (libtool() == NULL)
121 {
122 fatal_message("libtool requested, but know libtool was found");
123 }
124 }
125
126 // Find just the name of the application with no path
127 {
128 size_t found= arg.find_last_of("/\\");
129 if (found)
130 {
131 _exectuble_name= arg.substr(found +1);
132 }
133 else
134 {
135 _exectuble_name= arg;
136 }
137 }
138
139 if (_use_libtool and getenv("PWD"))
140 {
141 _exectuble_with_path+= getenv("PWD");
142 _exectuble_with_path+= "/";
143 }
144 _exectuble_with_path+= _exectuble;
145 }
146
147 Application::~Application()
148 {
149 murder();
150 delete_argv();
151 }
152
153 Application::error_t Application::run(const char *args[])
154 {
155 stdin_fd.reset();
156 stdout_fd.reset();
157 stderr_fd.reset();
158 _stdout_buffer.clear();
159 _stderr_buffer.clear();
160
161 posix_spawn_file_actions_t file_actions;
162 posix_spawn_file_actions_init(&file_actions);
163
164 stdin_fd.dup_for_spawn(Application::Pipe::READ, file_actions);
165 stdout_fd.dup_for_spawn(Application::Pipe::WRITE, file_actions);
166 stderr_fd.dup_for_spawn(Application::Pipe::WRITE, file_actions);
167
168 posix_spawnattr_t spawnattr;
169 posix_spawnattr_init(&spawnattr);
170
171 sigset_t set;
172 sigemptyset(&set);
173 fatal_assert(posix_spawnattr_setsigmask(&spawnattr, &set) == 0);
174
175 create_argv(args);
176
177 int spawn_ret;
178 if (_use_gdb)
179 {
180 std::string gdb_run_file= create_tmpfile(_exectuble_name);
181 std::fstream file_stream;
182 file_stream.open(gdb_run_file.c_str(), std::fstream::out | std::fstream::trunc);
183
184 _gdb_filename= create_tmpfile(_exectuble_name);
185 file_stream
186 << "set logging redirect on" << std::endl
187 << "set logging file " << _gdb_filename << std::endl
188 << "set logging overwrite on" << std::endl
189 << "set logging on" << std::endl
190 << "set environment LIBTEST_IN_GDB=1" << std::endl
191 << "run " << arguments() << std::endl
192 << "thread apply all bt" << std::endl
193 << "quit" << std::endl;
194
195 fatal_assert(file_stream.good());
196 file_stream.close();
197
198 if (_use_libtool)
199 {
200 // libtool --mode=execute gdb -f -x binary
201 char *argv[]= {
202 const_cast<char *>(libtool()),
203 const_cast<char *>("--mode=execute"),
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
212 spawn_ret= posix_spawnp(&_pid, libtool(), &file_actions, &spawnattr, argv, NULL);
213 }
214 else
215 {
216 // gdb binary
217 char *argv[]= {
218 const_cast<char *>("gdb"),
219 const_cast<char *>("-batch"),
220 const_cast<char *>("-f"),
221 const_cast<char *>("-x"),
222 const_cast<char *>(gdb_run_file.c_str()),
223 const_cast<char *>(_exectuble_with_path.c_str()),
224 0};
225 spawn_ret= posix_spawnp(&_pid, "gdb", &file_actions, &spawnattr, argv, NULL);
226 }
227 }
228 else
229 {
230 if (_use_libtool)
231 {
232 spawn_ret= posix_spawn(&_pid, built_argv[0], &file_actions, &spawnattr, built_argv, NULL);
233 }
234 else
235 {
236 spawn_ret= posix_spawnp(&_pid, built_argv[0], &file_actions, &spawnattr, built_argv, NULL);
237 }
238 }
239
240 posix_spawn_file_actions_destroy(&file_actions);
241 posix_spawnattr_destroy(&spawnattr);
242
243 stdin_fd.close(Application::Pipe::READ);
244 stdout_fd.close(Application::Pipe::WRITE);
245 stderr_fd.close(Application::Pipe::WRITE);
246
247 if (spawn_ret)
248 {
249 _pid= -1;
250 return Application::INVALID;
251 }
252
253 return Application::SUCCESS;
254 }
255
256 bool Application::check() const
257 {
258 if (_pid > 1 and kill(_pid, 0) == 0)
259 {
260 return true;
261 }
262
263 return false;
264 }
265
266 void Application::murder()
267 {
268 if (check())
269 {
270 int count= 5;
271 while ((count--) > 0 and check())
272 {
273 int kill_ret= kill(_pid, SIGTERM);
274 if (kill_ret == 0)
275 {
276 int status= 0;
277 pid_t waitpid_ret;
278 if ((waitpid_ret= waitpid(_pid, &status, WNOHANG)) == -1)
279 {
280 switch (errno)
281 {
282 case ECHILD:
283 case EINTR:
284 break;
285
286 default:
287 Error << "waitpid() failed after kill with error of " << strerror(errno);
288 break;
289 }
290 }
291
292 if (waitpid_ret == 0)
293 {
294 libtest::dream(1, 0);
295 }
296 }
297 else
298 {
299 Error << "kill(pid, SIGTERM) failed after kill with error of " << strerror(errno);
300 continue;
301 }
302
303 break;
304 }
305
306 // If for whatever reason it lives, kill it hard
307 if (check())
308 {
309 (void)kill(_pid, SIGKILL);
310 }
311 }
312 slurp();
313 }
314
315 // false means that no data was returned
316 bool Application::slurp()
317 {
318 struct pollfd fds[2];
319 fds[0].fd= stdout_fd.fd();
320 fds[0].events= POLLRDNORM;
321 fds[0].revents= 0;
322 fds[1].fd= stderr_fd.fd();
323 fds[1].events= POLLRDNORM;
324 fds[1].revents= 0;
325
326 int active_fd;
327 if ((active_fd= poll(fds, 2, 0)) == -1)
328 {
329 int error;
330 switch ((error= errno))
331 {
332 #ifdef TARGET_OS_LINUX
333 case ERESTART:
334 #endif
335 case EINTR:
336 break;
337
338 case EFAULT:
339 case ENOMEM:
340 fatal_message(strerror(error));
341 break;
342
343 case EINVAL:
344 fatal_message("RLIMIT_NOFILE exceeded, or if OSX the timeout value was invalid");
345 break;
346
347 default:
348 fatal_message(strerror(error));
349 break;
350 }
351
352 return false;
353 }
354
355 if (active_fd == 0)
356 {
357 return false;
358 }
359
360 bool data_was_read= false;
361 if (fds[0].revents & POLLRDNORM)
362 {
363 if (stdout_fd.read(_stdout_buffer) == true)
364 {
365 data_was_read= true;
366 }
367 }
368
369 if (fds[1].revents & POLLRDNORM)
370 {
371 if (stderr_fd.read(_stderr_buffer) == true)
372 {
373 data_was_read= true;
374 }
375 }
376
377 return data_was_read;
378 }
379
380 Application::error_t Application::wait(bool nohang)
381 {
382 if (_pid == -1)
383 {
384 return Application::INVALID;
385 }
386
387 slurp();
388
389 error_t exit_code= FAILURE;
390 {
391 int status= 0;
392 pid_t waited_pid;
393 if ((waited_pid= waitpid(_pid, &status, nohang ? WNOHANG : 0)) == -1)
394 {
395 switch (errno)
396 {
397 case ECHILD:
398 exit_code= Application::SUCCESS;
399 break;
400
401 case EINTR:
402 break;
403
404 default:
405 Error << "Error occured while waitpid(" << strerror(errno) << ") on pid " << int(_pid);
406 break;
407 }
408 }
409 else if (waited_pid == 0)
410 {
411 exit_code= Application::SUCCESS;
412 }
413 else
414 {
415 if (waited_pid != _pid)
416 {
417 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Pid mismatch, %d != %d", int(waited_pid), int(_pid));
418 }
419 exit_code= int_to_error_t(exited_successfully(status));
420 }
421 }
422
423 slurp();
424
425 #if 0
426 if (exit_code == Application::INVALID)
427 {
428 Error << print_argv(built_argv, _argc);
429 }
430 #endif
431
432 return exit_code;
433 }
434
435 void Application::add_long_option(const std::string& name, const std::string& option_value)
436 {
437 std::string arg(name);
438 arg+= option_value;
439 _options.push_back(std::make_pair(arg, std::string()));
440 }
441
442 void Application::add_option(const std::string& arg)
443 {
444 _options.push_back(std::make_pair(arg, std::string()));
445 }
446
447 void Application::add_option(const std::string& name, const std::string& value)
448 {
449 _options.push_back(std::make_pair(name, value));
450 }
451
452 Application::Pipe::Pipe(int arg) :
453 _std_fd(arg)
454 {
455 _pipe_fd[READ]= -1;
456 _pipe_fd[WRITE]= -1;
457 _open[READ]= false;
458 _open[WRITE]= false;
459 }
460
461 int Application::Pipe::Pipe::fd()
462 {
463 if (_std_fd == STDOUT_FILENO)
464 {
465 return _pipe_fd[READ];
466 }
467 else if (_std_fd == STDERR_FILENO)
468 {
469 return _pipe_fd[READ];
470 }
471
472 return _pipe_fd[WRITE]; // STDIN_FILENO
473 }
474
475
476 bool Application::Pipe::read(libtest::vchar_t& arg)
477 {
478 fatal_assert(_std_fd == STDOUT_FILENO or _std_fd == STDERR_FILENO);
479
480 bool data_was_read= false;
481
482 ssize_t read_length;
483 char buffer[1024]= { 0 };
484 while ((read_length= ::read(_pipe_fd[READ], buffer, sizeof(buffer))))
485 {
486 if (read_length == -1)
487 {
488 switch(errno)
489 {
490 case EAGAIN:
491 break;
492
493 default:
494 Error << strerror(errno);
495 break;
496 }
497
498 break;
499 }
500
501 data_was_read= true;
502 arg.reserve(read_length +1);
503 for (size_t x= 0; x < read_length; x++)
504 {
505 arg.push_back(buffer[x]);
506 }
507 // @todo Suck up all errput code here
508 }
509
510 return data_was_read;
511 }
512
513 void Application::Pipe::nonblock()
514 {
515 int ret;
516 if ((ret= fcntl(_pipe_fd[READ], F_GETFL, 0)) == -1)
517 {
518 Error << "fcntl(F_GETFL) " << strerror(errno);
519 throw strerror(errno);
520 }
521
522 if ((ret= fcntl(_pipe_fd[READ], F_SETFL, ret | O_NONBLOCK)) == -1)
523 {
524 Error << "fcntl(F_SETFL) " << strerror(errno);
525 throw strerror(errno);
526 }
527 }
528
529 void Application::Pipe::reset()
530 {
531 close(READ);
532 close(WRITE);
533
534 #if _GNU_SOURCE
535 if (pipe2(_pipe_fd, O_NONBLOCK) == -1)
536 #else
537 if (pipe(_pipe_fd) == -1)
538 #endif
539 {
540 fatal_message(strerror(errno));
541 }
542 _open[0]= true;
543 _open[1]= true;
544
545 if (true)
546 {
547 nonblock();
548 cloexec();
549 }
550 }
551
552 void Application::Pipe::cloexec()
553 {
554 int ret;
555 if ((ret= fcntl(_pipe_fd[WRITE], F_GETFD, 0)) == -1)
556 {
557 Error << "fcntl(F_GETFD) " << strerror(errno);
558 throw strerror(errno);
559 }
560
561 if ((ret= fcntl(_pipe_fd[WRITE], F_SETFD, ret | FD_CLOEXEC)) == -1)
562 {
563 Error << "fcntl(F_SETFD) " << strerror(errno);
564 throw strerror(errno);
565 }
566 }
567
568 Application::Pipe::~Pipe()
569 {
570 close(READ);
571 close(WRITE);
572 }
573
574 void Application::Pipe::dup_for_spawn(const close_t& arg, posix_spawn_file_actions_t& file_actions)
575 {
576 int type= int(arg);
577
578 int ret;
579 if ((ret= posix_spawn_file_actions_adddup2(&file_actions, _pipe_fd[type], _std_fd )) < 0)
580 {
581 Error << "posix_spawn_file_actions_adddup2(" << strerror(ret) << ")";
582 fatal_message(strerror(ret));
583 }
584
585 if ((ret= posix_spawn_file_actions_addclose(&file_actions, _pipe_fd[type])) < 0)
586 {
587 Error << "posix_spawn_file_actions_adddup2(" << strerror(ret) << ")";
588 fatal_message(strerror(ret));
589 }
590 }
591
592 void Application::Pipe::close(const close_t& arg)
593 {
594 int type= int(arg);
595
596 if (_open[type])
597 {
598 int ret;
599 if (::close(_pipe_fd[type]) == -1)
600 {
601 Error << "close(" << strerror(errno) << ")";
602 }
603 _open[type]= false;
604 _pipe_fd[type]= -1;
605 }
606 }
607
608 void Application::create_argv(const char *args[])
609 {
610 delete_argv();
611 fatal_assert(_argc == 0);
612
613 if (_use_libtool)
614 {
615 _argc+= 2; // +2 for libtool --mode=execute
616 }
617
618 _argc+= 1; // For the command
619
620 /*
621 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
622 */
623 if (_use_valgrind)
624 {
625 _argc+= 7;
626 }
627 else if (_use_gdb) // gdb
628 {
629 _argc+= 1;
630 }
631
632 for (Options::const_iterator iter= _options.begin(); iter != _options.end(); iter++)
633 {
634 _argc++;
635 if ((*iter).second.empty() == false)
636 {
637 _argc++;
638 }
639 }
640
641 if (args)
642 {
643 for (const char **ptr= args; *ptr; ++ptr)
644 {
645 _argc++;
646 }
647 }
648
649 _argc+= 1; // for the NULL
650
651 built_argv= new char * [_argc];
652
653 size_t x= 0;
654 if (_use_libtool)
655 {
656 assert(libtool());
657 built_argv[x++]= strdup(libtool());
658 built_argv[x++]= strdup("--mode=execute");
659 }
660
661 if (_use_valgrind)
662 {
663 /*
664 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
665 */
666 built_argv[x++]= strdup("valgrind");
667 built_argv[x++]= strdup("--error-exitcode=1");
668 built_argv[x++]= strdup("--leak-check=yes");
669 built_argv[x++]= strdup("--show-reachable=yes");
670 built_argv[x++]= strdup("--track-fds=yes");
671 built_argv[x++]= strdup("--malloc-fill=A5");
672 built_argv[x++]= strdup("--free-fill=DE");
673 }
674 else if (_use_gdb)
675 {
676 built_argv[x++]= strdup("gdb");
677 }
678
679 built_argv[x++]= strdup(_exectuble_with_path.c_str());
680
681 for (Options::const_iterator iter= _options.begin(); iter != _options.end(); iter++)
682 {
683 built_argv[x++]= strdup((*iter).first.c_str());
684 if ((*iter).second.empty() == false)
685 {
686 built_argv[x++]= strdup((*iter).second.c_str());
687 }
688 }
689
690 if (args)
691 {
692 for (const char **ptr= args; *ptr; ++ptr)
693 {
694 built_argv[x++]= strdup(*ptr);
695 }
696 }
697 built_argv[x++]= NULL;
698 fatal_assert(x == _argc);
699 }
700
701 std::string Application::print()
702 {
703 return print_argv(built_argv, _argc);
704 }
705
706 std::string Application::arguments()
707 {
708 std::stringstream arg_buffer;
709
710 for (size_t x= 1 + _use_libtool ? 2 : 0;
711 x < _argc and built_argv[x];
712 x++)
713 {
714 arg_buffer << built_argv[x] << " ";
715 }
716
717 return arg_buffer.str();
718 }
719
720 void Application::delete_argv()
721 {
722 if (built_argv)
723 {
724 for (size_t x= 0; x < _argc; x++)
725 {
726 if (built_argv[x])
727 {
728 ::free(built_argv[x]);
729 }
730 }
731 delete[] built_argv;
732 built_argv= NULL;
733 _argc= 0;
734 }
735 }
736
737
738 int exec_cmdline(const std::string& command, const char *args[], bool use_libtool)
739 {
740 Application app(command, use_libtool);
741
742 Application::error_t ret= app.run(args);
743
744 if (ret != Application::SUCCESS)
745 {
746 return int(ret);
747 }
748
749 return int(app.wait(false));
750 }
751
752 const char *gearmand_binary()
753 {
754 return GEARMAND_BINARY;
755 }
756
757 } // namespace exec_cmdline