summaryrefslogtreecommitdiff
path: root/process.cpp
blob: 41325c345eb00eb3435f21a8aba8bd6d1218e03d (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <iostream>
#include <cstdlib>
#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h>
#include "process.h"
#include "error.h"


Process::Process(const string_view execname)
		: execname(execname) {}

void Process::redirectStderr(const string_view fname) {
	stderrRedirect = fname;
}

void Process::run() {
	int stderrfd = -1;
	if (stderrRedirect) {
		stderrfd = open(stderrRedirect->data(), O_WRONLY|O_CREAT|O_TRUNC, 0644);
		if (stderrfd < 0) {
			perror("open");
			cout << endl << "ERROR: Cannot open player log file '" << *stderrRedirect << "'" << endl;
			throw StopCompetitionError();
		}
	}

	int pipefds[2];
	if (pipe(pipefds) < 0) {
		perror("pipe");
		exit(1);
	}
	infd = pipefds[1];
	int child_in = pipefds[0];

	if (pipe(pipefds) < 0) {
		perror("pipe");
		exit(1);
	}
	outfd = pipefds[0];
	int child_out = pipefds[1];

	pid = fork();
	if (pid < 0) {
		perror("fork");
		exit(1);
	}

	if (pid == 0) {
		if (stderrRedirect) dup2(stderrfd, STDERR_FILENO);
		dup2(child_in, STDIN_FILENO);
		dup2(child_out, STDOUT_FILENO);
		close(infd);
		close(outfd);
		close(child_in);
		close(child_out);

		execlp(execname.data(), execname.data(), NULL);
		cout << endl << "ERROR: Error executing player file '" << execname << "'" << endl;
		exit(255);
	}

	if (stderrfd >= 0) close(stderrfd);
	close(child_in);
	close(child_out);
}

int Process::waitPoll() {
	if (pid == -1) return exitStatus;

	while (true) {
		int status;
		int ret = waitpid(pid, &status, WNOHANG);
		if (ret < 0) {
			if (errno == ECHILD) return exitStatus;
			perror("waitpid");
			return -1;
		}
		if (ret == 0) {
			// Nothing changed yet
			return -1;
		}

		if (WIFEXITED(status)) {
			cleanup();
			exitStatus = WEXITSTATUS(status);
			return exitStatus;
		}

		if (WIFSIGNALED(status)) {
			cleanup();
			exitStatus = 1000 + WTERMSIG(status);
			return exitStatus;
		}
	}
}

int Process::wait() {
	if (pid == -1) return exitStatus;

	while (true) {
		int status;
		if (waitpid(pid, &status, 0) < 0) {
			if (errno == EINTR) continue;
			if (errno == ECHILD) return exitStatus;
			perror("waitpid");
			return -1;
		}

		if (WIFEXITED(status)) {
			cleanup();
			exitStatus = WEXITSTATUS(status);
			return exitStatus;
		}

		if (WIFSIGNALED(status)) {
			cleanup();
			exitStatus = 1000 + WTERMSIG(status);
			return exitStatus;
		}
	}
}

void Process::stop() {
	if (pid != -1) kill(pid, SIGSTOP);
}

void Process::unStop() {
	if (pid != -1) kill(pid, SIGCONT);
}

bool Process::writeLine(const string_view line) {
	if (pid == -1) return false;

	string str;
	str.reserve(line.size() + 1);
	str += line;
	str += '\n';

	size_t cursor = 0;
	while (cursor < str.size()) {
		ssize_t nw = write(infd, str.data() + cursor, str.size() - cursor);
		if (nw < 0) {
			if (errno == EINTR) continue;
			// perror("write");
			return false;
		}
		cursor += nw;
	}

	return true;
}

optional<string> Process::readLine() {
	if (pid == -1) {
		cout << "-- readLine on pid==-1 --" << endl;
		return nullopt;
	}

	size_t idx = readBuf.find('\n');
	if (idx != string::npos) {
		string res = readBuf.substr(0, idx);
		readBuf = readBuf.substr(idx + 1);
		return res;
	}

	while (true) {
		string s(1024, '\0');
		ssize_t nr = read(outfd, &s[0], s.size());
		if (nr < 0) {
			if (errno == EINTR) continue;
			perror("read");
			return nullopt;
		}
		if (nr == 0) {  // EOF
			cout << "-- eof in readLine --" << endl;
			return nullopt;
		}
		s.resize(nr);

		idx = s.find('\n');
		if (idx != string::npos) {
			string res = readBuf + s.substr(0, idx);
			readBuf = s.substr(idx + 1);
			return res;
		}

		readBuf += s;
	}
}

int Process::terminate() {
	// TODO: send only SIGTERM, then wait a little while, then send
	// SIGKILL if necessary
	if (pid != -1) {
		int ret = waitPoll();
		if (ret == -1) {
			kill(pid, SIGKILL);  // force kill
			ret = wait();
		}
		cleanup();
		return ret;
	}
	return exitStatus;
}

pid_t Process::getPid() const {
	return pid;
}

void Process::cleanup() {
	pid = -1;
	close(infd);
	close(outfd);
}