blob: abddfb352f9a1e8da3214266b239b88021c3e5d5 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once
#include <vespa/vespalib/util/guard.h>
#include <vespa/vespalib/util/time.h>
#include <vespa/vespalib/data/input.h>
#include <vespa/vespalib/data/output.h>
#include <vespa/vespalib/data/smart_buffer.h>
#include <unistd.h>
namespace vespalib {
/**
* A simple low-level class enabling you to start a process by running
* a command in the shell. Use 'close' to close the stdin pipe from
* the outside. Use 'join' to wait for process completion and exit
* status. The destructor will use SIGKILL to stop the process if it
* was not joined. The Process class implements the Input/Output
* interfaces to interact with stdout/stdin. If stderr is captured, it
* is merged with stdout.
*
* This class is primarily intended for use in tests. It has liberal
* REQUIRE usage and will crash when something is not right.
**/
class Process : public Output, public Input
{
private:
pid_t _pid;
FileDescriptor _in;
FileDescriptor _out;
SmartBuffer _in_buf;
SmartBuffer _out_buf;
bool _eof;
public:
Process(const vespalib::string &cmd, bool capture_stderr = false);
pid_t pid() const { return _pid; }
bool valid() const { return (_pid > 0); }
void close() { _in.reset(); }
Memory obtain() override; // Input (stdout)
Input &evict(size_t bytes) override; // Input (stdout)
WritableMemory reserve(size_t bytes) override; // Output (stdin)
Output &commit(size_t bytes) override; // Output (stdin)
vespalib::string read_line();
bool eof() const { return _eof; }
int join();
~Process();
static bool run(const vespalib::string &cmd, vespalib::string &output);
static bool run(const vespalib::string &cmd);
};
} // namespace vespalib
|