throw_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 <errno.h>
13
14#include <fcntl.h>
15#include <unistd.h>
16
19#include <boost/system/error_code.hpp>
20#include <boost/system/system_error.hpp>
21
23{
24
26 {
27 public:
28 template <class PosixExecutor>
29 void
30 on_fork_setup(PosixExecutor&) const
31 {
32 if (::pipe(fds_) == -1)
33 {
34 BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("pipe(2) failed");
35 }
36 if (::fcntl(fds_[1], F_SETFD, FD_CLOEXEC) == -1)
37 {
38 int e = errno;
39 ::close(fds_[0]);
40 ::close(fds_[1]);
41 BOOST_PROCESS_THROW(boost::system::system_error(
42 boost::system::error_code(e, boost::system::system_category()),
43 BOOST_PROCESS_SOURCE_LOCATION "fcntl(2) failed"));
44 }
45 }
46
47 template <class PosixExecutor>
48 void
49 on_fork_error(PosixExecutor&) const
50 {
51 int e = errno;
52 ::close(fds_[0]);
53 ::close(fds_[1]);
54 BOOST_PROCESS_THROW(boost::system::system_error(
55 boost::system::error_code(e, boost::system::system_category()),
56 BOOST_PROCESS_SOURCE_LOCATION "fork(2) failed"));
57 }
58
59 template <class PosixExecutor>
60 void
61 on_fork_success(PosixExecutor&) const
62 {
63 ::close(fds_[1]);
64 int code;
65 if (::read(fds_[0], &code, sizeof(int)) > 0)
66 {
67 ::close(fds_[0]);
68 BOOST_PROCESS_THROW(boost::system::system_error(
69 boost::system::error_code(code, boost::system::system_category()),
70 BOOST_PROCESS_SOURCE_LOCATION "execve(2) failed"));
71 }
72 ::close(fds_[0]);
73 }
74
75 template <class PosixExecutor>
76 void
77 on_exec_setup(PosixExecutor&) const
78 {
79 ::close(fds_[0]);
80 }
81
82 template <class PosixExecutor>
83 void
84 on_exec_error(PosixExecutor&) const
85 {
86 int e = errno;
87 while (::write(fds_[1], &e, sizeof(int)) == -1 && errno == EINTR)
88 ;
89 ::close(fds_[1]);
90 }
91
92 private:
93 mutable int fds_[2];
94 };
95
96} // namespace boost::process::posix::initializers
Defines various macros.
#define BOOST_PROCESS_THROW(EX)
Defines how exceptions are thrown.
Definition config.hpp:69