set_on_error.hpp
Go to the documentation of this file.
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 
10 #pragma once
11 
12 #include <boost/process/config.hpp>
14 #include <boost/system/error_code.hpp>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <errno.h>
18 
20 {
21 
23  {
24  public:
25  explicit set_on_error(boost::system::error_code& ec) : ec_(ec) {}
26 
27  template <class PosixExecutor>
28  void on_fork_setup(PosixExecutor&) const
29  {
30  if (::pipe(fds_) == -1)
31  {
32  BOOST_PROCESS_RETURN_LAST_SYSTEM_ERROR(ec_);
33  }
34  if (::fcntl(fds_[1], F_SETFD, FD_CLOEXEC) == -1)
35  {
36  BOOST_PROCESS_RETURN_LAST_SYSTEM_ERROR(ec_);
37  ::close(fds_[0]);
38  ::close(fds_[1]);
39  }
40  }
41 
42  template <class PosixExecutor>
43  void on_fork_error(PosixExecutor&) const
44  {
45  if (!ec_)
46  {
47  BOOST_PROCESS_RETURN_LAST_SYSTEM_ERROR(ec_);
48  ::close(fds_[0]);
49  ::close(fds_[1]);
50  }
51  }
52 
53  template <class PosixExecutor>
54  void on_fork_success(PosixExecutor&) const
55  {
56  if (!ec_)
57  {
58  ::close(fds_[1]);
59  int code;
60  if (::read(fds_[0], &code, sizeof(int)) > 0)
61  {
62  ec_ = boost::system::error_code(code,
63  boost::system::system_category());
64  }
65  ::close(fds_[0]);
66  }
67  }
68 
69  template <class PosixExecutor>
70  void on_exec_setup(PosixExecutor&) const
71  {
72  if (!ec_)
73  {
74  ::close(fds_[0]);
75  }
76  }
77 
78  template <class PosixExecutor>
79  void on_exec_error(PosixExecutor&) const
80  {
81  if (!ec_)
82  {
83  int e = errno;
84  while (::write(fds_[1], &e, sizeof(int)) == -1 && errno == EINTR)
85  ;
86  ::close(fds_[1]);
87  }
88  }
89 
90  private:
91  boost::system::error_code& ec_;
92  mutable int fds_[2];
93  };
94 
95 }
config.hpp
boost::process::posix::initializers::set_on_error::on_fork_error
void on_fork_error(PosixExecutor &) const
Definition: set_on_error.hpp:43
initializer_base.hpp
boost::process::posix::initializers::set_on_error::on_exec_error
void on_exec_error(PosixExecutor &) const
Definition: set_on_error.hpp:79
boost::process::posix::initializers::set_on_error::on_fork_success
void on_fork_success(PosixExecutor &) const
Definition: set_on_error.hpp:54
boost::process::posix::pipe
Definition: pipe.hpp:15
boost::process::posix::initializers::set_on_error::set_on_error
set_on_error(boost::system::error_code &ec)
Definition: set_on_error.hpp:25
boost::process::posix::initializers
Definition: bind_fd.hpp:15
boost::process::posix::initializers::set_on_error::on_fork_setup
void on_fork_setup(PosixExecutor &) const
Definition: set_on_error.hpp:28
armarx::read
void read(auto &eigen, auto *table)
Definition: FTSensorCalibrationGuiWidgetController.cpp:462
boost::process::posix::initializers::initializer_base
Definition: initializer_base.hpp:15
boost::process::posix::initializers::set_on_error::on_exec_setup
void on_exec_setup(PosixExecutor &) const
Definition: set_on_error.hpp:70
armarx::aron::write
requires data::isWriter< WriterT > void write(WriterT &aron_w, const Eigen::Matrix< EigenT, rows, cols, options > &input, typename WriterT::ReturnType &ret, const armarx::aron::Path &aron_p=armarx::aron::Path())
Definition: eigen.h:134
boost::process::posix::initializers::set_on_error
Definition: set_on_error.hpp:22