File: socketwrapper.h

package info (click to toggle)
libreswan 4.3-1%2Bdeb11u4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 62,688 kB
  • sloc: ansic: 108,293; sh: 25,973; xml: 11,756; python: 10,230; makefile: 1,580; javascript: 1,353; yacc: 825; sed: 647; perl: 584; lex: 159; awk: 156
file content (37 lines) | stat: -rw-r--r-- 700 bytes parent folder | download | duplicates (2)
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
#ifndef _SOCKET_WRAPPER_H_
#define _SOCKET_WRAPPER_H_

#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>

/*
 * This wrapper ensures that we close all file descriptors on exec.
 */
#ifdef FD_CLOEXEC
static inline int safe_socket(int domain, int type, int protocol)
{
	int fd = socket(domain, type, protocol);

	if (fd >= 0) {
		int arg = fcntl(fd, F_GETFD);

		if (arg < 0 || fcntl(fd, F_SETFD, arg | FD_CLOEXEC) < 0) {
			/* fcntl failure: close fd without disturbing errno */
			int saved_errno = errno;

			close(fd);
			fd = -1;
			errno = saved_errno;
		}
	}

	return fd;
}
#else
#define safe_socket(d, t, p) socket(d, t, p)
#endif

#endif