File: cloexec.h

package info (click to toggle)
libopenobex 1.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 972 kB
  • ctags: 1,552
  • sloc: ansic: 9,988; xml: 407; makefile: 96
file content (65 lines) | stat: -rw-r--r-- 1,890 bytes parent folder | download | duplicates (3)
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
/**
	\file cloexec.h
	close-on-exec wrapper functions.
	OpenOBEX library - Free implementation of the Object Exchange protocol.

	Copyright (c) 2010 Hendrik Sattler, All Rights Reserved.

	OpenOBEX 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 program 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 OpenOBEX. If not, see <http://www.gnu.org/>.
 */

#ifndef _WIN32
#include <sys/types.h> 
#include <sys/socket.h>
#include <fcntl.h>
static __inline void fcntl_cloexec(socket_t fd)
{
	if (fd != INVALID_SOCKET)
		(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
}
#else
static __inline void fcntl_cloexec(socket_t fd) { fd = fd; }
#endif

static __inline socket_t socket_cloexec(int domain, int type, int proto)
{
#ifdef SOCK_CLOEXEC
	return socket(domain, type | SOCK_CLOEXEC, proto);
#else
	socket_t fd = socket(domain, type, proto);
	fcntl_cloexec(fd);
	return fd;
#endif
}

/* This supports an accept4() equivalent on NetBSD 6 and later */
#if defined(SOCK_CLOEXEC) && defined(__NetBSD__)
static __inline int accept4(int s, struct sockaddr * addr, socklen_t *addrlen,
			    int flags)
{
     return paccept(s, addr, addrlen, NULL, flags);
}
#endif

static __inline socket_t accept_cloexec(socket_t sockfd, struct sockaddr *addr,
				      socklen_t *addrlen)
{
#ifdef SOCK_CLOEXEC
	return accept4(sockfd, addr, addrlen, SOCK_CLOEXEC);
#else
	socket_t fd = accept(sockfd, addr, addrlen);
	fcntl_cloexec(fd);
	return fd;
#endif
}