Add process group leader option
[folly.git] / folly / Subprocess.cpp
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef _GNU_SOURCE
18 #define _GNU_SOURCE
19 #endif
20
21 #include <folly/Subprocess.h>
22
23 #if __linux__
24 #include <sys/prctl.h>
25 #endif
26 #include <fcntl.h>
27 #include <poll.h>
28
29 #include <unistd.h>
30
31 #include <array>
32 #include <algorithm>
33 #include <system_error>
34
35 #include <boost/container/flat_set.hpp>
36 #include <boost/range/adaptors.hpp>
37
38 #include <glog/logging.h>
39
40 #include <folly/Conv.h>
41 #include <folly/Exception.h>
42 #include <folly/ScopeGuard.h>
43 #include <folly/String.h>
44 #include <folly/io/Cursor.h>
45
46 extern char** environ;
47
48 constexpr int kExecFailure = 127;
49 constexpr int kChildFailure = 126;
50
51 namespace folly {
52
53 ProcessReturnCode::State ProcessReturnCode::state() const {
54   if (rawStatus_ == RV_NOT_STARTED) return NOT_STARTED;
55   if (rawStatus_ == RV_RUNNING) return RUNNING;
56   if (WIFEXITED(rawStatus_)) return EXITED;
57   if (WIFSIGNALED(rawStatus_)) return KILLED;
58   throw std::runtime_error(to<std::string>(
59       "Invalid ProcessReturnCode: ", rawStatus_));
60 }
61
62 void ProcessReturnCode::enforce(State expected) const {
63   State s = state();
64   if (s != expected) {
65     throw std::logic_error(to<std::string>(
66       "Bad use of ProcessReturnCode; state is ", s, " expected ", expected
67     ));
68   }
69 }
70
71 int ProcessReturnCode::exitStatus() const {
72   enforce(EXITED);
73   return WEXITSTATUS(rawStatus_);
74 }
75
76 int ProcessReturnCode::killSignal() const {
77   enforce(KILLED);
78   return WTERMSIG(rawStatus_);
79 }
80
81 bool ProcessReturnCode::coreDumped() const {
82   enforce(KILLED);
83   return WCOREDUMP(rawStatus_);
84 }
85
86 std::string ProcessReturnCode::str() const {
87   switch (state()) {
88   case NOT_STARTED:
89     return "not started";
90   case RUNNING:
91     return "running";
92   case EXITED:
93     return to<std::string>("exited with status ", exitStatus());
94   case KILLED:
95     return to<std::string>("killed by signal ", killSignal(),
96                            (coreDumped() ? " (core dumped)" : ""));
97   }
98   CHECK(false);  // unreached
99 }
100
101 CalledProcessError::CalledProcessError(ProcessReturnCode rc)
102   : returnCode_(rc),
103     what_(returnCode_.str()) {
104 }
105
106 SubprocessSpawnError::SubprocessSpawnError(const char* executable,
107                                            int errCode,
108                                            int errnoValue)
109   : errnoValue_(errnoValue),
110     what_(to<std::string>(errCode == kExecFailure ?
111                             "failed to execute " :
112                             "error preparing to execute ",
113                           executable, ": ", errnoStr(errnoValue))) {
114 }
115
116 namespace {
117
118 // Copy pointers to the given strings in a format suitable for posix_spawn
119 std::unique_ptr<const char*[]> cloneStrings(const std::vector<std::string>& s) {
120   std::unique_ptr<const char*[]> d(new const char*[s.size() + 1]);
121   for (size_t i = 0; i < s.size(); i++) {
122     d[i] = s[i].c_str();
123   }
124   d[s.size()] = nullptr;
125   return d;
126 }
127
128 // Check a wait() status, throw on non-successful
129 void checkStatus(ProcessReturnCode returnCode) {
130   if (returnCode.state() != ProcessReturnCode::EXITED ||
131       returnCode.exitStatus() != 0) {
132     throw CalledProcessError(returnCode);
133   }
134 }
135
136 }  // namespace
137
138 Subprocess::Options& Subprocess::Options::fd(int fd, int action) {
139   if (action == Subprocess::PIPE) {
140     if (fd == 0) {
141       action = Subprocess::PIPE_IN;
142     } else if (fd == 1 || fd == 2) {
143       action = Subprocess::PIPE_OUT;
144     } else {
145       throw std::invalid_argument(
146           to<std::string>("Only fds 0, 1, 2 are valid for action=PIPE: ", fd));
147     }
148   }
149   fdActions_[fd] = action;
150   return *this;
151 }
152
153 Subprocess::Subprocess(
154     const std::vector<std::string>& argv,
155     const Options& options,
156     const char* executable,
157     const std::vector<std::string>* env)
158   : pid_(-1),
159     returnCode_(RV_NOT_STARTED) {
160   if (argv.empty()) {
161     throw std::invalid_argument("argv must not be empty");
162   }
163   if (!executable) executable = argv[0].c_str();
164   spawn(cloneStrings(argv), executable, options, env);
165 }
166
167 Subprocess::Subprocess(
168     const std::string& cmd,
169     const Options& options,
170     const std::vector<std::string>* env)
171   : pid_(-1),
172     returnCode_(RV_NOT_STARTED) {
173   if (options.usePath_) {
174     throw std::invalid_argument("usePath() not allowed when running in shell");
175   }
176   const char* shell = getenv("SHELL");
177   if (!shell) {
178     shell = "/bin/sh";
179   }
180
181   std::unique_ptr<const char*[]> argv(new const char*[4]);
182   argv[0] = shell;
183   argv[1] = "-c";
184   argv[2] = cmd.c_str();
185   argv[3] = nullptr;
186   spawn(std::move(argv), shell, options, env);
187 }
188
189 Subprocess::~Subprocess() {
190   CHECK_NE(returnCode_.state(), ProcessReturnCode::RUNNING)
191     << "Subprocess destroyed without reaping child";
192   closeAll();
193 }
194
195 namespace {
196 void closeChecked(int fd) {
197   checkUnixError(::close(fd), "close");
198 }
199
200 struct ChildErrorInfo {
201   int errCode;
202   int errnoValue;
203 };
204
205 FOLLY_NORETURN void childError(int errFd, int errCode, int errnoValue);
206 void childError(int errFd, int errCode, int errnoValue) {
207   ChildErrorInfo info = {errCode, errnoValue};
208   // Write the error information over the pipe to our parent process.
209   // We can't really do anything else if this write call fails.
210   writeNoInt(errFd, &info, sizeof(info));
211   // exit
212   _exit(errCode);
213 }
214
215 }  // namespace
216
217 void Subprocess::closeAll() {
218   for (auto& p : pipes_) {
219     closeChecked(p.parentFd);
220   }
221   pipes_.clear();
222 }
223
224 void Subprocess::setAllNonBlocking() {
225   for (auto& p : pipes_) {
226     int fd = p.parentFd;
227     int flags = ::fcntl(fd, F_GETFL);
228     checkUnixError(flags, "fcntl");
229     int r = ::fcntl(fd, F_SETFL, flags | O_NONBLOCK);
230     checkUnixError(r, "fcntl");
231   }
232 }
233
234 void Subprocess::spawn(
235     std::unique_ptr<const char*[]> argv,
236     const char* executable,
237     const Options& optionsIn,
238     const std::vector<std::string>* env) {
239   if (optionsIn.usePath_ && env) {
240     throw std::invalid_argument(
241         "usePath() not allowed when overriding environment");
242   }
243
244   // Make a copy, we'll mutate options
245   Options options(optionsIn);
246
247   // On error, close all of the pipes_
248   auto pipesGuard = makeGuard([&] {
249     for (auto& p : this->pipes_) {
250       CHECK_ERR(::close(p.parentFd));
251     }
252   });
253
254   // Create a pipe to use to receive error information from the child,
255   // in case it fails before calling exec()
256   int errFds[2];
257 #if FOLLY_HAVE_PIPE2
258   checkUnixError(::pipe2(errFds, O_CLOEXEC), "pipe2");
259 #else
260   checkUnixError(::pipe(errFds), "pipe");
261 #endif
262   SCOPE_EXIT {
263     CHECK_ERR(::close(errFds[0]));
264     if (errFds[1] >= 0) {
265       CHECK_ERR(::close(errFds[1]));
266     }
267   };
268
269 #if !FOLLY_HAVE_PIPE2
270   // Ask the child to close the read end of the error pipe.
271   checkUnixError(fcntl(errFds[0], F_SETFD, FD_CLOEXEC), "set FD_CLOEXEC");
272   // Set the close-on-exec flag on the write side of the pipe.
273   // This way the pipe will be closed automatically in the child if execve()
274   // succeeds.  If the exec fails the child can write error information to the
275   // pipe.
276   checkUnixError(fcntl(errFds[1], F_SETFD, FD_CLOEXEC), "set FD_CLOEXEC");
277 #endif
278
279   // Perform the actual work of setting up pipes then forking and
280   // executing the child.
281   spawnInternal(std::move(argv), executable, options, env, errFds[1]);
282
283   // After spawnInternal() returns the child is alive.  We have to be very
284   // careful about throwing after this point.  We are inside the constructor,
285   // so if we throw the Subprocess object will have never existed, and the
286   // destructor will never be called.
287   //
288   // We should only throw if we got an error via the errFd, and we know the
289   // child has exited and can be immediately waited for.  In all other cases,
290   // we have no way of cleaning up the child.
291
292   if (options.processGroupLeader_) {
293     // This is done both in the parent and the child to avoid the race where
294     // the parent assumes that the child is a leader, but the child has not
295     // yet run setprp().  Not checking error codes since we're deliberately
296     // racing the child, which may already have run execve(), and expect to
297     // lose frequently.
298     setpgid(pid_, pid_);
299   }
300   // Close writable side of the errFd pipe in the parent process
301   CHECK_ERR(::close(errFds[1]));
302   errFds[1] = -1;
303
304   // Read from the errFd pipe, to tell if the child ran into any errors before
305   // calling exec()
306   readChildErrorPipe(errFds[0], executable);
307
308   // We have fully succeeded now, so release the guard on pipes_
309   pipesGuard.dismiss();
310 }
311
312 void Subprocess::spawnInternal(
313     std::unique_ptr<const char*[]> argv,
314     const char* executable,
315     Options& options,
316     const std::vector<std::string>* env,
317     int errFd) {
318   // Parent work, pre-fork: create pipes
319   std::vector<int> childFds;
320   // Close all of the childFds as we leave this scope
321   SCOPE_EXIT {
322     // These are only pipes, closing them shouldn't fail
323     for (int cfd : childFds) {
324       CHECK_ERR(::close(cfd));
325     }
326   };
327
328   int r;
329   for (auto& p : options.fdActions_) {
330     if (p.second == PIPE_IN || p.second == PIPE_OUT) {
331       int fds[2];
332       // We're setting both ends of the pipe as close-on-exec. The child
333       // doesn't need to reset the flag on its end, as we always dup2() the fd,
334       // and dup2() fds don't share the close-on-exec flag.
335 #if FOLLY_HAVE_PIPE2
336       r = ::pipe2(fds, O_CLOEXEC);
337       checkUnixError(r, "pipe2");
338 #else
339       r = ::pipe(fds);
340       checkUnixError(r, "pipe");
341       r = fcntl(fds[0], F_SETFD, FD_CLOEXEC);
342       checkUnixError(r, "set FD_CLOEXEC");
343       r = fcntl(fds[1], F_SETFD, FD_CLOEXEC);
344       checkUnixError(r, "set FD_CLOEXEC");
345 #endif
346       PipeInfo pinfo;
347       pinfo.direction = p.second;
348       int cfd;
349       if (p.second == PIPE_IN) {
350         // Child gets reading end
351         pinfo.parentFd = fds[1];
352         cfd = fds[0];
353       } else {
354         pinfo.parentFd = fds[0];
355         cfd = fds[1];
356       }
357       p.second = cfd;  // ensure it gets dup2()ed
358       pinfo.childFd = p.first;
359       childFds.push_back(cfd);
360       pipes_.push_back(pinfo);
361     }
362   }
363
364   // This should already be sorted, as options.fdActions_ is
365   DCHECK(std::is_sorted(pipes_.begin(), pipes_.end()));
366
367   // Note that the const casts below are legit, per
368   // http://pubs.opengroup.org/onlinepubs/009695399/functions/exec.html
369
370   char** argVec = const_cast<char**>(argv.get());
371
372   // Set up environment
373   std::unique_ptr<const char*[]> envHolder;
374   char** envVec;
375   if (env) {
376     envHolder = cloneStrings(*env);
377     envVec = const_cast<char**>(envHolder.get());
378   } else {
379     envVec = environ;
380   }
381
382   // Block all signals around vfork; see http://ewontfix.com/7/.
383   //
384   // As the child may run in the same address space as the parent until
385   // the actual execve() system call, any (custom) signal handlers that
386   // the parent has might alter parent's memory if invoked in the child,
387   // with undefined results.  So we block all signals in the parent before
388   // vfork(), which will cause them to be blocked in the child as well (we
389   // rely on the fact that Linux, just like all sane implementations, only
390   // clones the calling thread).  Then, in the child, we reset all signals
391   // to their default dispositions (while still blocked), and unblock them
392   // (so the exec()ed process inherits the parent's signal mask)
393   //
394   // The parent also unblocks all signals as soon as vfork() returns.
395   sigset_t allBlocked;
396   r = sigfillset(&allBlocked);
397   checkUnixError(r, "sigfillset");
398   sigset_t oldSignals;
399
400   r = pthread_sigmask(SIG_SETMASK, &allBlocked, &oldSignals);
401   checkPosixError(r, "pthread_sigmask");
402   SCOPE_EXIT {
403     // Restore signal mask
404     r = pthread_sigmask(SIG_SETMASK, &oldSignals, nullptr);
405     CHECK_EQ(r, 0) << "pthread_sigmask: " << errnoStr(r);  // shouldn't fail
406   };
407
408   // Call c_str() here, as it's not necessarily safe after fork.
409   const char* childDir =
410     options.childDir_.empty() ? nullptr : options.childDir_.c_str();
411   pid_t pid = vfork();
412   if (pid == 0) {
413     int errnoValue = prepareChild(options, &oldSignals, childDir);
414     if (errnoValue != 0) {
415       childError(errFd, kChildFailure, errnoValue);
416     }
417
418     errnoValue = runChild(executable, argVec, envVec, options);
419     // If we get here, exec() failed.
420     childError(errFd, kExecFailure, errnoValue);
421   }
422   // In parent.  Make sure vfork() succeeded.
423   checkUnixError(pid, errno, "vfork");
424
425   // Child is alive.  We have to be very careful about throwing after this
426   // point.  We are inside the constructor, so if we throw the Subprocess
427   // object will have never existed, and the destructor will never be called.
428   //
429   // We should only throw if we got an error via the errFd, and we know the
430   // child has exited and can be immediately waited for.  In all other cases,
431   // we have no way of cleaning up the child.
432   pid_ = pid;
433   returnCode_ = ProcessReturnCode(RV_RUNNING);
434 }
435
436 int Subprocess::prepareChild(const Options& options,
437                              const sigset_t* sigmask,
438                              const char* childDir) const {
439   // While all signals are blocked, we must reset their
440   // dispositions to default.
441   for (int sig = 1; sig < NSIG; ++sig) {
442     ::signal(sig, SIG_DFL);
443   }
444
445   {
446     // Unblock signals; restore signal mask.
447     int r = pthread_sigmask(SIG_SETMASK, sigmask, nullptr);
448     if (r != 0) {
449       return r;  // pthread_sigmask() returns an errno value
450     }
451   }
452
453   // Change the working directory, if one is given
454   if (childDir) {
455     if (::chdir(childDir) == -1) {
456       return errno;
457     }
458   }
459
460   // We don't have to explicitly close the parent's end of all pipes,
461   // as they all have the FD_CLOEXEC flag set and will be closed at
462   // exec time.
463
464   // Close all fds that we're supposed to close.
465   for (auto& p : options.fdActions_) {
466     if (p.second == CLOSE) {
467       if (::close(p.first) == -1) {
468         return errno;
469       }
470     } else if (p.second != p.first) {
471       if (::dup2(p.second, p.first) == -1) {
472         return errno;
473       }
474     }
475   }
476
477   // If requested, close all other file descriptors.  Don't close
478   // any fds in options.fdActions_, and don't touch stdin, stdout, stderr.
479   // Ignore errors.
480   if (options.closeOtherFds_) {
481     for (int fd = getdtablesize() - 1; fd >= 3; --fd) {
482       if (options.fdActions_.count(fd) == 0) {
483         ::close(fd);
484       }
485     }
486   }
487
488 #if __linux__
489   // Opt to receive signal on parent death, if requested
490   if (options.parentDeathSignal_ != 0) {
491     if (prctl(PR_SET_PDEATHSIG, options.parentDeathSignal_, 0, 0, 0) == -1) {
492       return errno;
493     }
494   }
495 #endif
496
497   if (options.processGroupLeader_) {
498     if (setpgrp() == -1) {
499       return errno;
500     }
501   }
502
503   return 0;
504 }
505
506 int Subprocess::runChild(const char* executable,
507                          char** argv, char** env,
508                          const Options& options) const {
509   // Now, finally, exec.
510   if (options.usePath_) {
511     ::execvp(executable, argv);
512   } else {
513     ::execve(executable, argv, env);
514   }
515   return errno;
516 }
517
518 void Subprocess::readChildErrorPipe(int pfd, const char* executable) {
519   ChildErrorInfo info;
520   auto rc = readNoInt(pfd, &info, sizeof(info));
521   if (rc == 0) {
522     // No data means the child executed successfully, and the pipe
523     // was closed due to the close-on-exec flag being set.
524     return;
525   } else if (rc != sizeof(ChildErrorInfo)) {
526     // An error occurred trying to read from the pipe, or we got a partial read.
527     // Neither of these cases should really occur in practice.
528     //
529     // We can't get any error data from the child in this case, and we don't
530     // know if it is successfully running or not.  All we can do is to return
531     // normally, as if the child executed successfully.  If something bad
532     // happened the caller should at least get a non-normal exit status from
533     // the child.
534     LOG(ERROR) << "unexpected error trying to read from child error pipe " <<
535       "rc=" << rc << ", errno=" << errno;
536     return;
537   }
538
539   // We got error data from the child.  The child should exit immediately in
540   // this case, so wait on it to clean up.
541   wait();
542
543   // Throw to signal the error
544   throw SubprocessSpawnError(executable, info.errCode, info.errnoValue);
545 }
546
547 ProcessReturnCode Subprocess::poll() {
548   returnCode_.enforce(ProcessReturnCode::RUNNING);
549   DCHECK_GT(pid_, 0);
550   int status;
551   pid_t found = ::waitpid(pid_, &status, WNOHANG);
552   checkUnixError(found, "waitpid");
553   if (found != 0) {
554     returnCode_ = ProcessReturnCode(status);
555     pid_ = -1;
556   }
557   return returnCode_;
558 }
559
560 bool Subprocess::pollChecked() {
561   if (poll().state() == ProcessReturnCode::RUNNING) {
562     return false;
563   }
564   checkStatus(returnCode_);
565   return true;
566 }
567
568 ProcessReturnCode Subprocess::wait() {
569   returnCode_.enforce(ProcessReturnCode::RUNNING);
570   DCHECK_GT(pid_, 0);
571   int status;
572   pid_t found;
573   do {
574     found = ::waitpid(pid_, &status, 0);
575   } while (found == -1 && errno == EINTR);
576   checkUnixError(found, "waitpid");
577   DCHECK_EQ(found, pid_);
578   returnCode_ = ProcessReturnCode(status);
579   pid_ = -1;
580   return returnCode_;
581 }
582
583 void Subprocess::waitChecked() {
584   wait();
585   checkStatus(returnCode_);
586 }
587
588 void Subprocess::sendSignal(int signal) {
589   returnCode_.enforce(ProcessReturnCode::RUNNING);
590   int r = ::kill(pid_, signal);
591   checkUnixError(r, "kill");
592 }
593
594 pid_t Subprocess::pid() const {
595   return pid_;
596 }
597
598 namespace {
599
600 std::pair<const uint8_t*, size_t> queueFront(const IOBufQueue& queue) {
601   auto* p = queue.front();
602   if (!p) return std::make_pair(nullptr, 0);
603   return io::Cursor(p).peek();
604 }
605
606 // fd write
607 bool handleWrite(int fd, IOBufQueue& queue) {
608   for (;;) {
609     auto p = queueFront(queue);
610     if (p.second == 0) {
611       return true;  // EOF
612     }
613
614     ssize_t n = writeNoInt(fd, p.first, p.second);
615     if (n == -1 && errno == EAGAIN) {
616       return false;
617     }
618     checkUnixError(n, "write");
619     queue.trimStart(n);
620   }
621 }
622
623 // fd read
624 bool handleRead(int fd, IOBufQueue& queue) {
625   for (;;) {
626     auto p = queue.preallocate(100, 65000);
627     ssize_t n = readNoInt(fd, p.first, p.second);
628     if (n == -1 && errno == EAGAIN) {
629       return false;
630     }
631     checkUnixError(n, "read");
632     if (n == 0) {
633       return true;
634     }
635     queue.postallocate(n);
636   }
637 }
638
639 bool discardRead(int fd) {
640   static const size_t bufSize = 65000;
641   // Thread unsafe, but it doesn't matter.
642   static std::unique_ptr<char[]> buf(new char[bufSize]);
643
644   for (;;) {
645     ssize_t n = readNoInt(fd, buf.get(), bufSize);
646     if (n == -1 && errno == EAGAIN) {
647       return false;
648     }
649     checkUnixError(n, "read");
650     if (n == 0) {
651       return true;
652     }
653   }
654 }
655
656 }  // namespace
657
658 std::pair<std::string, std::string> Subprocess::communicate(
659     StringPiece input) {
660   IOBufQueue inputQueue;
661   inputQueue.wrapBuffer(input.data(), input.size());
662
663   auto outQueues = communicateIOBuf(std::move(inputQueue));
664   auto outBufs = std::make_pair(outQueues.first.move(),
665                                 outQueues.second.move());
666   std::pair<std::string, std::string> out;
667   if (outBufs.first) {
668     outBufs.first->coalesce();
669     out.first.assign(reinterpret_cast<const char*>(outBufs.first->data()),
670                      outBufs.first->length());
671   }
672   if (outBufs.second) {
673     outBufs.second->coalesce();
674     out.second.assign(reinterpret_cast<const char*>(outBufs.second->data()),
675                      outBufs.second->length());
676   }
677   return out;
678 }
679
680 std::pair<IOBufQueue, IOBufQueue> Subprocess::communicateIOBuf(
681     IOBufQueue input) {
682   // If the user supplied a non-empty input buffer, make sure
683   // that stdin is a pipe so we can write the data.
684   if (!input.empty()) {
685     // findByChildFd() will throw std::invalid_argument if no pipe for
686     // STDIN_FILENO exists
687     findByChildFd(STDIN_FILENO);
688   }
689
690   std::pair<IOBufQueue, IOBufQueue> out;
691
692   auto readCallback = [&] (int pfd, int cfd) -> bool {
693     if (cfd == STDOUT_FILENO) {
694       return handleRead(pfd, out.first);
695     } else if (cfd == STDERR_FILENO) {
696       return handleRead(pfd, out.second);
697     } else {
698       // Don't close the file descriptor, the child might not like SIGPIPE,
699       // just read and throw the data away.
700       return discardRead(pfd);
701     }
702   };
703
704   auto writeCallback = [&] (int pfd, int cfd) -> bool {
705     if (cfd == STDIN_FILENO) {
706       return handleWrite(pfd, input);
707     } else {
708       // If we don't want to write to this fd, just close it.
709       return true;
710     }
711   };
712
713   communicate(std::move(readCallback), std::move(writeCallback));
714
715   return out;
716 }
717
718 void Subprocess::communicate(FdCallback readCallback,
719                              FdCallback writeCallback) {
720   returnCode_.enforce(ProcessReturnCode::RUNNING);
721   setAllNonBlocking();
722
723   std::vector<pollfd> fds;
724   fds.reserve(pipes_.size());
725   std::vector<int> toClose;
726   toClose.reserve(pipes_.size());
727
728   while (!pipes_.empty()) {
729     fds.clear();
730     toClose.clear();
731
732     for (auto& p : pipes_) {
733       pollfd pfd;
734       pfd.fd = p.parentFd;
735       // Yes, backwards, PIPE_IN / PIPE_OUT are defined from the
736       // child's point of view.
737       if (!p.enabled) {
738         // Still keeping fd in watched set so we get notified of POLLHUP /
739         // POLLERR
740         pfd.events = 0;
741       } else if (p.direction == PIPE_IN) {
742         pfd.events = POLLOUT;
743       } else {
744         pfd.events = POLLIN;
745       }
746       fds.push_back(pfd);
747     }
748
749     int r;
750     do {
751       r = ::poll(fds.data(), fds.size(), -1);
752     } while (r == -1 && errno == EINTR);
753     checkUnixError(r, "poll");
754
755     for (size_t i = 0; i < pipes_.size(); ++i) {
756       auto& p = pipes_[i];
757       DCHECK_EQ(fds[i].fd, p.parentFd);
758       short events = fds[i].revents;
759
760       bool closed = false;
761       if (events & POLLOUT) {
762         DCHECK(!(events & POLLIN));
763         if (writeCallback(p.parentFd, p.childFd)) {
764           toClose.push_back(i);
765           closed = true;
766         }
767       }
768
769       // Call read callback on POLLHUP, to give it a chance to read (and act
770       // on) end of file
771       if (events & (POLLIN | POLLHUP)) {
772         DCHECK(!(events & POLLOUT));
773         if (readCallback(p.parentFd, p.childFd)) {
774           toClose.push_back(i);
775           closed = true;
776         }
777       }
778
779       if ((events & (POLLHUP | POLLERR)) && !closed) {
780         toClose.push_back(i);
781         closed = true;
782       }
783     }
784
785     // Close the fds in reverse order so the indexes hold after erase()
786     for (int idx : boost::adaptors::reverse(toClose)) {
787       auto pos = pipes_.begin() + idx;
788       closeChecked(pos->parentFd);
789       pipes_.erase(pos);
790     }
791   }
792 }
793
794 void Subprocess::enableNotifications(int childFd, bool enabled) {
795   pipes_[findByChildFd(childFd)].enabled = enabled;
796 }
797
798 bool Subprocess::notificationsEnabled(int childFd) const {
799   return pipes_[findByChildFd(childFd)].enabled;
800 }
801
802 int Subprocess::findByChildFd(int childFd) const {
803   auto pos = std::lower_bound(
804       pipes_.begin(), pipes_.end(), childFd,
805       [] (const PipeInfo& info, int fd) { return info.childFd < fd; });
806   if (pos == pipes_.end() || pos->childFd != childFd) {
807     throw std::invalid_argument(folly::to<std::string>(
808         "child fd not found ", childFd));
809   }
810   return pos - pipes_.begin();
811 }
812
813 void Subprocess::closeParentFd(int childFd) {
814   int idx = findByChildFd(childFd);
815   closeChecked(pipes_[idx].parentFd);
816   pipes_.erase(pipes_.begin() + idx);
817 }
818
819 namespace {
820
821 class Initializer {
822  public:
823   Initializer() {
824     // We like EPIPE, thanks.
825     ::signal(SIGPIPE, SIG_IGN);
826   }
827 };
828
829 Initializer initializer;
830
831 }  // namespace
832
833 }  // namespace folly