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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
/*
* Copyright 2019 Intel Corporation
* SPDX-License-Identifier: MIT
*/
#include <errno.h>
#include "os_socket.h"
#if defined(__linux__)
#include <fcntl.h>
#include <poll.h>
#include <stddef.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
int
os_socket_listen_abstract(const char *path, int count)
{
int s = socket(AF_UNIX, SOCK_STREAM, 0);
if (s < 0)
return -1;
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path + 1, path, sizeof(addr.sun_path) - 2);
/* Create an abstract socket */
int ret = bind(s, (struct sockaddr*)&addr,
offsetof(struct sockaddr_un, sun_path) +
strlen(path) + 1);
if (ret < 0)
return -1;
listen(s, count);
return s;
}
int
os_socket_accept(int s)
{
return accept(s, NULL, NULL);
}
ssize_t
os_socket_recv(int socket, void *buffer, size_t length, int flags)
{
return recv(socket, buffer, length, flags);
}
ssize_t
os_socket_send(int socket, const void *buffer, size_t length, int flags)
{
return send(socket, buffer, length, flags);
}
void
os_socket_block(int s, bool block)
{
int old = fcntl(s, F_GETFL, 0);
if (old == -1)
return;
/* TODO obey block */
if (block)
fcntl(s, F_SETFL, old & ~O_NONBLOCK);
else
fcntl(s, F_SETFL, old | O_NONBLOCK);
}
void
os_socket_close(int s)
{
close(s);
}
#else
int
os_socket_listen_abstract(const char *path, int count)
{
errno = -ENOSYS;
return -1;
}
int
os_socket_accept(int s)
{
errno = -ENOSYS;
return -1;
}
ssize_t
os_socket_recv(int socket, void *buffer, size_t length, int flags)
{
errno = -ENOSYS;
return -1;
}
ssize_t
os_socket_send(int socket, const void *buffer, size_t length, int flags)
{
errno = -ENOSYS;
return -1;
}
void
os_socket_block(int s, bool block)
{
}
void
os_socket_close(int s)
{
}
#endif
|