File indexing completed on 2025-10-31 08:54:39
0001 
0002 
0003 
0004 
0005 #ifndef BOOST_PROCESS_V2_POSIX_BIND_FD_HPP
0006 #define BOOST_PROCESS_V2_POSIX_BIND_FD_HPP
0007 
0008 #include <boost/process/v2/detail/config.hpp>
0009 #include <boost/process/v2/default_launcher.hpp>
0010 
0011 #include <fcntl.h>
0012 
0013 BOOST_PROCESS_V2_BEGIN_NAMESPACE
0014 
0015 namespace posix
0016 {
0017 
0018 
0019 struct bind_fd
0020 {
0021     int target;
0022     int fd;
0023     bool fd_needs_closing{false};
0024 
0025     ~bind_fd()
0026     {
0027         if (fd_needs_closing)
0028             ::close(fd);
0029     }
0030 
0031     bind_fd() = delete;
0032     
0033     
0034 
0035 
0036 
0037 
0038 
0039     bind_fd(int target) : target(target), fd(target) {}
0040 
0041     
0042     
0043 
0044 
0045 
0046 
0047 
0048 
0049 
0050     template<typename Stream>
0051     bind_fd(int target, Stream && str, decltype(std::declval<Stream>().native_handle()) = -1)
0052         : bind_fd(target, str.native_handle())
0053     {}
0054 
0055     
0056     
0057 
0058 
0059 
0060 
0061 
0062     bind_fd(int target, FILE * f) : bind_fd(target, fileno(f)) {}
0063     
0064     
0065     
0066 
0067 
0068 
0069 
0070 
0071     bind_fd(int target, int fd) : target(target), fd(fd) {}
0072     
0073     
0074     
0075 
0076 
0077 
0078 
0079 
0080     bind_fd(int target, std::nullptr_t) : bind_fd(target, filesystem::path("/dev/null")) {}
0081 
0082     
0083     
0084 
0085 
0086 
0087 
0088 
0089     bind_fd(int target, const filesystem::path & pth, int flags = O_RDWR | O_CREAT)
0090             : target(target), fd(::open(pth.c_str(), flags, 0660)), fd_needs_closing(true)
0091     {
0092     }
0093 
0094     error_code on_setup(posix::default_launcher & launcher, const filesystem::path &, const char * const *)
0095     {
0096         launcher.fd_whitelist.push_back(target);
0097         return {};
0098     }
0099 
0100     
0101     error_code on_exec_setup(posix::default_launcher & , const filesystem::path &, const char * const *)
0102     {
0103         if (::dup2(fd, target) == -1)
0104             return error_code(errno, system_category());
0105         return error_code ();
0106     }
0107 };
0108 
0109 }
0110 
0111 BOOST_PROCESS_V2_END_NAMESPACE
0112 
0113 #endif