ObjFW  Check-in [e9984d112a]

Overview
Comment:OFProcess: Use posix_spawnp if available
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: e9984d112a78fee988d5273e92420ab1989332975945e3ebc2e7208b2c575ad0
User & Date: js on 2014-12-14 17:35:13
Other Links: manifest | tags
Context
2014-12-14
23:42
PLATFORMS.md: Add ARM64 to Linux check-in: 55a1576fcb user: js tags: trunk
17:35
OFProcess: Use posix_spawnp if available check-in: e9984d112a user: js tags: trunk
2014-12-13
17:50
Use CLOEXEC for kqueue check-in: aebd220efc user: js tags: trunk
Changes

Modified configure.ac from [af044ec26c] to [365cd4b0a2].

889
890
891
892
893
894
895
896

897
898
899
900
901

902
903

904
905
906









907
908
909
910
911
912
913
889
890
891
892
893
894
895

896
897
898
899
900

901
902

903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922







-
+




-
+

-
+



+
+
+
+
+
+
+
+
+







		have_processes="yes"
		;;
	*-*-msdosdjgpp*)
		have_processes="no"
		;;
	*)
		AC_CHECK_FUNCS([fork dup2 execvp kill _exit], [
			if test x"$ac_cv_func_fork" = x"yes" \
			AS_IF([test x"$ac_cv_func_fork" = x"yes" \
			    -a x"$ac_cv_func_pipe" = x"yes" \
			    -a x"$ac_cv_func_dup2" = x"yes" \
			    -a x"$ac_cv_func_execvp" = x"yes" \
			    -a x"$ac_cv_func_kill" = x"yes" \
			    -a x"$ac_cv_func__exit" = x"yes"; then
			    -a x"$ac_cv_func__exit" = x"yes"], [
				have_processes="yes"
			fi
			])
		], [
			break
		])

		AC_CHECK_FUNCS(posix_spawnp)

		AS_IF([test x"$ac_cv_func_posix_spawnp" = x"yes" \
		    -a x"$ac_cv_func_kill" = x"yes"], [
			have_processes="yes"

			AC_CHECK_HEADERS(spawn.h)
		])
		;;
esac
AS_IF([test x"$have_processes" = x"yes"], [
	AC_SUBST(OFPROCESS_M, "OFProcess.m")
	AC_DEFINE(OF_HAVE_PROCESSES, 1, [Whether we have processes])
])

Modified src/OFProcess.m from [7cc001dd62] to [8e7954549c].

25
26
27
28
29
30
31
32
33


34
35
36
37
38
39
40
25
26
27
28
29
30
31


32
33
34
35
36
37
38
39
40







-
-
+
+








#ifndef _WIN32
# include <unistd.h>
# include <signal.h>
# include <sys/wait.h>
#endif

#ifdef __MACH__
# include <crt_externs.h>
#ifdef HAVE_SPAWN_H
# include <spawn.h>
#endif

#import "OFProcess.h"
#import "OFString.h"
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFDataArray.h"
51
52
53
54
55
56
57
58

59
60
61
62
63
64
65
66
51
52
53
54
55
56
57

58

59
60
61
62
63
64
65







-
+
-








#ifndef __MACH__
extern char **environ;
#endif

@interface OFProcess (OF_PRIVATE_CATEGORY)
#ifndef _WIN32
- (void)OF_getArgC: (int*)argc
- (void)OF_getArgV: (char***)argv
	   andArgV: (char***)argv
    forProgramName: (OFString*)programName
      andArguments: (OFArray*)arguments;
- (char**)OF_environmentForDictionary: (OFDictionary*)dictionary;
#else
- (of_char16_t*)OF_environmentForDictionary: (OFDictionary*)dictionary;
#endif
@end
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
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







-
-
+







-
+
-


+
+
+
-
+
+
+
+

+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-

-
-
-
-
-
+
+
+
+
+

-
-
+
+

+
+
+
+
+
-
-
-
-
+
+
+
+
-
-
-







