-
Notifications
You must be signed in to change notification settings - Fork 0
/
compat.c
62 lines (54 loc) · 1.33 KB
/
compat.c
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
#if defined(HAVE_PIPE2) || defined (HAVE_PROG_INVOCATION)
#define _GNU_SOURCE /* pipe2 or program_invocation_short_name */
#endif /* HAVE_PIPE2 */
/* pipe_cloexec */
#include <unistd.h>
#include <fcntl.h>
/* socket_cloexec */
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
/* program_name */
#include <errno.h>
#include <stdlib.h>
#include "compat.h"
int pipe_cloexec(int fds[2])
{
#ifdef HAVE_PIPE2
// atomic (on newer kernels) application of close-on-exec
if (pipe2(fds, O_CLOEXEC) < 0) {
return 1;
}
#else
// delayed application of close-on-exec
if (pipe(fds) < 0) {
return 1;
}
fcntl(fds[0], F_SETFD, FD_CLOEXEC);
fcntl(fds[1], F_SETFD, FD_CLOEXEC);
#endif /* HAVE_PIPE2 */
return 0;
}
int socket_cloexec(int domain, int type, int protocol)
{
#ifdef HAVE_SOCK_CLOEXEC_H
return socket(domain, (type | SOCK_CLOEXEC), protocol);
#else
int fd = socket(domain, type, protocol);
if (fd < 0) {
return fd;
}
fcntl(fd, F_SETFD, FD_CLOEXEC);
return fd;
#endif /* HAVE_SOCK_CLOEXEC_H */
}
const char *program_name(void)
{
#ifdef HAVE_PROG_INVOCATION
return program_invocation_short_name;
#elif HAVE_GETPROGNAME
return getprogname();
#else
#error "no progname impl"
#endif /* PROG_INVOCATION & GETPROGNAME */
}