Fix bad if path
[m6w6/libmemcached] / libtest / cmdline.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012-2013 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include "libtest/yatlcon.h"
38
39 #include "libtest/common.h"
40
41 using namespace libtest;
42
43 #include <cstdlib>
44 #include <cstring>
45 #include <cerrno>
46 #include <fcntl.h>
47 #include <fstream>
48 #include <memory>
49 #ifdef HAVE_POLL_H
50 # include <poll.h>
51 #endif
52 #ifdef HAVE_SPAWN_H
53 # include <spawn.h>
54 #endif
55 #include <sstream>
56 #include <string>
57 #include <sys/stat.h>
58 #include <sys/types.h>
59 #include <unistd.h>
60
61 #include <algorithm>
62 #include <stdexcept>
63
64 #ifndef __USE_GNU
65 static char **environ= NULL;
66 #endif
67
68 #ifndef FD_CLOEXEC
69 # define FD_CLOEXEC 0
70 #endif
71
72 namespace {
73
74 std::string print_argv(libtest::vchar_ptr_t& built_argv)
75 {
76 std::stringstream arg_buffer;
77
78 for (vchar_ptr_t::iterator iter= built_argv.begin();
79 iter != built_argv.end();
80 ++iter)
81 {
82 arg_buffer << *iter << " ";
83 }
84
85 return arg_buffer.str();
86 }
87
88 #if 0
89 std::string print_argv(char** argv)
90 {
91 std::stringstream arg_buffer;
92
93 for (char** ptr= argv; *ptr; ++ptr)
94 {
95 arg_buffer << *ptr << " ";
96 }
97
98 return arg_buffer.str();
99 }
100 #endif
101
102 static Application::error_t int_to_error_t(int arg)
103 {
104 switch (arg)
105 {
106 case 127:
107 return Application::INVALID_POSIX_SPAWN;
108
109 case 0:
110 return Application::SUCCESS;
111
112 case 1:
113 return Application::FAILURE;
114
115 default:
116 return Application::UNKNOWN;
117 }
118 }
119 }
120
121 namespace libtest {
122
123 Application::Application(const std::string& arg, const bool _use_libtool_arg) :
124 _use_libtool(_use_libtool_arg),
125 _use_valgrind(false),
126 _use_gdb(false),
127 _use_ptrcheck(false),
128 _will_fail(false),
129 _argc(0),
130 _exectuble(arg),
131 stdin_fd(STDIN_FILENO),
132 stdout_fd(STDOUT_FILENO),
133 stderr_fd(STDERR_FILENO),
134 _pid(-1),
135 _status(0),
136 _app_exit_state(UNINITIALIZED)
137 {
138 if (_use_libtool)
139 {
140 if (libtool() == NULL)
141 {
142 FATAL("libtool requested, but know libtool was found");
143 }
144 }
145
146 // Find just the name of the application with no path
147 {
148 size_t found= arg.find_last_of("/\\");
149 if (found)
150 {
151 _exectuble_name= arg.substr(found +1);
152 }
153 else
154 {
155 _exectuble_name= arg;
156 }
157 }
158
159 if (_use_libtool and getenv("PWD"))
160 {
161 _exectuble_with_path+= getenv("PWD");
162 _exectuble_with_path+= "/";
163 }
164 _exectuble_with_path+= _exectuble;
165 }
166
167 Application::~Application()
168 {
169 murder();
170 delete_argv();
171 }
172
173 Application::error_t Application::run(const char *args[])
174 {
175 stdin_fd.reset();
176 stdout_fd.reset();
177 stderr_fd.reset();
178 _stdout_buffer.clear();
179 _stderr_buffer.clear();
180
181 posix_spawn_file_actions_t file_actions;
182 posix_spawn_file_actions_init(&file_actions);
183
184 stdin_fd.dup_for_spawn(file_actions);
185 stdout_fd.dup_for_spawn(file_actions);
186 stderr_fd.dup_for_spawn(file_actions);
187
188 posix_spawnattr_t spawnattr;
189 posix_spawnattr_init(&spawnattr);
190
191 short flags= 0;
192
193 // Child should not block signals
194 flags |= POSIX_SPAWN_SETSIGMASK;
195
196 sigset_t mask;
197 sigemptyset(&mask);
198
199 fatal_assert(posix_spawnattr_setsigmask(&spawnattr, &mask) == 0);
200
201 #if defined(POSIX_SPAWN_USEVFORK) || defined(__linux__)
202 // Use USEVFORK on linux
203 flags |= POSIX_SPAWN_USEVFORK;
204 #endif
205
206 flags |= POSIX_SPAWN_SETPGROUP;
207 fatal_assert(posix_spawnattr_setpgroup(&spawnattr, 0) == 0);
208
209 fatal_assert(posix_spawnattr_setflags(&spawnattr, flags) == 0);
210
211 create_argv(args);
212
213 int spawn_ret;
214 if (_use_gdb)
215 {
216 std::string gdb_run_file= create_tmpfile(_exectuble_name);
217 std::fstream file_stream;
218 file_stream.open(gdb_run_file.c_str(), std::fstream::out | std::fstream::trunc);
219
220 _gdb_filename= create_tmpfile(_exectuble_name);
221 file_stream
222 << "set logging redirect on" << std::endl
223 << "set logging file " << _gdb_filename << std::endl
224 << "set logging overwrite on" << std::endl
225 << "set logging on" << std::endl
226 << "set environment LIBTEST_IN_GDB=1" << std::endl
227 << "run " << arguments() << std::endl
228 << "thread apply all bt" << std::endl
229 << "quit" << std::endl;
230
231 fatal_assert(file_stream.good());
232 file_stream.close();
233
234 if (_use_libtool)
235 {
236 // libtool --mode=execute gdb -f -x binary
237 char *argv[]= {
238 const_cast<char *>(libtool()),
239 const_cast<char *>("--mode=execute"),
240 const_cast<char *>("gdb"),
241 const_cast<char *>("-batch"),
242 const_cast<char *>("-f"),
243 const_cast<char *>("-x"),
244 const_cast<char *>(gdb_run_file.c_str()),
245 const_cast<char *>(_exectuble_with_path.c_str()),
246 0};
247
248 spawn_ret= posix_spawnp(&_pid, libtool(), &file_actions, &spawnattr, argv, environ);
249 }
250 else
251 {
252 // gdb binary
253 char *argv[]= {
254 const_cast<char *>("gdb"),
255 const_cast<char *>("-batch"),
256 const_cast<char *>("-f"),
257 const_cast<char *>("-x"),
258 const_cast<char *>(gdb_run_file.c_str()),
259 const_cast<char *>(_exectuble_with_path.c_str()),
260 0};
261 spawn_ret= posix_spawnp(&_pid, "gdb", &file_actions, &spawnattr, argv, environ);
262 }
263 }
264 else
265 {
266 spawn_ret= posix_spawn(&_pid, built_argv[0], &file_actions, &spawnattr, &built_argv[0], NULL);
267 }
268
269 posix_spawn_file_actions_destroy(&file_actions);
270 posix_spawnattr_destroy(&spawnattr);
271
272 stdin_fd.close(Application::Pipe::READ);
273 stdout_fd.close(Application::Pipe::WRITE);
274 stderr_fd.close(Application::Pipe::WRITE);
275
276 if (spawn_ret != 0)
277 {
278 if (_will_fail == false)
279 {
280 Error << strerror(spawn_ret) << "(" << spawn_ret << ")";
281 }
282 _pid= -1;
283 return Application::INVALID_POSIX_SPAWN;
284 }
285
286 assert(_pid != -1);
287 if (_pid == -1)
288 {
289 return Application::INVALID_POSIX_SPAWN;
290 }
291
292 #if 0
293 app_thread_st* _app_thread= new app_thread_st(_pid, _status, built_argv[0], _app_exit_state);
294 int error;
295 if ((error= pthread_create(&_thread, NULL, &app_thread, _app_thread)) != 0)
296 {
297 Error << "pthread_create() died during pthread_create(" << strerror(error) << ")";
298 return Application::FAILURE;
299 }
300 #endif
301
302 return Application::SUCCESS;
303 }
304
305 bool Application::check() const
306 {
307 if (_pid > 1 and kill(_pid, 0) == 0)
308 {
309 return true;
310 }
311
312 return false;
313 }
314
315 void Application::murder()
316 {
317 if (check())
318 {
319 int count= 5;
320 while ((count--) > 0 and check())
321 {
322 if (kill(_pid, SIGTERM) == 0)
323 {
324 join();
325 }
326 else
327 {
328 Error << "kill(pid, SIGTERM) failed after kill with error of " << strerror(errno);
329 continue;
330 }
331
332 break;
333 }
334
335 // If for whatever reason it lives, kill it hard
336 if (check())
337 {
338 Error << "using SIGKILL, things will likely go poorly from this point";
339 (void)kill(_pid, SIGKILL);
340 }
341 }
342 slurp();
343 }
344
345 // false means that no data was returned
346 bool Application::slurp()
347 {
348 struct pollfd fds[2];
349 fds[0].fd= stdout_fd.fd();
350 fds[0].events= POLLRDNORM;
351 fds[0].revents= 0;
352 fds[1].fd= stderr_fd.fd();
353 fds[1].events= POLLRDNORM;
354 fds[1].revents= 0;
355
356 int active_fd;
357 if ((active_fd= poll(fds, 2, 0)) == -1)
358 {
359 int error;
360 switch ((error= errno))
361 {
362 #ifdef __linux
363 case ERESTART:
364 #endif
365 case EINTR:
366 break;
367
368 case EFAULT:
369 case ENOMEM:
370 FATAL(strerror(error));
371 break;
372
373 case EINVAL:
374 FATAL("RLIMIT_NOFILE exceeded, or if OSX the timeout value was invalid");
375 break;
376
377 default:
378 FATAL(strerror(error));
379 break;
380 }
381
382 return false;
383 }
384
385 if (active_fd == 0)
386 {
387 return false;
388 }
389
390 bool data_was_read= false;
391 if (fds[0].revents & POLLRDNORM)
392 {
393 if (stdout_fd.read(_stdout_buffer) == true)
394 {
395 data_was_read= true;
396 }
397 }
398
399 if (fds[1].revents & POLLRDNORM)
400 {
401 if (stderr_fd.read(_stderr_buffer) == true)
402 {
403 data_was_read= true;
404 }
405 }
406
407 return data_was_read;
408 }
409
410 Application::error_t Application::join()
411 {
412 pid_t waited_pid= waitpid(_pid, &_status, WUNTRACED);
413 slurp();
414 if (waited_pid == _pid and WIFEXITED(_status) == false)
415 {
416 /*
417 What we are looking for here is how the exit status happened.
418 - 127 means that posix_spawn() itself had an error.
419 - If WEXITSTATUS is positive we need to see if it is a signal that we sent to kill the process. If not something bad happened in the process itself.
420 - Finally something has happened that we don't currently understand.
421 */
422 if (WEXITSTATUS(_status) == 127)
423 {
424 _app_exit_state= Application::INVALID_POSIX_SPAWN;
425 std::string error_string("posix_spawn() failed pid:");
426 error_string+= _pid;
427 error_string+= " name:";
428 error_string+= print_argv(built_argv);
429 if (stderr_result_length())
430 {
431 error_string+= " stderr: ";
432 error_string+= stderr_c_str();
433 }
434 throw std::logic_error(error_string);
435 }
436 else if (WIFSIGNALED(_status))
437 {
438 if (WTERMSIG(_status) != SIGTERM and WTERMSIG(_status) != SIGHUP)
439 {
440 slurp();
441 _app_exit_state= Application::INVALID_POSIX_SPAWN;
442 std::string error_string(print_argv(built_argv));
443 error_string+= " was killed by signal ";
444 error_string+= strsignal(WTERMSIG(_status));
445
446 if (stdout_result_length())
447 {
448 error_string+= " stdout: ";
449 error_string+= stdout_c_str();
450 }
451
452 if (stderr_result_length())
453 {
454 error_string+= " stderr: ";
455 error_string+= stderr_c_str();
456 }
457
458 throw std::runtime_error(error_string);
459 }
460
461 // If we terminted it on purpose then it counts as a success.
462 #if defined(DEBUG)
463 if (DEBUG)
464 {
465 Out << "waitpid() application terminated at request"
466 << " pid:" << _pid
467 << " name:" << built_argv[0];
468 }
469 #endif
470 }
471 else
472 {
473 _app_exit_state= Application::UNKNOWN;
474 Error << "Unknown logic state at exit:" << WEXITSTATUS(_status)
475 << " pid:" << _pid
476 << " name:" << built_argv[0];
477 }
478 }
479 else if (waited_pid == _pid and WIFEXITED(_status))
480 {
481 _app_exit_state= int_to_error_t(WEXITSTATUS(_status));
482 }
483 else if (waited_pid == -1)
484 {
485 std::string error_string;
486 if (stdout_result_length())
487 {
488 error_string+= " stdout: ";
489 error_string+= stdout_c_str();
490 }
491
492 if (stderr_result_length())
493 {
494 error_string+= " stderr: ";
495 error_string+= stderr_c_str();
496 }
497 Error << "waitpid() returned errno:" << strerror(errno) << " " << error_string;
498 _app_exit_state= Application::UNKNOWN;
499 }
500 else
501 {
502 _app_exit_state= Application::UNKNOWN;
503 throw std::logic_error("waitpid() returned an unknown value");
504 }
505
506 return _app_exit_state;
507 }
508
509 void Application::add_long_option(const std::string& name, const std::string& option_value)
510 {
511 std::string arg(name);
512 arg+= option_value;
513 _options.push_back(std::make_pair(arg, std::string()));
514 }
515
516 void Application::add_option(const std::string& arg)
517 {
518 _options.push_back(std::make_pair(arg, std::string()));
519 }
520
521 void Application::add_option(const std::string& name, const std::string& value)
522 {
523 _options.push_back(std::make_pair(name, value));
524 }
525
526 Application::Pipe::Pipe(int arg) :
527 _std_fd(arg)
528 {
529 _pipe_fd[READ]= -1;
530 _pipe_fd[WRITE]= -1;
531 _open[READ]= false;
532 _open[WRITE]= false;
533 }
534
535 int Application::Pipe::Pipe::fd()
536 {
537 if (_std_fd == STDOUT_FILENO)
538 {
539 return _pipe_fd[READ];
540 }
541 else if (_std_fd == STDERR_FILENO)
542 {
543 return _pipe_fd[READ];
544 }
545
546 return _pipe_fd[WRITE]; // STDIN_FILENO
547 }
548
549
550 bool Application::Pipe::read(libtest::vchar_t& arg)
551 {
552 fatal_assert(_std_fd == STDOUT_FILENO or _std_fd == STDERR_FILENO);
553
554 bool data_was_read= false;
555
556 libtest::vchar_t buffer;
557 buffer.resize(1024);
558 ssize_t read_length;
559 while ((read_length= ::read(_pipe_fd[READ], &buffer[0], buffer.size())))
560 {
561 if (read_length == -1)
562 {
563 switch(errno)
564 {
565 case EAGAIN:
566 break;
567
568 default:
569 Error << strerror(errno);
570 break;
571 }
572
573 break;
574 }
575
576 data_was_read= true;
577 arg.reserve(read_length +1);
578 for (size_t x= 0; x < size_t(read_length); ++x)
579 {
580 arg.push_back(buffer[x]);
581 }
582 // @todo Suck up all errput code here
583 }
584
585 return data_was_read;
586 }
587
588 void Application::Pipe::nonblock()
589 {
590 int flags;
591 do
592 {
593 flags= fcntl(_pipe_fd[READ], F_GETFL, 0);
594 } while (flags == -1 and (errno == EINTR or errno == EAGAIN));
595
596 if (flags == -1)
597 {
598 Error << "fcntl(F_GETFL) " << strerror(errno);
599 throw strerror(errno);
600 }
601
602 int rval;
603 do
604 {
605 rval= fcntl(_pipe_fd[READ], F_SETFL, flags | O_NONBLOCK);
606 } while (rval == -1 and (errno == EINTR or errno == EAGAIN));
607
608 if (rval == -1)
609 {
610 Error << "fcntl(F_SETFL) " << strerror(errno);
611 throw strerror(errno);
612 }
613 }
614
615 void Application::Pipe::reset()
616 {
617 close(READ);
618 close(WRITE);
619
620 #ifdef HAVE_PIPE2
621 if (pipe2(_pipe_fd, O_NONBLOCK|O_CLOEXEC) == -1)
622 #endif
623 {
624 if (pipe(_pipe_fd) == -1)
625 {
626 FATAL(strerror(errno));
627 }
628
629 // Since either pipe2() was not found/called we set the pipe directly
630 nonblock();
631 cloexec();
632 }
633 _open[0]= true;
634 _open[1]= true;
635 }
636
637 void Application::Pipe::cloexec()
638 {
639 //if (SOCK_CLOEXEC == 0)
640 {
641 if (FD_CLOEXEC)
642 {
643 int flags;
644 do
645 {
646 flags= fcntl(_pipe_fd[WRITE], F_GETFD, 0);
647 } while (flags == -1 and (errno == EINTR or errno == EAGAIN));
648
649 if (flags == -1)
650 {
651 Error << "fcntl(F_GETFD) " << strerror(errno);
652 throw strerror(errno);
653 }
654
655 int rval;
656 do
657 {
658 rval= fcntl(_pipe_fd[WRITE], F_SETFD, flags | FD_CLOEXEC);
659 } while (rval == -1 && (errno == EINTR or errno == EAGAIN));
660
661 if (rval == -1)
662 {
663 Error << "fcntl(F_SETFD) " << strerror(errno);
664 throw strerror(errno);
665 }
666 }
667 }
668 }
669
670 Application::Pipe::~Pipe()
671 {
672 if (_pipe_fd[0] != -1)
673 {
674 ::close(_pipe_fd[0]);
675 }
676
677 if (_pipe_fd[1] != -1)
678 {
679 ::close(_pipe_fd[1]);
680 }
681 }
682
683 void Application::Pipe::dup_for_spawn(posix_spawn_file_actions_t& file_actions)
684 {
685 int type= STDIN_FILENO == _std_fd ? 0 : 1;
686
687 int ret;
688 if ((ret= posix_spawn_file_actions_adddup2(&file_actions, _pipe_fd[type], _std_fd )) < 0)
689 {
690 FATAL("posix_spawn_file_actions_adddup2(%s)", strerror(ret));
691 }
692
693 if ((ret= posix_spawn_file_actions_addclose(&file_actions, _pipe_fd[type])) < 0)
694 {
695 FATAL("posix_spawn_file_actions_addclose(%s)", strerror(ret));
696 }
697 }
698
699 void Application::Pipe::close(const close_t& arg)
700 {
701 int type= int(arg);
702
703 if (_open[type])
704 {
705 if (::close(_pipe_fd[type]) == -1)
706 {
707 Error << "close(" << strerror(errno) << ")";
708 }
709 _open[type]= false;
710 _pipe_fd[type]= -1;
711 }
712 }
713
714 void Application::create_argv(const char *args[])
715 {
716 delete_argv();
717 if (_use_libtool)
718 {
719 assert(libtool());
720 vchar::append(built_argv, libtool());
721 vchar::append(built_argv, "--mode=execute");
722 }
723
724 if (_use_valgrind)
725 {
726 /*
727 valgrind --error-exitcode=1 --leak-check=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
728 */
729 vchar::append(built_argv, "valgrind");
730 vchar::append(built_argv, "--error-exitcode=1");
731 vchar::append(built_argv, "--leak-check=yes");
732 #if 0
733 vchar::append(built_argv, "--show-reachable=yes"));
734 #endif
735 vchar::append(built_argv, "--track-fds=yes");
736 #if 0
737 built_argv[x++]= strdup("--track-origin=yes");
738 #endif
739 vchar::append(built_argv, "--malloc-fill=A5");
740 vchar::append(built_argv, "--free-fill=DE");
741
742 std::string log_file= create_tmpfile("valgrind");
743 libtest::vchar_t buffer;
744 buffer.resize(1024);
745 int length= snprintf(&buffer[0], buffer.size(), "--log-file=%s", log_file.c_str());
746 fatal_assert(length > 0 and size_t(length) < buffer.size());
747 vchar::append(built_argv, &buffer[0]);
748 }
749 else if (_use_ptrcheck)
750 {
751 /*
752 valgrind --error-exitcode=1 --tool=exp-ptrcheck --log-file=
753 */
754 vchar::append(built_argv, "valgrind");
755 vchar::append(built_argv, "--error-exitcode=1");
756 vchar::append(built_argv, "--tool=exp-ptrcheck");
757 std::string log_file= create_tmpfile("ptrcheck");
758 libtest::vchar_t buffer;
759 buffer.resize(1024);
760 int length= snprintf(&buffer[0], buffer.size(), "--log-file=%s", log_file.c_str());
761 fatal_assert(length > 0 and size_t(length) < buffer.size());
762 vchar::append(built_argv, &buffer[0]);
763 }
764 else if (_use_gdb)
765 {
766 vchar::append(built_argv, "gdb");
767 }
768
769 vchar::append(built_argv, _exectuble_with_path.c_str());
770
771 for (Options::const_iterator iter= _options.begin(); iter != _options.end(); ++iter)
772 {
773 vchar::append(built_argv, (*iter).first.c_str());
774 if ((*iter).second.empty() == false)
775 {
776 vchar::append(built_argv, (*iter).second.c_str());
777 }
778 }
779
780 if (args)
781 {
782 for (const char **ptr= args; *ptr; ++ptr)
783 {
784 vchar::append(built_argv, *ptr);
785 }
786 }
787 built_argv.push_back(NULL);
788 }
789
790 std::string Application::print()
791 {
792 return print_argv(built_argv);
793 }
794
795 std::string Application::arguments()
796 {
797 std::stringstream arg_buffer;
798
799 // Skip printing out the libtool reference
800 for (size_t x= _use_libtool ? 2 : 0; x < _argc; ++x)
801 {
802 if (built_argv[x])
803 {
804 arg_buffer << built_argv[x] << " ";
805 }
806 }
807
808 return arg_buffer.str();
809 }
810
811 void Application::delete_argv()
812 {
813 std::for_each(built_argv.begin(), built_argv.end(), FreeFromVector());
814
815 built_argv.clear();
816 _argc= 0;
817 }
818
819
820 int exec_cmdline(const std::string& command, const char *args[], bool use_libtool)
821 {
822 Application app(command, use_libtool);
823
824 Application::error_t ret= app.run(args);
825
826 if (ret != Application::SUCCESS)
827 {
828 return int(ret);
829 }
830
831 return int(app.join());
832 }
833
834 } // namespace exec_cmdline