2016-06-07 08:23:15 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <memory>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBufferFromFile.h>
|
|
|
|
#include <IO/WriteBufferFromFile.h>
|
2016-06-07 08:23:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/** Lets you run the command,
|
2017-05-10 04:00:19 +00:00
|
|
|
* read it stdout and stderr; write to stdin;
|
|
|
|
* wait for completion.
|
2016-06-07 08:23:15 +00:00
|
|
|
*
|
2017-05-07 20:25:26 +00:00
|
|
|
* The implementation is similar to the popen function from POSIX (see libc source code).
|
2016-06-07 08:23:15 +00:00
|
|
|
*
|
2017-05-07 20:25:26 +00:00
|
|
|
* The most important difference: uses vfork instead of fork.
|
|
|
|
* This is done because fork does not work (with a memory shortage error),
|
|
|
|
* with some overcommit settings, if the address space of the process is more than half the amount of available memory.
|
|
|
|
* Also, changing memory maps - a fairly resource-intensive operation.
|
2016-06-07 08:23:15 +00:00
|
|
|
*
|
2017-05-10 04:00:19 +00:00
|
|
|
* The second difference - allows to work simultaneously with stdin, and with stdout, and with stderr of running process,
|
|
|
|
* and also to obtain the return code and completion status.
|
2016-06-07 08:23:15 +00:00
|
|
|
*/
|
|
|
|
class ShellCommand
|
|
|
|
{
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
pid_t pid;
|
2017-09-07 00:12:39 +00:00
|
|
|
bool wait_called = false;
|
2018-11-23 08:08:35 +00:00
|
|
|
bool terminate_in_destructor;
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2019-08-03 11:02:40 +00:00
|
|
|
ShellCommand(pid_t pid_, int in_fd_, int out_fd_, int err_fd_, bool terminate_in_destructor_);
|
2018-11-23 08:08:35 +00:00
|
|
|
|
2019-09-25 14:06:38 +00:00
|
|
|
static Poco::Logger * getLogger();
|
2019-12-09 14:49:21 +00:00
|
|
|
|
2019-09-27 12:23:34 +00:00
|
|
|
/// Print command name and the list of arguments to log. NOTE: No escaping of arguments is performed.
|
2019-09-25 14:06:38 +00:00
|
|
|
static void logCommand(const char * filename, char * const argv[]);
|
|
|
|
|
2018-11-23 08:08:35 +00:00
|
|
|
static std::unique_ptr<ShellCommand> executeImpl(const char * filename, char * const argv[], bool pipe_stdin_only, bool terminate_in_destructor);
|
2016-06-07 08:23:15 +00:00
|
|
|
|
|
|
|
public:
|
2017-05-07 20:25:26 +00:00
|
|
|
WriteBufferFromFile in; /// If the command reads from stdin, do not forget to call in.close() after writing all the data there.
|
2017-04-01 07:20:54 +00:00
|
|
|
ReadBufferFromFile out;
|
|
|
|
ReadBufferFromFile err;
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-09-07 00:12:39 +00:00
|
|
|
~ShellCommand();
|
|
|
|
|
2018-11-23 08:08:35 +00:00
|
|
|
/// Run the command using /bin/sh -c.
|
|
|
|
/// If terminate_in_destructor is true, send terminate signal in destructor and don't wait process.
|
|
|
|
static std::unique_ptr<ShellCommand> execute(const std::string & command, bool pipe_stdin_only = false, bool terminate_in_destructor = false);
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// Run the executable with the specified arguments. `arguments` - without argv[0].
|
2018-11-23 08:08:35 +00:00
|
|
|
/// If terminate_in_destructor is true, send terminate signal in destructor and don't wait process.
|
|
|
|
static std::unique_ptr<ShellCommand> executeDirect(const std::string & path, const std::vector<std::string> & arguments, bool terminate_in_destructor = false);
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// Wait for the process to end, throw an exception if the code is not 0 or if the process was not completed by itself.
|
2017-04-01 07:20:54 +00:00
|
|
|
void wait();
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// Wait for the process to finish, see the return code. To throw an exception if the process was not completed independently.
|
2017-04-01 07:20:54 +00:00
|
|
|
int tryWait();
|
2016-06-07 08:23:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|