ObjFW  Diff

Differences From Artifact [b53baca85e]:

To Artifact [be45f52a11]:


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
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

-
-
-
+


















-
+















-
+




-
-
+
+




-
-
+
+





-
-
-
+
+
+






-
-
-
-
+
+
+
+







/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
 *               2018, 2019, 2020
 *   Jonathan Schleifer <js@nil.im>
 * Copyright (c) 2008-2021 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <errno.h>
#include <string.h>

#import "OFProcess.h"
#import "OFSubprocess.h"
#import "OFArray.h"
#import "OFData.h"
#import "OFDictionary.h"
#import "OFLocale.h"
#import "OFString.h"
#import "OFSystemInfo.h"

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

#include <windows.h>

@interface OFProcess ()
@interface OFSubprocess ()
- (of_char16_t *)of_wideEnvironmentForDictionary: (OFDictionary *)dictionary;
- (char *)of_environmentForDictionary: (OFDictionary *)environment;
@end

@implementation OFProcess
+ (instancetype)processWithProgram: (OFString *)program
@implementation OFSubprocess
+ (instancetype)subprocessWithProgram: (OFString *)program
{
	return [[[self alloc] initWithProgram: program] autorelease];
}

+ (instancetype)processWithProgram: (OFString *)program
			 arguments: (OFArray *)arguments
+ (instancetype)subprocessWithProgram: (OFString *)program
			    arguments: (OFArray *)arguments
{
	return [[[self alloc] initWithProgram: program
				    arguments: arguments] autorelease];
}

+ (instancetype)processWithProgram: (OFString *)program
		       programName: (OFString *)programName
			 arguments: (OFArray *)arguments
+ (instancetype)subprocessWithProgram: (OFString *)program
			  programName: (OFString *)programName
			    arguments: (OFArray *)arguments
{
	return [[[self alloc] initWithProgram: program
				  programName: programName
				    arguments: arguments] autorelease];
}

+ (instancetype)processWithProgram: (OFString *)program
		       programName: (OFString *)programName
			 arguments: (OFArray *)arguments
		       environment: (OFDictionary *)environment
+ (instancetype)subprocessWithProgram: (OFString *)program
			  programName: (OFString *)programName
			    arguments: (OFArray *)arguments
			  environment: (OFDictionary *)environment
{
	return [[[self alloc] initWithProgram: program
				  programName: programName
				    arguments: arguments
				  environment: environment] autorelease];
}

115
116
117
118
119
120
121
122

123
124
125
126
127
128
129
113
114
115
116
117
118
119

120
121
122
123
124
125
126
127







-
+








	@try {
		SECURITY_ATTRIBUTES sa;
		PROCESS_INFORMATION pi;
		void *pool;
		OFMutableString *argumentsString;

		_process = INVALID_HANDLE_VALUE;
		_handle = INVALID_HANDLE_VALUE;
		_readPipe[0] = _writePipe[1] = NULL;

		sa.nLength = sizeof(sa);
		sa.bInheritHandle = TRUE;
		sa.lpSecurityDescriptor = NULL;

		if (!CreatePipe(&_readPipe[0], &_readPipe[1], &sa, 0))
228
229
230
231
232
233
234
235

236
237
238
239
240
241
242
226
227
228
229
230
231
232

233
234
235
236
237
238
239
240







-
+







			    NULL, &si, &pi))
				@throw [OFInitializationFailedException
				    exceptionWithClass: self.class];
		}

		objc_autoreleasePoolPop(pool);

		_process = pi.hProcess;
		_handle = pi.hProcess;
		CloseHandle(pi.hThread);

		CloseHandle(_readPipe[1]);
		CloseHandle(_writePipe[0]);
	} @catch (id e) {
		[self release];
		@throw e;
266
267
268
269
270
271
272
273

274
275

276
277
278
279

280
281
282

283
284
285
286
287
288
289
290
264
265
266
267
268
269
270

271


272

273
274

275

276

277

278
279
280
281
282
283
284







-
+
-
-
+
-


-
+
-

-
+
-








	env = [OFMutableData dataWithItemSize: sizeof(of_char16_t)];

	keyEnumerator = [environment keyEnumerator];
	objectEnumerator = [environment objectEnumerator];
	while ((key = [keyEnumerator nextObject]) != nil &&
	    (object = [objectEnumerator nextObject]) != nil) {
		[env addItems: key.UTF16String
		[env addItems: key.UTF16String count: key.UTF16StringLength];
			count: key.UTF16StringLength];
		[env addItems: &equal
		[env addItems: &equal count: 1];
			count: 1];
		[env addItems: object.UTF16String
			count: object.UTF16StringLength];
		[env addItems: &zero
		[env addItems: &zero count: 1];
			count: 1];
	}
	[env addItems: zero
	[env addItems: zero count: 2];
		count: 2];

	return env.mutableItems;
}

