Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inherit certain fd in boost.process while close all the other fd

I am using boost.process, trying to spawn a child process and do some communication btw parent and child process. I create unnamed socket pair and pass one end to the child process. I still want to use limit_handles to close all other fd, but also preserve one end of this socket pair. How to achieve this in boost.process? I did not find any example on how to achieve this.

like image 382
Howard Yu Avatar asked Dec 06 '25 16:12

Howard Yu


1 Answers

I think I knew how to do this. Basically I need to create a new initializer. Like this

struct PreservedFds : boost::process::detail::handler,
                      boost::process::detail::uses_handles {
  std::vector<int> fds;
  PreservedFds(std::vector<int> pfds) :
    fds(pfds)
  {}

  std::vector<int>& get_used_handles() {
    return fds;
  }
};

Then I can initialize my child process with following:

std::vector<int> pfds{5, 7, 9};
boost::process::child c("/usr/bin/ls", "/home", PreservedFds(pfds), 
                         boost::process::limit_handles);
like image 155
Howard Yu Avatar answered Dec 08 '25 07:12

Howard Yu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!