ObjFW  Check-in [06d2de4571]

Overview
Comment:Use CLOEXEC for files and sockets
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 06d2de457124884edb04da15fef6bffc27827e14a2a3884b74333b21dbbe99aa
User & Date: js on 2014-12-13 16:52:58
Other Links: manifest | tags
Context
2014-12-13
16:53
Fix compilation with GCC check-in: 12ceeb7853 user: js tags: trunk
16:52
Use CLOEXEC for files and sockets check-in: 06d2de4571 user: js tags: trunk
2014-12-07
18:19
Fix compilation on Win32 check-in: 258b45b81a user: js tags: trunk
Changes

Modified configure.ac from [6164418947] to [673fd6e03c].

733
734
735
736
737
738
739


740
741
742
743
744
745
746
			[Whether we have sys/socket.h])
	])
	AC_CHECK_HEADERS(netinet/in.h, [
		AC_DEFINE(OF_HAVE_NETINET_IN_H, 1,
			[Whether we have netinet/in.h])
	])
	AC_CHECK_HEADERS([arpa/inet.h netdb.h])



	AC_CHECK_FUNC(kqueue, [
		AC_DEFINE(HAVE_KQUEUE, 1, [Whether we have kqueue])
		AC_SUBST(OFKERNELEVENTOBSERVER_KQUEUE_M,
			"OFKernelEventObserver_kqueue.m")
	])
	AC_CHECK_HEADER(poll.h, [







>
>







733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
			[Whether we have sys/socket.h])
	])
	AC_CHECK_HEADERS(netinet/in.h, [
		AC_DEFINE(OF_HAVE_NETINET_IN_H, 1,
			[Whether we have netinet/in.h])
	])
	AC_CHECK_HEADERS([arpa/inet.h netdb.h])

	AC_CHECK_FUNCS([paccept accept4])

	AC_CHECK_FUNC(kqueue, [
		AC_DEFINE(HAVE_KQUEUE, 1, [Whether we have kqueue])
		AC_SUBST(OFKERNELEVENTOBSERVER_KQUEUE_M,
			"OFKernelEventObserver_kqueue.m")
	])
	AC_CHECK_HEADER(poll.h, [

Modified src/OFFile.m from [aaab12c640] to [752c197aa2].

89
90
91
92
93
94
95



96
97
98
99
100
101
102
# include <windows.h>
# include <direct.h>
#endif

#ifndef O_BINARY
# define O_BINARY 0
#endif



#ifndef O_EXLOCK
# define O_EXLOCK 0
#endif

#ifndef S_IRGRP
# define S_IRGRP 0
#endif







>
>
>







89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# include <windows.h>
# include <direct.h>
#endif

#ifndef O_BINARY
# define O_BINARY 0
#endif
#ifndef O_CLOEXEC
# define O_CLOEXEC 0
#endif
#ifndef O_EXLOCK
# define O_EXLOCK 0
#endif

#ifndef S_IRGRP
# define S_IRGRP 0
#endif
886
887
888
889
890
891
892


893
894
895
896
897
898
899
	self = [super init];

	@try {
		int flags;

		if ((flags = parseMode([mode UTF8String])) == -1)
			@throw [OFInvalidArgumentException exception];



#ifndef _WIN32
		if ((_fd = open([path cStringWithEncoding: [OFSystemInfo
		    native8BitEncoding]], flags, DEFAULT_MODE)) == -1)
#else
		if ((_fd = _wopen([path UTF16String], flags,
		    DEFAULT_MODE)) == -1)







>
>







889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
	self = [super init];

	@try {
		int flags;

		if ((flags = parseMode([mode UTF8String])) == -1)
			@throw [OFInvalidArgumentException exception];

		flags |= O_CLOEXEC;

#ifndef _WIN32
		if ((_fd = open([path cStringWithEncoding: [OFSystemInfo
		    native8BitEncoding]], flags, DEFAULT_MODE)) == -1)
#else
		if ((_fd = _wopen([path UTF16String], flags,
		    DEFAULT_MODE)) == -1)

Modified src/OFTCPSocket.m from [e59925afa4] to [c059520ca3].

286
287
288
289
290
291
292



293
294

295
296





297
298
299
300
301
302
303
		port = _SOCKS5Port;
	}

	results = of_resolve_host(host, port, SOCK_STREAM);

	for (iter = results; *iter != NULL; iter++) {
		of_resolver_result_t *result = *iter;




		if ((_socket = socket(result->family, result->type,

		    result->protocol)) == INVALID_SOCKET)
			continue;






		if (connect(_socket, result->address,
		    result->addressLength) == -1) {
			close(_socket);
			_socket = INVALID_SOCKET;
			continue;
		}







>
>
>

|
>


>
>
>
>
>







286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
		port = _SOCKS5Port;
	}

	results = of_resolve_host(host, port, SOCK_STREAM);

	for (iter = results; *iter != NULL; iter++) {
		of_resolver_result_t *result = *iter;
#if SOCK_CLOEXEC == 0
		int flags;
#endif

		if ((_socket = socket(result->family,
		    result->type | SOCK_CLOEXEC,
		    result->protocol)) == INVALID_SOCKET)
			continue;

#if SOCK_CLOEXEC == 0
		if ((flags = fcntl(_socket, F_GETFD, 0)) != -1)
			fcntl(_socket, F_SETFD, flags | FD_CLOEXEC);
#endif

		if (connect(_socket, result->address,
		    result->addressLength) == -1) {
			close(_socket);
			_socket = INVALID_SOCKET;
			continue;
		}
381
382
383
384
385
386
387




388

389
390
391
392





393
394
395
396
397
398
399
#ifdef __wii__
	if (port == 0)
		port = freePort--;
#endif

	results = of_resolve_host(host, port, SOCK_STREAM);
	@try {




		if ((_socket = socket(results[0]->family, results[0]->type,

		    results[0]->protocol)) == INVALID_SOCKET)
			@throw [OFBindFailedException exceptionWithHost: host
								   port: port
								 socket: self];






		if (setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR,
		    (const char*)&one, (socklen_t)sizeof(one)))
			@throw [OFSetOptionFailedException
			    exceptionWithStream: self];

		if (bind(_socket, results[0]->address,







>
>
>
>
|
>




>
>
>
>
>







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
#ifdef __wii__
	if (port == 0)
		port = freePort--;
#endif

	results = of_resolve_host(host, port, SOCK_STREAM);
	@try {
#if SOCK_CLOEXEC == 0
		int flags;
#endif

		if ((_socket = socket(results[0]->family,
		    results[0]->type | SOCK_CLOEXEC,
		    results[0]->protocol)) == INVALID_SOCKET)
			@throw [OFBindFailedException exceptionWithHost: host
								   port: port
								 socket: self];

#if SOCK_CLOEXEC == 0
		if ((flags = fcntl(_socket, F_GETFD, 0)) != -1)
			fcntl(_socket, F_SETFD, flags | FD_CLOEXEC);
#endif

		if (setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR,
		    (const char*)&one, (socklen_t)sizeof(one)))
			@throw [OFSetOptionFailedException
			    exceptionWithStream: self];

		if (bind(_socket, results[0]->address,
452
453
454
455
456
457
458



459
460
461
462
463









464
465
466




467
468
469
470
471
472
473

	_listening = true;
}

- (instancetype)accept
{
	OFTCPSocket *client = [[[[self class] alloc] init] autorelease];




	client->_address = [client
	    allocMemoryWithSize: sizeof(struct sockaddr_storage)];
	client->_addressLength = (socklen_t)sizeof(struct sockaddr_storage);










	if ((client->_socket = accept(_socket, client->_address,
	   &client->_addressLength)) == INVALID_SOCKET)
		@throw [OFAcceptFailedException exceptionWithSocket: self];





	assert(client->_addressLength <= sizeof(struct sockaddr_storage));

	if (client->_addressLength != sizeof(struct sockaddr_storage)) {
		@try {
			client->_address = [client
			    resizeMemory: client->_address







>
>
>





>
>
>
>
>
>
>
>
>



>
>
>
>







471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508

	_listening = true;
}

- (instancetype)accept
{
	OFTCPSocket *client = [[[[self class] alloc] init] autorelease];
#if (!defined(HAVE_PACCEPT) && !defined(HAVE_ACCEPT4)) || !defined(SOCK_CLOEXEC)
	int flags;
#endif

	client->_address = [client
	    allocMemoryWithSize: sizeof(struct sockaddr_storage)];
	client->_addressLength = (socklen_t)sizeof(struct sockaddr_storage);

#if defined(HAVE_PACCEPT) && defined(SOCK_CLOEXEC)
	if ((client->_socket = paccept(_socket, client->_address,
	   &client->_addressLength, NULL, SOCK_CLOEXEC)) == INVALID_SOCKET)
		@throw [OFAcceptFailedException exceptionWithSocket: self];
#elif defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
	if ((client->_socket = accept4(_socket, client->_address,
	   &client->_addressLength, SOCK_CLOEXEC)) == INVALID_SOCKET)
		@throw [OFAcceptFailedException exceptionWithSocket: self];
#else
	if ((client->_socket = accept(_socket, client->_address,
	   &client->_addressLength)) == INVALID_SOCKET)
		@throw [OFAcceptFailedException exceptionWithSocket: self];

	if ((flags = fcntl(client->_socket, F_GETFD, 0)) != -1)
		fcntl(client->_socket, F_SETFD, flags | FD_CLOEXEC);
#endif

	assert(client->_addressLength <= sizeof(struct sockaddr_storage));

	if (client->_addressLength != sizeof(struct sockaddr_storage)) {
		@try {
			client->_address = [client
			    resizeMemory: client->_address

Modified src/OFUDPSocket.m from [2b1705fa68] to [a6f8078486].

391
392
393
394
395
396
397




398

399
400
401
402





403
404
405
406
407
408
409
#ifdef __wii__
	if (port == 0)
		port = freePort--;
#endif

	results = of_resolve_host(host, port, SOCK_DGRAM);
	@try {




		if ((_socket = socket(results[0]->family, results[0]->type,

		    results[0]->protocol)) == INVALID_SOCKET)
			@throw [OFBindFailedException exceptionWithHost: host
								   port: port
								 socket: self];






		if (bind(_socket, results[0]->address,
		    results[0]->addressLength) == -1) {
			close(_socket);
			_socket = INVALID_SOCKET;
			@throw [OFBindFailedException exceptionWithHost: host
								   port: port







>
>
>
>
|
>




>
>
>
>
>







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
#ifdef __wii__
	if (port == 0)
		port = freePort--;
#endif

	results = of_resolve_host(host, port, SOCK_DGRAM);
	@try {
#if SOCK_CLOEXEC == 0
		int flags;
#endif

		if ((_socket = socket(results[0]->family,
		    results[0]->type | SOCK_CLOEXEC,
		    results[0]->protocol)) == INVALID_SOCKET)
			@throw [OFBindFailedException exceptionWithHost: host
								   port: port
								 socket: self];

#if SOCK_CLOEXEC == 0
		if ((flags = fcntl(_socket, F_GETFD, 0)) != -1)
			fcntl(_socket, F_SETFD, flags | FD_CLOEXEC);
#endif

		if (bind(_socket, results[0]->address,
		    results[0]->addressLength) == -1) {
			close(_socket);
			_socket = INVALID_SOCKET;
			@throw [OFBindFailedException exceptionWithHost: host
								   port: port

Modified src/socket.h from [bc099d612b] to [57c57cf50c].

17
18
19
20
21
22
23


24
25
26
27
28
29
30
#import "objfw-defs.h"

#ifndef OF_HAVE_SOCKETS
# error No sockets available!
#endif

#include <stdbool.h>



#ifdef OF_HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef OF_HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif







>
>







17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#import "objfw-defs.h"

#ifndef OF_HAVE_SOCKETS
# error No sockets available!
#endif

#include <stdbool.h>

#include <fcntl.h>

#ifdef OF_HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef OF_HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif

Modified src/socket_helpers.h from [698be452dd] to [4282bb4929].

44
45
46
47
48
49
50




51
52
53
54
55
56
57
#ifndef INADDR_NONE
# define INADDR_NONE ((in_addr_t)-1)
#endif

#ifndef SOMAXCONN
# define SOMAXCONN 32
#endif





#ifdef _WIN32
# define close(sock) closesocket(sock)
#endif

#ifdef _PSP
/* PSP defines AF_INET6, even though sockaddr_in6 is missing */







>
>
>
>







44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef INADDR_NONE
# define INADDR_NONE ((in_addr_t)-1)
#endif

#ifndef SOMAXCONN
# define SOMAXCONN 32
#endif

#ifndef SOCK_CLOEXEC
# define SOCK_CLOEXEC 0
#endif

#ifdef _WIN32
# define close(sock) closesocket(sock)
#endif

#ifdef _PSP
/* PSP defines AF_INET6, even though sockaddr_in6 is missing */