summaryrefslogtreecommitdiffstats
path: root/ProcessUtilities.posix.cc
blob: 3cb041f0a4996c1928f2a3e0a50eccd8d75cae4e (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
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
216
217
218
219
220
221
222
#include "ProcessUtilities.h"

#include <sys/wait.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <vector>

class ProcessInfo
{
	public:
		int PID;
		int StdOutput;
		int StdError;
		ProcessController* Master;

		bool cycleStandardOutput();
		bool cycleStandardError();
		void waitForFinish();
};

void ProcessInfo::waitForFinish()
{
	int ret=0;
	waitpid(PID,&ret,0);
	if (WIFEXITED(ret))
	{
		ret = WEXITSTATUS(ret);
		Master->finished(ret);
		for (unsigned int i=0; i<Master->Public.size(); ++i)
			Master->Public[i]->processTerminated(Master,ret);
	}
}

bool ProcessInfo::cycleStandardOutput()
{
	char Buffer[1025];
	int res = read(StdOutput,Buffer,1024);
	if (res < 0)
		std::cout << "Read failed" << std::endl;
	else if (res == 0)
	{
		waitForFinish();
		return true;
	}
	else
		Master->standardOutput(std::string(Buffer,res));
	return false;
}

bool ProcessInfo::cycleStandardError()
{
	char Buffer[1025];
	int res = read(StdError,Buffer,1024);
	if (res < 0)
		std::cout << "Read failed" << std::endl;
	else if (res == 0)
	{
	}
	else
		Master->standardError(std::string(Buffer,res));
	return false;
}


std::vector<ProcessInfo> Processes;

bool launchProcess(ProcessController* theController,
	const std::string& Executable, const std::vector<std::string>& Args)
{
/*	std::cout << "EXEC : " << Executable;
	for (unsigned int i=0; i<Args.size(); ++i)
		std::cout << " {.} " << Args[i];
	std::cout << std::endl; */
	int tubeStandardOutput[2];
	int result = pipe(tubeStandardOutput);
	if (result < 0)
	{
		theController->failed();
		std::cerr << "Failed to create pipe stdout " << errno << std::endl;
		return false;
	}
	int tubeStandardError[2];
	result = pipe(tubeStandardError);
	if (result < 0)
	{
		close(tubeStandardOutput[0]);
		close(tubeStandardOutput[1]);
		theController->failed();
		std::cerr << "Failed to create pipe stderr " << errno << std::endl;
		return false;
	}
	pid_t pid = fork();
	if (pid < 0)
	{
		close(tubeStandardOutput[0]);
		close(tubeStandardOutput[1]);
		close(tubeStandardError[0]);
		close(tubeStandardError[1]);
		theController->failed();
		std::cerr << "Failed to fork" << std::endl;
		return false;
	}
	else if (pid)
	{
		// parent
		close(tubeStandardOutput[1]);
		close(tubeStandardError[1]);
		ProcessInfo PI;
		PI.Master = theController;
		PI.PID = pid;
		PI.StdOutput = tubeStandardOutput[0];
		PI.StdError = tubeStandardError[0];
		Processes.push_back(PI);
		return true;

	}
	else
	{
		// child
		close(tubeStandardOutput[0]);
		close(tubeStandardError[0]);
		int res = dup2(tubeStandardOutput[1],STDOUT_FILENO);
		if (res < 0)
		{
			std::cerr << "Failed to dup2 standard output" << std::endl;
			close(tubeStandardOutput[1]);
			close(tubeStandardError[1]);
			exit(1);	
		}
		res = dup2(tubeStandardError[1],STDERR_FILENO);
		if (res < 0)
		{
			std::cerr << "Failed to dup2 standard error" << std::endl;
			close(tubeStandardOutput[1]);
			close(tubeStandardError[1]);
			exit(1);	
		}
		char** args = (char**)malloc(sizeof(char*)*(Args.size()+2));
		args[0] = (char*)malloc(sizeof(char)*Executable.length()+1);
		strcpy(args[0],Executable.c_str());
		for (unsigned int i=0; i<Args.size(); ++i)
		{
			args[i+1] = (char*)malloc(sizeof(char)*Args[i].length()+1);
			strcpy(args[i+1],Args[i].c_str());
		}
		args[Args.size()+1] = 0;
		int err = execvp(Executable.c_str(),args);
		exit(42);
	};
}

unsigned int waitForProcessEvent()
{
	if (!Processes.size()) return Processes.size();
	int MaxId = 0;
	fd_set ReadSignals;
	FD_ZERO(&ReadSignals);
	for (unsigned int i=0; i<Processes.size(); ++i)
	{
		FD_SET(Processes[i].StdOutput,&ReadSignals);
		if (MaxId < Processes[i].StdOutput)
			MaxId = Processes[i].StdOutput;
		FD_SET(Processes[i].StdError,&ReadSignals);
		if (MaxId < Processes[i].StdError)
			MaxId = Processes[i].StdError;
	}
	timeval tv;
	tv.tv_sec = 1;
	tv.tv_usec = 0;
	int res = select(MaxId+1, &ReadSignals ,NULL,NULL,&tv);
	if (res < 0)
	{
		std::cout << "Select failed" << std::endl;
		return Processes.size();
	}
	for (unsigned int i=Processes.size(); i; --i)
	{
		if (FD_ISSET(Processes[i-1].StdOutput,&ReadSignals))
		{
			if (Processes[i-1].cycleStandardOutput())
			{
				Processes.erase(Processes.begin()+(i-1));
			}
		}
		if (FD_ISSET(Processes[i-1].StdError,&ReadSignals))
		{
			if (Processes[i-1].cycleStandardError())
			{
				Processes.erase(Processes.begin()+(i-1));
			}
		}
	}
	return Processes.size();
}

bool isShellRedirective(const std::string& S)
{
	return (S=="|") || (S=="<") || (S==">");
}

bool shellLaunchProcess(ProcessController* theController,
	const std::string& Executable, const std::vector<std::string>& Args)
{
	unsigned int i=0;
	for (i=0; i<Args.size(); ++i)
		if (isShellRedirective(Args[i]))
			break;
	if (i==Args.size())
		return launchProcess(theController, Executable, Args);
	std::string ShellCommand(Executable);
	for (unsigned int i=0; i<Args.size(); ++i)
	{
		ShellCommand += " ";
		ShellCommand += Args[i];
	}
	std::vector<std::string> NewArgs;
	NewArgs.push_back("-c");
	NewArgs.push_back(ShellCommand);
	return launchProcess(theController, "/bin/sh", NewArgs);
}