projects
/
folly.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Fix fibers gdb utils script
[folly.git]
/
folly
/
Subprocess.cpp
diff --git
a/folly/Subprocess.cpp
b/folly/Subprocess.cpp
index b517c55df33c54377ebb886b4bbd052b964ef69b..93e3f577157a1ad324411eaa3bafdc6ad1292b5f 100644
(file)
--- a/
folly/Subprocess.cpp
+++ b/
folly/Subprocess.cpp
@@
-1,5
+1,5
@@
/*
/*
- * Copyright 201
5
Facebook, Inc.
+ * Copyright 201
6
Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@
-24,9
+24,6
@@
#include <sys/prctl.h>
#endif
#include <fcntl.h>
#include <sys/prctl.h>
#endif
#include <fcntl.h>
-#include <poll.h>
-
-#include <unistd.h>
#include <array>
#include <algorithm>
#include <array>
#include <algorithm>
@@
-40,10
+37,12
@@
#include <folly/Conv.h>
#include <folly/Exception.h>
#include <folly/ScopeGuard.h>
#include <folly/Conv.h>
#include <folly/Exception.h>
#include <folly/ScopeGuard.h>
+#include <folly/Shell.h>
#include <folly/String.h>
#include <folly/io/Cursor.h>
#include <folly/String.h>
#include <folly/io/Cursor.h>
-
-extern char** environ;
+#include <folly/portability/Environment.h>
+#include <folly/portability/Sockets.h>
+#include <folly/portability/Unistd.h>
constexpr int kExecFailure = 127;
constexpr int kChildFailure = 126;
constexpr int kExecFailure = 127;
constexpr int kChildFailure = 126;
@@
-163,13
+162,13
@@
Subprocess::Options& Subprocess::Options::fd(int fd, int action) {
return *this;
}
return *this;
}
+Subprocess::Subprocess() {}
+
Subprocess::Subprocess(
const std::vector<std::string>& argv,
const Options& options,
const char* executable,
Subprocess::Subprocess(
const std::vector<std::string>& argv,
const Options& options,
const char* executable,
- const std::vector<std::string>* env)
- : pid_(-1),
- returnCode_(RV_NOT_STARTED) {
+ const std::vector<std::string>* env) {
if (argv.empty()) {
throw std::invalid_argument("argv must not be empty");
}
if (argv.empty()) {
throw std::invalid_argument("argv must not be empty");
}
@@
-180,23
+179,13
@@
Subprocess::Subprocess(
Subprocess::Subprocess(
const std::string& cmd,
const Options& options,
Subprocess::Subprocess(
const std::string& cmd,
const Options& options,
- const std::vector<std::string>* env)
- : pid_(-1),
- returnCode_(RV_NOT_STARTED) {
+ const std::vector<std::string>* env) {
if (options.usePath_) {
throw std::invalid_argument("usePath() not allowed when running in shell");
}
if (options.usePath_) {
throw std::invalid_argument("usePath() not allowed when running in shell");
}
- const char* shell = getenv("SHELL");
- if (!shell) {
- shell = "/bin/sh";
- }
- std::unique_ptr<const char*[]> argv(new const char*[4]);
- argv[0] = shell;
- argv[1] = "-c";
- argv[2] = cmd.c_str();
- argv[3] = nullptr;
- spawn(std::move(argv), shell, options, env);
+ std::vector<std::string> argv = {"/bin/sh", "-c", cmd};
+ spawn(cloneStrings(argv), argv[0].c_str(), options, env);
}
Subprocess::~Subprocess() {
}
Subprocess::~Subprocess() {
@@
-211,8
+200,7
@@
struct ChildErrorInfo {
int errnoValue;
};
int errnoValue;
};
-FOLLY_NORETURN void childError(int errFd, int errCode, int errnoValue);
-void childError(int errFd, int errCode, int errnoValue) {
+[[noreturn]] void childError(int errFd, int errCode, int errnoValue) {
ChildErrorInfo info = {errCode, errnoValue};
// Write the error information over the pipe to our parent process.
// We can't really do anything else if this write call fails.
ChildErrorInfo info = {errCode, errnoValue};
// Write the error information over the pipe to our parent process.
// We can't really do anything else if this write call fails.
@@
-481,7
+469,9
@@
int Subprocess::prepareChild(const Options& options,
#if __linux__
// Opt to receive signal on parent death, if requested
if (options.parentDeathSignal_ != 0) {
#if __linux__
// Opt to receive signal on parent death, if requested
if (options.parentDeathSignal_ != 0) {
- if (prctl(PR_SET_PDEATHSIG, options.parentDeathSignal_, 0, 0, 0) == -1) {
+ const auto parentDeathSignal =
+ static_cast<unsigned long>(options.parentDeathSignal_);
+ if (prctl(PR_SET_PDEATHSIG, parentDeathSignal, 0, 0, 0) == -1) {
return errno;
}
}
return errno;
}
}
@@
-493,6
+483,13
@@
int Subprocess::prepareChild(const Options& options,
}
}
}
}
+ // The user callback comes last, so that the child is otherwise all set up.
+ if (options.dangerousPostForkPreExecCallback_) {
+ if (int error = (*options.dangerousPostForkPreExecCallback_)()) {
+ return error;
+ }
+ }
+
return 0;
}
return 0;
}
@@
-599,21
+596,23
@@
pid_t Subprocess::pid() const {
namespace {
namespace {
-
std::pair<const uint8_t*, size_t>
queueFront(const IOBufQueue& queue) {
+
ByteRange
queueFront(const IOBufQueue& queue) {
auto* p = queue.front();
auto* p = queue.front();
- if (!p) return std::make_pair(nullptr, 0);
- return io::Cursor(p).peek();
+ if (!p) {
+ return ByteRange{};
+ }
+ return io::Cursor(p).peekBytes();
}
// fd write
bool handleWrite(int fd, IOBufQueue& queue) {
for (;;) {
}
// fd write
bool handleWrite(int fd, IOBufQueue& queue) {
for (;;) {
- auto
p
= queueFront(queue);
- if (
p.second == 0
) {
+ auto
b
= queueFront(queue);
+ if (
b.empty()
) {
return true; // EOF
}
return true; // EOF
}
- ssize_t n = writeNoInt(fd,
p.first, p.second
);
+ ssize_t n = writeNoInt(fd,
b.data(), b.size()
);
if (n == -1 && errno == EAGAIN) {
return false;
}
if (n == -1 && errno == EAGAIN) {
return false;
}
@@
-826,7
+825,8
@@
std::vector<Subprocess::ChildPipe> Subprocess::takeOwnershipOfPipes() {
for (auto& p : pipes_) {
pipes.emplace_back(p.childFd, std::move(p.pipe));
}
for (auto& p : pipes_) {
pipes.emplace_back(p.childFd, std::move(p.pipe));
}
- pipes_.clear();
+ // release memory
+ std::vector<Pipe>().swap(pipes_);
return pipes;
}
return pipes;
}