summaryrefslogtreecommitdiff
path: root/process.h
blob: 0588a40e742a95eb34b3232a49f8a64582e9e67f (plain)
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
#pragma once

#include <string>
#include <optional>
#include <string_view>
#include <unistd.h>

using namespace std;


class Process {
	string execname;
	optional<string> stderrRedirect;
	pid_t pid = -1;
	int infd = -1, outfd = -1;

	int exitStatus = -1;

	string readBuf;

	void cleanup();

public:
	Process(const string_view execname);
	Process(const Process&) = delete;
	Process(Process&&) = default;

	void redirectStderr(const string_view fname);

	void run();
	void stop();
	void unStop();
	// These return -1 if an error occurred, and the process return value otherwise.
	// If the process was killed by a signal, (1000 + signal) is returned.
	int wait();
	int waitPoll();  // Also returns -1 if process hasn't finished yet
	int terminate();

	pid_t getPid() const;

	bool writeLine(const string_view line, bool allowBrokenPipe);
	optional<string> readLine();
	optional<string> readLineTimeout(size_t millis);
};