• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2019 Klemens D. Morgenstern
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef BOOST_PROCESS_HANDLES_HPP_
7 #define BOOST_PROCESS_HANDLES_HPP_
8 
9 /**
10  * \file boost/process/handles.hpp
11  *
12  * Defines functions to obtain handles of the current process and limit the amount for inherited ones.
13  */
14 
15 #include <boost/process/detail/config.hpp>
16 
17 #if defined(BOOST_POSIX_API)
18 #include <boost/process/detail/posix/handles.hpp>
19 #elif defined(BOOST_WINDOWS_API)
20 #include <boost/process/detail/windows/handles.hpp>
21 #endif
22 
23 #include <boost/process/detail/used_handles.hpp>
24 
25 
26 namespace boost { namespace this_process
27 {
28 
29 ///The native type for handles
30 using native_handle_type = ::boost::process::detail::api::native_handle_type;
31 
32 /**
33  * Get a snapshot of all handles of the process (i.e. file descriptors on posix and handles on windows) of the current process.
34  *
35  * \note This function might not work on certain posix systems.
36  *
37  * \note On Windows version older than windows 8 this function will iterate all the system handles, meaning it might be quite slow.
38  *
39  * \warning This functionality is utterly prone to race conditions, since other threads might open or close handles.
40  *
41  * \return The list of all open handles of the current process
42  */
get_handles()43 inline std::vector<native_handle_type> get_handles()
44 {
45     return ::boost::process::detail::api::get_handles();
46 }
47 
48 
49 /** \overload std::vector<native_handle_type> get_handles() */
get_handles(std::error_code & ec)50 inline std::vector<native_handle_type> get_handles(std::error_code &ec)
51 {
52     return ::boost::process::detail::api::get_handles(ec);
53 }
54 
55 /** Determines if a given handle is a a stream-handle, i.e. any handle that can be used with read and write functions.
56  * Stream handles include pipes, regular files and sockets.
57  *
58  * \return Indicates if it's a stream handle.
59  */
is_stream_handle(native_handle_type handle)60 inline bool is_stream_handle(native_handle_type handle)
61 {
62     return ::boost::process::detail::api::is_stream_handle(handle);
63 }
64 
65 
66 /** \overload bool is_stream_handle(native_handle_type handle) */
is_stream_handle(native_handle_type handle,std::error_code & ec)67 inline bool is_stream_handle(native_handle_type handle, std::error_code &ec)
68 {
69     return ::boost::process::detail::api::is_stream_handle(handle, ec);
70 }
71 
72 }
73 namespace process
74 {
75 
76 namespace detail
77 {
78 
79 using limit_handles_ = ::boost::process::detail::api::limit_handles_;
80 
81 
82 }
83 
84 /**
85  * The limit_handles property sets all properties to be inherited only expcitly. It closes all unused file-descriptors on posix after the fork and
86  * removes the inherit flags on windows.
87  *
88  * \note This is executed after the fork on posix.
89  *
90  * \code{.cpp}
91  * system("gcc", limit_handles);
92  * \endcode
93  *
94  * Since limit also closes the standard handles unless they are explicitly redirected they can be ignored by `limit_handles` in the following way.
95  *
96  * \code{.cpp}
97  * system("gcc", limit_handles.allowStd())
98  * \endcode
99  *
100 */
101 const static ::boost::process::detail::api::limit_handles_ limit_handles;
102 
103 
104 }
105 }
106 
107 #endif //BOOST_PROCESS_HANDLES_HPP_
108