aboutsummaryrefslogtreecommitdiff
path: root/src/event/WakeFD.cxx
blob: 1a84f5645f063a6fd2c2c24cbf4400c87eea017c (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * Copyright (C) 2003-2013 The Music Player Daemon Project
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"
#include "WakeFD.hxx"
#include "fd_util.h"
#include "gcc.h"

#include <unistd.h>

#ifdef WIN32
#include <ws2tcpip.h>
#include <winsock2.h>
#include <cstring> /* for memset() */
#endif

#ifdef HAVE_EVENTFD
#include <sys/eventfd.h>
#endif

#ifdef WIN32
static bool PoorSocketPair(int fd[2]);
#endif

bool
WakeFD::Create()
{
	assert(fds[0] == -1);
	assert(fds[1] == -1);

#ifdef WIN32
	return PoorSocketPair(fds);
#else
#ifdef HAVE_EVENTFD
	fds[0] = eventfd_cloexec_nonblock(0, 0);
	if (fds[0] >= 0) {
		fds[1] = -2;
		return true;
	}
#endif
	return pipe_cloexec_nonblock(fds) >= 0;
#endif
}

void
WakeFD::Destroy()
{
#ifdef WIN32
	closesocket(fds[0]);
	closesocket(fds[1]);
#else
	close(fds[0]);
#ifdef HAVE_EVENTFD
	if (!IsEventFD())
#endif
		close(fds[1]);
#endif

#ifndef NDEBUG
	fds[0] = -1;
	fds[1] = -1;
#endif
}

bool
WakeFD::Read()
{
	assert(fds[0] >= 0);

#ifdef WIN32
	assert(fds[1] >= 0);
	char buffer[256];
	return recv(fds[0], buffer, sizeof(buffer), 0) > 0;
#else

#ifdef HAVE_EVENTFD
	if (IsEventFD()) {
		eventfd_t value;
		return read(fds[0], &value,
			    sizeof(value)) == (ssize_t)sizeof(value);
	}
#endif

	assert(fds[1] >= 0);

	char buffer[256];
	return read(fds[0], buffer, sizeof(buffer)) > 0;
#endif
}

void
WakeFD::Write()
{
	assert(fds[0] >= 0);

#ifdef WIN32
	assert(fds[1] >= 0);

	send(fds[1], "", 1, 0);
#else

#ifdef HAVE_EVENTFD
	if (IsEventFD()) {
		static constexpr eventfd_t value = 1;
		gcc_unused ssize_t nbytes =
			write(fds[0], &value, sizeof(value));
		return;
	}
#endif

	assert(fds[1] >= 0);

	gcc_unused ssize_t nbytes = write(fds[1], "", 1);
#endif
}

#ifdef WIN32

static void SafeCloseSocket(SOCKET s)
{
	int error = WSAGetLastError();
	closesocket(s);
	WSASetLastError(error);
}

/* Our poor man's socketpair() implementation
 * Due to limited protocol/address family support and primitive error handling
 * it's better to keep this as a private implementation detail of WakeFD
 * rather than wide-available API.
 */
static bool PoorSocketPair(int fd[2])
{
	assert (fd != nullptr);

	SOCKET listen_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (listen_socket == INVALID_SOCKET)
		return false;

	sockaddr_in address;
	std::memset(&address, 0, sizeof(address));
	address.sin_family = AF_INET;
	address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

	int ret = bind(listen_socket,
		       reinterpret_cast<sockaddr*>(&address),
		       sizeof(address));
	
	if (ret < 0) {
		SafeCloseSocket(listen_socket);
		return false;
	}

	ret = listen(listen_socket, 1);
	
	if (ret < 0) {
		SafeCloseSocket(listen_socket);
		return false;
	}

	int address_len = sizeof(address);
	ret = getsockname(listen_socket,
			  reinterpret_cast<sockaddr*>(&address),
			  &address_len);

	if (ret < 0) {
		SafeCloseSocket(listen_socket);
		return false;
	}

	SOCKET socket0 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (socket0 == INVALID_SOCKET) {
		SafeCloseSocket(listen_socket);
		return false;
	}

	ret = connect(socket0,
		      reinterpret_cast<sockaddr*>(&address),
		      sizeof(address));

	if (ret < 0) {
		SafeCloseSocket(listen_socket);
		SafeCloseSocket(socket0);
		return false;
	}

	SOCKET socket1 = accept(listen_socket, nullptr, nullptr);
	if (socket1 == INVALID_SOCKET) {
		SafeCloseSocket(listen_socket);
		SafeCloseSocket(socket0);
		return false;
	}

	SafeCloseSocket(listen_socket);

	u_long non_block = 1;
	if (ioctlsocket(socket0, FIONBIO, &non_block) < 0
	    || ioctlsocket(socket1, FIONBIO, &non_block) < 0) {
		SafeCloseSocket(socket0);
		SafeCloseSocket(socket1);
		return false;
	}

	fd[0] = static_cast<int>(socket0);
	fd[1] = static_cast<int>(socket1);

	return true;
}

#endif