fix missing header in Range.h
[folly.git] / folly / Subprocess.cpp
index 4cd2c7c1088b0021d2244477fd03134c2cf29e82..8de5d36979b8e28b1d3cf260cd66083cc9d8b6b3 100644 (file)
@@ -118,7 +118,7 @@ namespace {
 // Copy pointers to the given strings in a format suitable for posix_spawn
 std::unique_ptr<const char*[]> cloneStrings(const std::vector<std::string>& s) {
   std::unique_ptr<const char*[]> d(new const char*[s.size() + 1]);
-  for (int i = 0; i < s.size(); i++) {
+  for (size_t i = 0; i < s.size(); i++) {
     d[i] = s[i].c_str();
   }
   d[s.size()] = nullptr;
@@ -289,6 +289,14 @@ void Subprocess::spawn(
   // child has exited and can be immediately waited for.  In all other cases,
   // we have no way of cleaning up the child.
 
+  if (options.processGroupLeader_) {
+    // This is done both in the parent and the child to avoid the race where
+    // the parent assumes that the child is a leader, but the child has not
+    // yet run setprp().  Not checking error codes since we're deliberately
+    // racing the child, which may already have run execve(), and expect to
+    // lose frequently.
+    setpgid(pid_, pid_);
+  }
   // Close writable side of the errFd pipe in the parent process
   CHECK_ERR(::close(errFds[1]));
   errFds[1] = -1;
@@ -486,6 +494,12 @@ int Subprocess::prepareChild(const Options& options,
   }
 #endif
 
+  if (options.processGroupLeader_) {
+    if (setpgrp() == -1) {
+      return errno;
+    }
+  }
+
   return 0;
 }
 
@@ -493,7 +507,6 @@ int Subprocess::runChild(const char* executable,
                          char** argv, char** env,
                          const Options& options) const {
   // Now, finally, exec.
-  int r;
   if (options.usePath_) {
     ::execvp(executable, argv);
   } else {
@@ -739,7 +752,7 @@ void Subprocess::communicate(FdCallback readCallback,
     } while (r == -1 && errno == EINTR);
     checkUnixError(r, "poll");
 
-    for (int i = 0; i < pipes_.size(); ++i) {
+    for (size_t i = 0; i < pipes_.size(); ++i) {
       auto& p = pipes_[i];
       DCHECK_EQ(fds[i].fd, p.parentFd);
       short events = fds[i].revents;
@@ -818,4 +831,3 @@ Initializer initializer;
 }  // namespace
 
 }  // namespace folly
-