ObjFW  Check-in [20ba008347]

Overview
Comment:OFProcess: Make sure that we don't leave zombies behind.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 20ba008347110efe6f34f6f824f866bd34f60439fc8b46607dd89a1f33abfacb
User & Date: js on 2011-10-06 00:10:17
Other Links: manifest | tags
Context
2011-10-06
00:11
Add forgotten copyright. check-in: 85ba47f0ea user: js tags: trunk
00:10
OFProcess: Make sure that we don't leave zombies behind. check-in: 20ba008347 user: js tags: trunk
2011-10-05
23:26
Add OFProcess. check-in: 02ab9aa8a9 user: js tags: trunk
Changes

Modified src/OFProcess.h from [a96b274fb8] to [32c2fe9e1e].



1
2
3
4
5
6
7

8

9
10
11
12
13
14
15


#import "OFStream.h"

/**
 * \brief A class for stream-like communication with a newly created process.
 */
@interface OFProcess: OFStream
{

	int readPipe[2], writePipe[2];

	BOOL atEndOfStream;
}

/**
 * \brief Creates a new OFProcess with the specified program, program name and
 *	  arguments and invokes the program.
 *
>
>







>

>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <sys/types.h>

#import "OFStream.h"

/**
 * \brief A class for stream-like communication with a newly created process.
 */
@interface OFProcess: OFStream
{
	pid_t pid;
	int readPipe[2], writePipe[2];
	int status;
	BOOL atEndOfStream;
}

/**
 * \brief Creates a new OFProcess with the specified program, program name and
 *	  arguments and invokes the program.
 *

Modified src/OFProcess.m from [a1d37f278e] to [12b36ce01a].

1
2
3
4
5


6
7
8
9
10
11
12
#include "config.h"

#include <alloca.h>
#include <unistd.h>



#import "OFProcess.h"
#import "OFString.h"
#import "OFArray.h"

#import "OFInitializationFailedException.h"
#import "OFReadFailedException.h"
#import "OFWriteFailedException.h"





>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "config.h"

#include <alloca.h>
#include <unistd.h>

#include <sys/wait.h>

#import "OFProcess.h"
#import "OFString.h"
#import "OFArray.h"

#import "OFInitializationFailedException.h"
#import "OFReadFailedException.h"
#import "OFWriteFailedException.h"
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
	self = [super init];

	@try {
		if (pipe(readPipe) != 0 || pipe(writePipe) != 0)
			@throw [OFInitializationFailedException
			    exceptionWithClass: isa];

		switch (fork()) {
		case 0:;
			OFString **cArray = [arguments cArray];
			size_t i, count = [arguments count];
			char **argv = alloca((count + 2) * sizeof(char*));

			argv[0] = (char*)[programName cStringWithEncoding:
			    OF_STRING_ENCODING_NATIVE];







|







30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
	self = [super init];

	@try {
		if (pipe(readPipe) != 0 || pipe(writePipe) != 0)
			@throw [OFInitializationFailedException
			    exceptionWithClass: isa];

		switch ((pid = fork())) {
		case 0:;
			OFString **cArray = [arguments cArray];
			size_t i, count = [arguments count];
			char **argv = alloca((count + 2) * sizeof(char*));

			argv[0] = (char*)[programName cStringWithEncoding:
			    OF_STRING_ENCODING_NATIVE];
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
		@throw [OFWriteFailedException exceptionWithClass: isa
							   stream: self
						  requestedLength: length];
}

- (void)dealloc
{
	if (readPipe[0] != -1)
		close(readPipe[0]);
	if (writePipe[1] != -1)
		close(writePipe[1]);

	[super dealloc];
}

/*
 * FIXME: Add -[fileDescriptor]. The problem is that we have two FDs, which is
 *	  not yet supported by OFStreamObserver. This has to be split into one
 *	  FD for reading and one for writing.
 */

- (void)close
{
	if (readPipe[0] != -1)
		close(readPipe[0]);
	if (writePipe[1] != -1)
		close(writePipe[1]);





	readPipe[0] = -1;
	writePipe[1] = -1;
}
@end







<
|
<
<

















>
>
>
>




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
		@throw [OFWriteFailedException exceptionWithClass: isa
							   stream: self
						  requestedLength: length];
}

- (void)dealloc
{

	[self close];



	[super dealloc];
}

/*
 * FIXME: Add -[fileDescriptor]. The problem is that we have two FDs, which is
 *	  not yet supported by OFStreamObserver. This has to be split into one
 *	  FD for reading and one for writing.
 */

- (void)close
{
	if (readPipe[0] != -1)
		close(readPipe[0]);
	if (writePipe[1] != -1)
		close(writePipe[1]);

	if (pid != -1)
		waitpid(pid, &status, WNOHANG);

	pid = -1;
	readPipe[0] = -1;
	writePipe[1] = -1;
}
@end