FreeRDP
Loading...
Searching...
No Matches
event.c
1
21
22#include <winpr/config.h>
23
24#include <stdio.h>
25#include <string.h>
26#include <stdlib.h>
27
28#include <winpr/synch.h>
29
30#ifndef _WIN32
31
32#include "synch.h"
33
34#ifdef WINPR_HAVE_UNISTD_H
35#include <unistd.h>
36#endif
37
38#ifdef WINPR_HAVE_SYS_EVENTFD_H
39#include <sys/eventfd.h>
40#endif
41
42#include <fcntl.h>
43#include <errno.h>
44
45#include "../handle/handle.h"
46#include "../pipe/pipe.h"
47
48#include "../log.h"
49#include "event.h"
50#define TAG WINPR_TAG("synch.event")
51
52#if defined(WITH_DEBUG_EVENTS)
53static wArrayList* global_event_list = nullptr;
54
55static void dump_event(WINPR_EVENT* event, size_t index)
56{
57 char** msg = nullptr;
58 size_t used = 0;
59
60 WLog_DBG(TAG, "Event handle created still not closed! [%" PRIuz ", %p]", index, (void*)event);
61 msg = winpr_backtrace_symbols(event->create_stack, &used);
62
63 for (size_t i = 2; i < used; i++)
64 WLog_DBG(TAG, "[%" PRIuz "]: %s", i, msg[i]);
65
66 free((void*)msg);
67}
68#endif /* WITH_DEBUG_EVENTS */
69
70#ifdef WINPR_HAVE_SYS_EVENTFD_H
71#if !defined(WITH_EVENTFD_READ_WRITE)
72WINPR_ATTR_NODISCARD
73static int eventfd_read(int fd, eventfd_t* value)
74{
75 return (read(fd, value, sizeof(*value)) == sizeof(*value)) ? 0 : -1;
76}
77
78WINPR_ATTR_NODISCARD
79static int eventfd_write(int fd, eventfd_t value)
80{
81 return (write(fd, &value, sizeof(value)) == sizeof(value)) ? 0 : -1;
82}
83#endif
84#endif
85
86WINPR_ATTR_NODISCARD
87static BOOL set_non_blocking_fd(int fd)
88{
89 int flags = fcntl(fd, F_GETFL);
90 if (flags < 0)
91 return FALSE;
92
93 return fcntl(fd, F_SETFL, flags | O_NONBLOCK) >= 0;
94}
95
96BOOL winpr_event_init(WINPR_EVENT_IMPL* event)
97{
98#ifdef WINPR_HAVE_SYS_EVENTFD_H
99 event->fds[1] = -1;
100 event->fds[0] = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
101
102 if ((event->fds[0] < 0) && (errno == EINVAL))
103 {
104 /* kernels older than 2.6.27 only support the flag-less eventfd() call; fall back to
105 * that and apply non-blocking/close-on-exec separately afterward */
106 event->fds[0] = eventfd(0, 0);
107 if (event->fds[0] >= 0)
108 {
109 if (!set_non_blocking_fd(event->fds[0]) || !winpr_set_cloexec(event->fds[0], TRUE))
110 {
111 close(event->fds[0]);
112 event->fds[0] = -1;
113 }
114 }
115 }
116
117 return event->fds[0] >= 0;
118#else
119#ifdef WINPR_HAVE_PIPE2
120 if (pipe2(event->fds, O_CLOEXEC) < 0)
121 return FALSE;
122#else
123 if (pipe(event->fds) < 0)
124 return FALSE;
125
126 if (!winpr_set_cloexec(event->fds[0], TRUE) || !winpr_set_cloexec(event->fds[1], TRUE))
127 goto out_error;
128#endif
129
130 if (!set_non_blocking_fd(event->fds[0]) || !set_non_blocking_fd(event->fds[1]))
131 goto out_error;
132
133 return TRUE;
134
135out_error:
136 winpr_event_uninit(event);
137 return FALSE;
138#endif
139}
140
141void winpr_event_init_from_fd(WINPR_EVENT_IMPL* event, int fd)
142{
143 event->fds[0] = fd;
144#ifndef WINPR_HAVE_SYS_EVENTFD_H
145 event->fds[1] = fd;
146#endif
147}
148
149BOOL winpr_event_set(WINPR_EVENT_IMPL* event)
150{
151 SSIZE_T ret = 0;
152 do
153 {
154#ifdef WINPR_HAVE_SYS_EVENTFD_H
155 eventfd_t value = 1;
156 ret = eventfd_write(event->fds[0], value);
157#else
158 ret = write(event->fds[1], "-", 1);
159#endif
160 } while (ret < 0 && errno == EINTR);
161
162 return ret >= 0;
163}
164
165BOOL winpr_event_reset(WINPR_EVENT_IMPL* event)
166{
167 SSIZE_T ret = 0;
168 do
169 {
170 do
171 {
172#ifdef WINPR_HAVE_SYS_EVENTFD_H
173 eventfd_t value = 1;
174 ret = eventfd_read(event->fds[0], &value);
175#else
176 char value;
177 ret = read(event->fds[0], &value, 1);
178#endif
179 } while (ret < 0 && errno == EINTR);
180 } while (ret >= 0);
181
182 return (errno == EAGAIN);
183}
184
185void winpr_event_uninit(WINPR_EVENT_IMPL* event)
186{
187 if (event->fds[0] >= 0)
188 {
189 close(event->fds[0]);
190 event->fds[0] = -1;
191 }
192
193 if (event->fds[1] >= 0)
194 {
195 close(event->fds[1]);
196 event->fds[1] = -1;
197 }
198}
199
200static BOOL EventCloseHandle(HANDLE handle);
201
202WINPR_ATTR_NODISCARD
203static BOOL EventIsHandled(HANDLE handle)
204{
205 return WINPR_HANDLE_IS_HANDLED(handle, HANDLE_TYPE_EVENT, FALSE);
206}
207
208WINPR_ATTR_NODISCARD
209static int EventGetFd(HANDLE handle)
210{
211 WINPR_EVENT* event = (WINPR_EVENT*)handle;
212
213 if (!EventIsHandled(handle))
214 return -1;
215
216 return event->impl.fds[0];
217}
218
219static BOOL EventCloseHandle_(WINPR_EVENT* event)
220{
221 if (!event)
222 return FALSE;
223
224 if (event->bAttached)
225 {
226 // don't close attached file descriptor
227 event->impl.fds[0] = -1; // mark as invalid
228 }
229
230 winpr_event_uninit(&event->impl);
231
232#if defined(WITH_DEBUG_EVENTS)
233 if (global_event_list)
234 {
235 ArrayList_Remove(global_event_list, event);
236 if (ArrayList_Count(global_event_list) < 1)
237 {
238 ArrayList_Free(global_event_list);
239 global_event_list = nullptr;
240 }
241 }
242
243 winpr_backtrace_free(event->create_stack);
244#endif
245 free(event->name);
246 event->name = nullptr;
247 return TRUE;
248}
249
250static BOOL EventCloseHandle(HANDLE handle)
251{
252 WINPR_EVENT* event = (WINPR_EVENT*)handle;
253
254 if (!EventIsHandled(handle))
255 return FALSE;
256
257 return EventCloseHandle_(event);
258}
259
260/* Implement auto-reset semantics on POSIX.
261 * Upstream leaves CleanupHandle nullptr, so an auto-reset event stays signaled
262 * forever after the first SetEvent -> WaitForMultipleObjects returns spurious
263 * ready on every call, breaking FreeRDP's transport event loop (on Android
264 * the connection is aborted right after the TLS handshake). Consume the
265 * signal after a successful wait, exactly like Win32 auto-reset. */
266static DWORD EventCleanupHandle(HANDLE handle)
267{
268 WINPR_EVENT* event = (WINPR_EVENT*)handle;
269
270 if (!EventIsHandled(handle))
271 return WAIT_FAILED;
272
273 /* An attached descriptor (CreateFileDescriptorEvent, CreateWaitObjectEvent) belongs to the
274 * caller: draining it would discard the caller's data. */
275 if (!event->bManualReset && !event->bAttached)
276 winpr_event_reset(&event->impl);
277
278 return WAIT_OBJECT_0;
279}
280
281static HANDLE_OPS ops = { EventIsHandled, EventCloseHandle, EventGetFd, EventCleanupHandle,
282 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
283 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
284 nullptr, nullptr, nullptr, nullptr, nullptr };
285
286HANDLE CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState,
287 LPCWSTR lpName)
288{
289 HANDLE handle = nullptr;
290 char* name = nullptr;
291
292 if (lpName)
293 {
294 name = ConvertWCharToUtf8Alloc(lpName, nullptr);
295 if (!name)
296 return nullptr;
297 }
298
299 handle = CreateEventA(lpEventAttributes, bManualReset, bInitialState, name);
300 free(name);
301 return handle;
302}
303
304HANDLE CreateEventA(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState,
305 LPCSTR lpName)
306{
307 WINPR_EVENT* event = (WINPR_EVENT*)calloc(1, sizeof(WINPR_EVENT));
308
309 if (lpEventAttributes)
310 WLog_WARN(TAG, "[%s] does not support lpEventAttributes", lpName);
311
312 if (!event)
313 return nullptr;
314
315 if (lpName)
316 event->name = strdup(lpName);
317
318 event->impl.fds[0] = -1;
319 event->impl.fds[1] = -1;
320 event->bAttached = FALSE;
321 event->bManualReset = bManualReset;
322 event->common.ops = &ops;
323 WINPR_HANDLE_SET_TYPE_AND_MODE(event, HANDLE_TYPE_EVENT, FD_READ);
324
325 if (!winpr_event_init(&event->impl))
326 goto fail;
327
328 if (bInitialState)
329 {
330 if (!SetEvent(event))
331 goto fail;
332 }
333
334#if defined(WITH_DEBUG_EVENTS)
335 event->create_stack = winpr_backtrace(20);
336 if (!global_event_list)
337 global_event_list = ArrayList_New(TRUE);
338
339 if (global_event_list)
340 ArrayList_Append(global_event_list, event);
341#endif
342 return (HANDLE)event;
343fail:
344 EventCloseHandle_(event);
345 free(event);
346 return nullptr;
347}
348
349HANDLE CreateEventExW(LPSECURITY_ATTRIBUTES lpEventAttributes, LPCWSTR lpName, DWORD dwFlags,
350 DWORD dwDesiredAccess)
351{
352 BOOL initial = FALSE;
353 BOOL manual = FALSE;
354
355 if (dwFlags & CREATE_EVENT_INITIAL_SET)
356 initial = TRUE;
357
358 if (dwFlags & CREATE_EVENT_MANUAL_RESET)
359 manual = TRUE;
360
361 if (dwDesiredAccess != 0)
362 {
363 char name[MAX_PATH] = WINPR_C_ARRAY_INIT;
364 ConvertWCharToUtf8(lpName, name, sizeof(name) - 1);
365 WLog_WARN(TAG, "[%s] does not support dwDesiredAccess 0x%08" PRIx32, name, dwDesiredAccess);
366 }
367
368 return CreateEventW(lpEventAttributes, manual, initial, lpName);
369}
370
371HANDLE CreateEventExA(LPSECURITY_ATTRIBUTES lpEventAttributes, LPCSTR lpName, DWORD dwFlags,
372 DWORD dwDesiredAccess)
373{
374 BOOL initial = FALSE;
375 BOOL manual = FALSE;
376
377 if (dwFlags & CREATE_EVENT_INITIAL_SET)
378 initial = TRUE;
379
380 if (dwFlags & CREATE_EVENT_MANUAL_RESET)
381 manual = TRUE;
382
383 if (dwDesiredAccess != 0)
384 WLog_WARN(TAG, "[%s] does not support dwDesiredAccess 0x%08" PRIx32, lpName,
385 dwDesiredAccess);
386
387 return CreateEventA(lpEventAttributes, manual, initial, lpName);
388}
389
390HANDLE OpenEventW(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpName)
391{
392 /* TODO: Implement */
393 WINPR_UNUSED(dwDesiredAccess);
394 WINPR_UNUSED(bInheritHandle);
395 WINPR_UNUSED(lpName);
396 WLog_ERR(TAG, "not implemented");
397 return nullptr;
398}
399
400HANDLE OpenEventA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName)
401{
402 /* TODO: Implement */
403 WINPR_UNUSED(dwDesiredAccess);
404 WINPR_UNUSED(bInheritHandle);
405 WINPR_UNUSED(lpName);
406 WLog_ERR(TAG, "not implemented");
407 return nullptr;
408}
409
410BOOL SetEvent(HANDLE hEvent)
411{
412 ULONG Type = 0;
413 WINPR_HANDLE* Object = nullptr;
414 WINPR_EVENT* event = nullptr;
415
416 if (!winpr_Handle_GetInfo(hEvent, &Type, &Object) || Type != HANDLE_TYPE_EVENT)
417 {
418 WLog_ERR(TAG, "SetEvent: hEvent is not an event");
419 SetLastError(ERROR_INVALID_PARAMETER);
420 return FALSE;
421 }
422
423 event = (WINPR_EVENT*)Object;
424 return winpr_event_set(&event->impl);
425}
426
427BOOL ResetEvent(HANDLE hEvent)
428{
429 ULONG Type = 0;
430 WINPR_HANDLE* Object = nullptr;
431 WINPR_EVENT* event = nullptr;
432
433 if (!winpr_Handle_GetInfo(hEvent, &Type, &Object) || Type != HANDLE_TYPE_EVENT)
434 {
435 WLog_ERR(TAG, "ResetEvent: hEvent is not an event");
436 SetLastError(ERROR_INVALID_PARAMETER);
437 return FALSE;
438 }
439
440 event = (WINPR_EVENT*)Object;
441 return winpr_event_reset(&event->impl);
442}
443
444#endif
445
446HANDLE CreateFileDescriptorEventW(WINPR_ATTR_UNUSED LPSECURITY_ATTRIBUTES lpEventAttributes,
447 BOOL bManualReset, WINPR_ATTR_UNUSED BOOL bInitialState,
448 int FileDescriptor, ULONG mode)
449{
450#ifndef _WIN32
451 WINPR_EVENT* event = nullptr;
452 HANDLE handle = nullptr;
453 event = (WINPR_EVENT*)calloc(1, sizeof(WINPR_EVENT));
454
455 if (event)
456 {
457 event->impl.fds[0] = -1;
458 event->impl.fds[1] = -1;
459 event->bAttached = TRUE;
460 event->bManualReset = bManualReset;
461 winpr_event_init_from_fd(&event->impl, FileDescriptor);
462 event->common.ops = &ops;
463 WINPR_HANDLE_SET_TYPE_AND_MODE(event, HANDLE_TYPE_EVENT, mode);
464 handle = (HANDLE)event;
465 }
466
467 return handle;
468#else
469 return nullptr;
470#endif
471}
472
473HANDLE CreateFileDescriptorEventA(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset,
474 BOOL bInitialState, int FileDescriptor, ULONG mode)
475{
476 return CreateFileDescriptorEventW(lpEventAttributes, bManualReset, bInitialState,
477 FileDescriptor, mode);
478}
479
483HANDLE CreateWaitObjectEvent(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset,
484 BOOL bInitialState, void* pObject)
485{
486#ifndef _WIN32
487 return CreateFileDescriptorEventW(lpEventAttributes, bManualReset, bInitialState,
488 (int)(ULONG_PTR)pObject, WINPR_FD_READ);
489#else
490 HANDLE hEvent = nullptr;
491 DuplicateHandle(GetCurrentProcess(), pObject, GetCurrentProcess(), &hEvent, 0, FALSE,
492 DUPLICATE_SAME_ACCESS);
493 return hEvent;
494#endif
495}
496
497/*
498 * Returns inner file descriptor for usage with select()
499 * This file descriptor is not usable on Windows
500 */
501
502int GetEventFileDescriptor(HANDLE hEvent)
503{
504#ifndef _WIN32
505 return winpr_Handle_getFd(hEvent);
506#else
507 return -1;
508#endif
509}
510
511/*
512 * Set inner file descriptor for usage with select()
513 * This file descriptor is not usable on Windows
514 */
515
516int SetEventFileDescriptor(HANDLE hEvent, int FileDescriptor, ULONG mode)
517{
518#ifndef _WIN32
519 ULONG Type = 0;
520 WINPR_HANDLE* Object = nullptr;
521 WINPR_EVENT* event = nullptr;
522
523 if (!winpr_Handle_GetInfo(hEvent, &Type, &Object) || Type != HANDLE_TYPE_EVENT)
524 {
525 WLog_ERR(TAG, "SetEventFileDescriptor: hEvent is not an event");
526 SetLastError(ERROR_INVALID_PARAMETER);
527 return -1;
528 }
529
530 event = (WINPR_EVENT*)Object;
531
532 if (!event->bAttached && event->impl.fds[0] >= 0 && event->impl.fds[0] != FileDescriptor)
533 close(event->impl.fds[0]);
534
535 event->bAttached = TRUE;
536 event->common.Mode = mode;
537 event->impl.fds[0] = FileDescriptor;
538 return 0;
539#else
540 return -1;
541#endif
542}
543
553
554void* GetEventWaitObject(HANDLE hEvent)
555{
556#ifndef _WIN32
557 int fd = 0;
558 void* obj = nullptr;
559 fd = GetEventFileDescriptor(hEvent);
560 obj = ((void*)(long)fd);
561 return obj;
562#else
563 return hEvent;
564#endif
565}
566#if defined(WITH_DEBUG_EVENTS)
567#include <unistd.h>
568#include <fcntl.h>
569#include <sys/time.h>
570#include <sys/resource.h>
571
572static BOOL dump_handle_list(void* data, size_t index, WINPR_ATTR_UNUSED va_list ap)
573{
574 WINPR_EVENT* event = data;
575 dump_event(event, index);
576 return TRUE;
577}
578
579void DumpEventHandles_(const char* fkt, const char* file, size_t line)
580{
581 struct rlimit r = WINPR_C_ARRAY_INIT;
582 int rc = getrlimit(RLIMIT_NOFILE, &r);
583 if (rc >= 0)
584 {
585 size_t count = 0;
586 for (rlim_t x = 0; x < r.rlim_cur; x++)
587 {
588 const int fd = WINPR_ASSERTING_INT_CAST(int, x);
589 int flags = fcntl(fd, F_GETFD);
590 if (flags >= 0)
591 count++;
592 }
593 WLog_INFO(TAG, "------- limits [%lu/%lu] open files %" PRIuz, (unsigned long)r.rlim_cur,
594 (unsigned long)r.rlim_max, count);
595 }
596 WLog_DBG(TAG, "--------- Start dump [%s %s:%" PRIuz "]", fkt, file, line);
597 if (global_event_list)
598 {
599 ArrayList_Lock(global_event_list);
600 (void)ArrayList_ForEach(global_event_list, dump_handle_list);
601 ArrayList_Unlock(global_event_list);
602 }
603 WLog_DBG(TAG, "--------- End dump [%s %s:%" PRIuz "]", fkt, file, line);
604}
605#endif