vfork.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) 2006, 2007 Julio M. Merino Vidal
  2. // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
  3. // Copyright (c) 2009 Boris Schaeling
  4. // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
  5. // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #define BOOST_TEST_MAIN
  10. #define BOOST_TEST_IGNORE_SIGCHLD
  11. #include <boost/test/included/unit_test.hpp>
  12. #include <boost/process.hpp>
  13. #include <boost/process/posix.hpp>
  14. #include <system_error>
  15. #include <string>
  16. #include <sys/wait.h>
  17. #include <errno.h>
  18. namespace bp = boost::process;
  19. #if defined(BOOST_POSIX_HAS_VFORK)
  20. BOOST_AUTO_TEST_CASE(bind_fd, *boost::unit_test::timeout(2))
  21. {
  22. using boost::unit_test::framework::master_test_suite;
  23. bp::pipe p;
  24. std::error_code ec;
  25. bp::child c(
  26. master_test_suite().argv[1],
  27. "test", "--posix-echo-one", "3", "hello",
  28. bp::posix::fd.bind(3, p.native_sink()),
  29. bp::posix::use_vfork,
  30. ec
  31. );
  32. BOOST_CHECK(!ec);
  33. bp::ipstream is(std::move(p));
  34. std::string s;
  35. is >> s;
  36. BOOST_CHECK_EQUAL(s, "hello");
  37. }
  38. BOOST_AUTO_TEST_CASE(execve_set_on_error, *boost::unit_test::timeout(2))
  39. {
  40. std::error_code ec;
  41. bp::spawn(
  42. "doesnt-exist",
  43. bp::posix::use_vfork,
  44. ec
  45. );
  46. BOOST_CHECK(ec);
  47. BOOST_CHECK_EQUAL(ec.value(), ENOENT);
  48. }
  49. BOOST_AUTO_TEST_CASE(execve_throw_on_error, *boost::unit_test::timeout(2))
  50. {
  51. try
  52. {
  53. bp::spawn("doesnt-exist", bp::posix::use_vfork);
  54. BOOST_CHECK(false);
  55. }
  56. catch (std::system_error &e)
  57. {
  58. BOOST_CHECK(e.code());
  59. BOOST_CHECK_EQUAL(e.code().value(), ENOENT);
  60. }
  61. }
  62. #else
  63. BOOST_AUTO_TEST_CASE(dummy) {}
  64. #endif