Howard  Yu
Howard Yu

Reputation: 311

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.

Upvotes: 3

Views: 428

Answers (1)

Howard  Yu
Howard Yu

Reputation: 311

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);

Upvotes: 4

Related Questions