- (char *)of_environmentForDictionary: (OFDictionary *)environment
{
	of_string_encoding_t encoding = [OFLocale encoding];
299
300
301
302
303
304
305
306

307
308
309
310

311
312
313

314
315
316
317
318
319
320
321
322
323
324
325
326
327

328
329
330
331
332
333
334
335
293
294
295
296
297
298
299

300

301
302

303

304

305

306
307
308
309
310
311
312
313
314
315
316
317

318

319
320
321
322
323
324
325







-
+
-


-
+
-

-
+
-












-
+
-








	keyEnumerator = [environment keyEnumerator];
	objectEnumerator = [environment objectEnumerator];
	while ((key = [keyEnumerator nextObject]) != nil &&
	    (object = [objectEnumerator nextObject]) != nil) {
		[env addItems: [key cStringWithEncoding: encoding]
			count: [key cStringLengthWithEncoding: encoding]];
		[env addItems: "="
		[env addItems: "=" count: 1];
			count: 1];
		[env addItems: [object cStringWithEncoding: encoding]
			count: [object cStringLengthWithEncoding: encoding]];
		[env addItems: ""
		[env addItems: "" count: 1];
			count: 1];
	}
	[env addItems: "\0"
	[env addItems: "\0" count: 2];
		count: 2];

	return env.mutableItems;
}

- (bool)lowlevelIsAtEndOfStream
{
	if (_readPipe[0] == NULL)
		@throw [OFNotOpenException exceptionWithObject: self];

	return _atEndOfStream;
}

- (size_t)lowlevelReadIntoBuffer: (void *)buffer
- (size_t)lowlevelReadIntoBuffer: (void *)buffer length: (size_t)length
			  length: (size_t)length
{
	DWORD ret;

	if (length > UINT32_MAX)
		@throw [OFOutOfRangeException exception];

	if (_readPipe[0] == NULL)
348
349
350
351
352
353
354
355

356
357
358
359
360
361
362
363
338
339
340
341
342
343
344

345

346
347
348
349
350
351
352







-
+
-








	if (ret == 0)
		_atEndOfStream = true;

	return ret;
}

- (size_t)lowlevelWriteBuffer: (const void *)buffer
- (size_t)lowlevelWriteBuffer: (const void *)buffer length: (size_t)length
		       length: (size_t)length
{
	DWORD bytesWritten;

	if (length > UINT32_MAX)
		@throw [OFOutOfRangeException exception];

	if (_writePipe[1] == NULL)
391
392
393
394
395
396
397
398
399
400



401
402
403

404
405
406
407
408
409
410
411
412
413
414

415
416
417

418
419

420
421
422
423
424
425


426
427
428
429
430
380
381
382
383
384
385
386



387
388
389
390
391

392
393
394
395
396
397
398
399
400
401
402

403
404
405

406
407

408
409
410
411
412


413
414
415
416
417
418
419







-
-
-
+
+
+


-
+










-
+


-
+

-
+




-
-
+
+





{
	if (_readPipe[0] == NULL)
		@throw [OFNotOpenException exceptionWithObject: self];

	[self closeForWriting];
	CloseHandle(_readPipe[0]);

	if (_process != INVALID_HANDLE_VALUE) {
		TerminateProcess(_process, 0);
		CloseHandle(_process);
	if (_handle != INVALID_HANDLE_VALUE) {
		TerminateProcess(_handle, 0);
		CloseHandle(_handle);
	}

	_process = INVALID_HANDLE_VALUE;
	_handle = INVALID_HANDLE_VALUE;
	_readPipe[0] = NULL;

	[super close];
}

- (int)waitForTermination
{
	if (_readPipe[0] == NULL)
		@throw [OFNotOpenException exceptionWithObject: self];

	if (_process != INVALID_HANDLE_VALUE) {
	if (_handle != INVALID_HANDLE_VALUE) {
		DWORD exitCode;

		WaitForSingleObject(_process, INFINITE);
		WaitForSingleObject(_handle, INFINITE);

		if (GetExitCodeProcess(_process, &exitCode))
		if (GetExitCodeProcess(_handle, &exitCode))
			_status = exitCode;
		else
			_status = GetLastError();

		CloseHandle(_process);
		_process = INVALID_HANDLE_VALUE;
		CloseHandle(_handle);
		_handle = INVALID_HANDLE_VALUE;
	}

	return _status;
}
@end