2015-12-13 08:51:28 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <dlfcn.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/ShellCommand.h>
|
2018-11-23 08:08:35 +00:00
|
|
|
#include <common/logger_useful.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2018-03-23 16:05:14 +00:00
|
|
|
#include <port/unistd.h>
|
2018-11-22 15:59:00 +00:00
|
|
|
#include <csignal>
|
2019-02-09 21:40:10 +00:00
|
|
|
#include <common/Pipe.h>
|
2015-12-13 08:51:28 +00:00
|
|
|
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-02-03 09:57:12 +00:00
|
|
|
namespace ErrorCodes
|
2015-12-13 08:51:28 +00:00
|
|
|
{
|
2019-02-03 21:30:45 +00:00
|
|
|
extern const int CANNOT_DLSYM;
|
|
|
|
extern const int CANNOT_FORK;
|
|
|
|
extern const int CANNOT_WAITPID;
|
|
|
|
extern const int CHILD_WAS_NOT_EXITED_NORMALLY;
|
|
|
|
extern const int CANNOT_CREATE_CHILD_PROCESS;
|
2015-12-13 08:51:28 +00:00
|
|
|
}
|
|
|
|
|
2019-02-03 09:57:12 +00:00
|
|
|
/// By these return codes from the child process, we learn (for sure) about errors when creating it.
|
|
|
|
enum class ReturnCodes : int
|
2015-12-13 08:51:28 +00:00
|
|
|
{
|
2019-02-03 09:57:12 +00:00
|
|
|
CANNOT_DUP_STDIN = 0x55555555, /// The value is not important, but it is chosen so that it's rare to conflict with the program return code.
|
|
|
|
CANNOT_DUP_STDOUT = 0x55555556,
|
|
|
|
CANNOT_DUP_STDERR = 0x55555557,
|
|
|
|
CANNOT_EXEC = 0x55555558,
|
|
|
|
};
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2018-11-23 08:08:35 +00:00
|
|
|
ShellCommand::ShellCommand(pid_t pid, int in_fd, int out_fd, int err_fd, bool terminate_in_destructor_)
|
|
|
|
: pid(pid)
|
|
|
|
, terminate_in_destructor(terminate_in_destructor_)
|
|
|
|
, log(&Poco::Logger::get("ShellCommand"))
|
|
|
|
, in(in_fd)
|
|
|
|
, out(out_fd)
|
|
|
|
, err(err_fd) {}
|
|
|
|
|
2017-09-07 00:12:39 +00:00
|
|
|
ShellCommand::~ShellCommand()
|
|
|
|
{
|
2018-11-23 08:08:35 +00:00
|
|
|
if (terminate_in_destructor)
|
|
|
|
{
|
|
|
|
int retcode = kill(pid, SIGTERM);
|
|
|
|
if (retcode != 0)
|
|
|
|
LOG_WARNING(log, "Cannot kill pid " << pid << " errno '" << errnoToString(retcode) << "'");
|
|
|
|
}
|
2018-11-22 15:59:00 +00:00
|
|
|
else if (!wait_called)
|
2017-09-07 00:12:39 +00:00
|
|
|
tryWait();
|
|
|
|
}
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2018-11-23 08:08:35 +00:00
|
|
|
std::unique_ptr<ShellCommand> ShellCommand::executeImpl(const char * filename, char * const argv[], bool pipe_stdin_only, bool terminate_in_destructor)
|
2015-12-13 08:51:28 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/** Here it is written that with a normal call `vfork`, there is a chance of deadlock in multithreaded programs,
|
|
|
|
* because of the resolving of characters in the shared library
|
|
|
|
* http://www.oracle.com/technetwork/server-storage/solaris10/subprocess-136439.html
|
|
|
|
* Therefore, separate the resolving of the symbol from the call.
|
|
|
|
*/
|
|
|
|
static void * real_vfork = dlsym(RTLD_DEFAULT, "vfork");
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!real_vfork)
|
|
|
|
throwFromErrno("Cannot find symbol vfork in myself", ErrorCodes::CANNOT_DLSYM);
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Pipe pipe_stdin;
|
|
|
|
Pipe pipe_stdout;
|
|
|
|
Pipe pipe_stderr;
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
pid_t pid = reinterpret_cast<pid_t(*)()>(real_vfork)();
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (-1 == pid)
|
|
|
|
throwFromErrno("Cannot vfork", ErrorCodes::CANNOT_FORK);
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (0 == pid)
|
|
|
|
{
|
|
|
|
/// We are in the freshly created process.
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Why `_exit` and not `exit`? Because `exit` calls `atexit` and destructors of thread local storage.
|
|
|
|
/// And there is a lot of garbage (including, for example, mutex is blocked). And this can not be done after `vfork` - deadlock happens.
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Replace the file descriptors with the ends of our pipes.
|
2019-02-09 21:40:10 +00:00
|
|
|
if (STDIN_FILENO != dup2(pipe_stdin.fds_rw[0], STDIN_FILENO))
|
2017-04-01 07:20:54 +00:00
|
|
|
_exit(int(ReturnCodes::CANNOT_DUP_STDIN));
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!pipe_stdin_only)
|
|
|
|
{
|
2019-02-09 21:40:10 +00:00
|
|
|
if (STDOUT_FILENO != dup2(pipe_stdout.fds_rw[1], STDOUT_FILENO))
|
2017-04-01 07:20:54 +00:00
|
|
|
_exit(int(ReturnCodes::CANNOT_DUP_STDOUT));
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2019-02-09 21:40:10 +00:00
|
|
|
if (STDERR_FILENO != dup2(pipe_stderr.fds_rw[1], STDERR_FILENO))
|
2017-04-01 07:20:54 +00:00
|
|
|
_exit(int(ReturnCodes::CANNOT_DUP_STDERR));
|
|
|
|
}
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
execv(filename, argv);
|
|
|
|
/// If the process is running, then `execv` does not return here.
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
_exit(int(ReturnCodes::CANNOT_EXEC));
|
|
|
|
}
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2019-02-09 21:40:10 +00:00
|
|
|
std::unique_ptr<ShellCommand> res(new ShellCommand(pid, pipe_stdin.fds_rw[1], pipe_stdout.fds_rw[0], pipe_stderr.fds_rw[0], terminate_in_destructor));
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Now the ownership of the file descriptors is passed to the result.
|
2019-02-09 21:40:10 +00:00
|
|
|
pipe_stdin.fds_rw[1] = -1;
|
|
|
|
pipe_stdout.fds_rw[0] = -1;
|
|
|
|
pipe_stderr.fds_rw[0] = -1;
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2015-12-13 08:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-23 08:08:35 +00:00
|
|
|
std::unique_ptr<ShellCommand> ShellCommand::execute(const std::string & command, bool pipe_stdin_only, bool terminate_in_destructor)
|
2015-12-13 08:51:28 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Arguments in non-constant chunks of memory (as required for `execv`).
|
|
|
|
/// Moreover, their copying must be done before calling `vfork`, so after `vfork` do a minimum of things.
|
2019-01-07 10:40:58 +00:00
|
|
|
std::vector<char> argv0("sh", &("sh"[3]));
|
|
|
|
std::vector<char> argv1("-c", &("-c"[3]));
|
2017-04-01 07:20:54 +00:00
|
|
|
std::vector<char> argv2(command.data(), command.data() + command.size() + 1);
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
char * const argv[] = { argv0.data(), argv1.data(), argv2.data(), nullptr };
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2018-11-23 08:08:35 +00:00
|
|
|
return executeImpl("/bin/sh", argv, pipe_stdin_only, terminate_in_destructor);
|
2015-12-13 08:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-23 08:08:35 +00:00
|
|
|
std::unique_ptr<ShellCommand> ShellCommand::executeDirect(const std::string & path, const std::vector<std::string> & arguments, bool terminate_in_destructor)
|
2015-12-13 08:51:28 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t argv_sum_size = path.size() + 1;
|
|
|
|
for (const auto & arg : arguments)
|
|
|
|
argv_sum_size += arg.size() + 1;
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::vector<char *> argv(arguments.size() + 2);
|
|
|
|
std::vector<char> argv_data(argv_sum_size);
|
|
|
|
WriteBuffer writer(argv_data.data(), argv_sum_size);
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
argv[0] = writer.position();
|
|
|
|
writer.write(path.data(), path.size() + 1);
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0, size = arguments.size(); i < size; ++i)
|
|
|
|
{
|
|
|
|
argv[i + 1] = writer.position();
|
|
|
|
writer.write(arguments[i].data(), arguments[i].size() + 1);
|
|
|
|
}
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
argv[arguments.size() + 1] = nullptr;
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2018-11-23 08:08:35 +00:00
|
|
|
return executeImpl(path.data(), argv.data(), false, terminate_in_destructor);
|
2015-12-13 08:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ShellCommand::tryWait()
|
|
|
|
{
|
2017-09-07 00:12:39 +00:00
|
|
|
wait_called = true;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
int status = 0;
|
|
|
|
if (-1 == waitpid(pid, &status, 0))
|
|
|
|
throwFromErrno("Cannot waitpid", ErrorCodes::CANNOT_WAITPID);
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (WIFEXITED(status))
|
|
|
|
return WEXITSTATUS(status);
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (WIFSIGNALED(status))
|
|
|
|
throw Exception("Child process was terminated by signal " + toString(WTERMSIG(status)), ErrorCodes::CHILD_WAS_NOT_EXITED_NORMALLY);
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (WIFSTOPPED(status))
|
|
|
|
throw Exception("Child process was stopped by signal " + toString(WSTOPSIG(status)), ErrorCodes::CHILD_WAS_NOT_EXITED_NORMALLY);
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Child process was not exited normally by unknown reason", ErrorCodes::CHILD_WAS_NOT_EXITED_NORMALLY);
|
2015-12-13 08:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ShellCommand::wait()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
int retcode = tryWait();
|
|
|
|
|
|
|
|
if (retcode != EXIT_SUCCESS)
|
|
|
|
{
|
|
|
|
switch (retcode)
|
|
|
|
{
|
|
|
|
case int(ReturnCodes::CANNOT_DUP_STDIN):
|
|
|
|
throw Exception("Cannot dup2 stdin of child process", ErrorCodes::CANNOT_CREATE_CHILD_PROCESS);
|
|
|
|
case int(ReturnCodes::CANNOT_DUP_STDOUT):
|
|
|
|
throw Exception("Cannot dup2 stdout of child process", ErrorCodes::CANNOT_CREATE_CHILD_PROCESS);
|
|
|
|
case int(ReturnCodes::CANNOT_DUP_STDERR):
|
|
|
|
throw Exception("Cannot dup2 stderr of child process", ErrorCodes::CANNOT_CREATE_CHILD_PROCESS);
|
|
|
|
case int(ReturnCodes::CANNOT_EXEC):
|
|
|
|
throw Exception("Cannot execv in child process", ErrorCodes::CANNOT_CREATE_CHILD_PROCESS);
|
|
|
|
default:
|
|
|
|
throw Exception("Child process was exited with return code " + toString(retcode), ErrorCodes::CHILD_WAS_NOT_EXITED_NORMALLY);
|
|
|
|
}
|
|
|
|
}
|
2015-12-13 08:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|