{
	self = [super init];

	@try {
#ifndef _WIN32
		void *pool = objc_autoreleasePoolPush();
		const char *path;
		int argc;
		char **argv, **env;
		char **argv;

		if (pipe(_readPipe) != 0 || pipe(_writePipe) != 0)
			@throw [OFInitializationFailedException
			    exceptionWithClass: [self class]];

		path = [program cStringWithEncoding:
		    [OFSystemInfo native8BitEncoding]];
		[self OF_getArgC: &argc
		[self OF_getArgV: &argv
			 andArgV: &argv
		  forProgramName: programName
		    andArguments: arguments];

		@try {
			char **env = [self
		env = [self OF_environmentForDictionary: environment];
			    OF_environmentForDictionary: environment];
# ifdef HAVE_POSIX_SPAWNP
			posix_spawn_file_actions_t actions;
			posix_spawnattr_t attr;

			if (posix_spawn_file_actions_init(&actions) != 0)
				@throw [OFInitializationFailedException
				    exceptionWithClass: [self class]];
		if ((_pid = fork()) == 0) {
#ifdef __MACH__
			*_NSGetEnviron() = env;
#else
			environ = env;

			if (posix_spawnattr_init(&attr) != 0) {
				posix_spawn_file_actions_destroy(&actions);

				@throw [OFInitializationFailedException
				    exceptionWithClass: [self class]];
			}

			@try {
				if (posix_spawn_file_actions_addclose(&actions,
				    _readPipe[0]) != 0 ||
				    posix_spawn_file_actions_addclose(&actions,
				    _writePipe[1]) != 0 ||
				    posix_spawn_file_actions_adddup2(&actions,
				    _writePipe[0], 0) != 0 ||
				    posix_spawn_file_actions_adddup2(&actions,
				    _readPipe[1], 1) != 0)
					@throw [OFInitializationFailedException
					    exceptionWithClass: [self class]];

#  ifdef POSIX_SPAWN_CLOEXEC_DEFAULT
				if (posix_spawnattr_setflags(&attr,
				    POSIX_SPAWN_CLOEXEC_DEFAULT) != 0)
					@throw [OFInitializationFailedException
					    exceptionWithClass: [self class]];
#  endif

				if (posix_spawnp(&_pid, path, &actions, &attr,
				    argv, env) != 0)
					@throw [OFInitializationFailedException
					    exceptionWithClass: [self class]];
			} @finally {
				posix_spawn_file_actions_destroy(&actions);
				posix_spawnattr_destroy(&attr);
			}
# else
			if ((_pid = fork()) == 0) {
				environ = env;
#endif

			close(_readPipe[0]);
			close(_writePipe[1]);
			dup2(_writePipe[0], 0);
			dup2(_readPipe[1], 1);
			execvp(path, argv);
				close(_readPipe[0]);
				close(_writePipe[1]);
				dup2(_writePipe[0], 0);
				dup2(_readPipe[1], 1);
				execvp(path, argv);

			_exit(EXIT_FAILURE);
		}
				_exit(EXIT_FAILURE);
			}

			if (_pid == -1)
				@throw [OFInitializationFailedException
				    exceptionWithClass: [self class]];
# endif
		} @finally {
		close(_readPipe[1]);
		close(_writePipe[0]);
		[self freeMemory: argv];

			close(_readPipe[1]);
			close(_writePipe[0]);
			[self freeMemory: argv];
		}
		if (_pid == -1)
			@throw [OFInitializationFailedException
			    exceptionWithClass: [self class]];

		objc_autoreleasePoolPop(pool);
#else
		SECURITY_ATTRIBUTES sa;
		PROCESS_INFORMATION pi;
		STARTUPINFOW si;
		void *pool;
295
296
297
298
299
300
301
302

303
304
305
306
307
308
309
310
335
336
337
338
339
340
341

342

343
344
345
346
347
348
349







-
+
-







{
	[self close];

	[super dealloc];
}

#ifndef _WIN32
- (void)OF_getArgC: (int*)argc
- (void)OF_getArgV: (char***)argv
	   andArgV: (char***)argv
    forProgramName: (OFString*)programName
      andArguments: (OFArray*)arguments
{
	OFString *const *objects = [arguments objects];
	size_t i, count = [arguments count];
	of_string_encoding_t encoding;