Skip to content

Commit

Permalink
Get Subprocess running for Mac OS X
Browse files Browse the repository at this point in the history
Summary:
- D1030008 added Subprocess to libfolly in automake builds. This
surfaced some ambient compilation errors that slipped through in my
prior run through porting this.
- Mac OS X uses a nonstandard location for wait.h
- Non-Linux platforms don't support prctl; gate that on Linux

Test Plan:
- fbmake runtests in fbcode
- make check on Mac OS X

Reviewed By: [email protected]

FB internal diff: D1066273

Blame Revision: D1030008
  • Loading branch information
Peter Griess committed Nov 26, 2013
1 parent c807fe1 commit 54e29e7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
7 changes: 5 additions & 2 deletions folly/Subprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

#include "folly/Subprocess.h"

#if __linux__
#include <sys/prctl.h>
#endif
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>
#include <wait.h>

#include <array>
#include <algorithm>
Expand Down Expand Up @@ -362,7 +363,7 @@ void Subprocess::spawnInternal(
//
// The parent also unblocks all signals as soon as vfork() returns.
sigset_t allBlocked;
r = ::sigfillset(&allBlocked);
r = sigfillset(&allBlocked);
checkUnixError(r, "sigfillset");
sigset_t oldSignals;

Expand Down Expand Up @@ -445,13 +446,15 @@ int Subprocess::prepareChild(const Options& options,
}
}

#if __linux__
// Opt to receive signal on parent death, if requested
if (options.parentDeathSignal_ != 0) {
r = prctl(PR_SET_PDEATHSIG, options.parentDeathSignal_, 0, 0, 0);
if (r == -1) {
return errno;
}
}
#endif

return 0;
}
Expand Down
13 changes: 10 additions & 3 deletions folly/Subprocess.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@

#include <sys/types.h>
#include <signal.h>
#if __APPLE__
#include <sys/wait.h>
#else
#include <wait.h>
#endif

#include <exception>
#include <vector>
Expand Down Expand Up @@ -201,8 +205,7 @@ class Subprocess : private boost::noncopyable {
public:
Options()
: closeOtherFds_(false),
usePath_(false),
parentDeathSignal_(0) {
usePath_(false) {
}

/**
Expand Down Expand Up @@ -261,13 +264,15 @@ class Subprocess : private boost::noncopyable {
*/
Options& usePath() { usePath_ = true; return *this; }

#if __linux__
/**
* Child will receive a signal when the parent exits.
*/
Options& parentDeathSignal(int sig) {
parentDeathSignal_ = sig;
return *this;
}
#endif

/**
* Helpful way to combine Options.
Expand All @@ -279,7 +284,9 @@ class Subprocess : private boost::noncopyable {
FdMap fdActions_;
bool closeOtherFds_;
bool usePath_;
int parentDeathSignal_;
#if __linux__
int parentDeathSignal_{0};
#endif
};

static Options pipeStdin() { return Options().stdin(PIPE); }
Expand Down

0 comments on commit 54e29e7

Please sign in to comment.