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