posix.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. #include <boost/process.hpp>
  10. #include <boost/process/posix.hpp>
  11. #include <boost/process/extend.hpp>
  12. #include <iostream>
  13. #include <fstream>
  14. #include <unistd.h>
  15. #include <errno.h>
  16. namespace bp = boost::process;
  17. int main()
  18. {
  19. //duplicate our pipe descriptor into literal position 4
  20. bp::pipe p;
  21. bp::system("test", bp::posix::fd.bind(4, p.native_sink()));
  22. //close file-descriptor from explicit integral value
  23. bp::system("test", bp::posix::fd.close(STDIN_FILENO));
  24. //close file-descriptors from explicit integral values
  25. bp::system("test", bp::posix::fd.close({STDIN_FILENO, STDOUT_FILENO}));
  26. //add custom handlers
  27. const char *env[2] = { 0 };
  28. env[0] = "LANG=de";
  29. bp::system("test",
  30. bp::extend::on_setup([env](auto &e) { e.env = const_cast<char**>(env); }),
  31. bp::extend::on_fork_error([](auto&, const std::error_code & ec)
  32. { std::cerr << errno << std::endl; }),
  33. bp::extend::on_exec_setup([](auto&)
  34. { ::chroot("/new/root/directory/"); }),
  35. bp::extend::on_exec_error([](auto&, const std::error_code & ec)
  36. { std::ofstream ofs("log.txt"); if (ofs) ofs << errno; })
  37. );
  38. }