2012-05-29 22:14:26 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Synchronization Functions
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2017-02-20 20:31:58 +03:00
|
|
|
* Copyright 2017 Armin Novak <armin.novak@thincast.com>
|
|
|
|
* Copyright 2017 Thincast Technologies GmbH
|
2012-05-29 22:14:26 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 12:08:00 +03:00
|
|
|
#include <winpr/config.h>
|
2012-08-15 01:20:53 +04:00
|
|
|
|
2012-11-27 05:34:36 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2012-05-29 22:14:26 +04:00
|
|
|
|
2012-11-27 05:34:36 +04:00
|
|
|
#include <winpr/synch.h>
|
2012-09-18 20:57:19 +04:00
|
|
|
|
2012-09-19 02:36:13 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
|
|
|
#include "synch.h"
|
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_UNISTD_H
|
2012-09-19 02:36:13 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2012-09-18 20:57:19 +04:00
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_SYS_EVENTFD_H
|
2013-01-24 00:59:01 +04:00
|
|
|
#include <sys/eventfd.h>
|
|
|
|
#endif
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
#include <fcntl.h>
|
2015-06-17 15:13:28 +03:00
|
|
|
#include <errno.h>
|
|
|
|
|
2013-05-17 01:32:58 +04:00
|
|
|
#include "../handle/handle.h"
|
2014-02-07 18:18:17 +04:00
|
|
|
#include "../pipe/pipe.h"
|
2013-05-17 01:32:58 +04:00
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
#include "../log.h"
|
2021-03-24 20:32:43 +03:00
|
|
|
#include "event.h"
|
2014-08-18 20:57:08 +04:00
|
|
|
#define TAG WINPR_TAG("synch.event")
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2021-09-17 09:50:32 +03:00
|
|
|
#if defined(WITH_DEBUG_EVENTS)
|
|
|
|
static wArrayList* global_event_list = NULL;
|
|
|
|
|
|
|
|
static void dump_event(WINPR_EVENT* event, size_t index)
|
|
|
|
{
|
|
|
|
char** msg;
|
|
|
|
size_t used, i;
|
|
|
|
#if 0
|
|
|
|
void* stack = winpr_backtrace(20);
|
|
|
|
WLog_DBG(TAG, "Called from:");
|
|
|
|
msg = winpr_backtrace_symbols(stack, &used);
|
|
|
|
|
|
|
|
for (i = 0; i < used; i++)
|
|
|
|
WLog_DBG(TAG, "[%" PRIdz "]: %s", i, msg[i]);
|
|
|
|
|
|
|
|
free(msg);
|
|
|
|
winpr_backtrace_free(stack);
|
|
|
|
#endif
|
|
|
|
WLog_DBG(TAG, "Event handle created still not closed! [%" PRIuz ", %p]", index, event);
|
|
|
|
msg = winpr_backtrace_symbols(event->create_stack, &used);
|
|
|
|
|
|
|
|
for (i = 2; i < used; i++)
|
|
|
|
WLog_DBG(TAG, "[%" PRIdz "]: %s", i, msg[i]);
|
|
|
|
|
|
|
|
free(msg);
|
|
|
|
}
|
2022-08-03 09:30:51 +03:00
|
|
|
#endif /* WITH_DEBUG_EVENTS */
|
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_SYS_EVENTFD_H
|
2021-03-24 20:32:43 +03:00
|
|
|
#if !defined(WITH_EVENTFD_READ_WRITE)
|
|
|
|
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
|
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifndef WINPR_HAVE_SYS_EVENTFD_H
|
2022-08-03 09:30:51 +03:00
|
|
|
static BOOL set_non_blocking_fd(int fd)
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
flags = fcntl(fd, F_GETFL);
|
|
|
|
if (flags < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return fcntl(fd, F_SETFL, flags | O_NONBLOCK) >= 0;
|
|
|
|
}
|
2023-01-10 16:30:23 +03:00
|
|
|
#endif /* !WINPR_HAVE_SYS_EVENTFD_H */
|
2022-08-03 09:30:51 +03:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
BOOL winpr_event_init(WINPR_EVENT_IMPL* event)
|
|
|
|
{
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_SYS_EVENTFD_H
|
2021-03-24 20:32:43 +03:00
|
|
|
event->fds[1] = -1;
|
|
|
|
event->fds[0] = eventfd(0, EFD_NONBLOCK);
|
|
|
|
|
|
|
|
return event->fds[0] >= 0;
|
|
|
|
#else
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
if (pipe(event->fds) < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
2022-08-03 09:30:51 +03:00
|
|
|
if (!set_non_blocking_fd(event->fds[0]) || !set_non_blocking_fd(event->fds[1]))
|
2021-03-24 20:32:43 +03:00
|
|
|
goto out_error;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
out_error:
|
2021-04-22 12:57:44 +03:00
|
|
|
winpr_event_uninit(event);
|
2021-03-24 20:32:43 +03:00
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void winpr_event_init_from_fd(WINPR_EVENT_IMPL* event, int fd)
|
|
|
|
{
|
|
|
|
event->fds[0] = fd;
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifndef WINPR_HAVE_SYS_EVENTFD_H
|
2021-03-24 20:32:43 +03:00
|
|
|
event->fds[1] = fd;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL winpr_event_set(WINPR_EVENT_IMPL* event)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
do
|
|
|
|
{
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_SYS_EVENTFD_H
|
2021-03-24 20:32:43 +03:00
|
|
|
eventfd_t value = 1;
|
|
|
|
ret = eventfd_write(event->fds[0], value);
|
|
|
|
#else
|
|
|
|
ret = write(event->fds[1], "-", 1);
|
|
|
|
#endif
|
|
|
|
} while (ret < 0 && errno == EINTR);
|
|
|
|
|
|
|
|
return ret >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL winpr_event_reset(WINPR_EVENT_IMPL* event)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_SYS_EVENTFD_H
|
2021-03-24 20:32:43 +03:00
|
|
|
eventfd_t value = 1;
|
|
|
|
ret = eventfd_read(event->fds[0], &value);
|
|
|
|
#else
|
|
|
|
char value;
|
2021-04-20 14:51:55 +03:00
|
|
|
ret = read(event->fds[0], &value, 1);
|
2021-03-24 20:32:43 +03:00
|
|
|
#endif
|
|
|
|
} while (ret < 0 && errno == EINTR);
|
|
|
|
} while (ret >= 0);
|
|
|
|
|
|
|
|
return (errno == EAGAIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
void winpr_event_uninit(WINPR_EVENT_IMPL* event)
|
|
|
|
{
|
|
|
|
if (event->fds[0] != -1)
|
2021-05-26 04:03:03 +03:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
close(event->fds[0]);
|
2021-05-26 04:03:03 +03:00
|
|
|
event->fds[0] = -1;
|
|
|
|
}
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
if (event->fds[1] != -1)
|
2021-05-26 04:03:03 +03:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
close(event->fds[1]);
|
2021-05-26 04:03:03 +03:00
|
|
|
event->fds[1] = -1;
|
|
|
|
}
|
2021-03-24 20:32:43 +03:00
|
|
|
}
|
|
|
|
|
2015-03-11 17:11:11 +03:00
|
|
|
static BOOL EventCloseHandle(HANDLE handle);
|
|
|
|
|
|
|
|
static BOOL EventIsHandled(HANDLE handle)
|
|
|
|
{
|
2022-04-27 16:56:39 +03:00
|
|
|
return WINPR_HANDLE_IS_HANDLED(handle, HANDLE_TYPE_EVENT, FALSE);
|
2015-03-11 17:11:11 +03:00
|
|
|
}
|
|
|
|
|
2017-02-20 20:31:58 +03:00
|
|
|
static int EventGetFd(HANDLE handle)
|
|
|
|
{
|
|
|
|
WINPR_EVENT* event = (WINPR_EVENT*)handle;
|
|
|
|
|
2015-03-11 19:57:01 +03:00
|
|
|
if (!EventIsHandled(handle))
|
|
|
|
return -1;
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
return event->impl.fds[0];
|
2015-03-11 17:11:11 +03:00
|
|
|
}
|
|
|
|
|
2019-02-08 11:05:59 +03:00
|
|
|
static BOOL EventCloseHandle_(WINPR_EVENT* event)
|
2017-02-20 20:31:58 +03:00
|
|
|
{
|
2019-02-08 11:05:59 +03:00
|
|
|
if (!event)
|
2015-03-11 17:11:11 +03:00
|
|
|
return FALSE;
|
|
|
|
|
2021-05-26 04:03:03 +03:00
|
|
|
if (event->bAttached)
|
|
|
|
{
|
|
|
|
// don't close attached file descriptor
|
|
|
|
event->impl.fds[0] = -1; // mark as invalid
|
|
|
|
}
|
|
|
|
|
|
|
|
winpr_event_uninit(&event->impl);
|
2015-07-03 10:25:41 +03:00
|
|
|
|
2021-09-17 09:50:32 +03:00
|
|
|
#if defined(WITH_DEBUG_EVENTS)
|
|
|
|
if (global_event_list)
|
|
|
|
{
|
|
|
|
ArrayList_Remove(global_event_list, event);
|
|
|
|
if (ArrayList_Count(global_event_list) < 1)
|
|
|
|
{
|
|
|
|
ArrayList_Free(global_event_list);
|
|
|
|
global_event_list = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
winpr_backtrace_free(event->create_stack);
|
|
|
|
#endif
|
2019-02-08 11:05:59 +03:00
|
|
|
free(event->name);
|
2015-07-03 10:25:41 +03:00
|
|
|
free(event);
|
|
|
|
return TRUE;
|
2015-03-11 17:11:11 +03:00
|
|
|
}
|
|
|
|
|
2019-02-08 11:05:59 +03:00
|
|
|
static BOOL EventCloseHandle(HANDLE handle)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_EVENT* event = (WINPR_EVENT*)handle;
|
2019-02-08 11:05:59 +03:00
|
|
|
|
|
|
|
if (!EventIsHandled(handle))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return EventCloseHandle_(event);
|
|
|
|
}
|
|
|
|
|
2022-04-13 10:34:05 +03:00
|
|
|
static HANDLE_OPS ops = { EventIsHandled,
|
|
|
|
EventCloseHandle,
|
|
|
|
EventGetFd,
|
|
|
|
NULL, /* CleanupHandle */
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL };
|
2015-03-16 02:29:37 +03:00
|
|
|
|
2017-02-20 20:31:58 +03:00
|
|
|
HANDLE CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState,
|
|
|
|
LPCWSTR lpName)
|
2019-02-07 18:43:55 +03:00
|
|
|
{
|
|
|
|
HANDLE handle;
|
|
|
|
char* name = NULL;
|
|
|
|
|
|
|
|
if (lpName)
|
|
|
|
{
|
2022-10-28 09:09:27 +03:00
|
|
|
name = ConvertWCharToUtf8Alloc(lpName, NULL);
|
|
|
|
if (!name)
|
2019-02-07 18:43:55 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle = CreateEventA(lpEventAttributes, bManualReset, bInitialState, name);
|
|
|
|
free(name);
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE CreateEventA(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState,
|
|
|
|
LPCSTR lpName)
|
2012-09-18 20:57:19 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_EVENT* event = (WINPR_EVENT*)calloc(1, sizeof(WINPR_EVENT));
|
2012-09-19 02:36:13 +04:00
|
|
|
|
2019-02-07 18:43:55 +03:00
|
|
|
if (lpEventAttributes)
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_WARN(TAG, "[%s] does not support lpEventAttributes", lpName);
|
2019-02-07 18:43:55 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!event)
|
|
|
|
return NULL;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2019-02-08 11:05:59 +03:00
|
|
|
if (lpName)
|
|
|
|
event->name = strdup(lpName);
|
|
|
|
|
2021-05-26 04:03:03 +03:00
|
|
|
event->impl.fds[0] = -1;
|
|
|
|
event->impl.fds[1] = -1;
|
2015-04-03 17:21:01 +03:00
|
|
|
event->bAttached = FALSE;
|
|
|
|
event->bManualReset = bManualReset;
|
2022-04-27 16:56:39 +03:00
|
|
|
event->common.ops = &ops;
|
2015-07-03 10:25:41 +03:00
|
|
|
WINPR_HANDLE_SET_TYPE_AND_MODE(event, HANDLE_TYPE_EVENT, FD_READ);
|
2015-04-03 17:21:01 +03:00
|
|
|
|
|
|
|
if (!event->bManualReset)
|
|
|
|
WLog_ERR(TAG, "auto-reset events not yet implemented");
|
2012-09-19 03:24:03 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
if (!winpr_event_init(&event->impl))
|
2017-02-20 20:31:58 +03:00
|
|
|
goto fail;
|
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (bInitialState)
|
2019-02-21 14:20:06 +03:00
|
|
|
{
|
|
|
|
if (!SetEvent(event))
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-09-19 02:36:13 +04:00
|
|
|
|
2021-09-17 09:50:32 +03:00
|
|
|
#if defined(WITH_DEBUG_EVENTS)
|
|
|
|
event->create_stack = winpr_backtrace(20);
|
|
|
|
if (!global_event_list)
|
|
|
|
global_event_list = ArrayList_New(TRUE);
|
|
|
|
|
|
|
|
if (global_event_list)
|
|
|
|
ArrayList_Append(global_event_list, event);
|
|
|
|
#endif
|
2014-11-16 19:06:42 +03:00
|
|
|
return (HANDLE)event;
|
2017-02-20 20:31:58 +03:00
|
|
|
fail:
|
2019-02-08 11:05:59 +03:00
|
|
|
EventCloseHandle_(event);
|
2017-02-20 20:31:58 +03:00
|
|
|
return NULL;
|
2012-09-18 20:57:19 +04:00
|
|
|
}
|
|
|
|
|
2017-02-20 20:31:58 +03:00
|
|
|
HANDLE CreateEventExW(LPSECURITY_ATTRIBUTES lpEventAttributes, LPCWSTR lpName, DWORD dwFlags,
|
|
|
|
DWORD dwDesiredAccess)
|
2012-09-18 20:57:19 +04:00
|
|
|
{
|
2019-02-07 18:43:55 +03:00
|
|
|
BOOL initial = FALSE;
|
|
|
|
BOOL manual = FALSE;
|
|
|
|
|
|
|
|
if (dwFlags & CREATE_EVENT_INITIAL_SET)
|
|
|
|
initial = TRUE;
|
|
|
|
|
|
|
|
if (dwFlags & CREATE_EVENT_MANUAL_RESET)
|
|
|
|
manual = TRUE;
|
|
|
|
|
|
|
|
if (dwDesiredAccess != 0)
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_WARN(TAG, "[%s] does not support dwDesiredAccess 0x%08" PRIx32, lpName,
|
|
|
|
dwDesiredAccess);
|
2019-02-07 18:43:55 +03:00
|
|
|
|
|
|
|
return CreateEventW(lpEventAttributes, manual, initial, lpName);
|
2012-09-18 20:57:19 +04:00
|
|
|
}
|
|
|
|
|
2017-02-20 20:31:58 +03:00
|
|
|
HANDLE CreateEventExA(LPSECURITY_ATTRIBUTES lpEventAttributes, LPCSTR lpName, DWORD dwFlags,
|
|
|
|
DWORD dwDesiredAccess)
|
2012-09-18 20:57:19 +04:00
|
|
|
{
|
2019-02-07 18:43:55 +03:00
|
|
|
BOOL initial = FALSE;
|
|
|
|
BOOL manual = FALSE;
|
|
|
|
|
|
|
|
if (dwFlags & CREATE_EVENT_INITIAL_SET)
|
|
|
|
initial = TRUE;
|
|
|
|
|
|
|
|
if (dwFlags & CREATE_EVENT_MANUAL_RESET)
|
|
|
|
manual = TRUE;
|
|
|
|
|
|
|
|
if (dwDesiredAccess != 0)
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_WARN(TAG, "[%s] does not support dwDesiredAccess 0x%08" PRIx32, lpName,
|
|
|
|
dwDesiredAccess);
|
2019-02-07 18:43:55 +03:00
|
|
|
|
|
|
|
return CreateEventA(lpEventAttributes, manual, initial, lpName);
|
2012-09-18 20:57:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE OpenEventW(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpName)
|
|
|
|
{
|
2019-02-07 18:43:55 +03:00
|
|
|
/* TODO: Implement */
|
|
|
|
WINPR_UNUSED(dwDesiredAccess);
|
|
|
|
WINPR_UNUSED(bInheritHandle);
|
|
|
|
WINPR_UNUSED(lpName);
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_ERR(TAG, "not implemented");
|
2012-09-18 20:57:19 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-05-29 22:14:26 +04:00
|
|
|
|
2012-09-19 02:36:13 +04:00
|
|
|
HANDLE OpenEventA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName)
|
|
|
|
{
|
2019-02-07 18:43:55 +03:00
|
|
|
/* TODO: Implement */
|
|
|
|
WINPR_UNUSED(dwDesiredAccess);
|
|
|
|
WINPR_UNUSED(bInheritHandle);
|
|
|
|
WINPR_UNUSED(lpName);
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_ERR(TAG, "not implemented");
|
2012-09-19 02:36:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-18 20:57:19 +04:00
|
|
|
BOOL SetEvent(HANDLE hEvent)
|
|
|
|
{
|
2012-09-19 02:36:13 +04:00
|
|
|
ULONG Type;
|
2015-07-03 10:25:41 +03:00
|
|
|
WINPR_HANDLE* Object;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_EVENT* event;
|
2012-09-19 02:36:13 +04:00
|
|
|
|
2022-09-21 12:01:52 +03:00
|
|
|
if (!winpr_Handle_GetInfo(hEvent, &Type, &Object) || Type != HANDLE_TYPE_EVENT)
|
2012-12-03 23:57:15 +04:00
|
|
|
{
|
2022-09-21 12:01:52 +03:00
|
|
|
WLog_ERR(TAG, "SetEvent: hEvent is not an event");
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
2012-12-03 23:57:15 +04:00
|
|
|
}
|
2012-09-19 02:36:13 +04:00
|
|
|
|
2022-09-21 12:01:52 +03:00
|
|
|
event = (WINPR_EVENT*)Object;
|
|
|
|
return winpr_event_set(&event->impl);
|
2012-09-18 20:57:19 +04:00
|
|
|
}
|
2012-05-29 22:14:26 +04:00
|
|
|
|
2012-09-18 20:57:19 +04:00
|
|
|
BOOL ResetEvent(HANDLE hEvent)
|
|
|
|
{
|
2012-09-19 02:36:13 +04:00
|
|
|
ULONG Type;
|
2015-07-03 10:25:41 +03:00
|
|
|
WINPR_HANDLE* Object;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_EVENT* event;
|
2012-09-19 02:36:13 +04:00
|
|
|
|
2022-09-21 12:01:52 +03:00
|
|
|
if (!winpr_Handle_GetInfo(hEvent, &Type, &Object) || Type != HANDLE_TYPE_EVENT)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "ResetEvent: hEvent is not an event");
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
2015-06-17 15:13:28 +03:00
|
|
|
return FALSE;
|
2022-09-21 12:01:52 +03:00
|
|
|
}
|
2015-06-17 15:13:28 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
event = (WINPR_EVENT*)Object;
|
2021-03-24 20:32:43 +03:00
|
|
|
return winpr_event_reset(&event->impl);
|
2012-09-18 20:57:19 +04:00
|
|
|
}
|
2012-09-19 02:36:13 +04:00
|
|
|
|
|
|
|
#endif
|
2012-11-27 03:02:41 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
HANDLE CreateFileDescriptorEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset,
|
|
|
|
BOOL bInitialState, int FileDescriptor, ULONG mode)
|
2012-11-27 03:02:41 +04:00
|
|
|
{
|
2012-11-27 05:34:36 +04:00
|
|
|
#ifndef _WIN32
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_EVENT* event;
|
2012-11-27 03:02:41 +04:00
|
|
|
HANDLE handle = NULL;
|
2019-11-06 17:24:51 +03:00
|
|
|
event = (WINPR_EVENT*)calloc(1, sizeof(WINPR_EVENT));
|
2012-11-27 03:02:41 +04:00
|
|
|
|
|
|
|
if (event)
|
|
|
|
{
|
2021-05-26 04:03:03 +03:00
|
|
|
event->impl.fds[0] = -1;
|
|
|
|
event->impl.fds[1] = -1;
|
2012-11-27 03:02:41 +04:00
|
|
|
event->bAttached = TRUE;
|
|
|
|
event->bManualReset = bManualReset;
|
2021-03-24 20:32:43 +03:00
|
|
|
winpr_event_init_from_fd(&event->impl, FileDescriptor);
|
2022-04-27 16:56:39 +03:00
|
|
|
event->common.ops = &ops;
|
2015-07-03 10:25:41 +03:00
|
|
|
WINPR_HANDLE_SET_TYPE_AND_MODE(event, HANDLE_TYPE_EVENT, mode);
|
2019-11-06 17:24:51 +03:00
|
|
|
handle = (HANDLE)event;
|
2012-11-27 03:02:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return handle;
|
2012-11-27 05:34:36 +04:00
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
2012-11-27 03:02:41 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
HANDLE CreateFileDescriptorEventA(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset,
|
|
|
|
BOOL bInitialState, int FileDescriptor, ULONG mode)
|
2012-11-27 03:02:41 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
return CreateFileDescriptorEventW(lpEventAttributes, bManualReset, bInitialState,
|
|
|
|
FileDescriptor, mode);
|
2012-11-27 03:02:41 +04:00
|
|
|
}
|
|
|
|
|
2012-12-21 07:32:02 +04:00
|
|
|
/**
|
|
|
|
* Returns an event based on the handle returned by GetEventWaitObject()
|
|
|
|
*/
|
2019-11-06 17:24:51 +03:00
|
|
|
HANDLE CreateWaitObjectEvent(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset,
|
|
|
|
BOOL bInitialState, void* pObject)
|
2012-12-21 07:32:02 +04:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
2019-11-06 17:24:51 +03:00
|
|
|
return CreateFileDescriptorEventW(lpEventAttributes, bManualReset, bInitialState,
|
|
|
|
(int)(ULONG_PTR)pObject, WINPR_FD_READ);
|
2012-12-21 07:32:02 +04:00
|
|
|
#else
|
|
|
|
HANDLE hEvent = NULL;
|
2017-02-20 20:31:58 +03:00
|
|
|
DuplicateHandle(GetCurrentProcess(), pObject, GetCurrentProcess(), &hEvent, 0, FALSE,
|
|
|
|
DUPLICATE_SAME_ACCESS);
|
2012-12-21 07:32:02 +04:00
|
|
|
return hEvent;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-12-19 21:16:39 +04:00
|
|
|
/*
|
|
|
|
* Returns inner file descriptor for usage with select()
|
|
|
|
* This file descriptor is not usable on Windows
|
|
|
|
*/
|
|
|
|
|
2012-11-27 03:02:41 +04:00
|
|
|
int GetEventFileDescriptor(HANDLE hEvent)
|
|
|
|
{
|
2012-11-27 05:34:36 +04:00
|
|
|
#ifndef _WIN32
|
2022-09-21 12:01:52 +03:00
|
|
|
WINPR_HANDLE* hdl;
|
|
|
|
ULONG type;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hEvent, &type, &hdl) || type != HANDLE_TYPE_EVENT)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "GetEventFileDescriptor: hEvent is not an event");
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-10-02 15:58:32 +03:00
|
|
|
return winpr_Handle_getFd(hEvent);
|
2012-11-27 05:34:36 +04:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
2012-11-27 03:02:41 +04:00
|
|
|
}
|
2012-12-19 21:16:39 +04:00
|
|
|
|
2013-05-01 01:16:38 +04:00
|
|
|
/*
|
|
|
|
* Set inner file descriptor for usage with select()
|
|
|
|
* This file descriptor is not usable on Windows
|
|
|
|
*/
|
|
|
|
|
2015-07-03 10:25:41 +03:00
|
|
|
int SetEventFileDescriptor(HANDLE hEvent, int FileDescriptor, ULONG mode)
|
2013-05-01 01:16:38 +04:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
ULONG Type;
|
2015-07-03 10:25:41 +03:00
|
|
|
WINPR_HANDLE* Object;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_EVENT* event;
|
2013-05-01 01:16:38 +04:00
|
|
|
|
2022-09-21 12:01:52 +03:00
|
|
|
if (!winpr_Handle_GetInfo(hEvent, &Type, &Object) || Type != HANDLE_TYPE_EVENT)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "SetEventFileDescriptor: hEvent is not an event");
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
2013-05-01 01:16:38 +04:00
|
|
|
return -1;
|
2022-09-21 12:01:52 +03:00
|
|
|
}
|
2013-05-01 01:16:38 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
event = (WINPR_EVENT*)Object;
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
if (!event->bAttached && event->impl.fds[0] >= 0 && event->impl.fds[0] != FileDescriptor)
|
|
|
|
close(event->impl.fds[0]);
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2015-07-03 13:31:29 +03:00
|
|
|
event->bAttached = TRUE;
|
2022-04-27 16:56:39 +03:00
|
|
|
event->common.Mode = mode;
|
2021-03-24 20:32:43 +03:00
|
|
|
event->impl.fds[0] = FileDescriptor;
|
2013-05-01 01:16:38 +04:00
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-12-19 21:16:39 +04:00
|
|
|
/**
|
|
|
|
* 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()
|
|
|
|
*/
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void* GetEventWaitObject(HANDLE hEvent)
|
2012-12-19 21:16:39 +04:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
int fd;
|
2014-08-18 21:34:47 +04:00
|
|
|
void* obj;
|
2012-12-19 21:16:39 +04:00
|
|
|
fd = GetEventFileDescriptor(hEvent);
|
2019-11-06 17:24:51 +03:00
|
|
|
obj = ((void*)(long)fd);
|
2013-03-28 04:06:10 +04:00
|
|
|
return obj;
|
2012-12-19 21:16:39 +04:00
|
|
|
#else
|
|
|
|
return hEvent;
|
|
|
|
#endif
|
|
|
|
}
|
2021-09-17 09:50:32 +03:00
|
|
|
#if defined(WITH_DEBUG_EVENTS)
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
|
|
|
|
static BOOL dump_handle_list(void* data, size_t index, va_list ap)
|
|
|
|
{
|
|
|
|
WINPR_EVENT* event = data;
|
|
|
|
dump_event(event, index);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DumpEventHandles_(const char* fkt, const char* file, size_t line)
|
|
|
|
{
|
|
|
|
struct rlimit r = { 0 };
|
|
|
|
int rc = getrlimit(RLIMIT_NOFILE, &r);
|
|
|
|
if (rc >= 0)
|
|
|
|
{
|
|
|
|
rlim_t x;
|
|
|
|
size_t count = 0;
|
|
|
|
for (x = 0; x < r.rlim_cur; x++)
|
|
|
|
{
|
|
|
|
int flags = fcntl(x, F_GETFD);
|
|
|
|
if (flags >= 0)
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
WLog_INFO(TAG, "------- limits [%d/%d] open files %" PRIuz, r.rlim_cur, r.rlim_max, count);
|
|
|
|
}
|
|
|
|
WLog_DBG(TAG, "--------- Start dump [%s %s:%" PRIuz "]", fkt, file, line);
|
|
|
|
if (global_event_list)
|
|
|
|
{
|
|
|
|
ArrayList_Lock(global_event_list);
|
|
|
|
ArrayList_ForEach(global_event_list, dump_handle_list);
|
|
|
|
ArrayList_Unlock(global_event_list);
|
|
|
|
}
|
|
|
|
WLog_DBG(TAG, "--------- End dump [%s %s:%" PRIuz "]", fkt, file, line);
|
|
|
|
}
|
|
|
|
#endif
|