File: childprocess.cpp

package info (click to toggle)
libwibble 0.1.9
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 500 kB
  • ctags: 1,183
  • sloc: cpp: 5,760; sh: 113; makefile: 71
file content (256 lines) | stat: -rw-r--r-- 7,063 bytes parent folder | download
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * OO base class for process functions and child processes
 *
 * Copyright (C) 2003--2006  Enrico Zini <enrico@debian.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 */

#include <wibble/sys/childprocess.h>

#include <sys/types.h>		// fork, waitpid, kill, open, getpw*, getgr*, initgroups
#include <sys/stat.h>		// open
#include <sys/resource.h>	// getrlimit, setrlimit
#include <unistd.h>			// fork, dup2, pipe, close, setsid, _exit, chdir
#include <fcntl.h>			// open
#include <sys/wait.h>		// waitpid
#include <signal.h>			// kill
#include <stdio.h>			// flockfile, funlockfile
#include <ctype.h>			// is*
#include <pwd.h>			// getpw*
#include <grp.h>			// getgr*, initgroups
#include <errno.h>

#include <sstream>

namespace wibble {
namespace sys {

using namespace std;

pid_t ChildProcess::fork()
{
	flockfile(stdin);
	flockfile(stdout);
	flockfile(stderr);

	pid_t pid;
	if ((pid = ::fork()) == 0)
	{
		// Tell the logging system we're in a new process
		//Log::Logger::instance()->setupForkedChild();

		// Child process
		try {
			// no need to funlockfile here, since the library resets the stream
			// locks in the child after a fork

			// Call the process main function and return the exit status
			_exit(main());
		} catch (std::exception& e) {
			//log_err(string(e.type()) + ": " + e.desc());
		}
		_exit(EXIT_FAILURE);
	} else if (pid < 0) {
		funlockfile(stdin);
		funlockfile(stderr);
		funlockfile(stdout);
		throw wibble::exception::System("trying to fork the child process to run action script");
	} else {
		funlockfile(stdin);
		funlockfile(stderr);
		funlockfile(stdout);

		// Parent process
		return _pid = pid;
	}
}

pid_t ChildProcess::forkAndRedirect(int* stdinfd, int* stdoutfd, int* stderrfd)
{
	int pipes[3][2];

	if (stdinfd)
	{
		if (pipe(pipes[0]) == -1)
			throw wibble::exception::System("trying to create the pipe to connect to child standard input");
		*stdinfd = pipes[0][1];
	}
	if (stdoutfd)
	{
		if (pipe(pipes[1]) == -1)
			throw wibble::exception::System("trying to create the pipe to connect to child standard output");
		*stdoutfd = pipes[1][0];
		if (stderrfd == stdoutfd)
			*stderrfd = pipes[1][0];
	}
	
	if (stderrfd && stderrfd != stdoutfd)
	{
		if (pipe(pipes[2]) == -1)
			throw wibble::exception::System("trying to create the pipe to connect to child standard error");
		*stderrfd = pipes[2][0];
	}

	flockfile(stdin);
	flockfile(stdout);
	flockfile(stderr);

	pid_t pid;
	if ((pid = ::fork()) == 0)
	{
		// Tell the logging system we're in a new process
		//Log::Logger::instance()->setupForkedChild();

		// Child process
		try {
			if (stdinfd)
			{
				// Redirect input from the parent to stdin
				if (close(pipes[0][1]) == -1)
					throw wibble::exception::System("closing write end of parent stdin pipe");
				if (dup2(pipes[0][0], 0) == -1)
					throw wibble::exception::System("dup2-ing parent stdin pipe to stdin");
				if (close(pipes[0][0]) == -1)
					throw wibble::exception::System("closing original read end of parent stdin pipe");
			}

			if (stdoutfd)
			{
				// Redirect output to the parent stdout fd
				if (close(pipes[1][0]) == -1)
					throw wibble::exception::System("closing read end of parent stdout pipe");
				if (dup2(pipes[1][1], 1) == -1)
					throw wibble::exception::System("dup2-ing stdout to parent stdout pipe");
				if (stderrfd == stdoutfd)
					if (dup2(pipes[1][1], 2) == -1)
						throw wibble::exception::System("dup2-ing stderr to parent stdout/stderr pipe");
				if (close(pipes[1][1]) == -1)
					throw wibble::exception::System("closing original write end of parent stdout pipe");
			}

			if (stderrfd && stderrfd != stdoutfd)
			{
				// Redirect all output to the parent
				if (close(pipes[2][0]) == -1)
					throw wibble::exception::System("closing read end of parent stderr pipe");
				if (dup2(pipes[2][1], 2) == -1)
					throw wibble::exception::System("dup2-ing stderr to parent stderr pipe");
				if (close(pipes[2][1]) == -1)
					throw wibble::exception::System("closing original write end of parent stderr pipe");
			}

			// Call the process main function and return the exit status
			_exit(main());
		} catch (std::exception& e) {
			//log_err(string(e.type()) + ": " + e.desc());
		}
		_exit(EXIT_FAILURE);
	} else if (pid < 0) {
		funlockfile(stdin);
		funlockfile(stderr);
		funlockfile(stdout);
		if (stdinfd)
		{
			close(pipes[0][0]);
			close(pipes[0][1]);
		}
		if (stdoutfd)
		{
			close(pipes[1][0]);
			close(pipes[1][1]);
		}
		if (stderrfd && stderrfd != stdoutfd)
		{
			close(pipes[2][0]);
			close(pipes[2][1]);
		}
		throw wibble::exception::System("trying to fork the child process to run action script");
	} else {
		funlockfile(stdin);
		funlockfile(stderr);
		funlockfile(stdout);

		// Parent process
		_pid = pid;
		try {
			if (stdinfd)
				if (close(pipes[0][0]) == -1)
					throw wibble::exception::System("closing read end of stdin child pipe");
			if (stdoutfd)
				if (close(pipes[1][1]) == -1)
					throw wibble::exception::System("closing write end of stdout child pipe");
			if (stderrfd && stderrfd != stdoutfd)
				if (close(pipes[2][1]) == -1)
					throw wibble::exception::System("closing write end of stderr child pipe");
			return pid;
		} catch (wibble::exception::System& e) {
			// Try to kill the child process if any errors occurs here
			::kill(pid, 15);
			throw e;
		}
	}
}

int ChildProcess::wait()
{
	if (_pid == -1)
	{
		//log_debug("Child already finished");
		return -1;		// FIXME: for lack of better ideas
	}

	int status;
	if (waitpid(_pid, &status, 0) == -1)
		if (errno == EINTR)
			throw wibble::exception::Interrupted("waiting for child termination");
		else
			throw wibble::exception::System("waiting for child termination");
	_pid = -1;
	return status;
}

int ChildProcess::wait(struct rusage* ru)
{
	if (_pid == -1)
	{
		//log_debug("Child already finished");
		return -1;		// FIXME: for lack of better ideas
	}

	int status;
	if (wait4(_pid, &status, 0, ru) == -1)
		if (errno == EINTR)
			throw wibble::exception::Interrupted("waiting for child termination");
		else
			throw wibble::exception::System("waiting for child termination");
	_pid = -1;
	return status;
}

void ChildProcess::kill(int signal)
{
	if (::kill(_pid, signal) == -1)
	{
		stringstream str;
		str << "killing process " + _pid;
		throw wibble::exception::System(str.str());
	}
}

}
}

// vim:set ts=4 sw=4: