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