/
event.c
351 lines (280 loc) · 6.96 KB
/
event.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
* WinPR: Windows Portable Runtime
* Synchronization Functions
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <winpr/synch.h>
#ifndef _WIN32
#include "synch.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_EVENTFD_H
#include <sys/eventfd.h>
#include <errno.h>
#endif
#include "../handle/handle.h"
#include "../pipe/pipe.h"
CRITICAL_SECTION cs = { NULL, 0, 0, NULL, NULL, 0 };
HANDLE CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCWSTR lpName)
{
WINPR_EVENT* event;
HANDLE handle = NULL;
event = (WINPR_EVENT*) malloc(sizeof(WINPR_EVENT));
if (event)
{
event->bAttached = FALSE;
event->bManualReset = bManualReset;
if (!event->bManualReset)
{
fprintf(stderr, "CreateEventW: auto-reset events not yet implemented\n");
}
event->pipe_fd[0] = -1;
event->pipe_fd[1] = -1;
#ifdef HAVE_EVENTFD_H
event->pipe_fd[0] = eventfd(0, EFD_NONBLOCK);
if (event->pipe_fd[0] < 0)
{
fprintf(stderr, "CreateEventW: failed to create event\n");
free(event);
return NULL;
}
#else
if (pipe(event->pipe_fd) < 0)
{
fprintf(stderr, "CreateEventW: failed to create event\n");
free(event);
return NULL;
}
#endif
WINPR_HANDLE_SET_TYPE(event, HANDLE_TYPE_EVENT);
handle = (HANDLE) event;
if (bInitialState)
SetEvent(handle);
}
if (!cs.LockSemaphore)
InitializeCriticalSection(&cs);
return handle;
}
HANDLE CreateEventA(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCSTR lpName)
{
return CreateEventW(lpEventAttributes, bManualReset, bInitialState, NULL);
}
HANDLE CreateEventExW(LPSECURITY_ATTRIBUTES lpEventAttributes, LPCWSTR lpName, DWORD dwFlags, DWORD dwDesiredAccess)
{
return NULL;
}
HANDLE CreateEventExA(LPSECURITY_ATTRIBUTES lpEventAttributes, LPCSTR lpName, DWORD dwFlags, DWORD dwDesiredAccess)
{
return NULL;
}
HANDLE OpenEventW(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpName)
{
return NULL;
}
HANDLE OpenEventA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName)
{
return NULL;
}
#ifdef HAVE_EVENTFD_H
#if defined(__UCLIBC__)
static int eventfd_read(int fd, eventfd_t* value)
{
return (read(fd, value, sizeof(*value)) == sizeof(*value)) ? 0 : -1;
}
static int eventfd_write(int fd, eventfd_t value)
{
return (write(fd, &value, sizeof(value)) == sizeof(value)) ? 0 : -1;
}
#endif
#endif
BOOL SetEvent(HANDLE hEvent)
{
ULONG Type;
PVOID Object;
int length;
BOOL status;
WINPR_EVENT* event;
status = FALSE;
if (winpr_Handle_GetInfo(hEvent, &Type, &Object))
{
event = (WINPR_EVENT*) Object;
#ifdef HAVE_EVENTFD_H
eventfd_t val = 1;
do
{
length = eventfd_write(event->pipe_fd[0], val);
}
while ((length < 0) && (errno == EINTR));
status = (length == 0) ? TRUE : FALSE;
#else
if (WaitForSingleObject(hEvent, 0) != WAIT_OBJECT_0)
{
length = write(event->pipe_fd[1], "-", 1);
if (length == 1)
status = TRUE;
}
else
{
status = TRUE;
}
#endif
}
return status;
}
BOOL ResetEvent(HANDLE hEvent)
{
ULONG Type;
PVOID Object;
int length;
BOOL status;
WINPR_EVENT* event;
status = FALSE;
if (winpr_Handle_GetInfo(hEvent, &Type, &Object))
{
event = (WINPR_EVENT*) Object;
while (WaitForSingleObject(hEvent, 0) == WAIT_OBJECT_0)
{
#ifdef HAVE_EVENTFD_H
eventfd_t value;
do
{
length = eventfd_read(event->pipe_fd[0], &value);
}
while ((length < 0) && (errno == EINTR));
if ((length > 0) && (!status))
status = TRUE;
#else
length = read(event->pipe_fd[0], &length, 1);
if ((length == 1) && (!status))
status = TRUE;
#endif
}
}
return status;
}
#endif
HANDLE CreateFileDescriptorEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, int FileDescriptor)
{
#ifndef _WIN32
WINPR_EVENT* event;
HANDLE handle = NULL;
event = (WINPR_EVENT*) malloc(sizeof(WINPR_EVENT));
if (event)
{
event->bAttached = TRUE;
event->bManualReset = bManualReset;
event->pipe_fd[0] = FileDescriptor;
event->pipe_fd[1] = -1;
WINPR_HANDLE_SET_TYPE(event, HANDLE_TYPE_EVENT);
handle = (HANDLE) event;
}
return handle;
#else
return NULL;
#endif
}
HANDLE CreateFileDescriptorEventA(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, int FileDescriptor)
{
return CreateFileDescriptorEventW(lpEventAttributes, bManualReset, bInitialState, FileDescriptor);
}
/**
* Returns an event based on the handle returned by GetEventWaitObject()
*/
HANDLE CreateWaitObjectEvent(LPSECURITY_ATTRIBUTES lpEventAttributes,
BOOL bManualReset, BOOL bInitialState, void* pObject)
{
#ifndef _WIN32
return CreateFileDescriptorEventW(lpEventAttributes, bManualReset, bInitialState, (int) (ULONG_PTR) pObject);
#else
HANDLE hEvent = NULL;
DuplicateHandle(GetCurrentProcess(), pObject, GetCurrentProcess(), &hEvent, 0, FALSE, DUPLICATE_SAME_ACCESS);
return hEvent;
#endif
}
/*
* Returns inner file descriptor for usage with select()
* This file descriptor is not usable on Windows
*/
int GetEventFileDescriptor(HANDLE hEvent)
{
#ifndef _WIN32
ULONG Type;
PVOID Object;
WINPR_EVENT* event;
if (!winpr_Handle_GetInfo(hEvent, &Type, &Object))
return -1;
event = (WINPR_EVENT*) Object;
if (Type == HANDLE_TYPE_NAMED_PIPE)
{
WINPR_NAMED_PIPE *named = (WINPR_NAMED_PIPE *)hEvent;
if (named->ServerMode) {
return named->serverfd;
} else {
return named->clientfd;
}
}
return event->pipe_fd[0];
#else
return -1;
#endif
}
/*
* Set inner file descriptor for usage with select()
* This file descriptor is not usable on Windows
*/
int SetEventFileDescriptor(HANDLE hEvent, int FileDescriptor)
{
#ifndef _WIN32
ULONG Type;
PVOID Object;
WINPR_EVENT* event;
if (!winpr_Handle_GetInfo(hEvent, &Type, &Object))
return -1;
event = (WINPR_EVENT*) Object;
event->pipe_fd[0] = FileDescriptor;
return 0;
#else
return -1;
#endif
}
/**
* Returns platform-specific wait object as a void pointer
*
* On Windows, the returned object is the same as the hEvent
* argument and is an event HANDLE usable in WaitForMultipleObjects
*
* On other platforms, the returned object can be cast to an int
* to obtain a file descriptor usable in select()
*/
void* GetEventWaitObject(HANDLE hEvent)
{
#ifndef _WIN32
int fd;
void* obj;
fd = GetEventFileDescriptor(hEvent);
obj = ((void*) (long) fd);
return obj;
#else
return hEvent;
#endif
}