/*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2013 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include "folly/Conv.h"
#include "folly/ScopeGuard.h"
#include "folly/String.h"
-#include "folly/experimental/io/Cursor.h"
+#include "folly/io/Cursor.h"
extern char** environ;
}
Subprocess::~Subprocess() {
- if (returnCode_.state() == ProcessReturnCode::RUNNING) {
- LOG(ERROR) << "Subprocess destroyed without reaping; killing child.";
- try {
- kill();
- wait();
- } catch (...) {
- LOG(FATAL) << "Killing child failed, terminating: "
- << exceptionStr(std::current_exception());
- }
- }
- try {
- closeAll();
- } catch (...) {
- LOG(FATAL) << "close failed, terminating: "
- << exceptionStr(std::current_exception());
- }
+ CHECK_NE(returnCode_.state(), ProcessReturnCode::RUNNING)
+ << "Subprocess destroyed without reaping child";
}
namespace {
}
namespace {
-void setNonBlocking(int fd) {
- int flags = ::fcntl(fd, F_GETFL);
- checkUnixError(flags, "fcntl");
- int r = ::fcntl(fd, F_SETFL, flags | O_NONBLOCK);
- checkUnixError(r, "fcntl");
-}
std::pair<const uint8_t*, size_t> queueFront(const IOBufQueue& queue) {
auto* p = queue.front();
} // namespace
std::pair<std::string, std::string> Subprocess::communicate(
- int flags,
+ const CommunicateFlags& flags,
StringPiece data) {
IOBufQueue dataQueue;
dataQueue.wrapBuffer(data.data(), data.size());
}
std::pair<IOBufQueue, IOBufQueue> Subprocess::communicateIOBuf(
- int flags,
+ const CommunicateFlags& flags,
IOBufQueue data) {
std::pair<IOBufQueue, IOBufQueue> out;
- auto readCallback = [&, flags] (int pfd, int cfd) {
- if (cfd == 1 && (flags & READ_STDOUT)) {
+ auto readCallback = [&] (int pfd, int cfd) -> bool {
+ if (cfd == 1 && flags.readStdout_) {
return handleRead(pfd, out.first);
- } else if (cfd == 2 && (flags & READ_STDERR)) {
+ } else if (cfd == 2 && flags.readStderr_) {
return handleRead(pfd, out.second);
} else {
// Don't close the file descriptor, the child might not like SIGPIPE,
}
};
- auto writeCallback = [&, flags] (int pfd, int cfd) {
- if (cfd == 0 && (flags & WRITE_STDIN)) {
+ auto writeCallback = [&] (int pfd, int cfd) -> bool {
+ if (cfd == 0 && flags.writeStdin_) {
return handleWrite(pfd, data);
} else {
// If we don't want to write to this fd, just close it.