summaryrefslogtreecommitdiff
path: root/competition/process.cpp
blob: a728401fc3dae4ec7a59f5c56ff76d27b80e463d (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
#include <iostream>
#include <cstdlib>
#include <fcntl.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);

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

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

void Process::wait() {
	while (true) {
		int status;
		if (waitpid(pid, &status, 0) < 0) {
			if (errno == EINTR) continue;
			perror("waitpid");
			break;
		}
		if (WIFEXITED(status)) break;
	}
}

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) {
	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() {
	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;
		}
		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;
	}
}

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