2012-09-18 20:57:19 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Synchronization Functions
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2014-07-02 17:15:46 +04:00
|
|
|
* Copyright 2014 Hardening <contact@hardening-consulting.com>
|
2012-09-18 20:57:19 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-09-19 02:36:13 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2021-06-09 15:03:34 +03:00
|
|
|
#include <winpr/assert.h>
|
2013-10-23 14:13:53 +04:00
|
|
|
#include <errno.h>
|
2013-08-13 16:05:38 +04:00
|
|
|
|
2012-09-19 02:36:13 +04:00
|
|
|
#include <winpr/crt.h>
|
2012-09-18 20:57:19 +04:00
|
|
|
#include <winpr/synch.h>
|
2013-10-25 18:48:37 +04:00
|
|
|
#include <winpr/platform.h>
|
2021-03-24 20:32:43 +03:00
|
|
|
#include <winpr/sysinfo.h>
|
2012-09-18 20:57:19 +04:00
|
|
|
|
|
|
|
#include "synch.h"
|
2021-03-16 17:15:13 +03:00
|
|
|
#include "pollset.h"
|
2013-01-23 21:54:09 +04:00
|
|
|
#include "../thread/thread.h"
|
|
|
|
#include <winpr/thread.h>
|
2015-07-03 10:25:41 +03:00
|
|
|
#include <winpr/debug.h>
|
2012-09-18 20:57:19 +04:00
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
#include "../log.h"
|
2014-08-18 20:57:08 +04:00
|
|
|
#define TAG WINPR_TAG("sync.wait")
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2012-09-18 20:57:19 +04:00
|
|
|
/**
|
|
|
|
* WaitForSingleObject
|
|
|
|
* WaitForSingleObjectEx
|
|
|
|
* WaitForMultipleObjectsEx
|
2012-09-18 23:51:33 +04:00
|
|
|
* SignalObjectAndWait
|
2012-09-18 20:57:19 +04:00
|
|
|
*/
|
|
|
|
|
2012-09-19 01:33:52 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
2015-03-10 01:21:28 +03:00
|
|
|
#include <stdlib.h>
|
2013-10-25 18:03:57 +04:00
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
2013-09-23 01:23:00 +04:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2013-05-17 01:32:58 +04:00
|
|
|
#include "../handle/handle.h"
|
|
|
|
|
2013-09-12 03:00:32 +04:00
|
|
|
#include "../pipe/pipe.h"
|
|
|
|
|
2016-09-22 23:42:43 +03:00
|
|
|
/* clock_gettime is not implemented on OSX prior to 10.12 */
|
2013-10-25 18:03:57 +04:00
|
|
|
#ifdef __MACH__
|
|
|
|
|
|
|
|
#include <mach/mach_time.h>
|
|
|
|
|
2017-04-06 09:05:39 +03:00
|
|
|
#ifndef CLOCK_REALTIME
|
2013-10-25 18:03:57 +04:00
|
|
|
#define CLOCK_REALTIME 0
|
2017-04-06 09:05:39 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CLOCK_MONOTONIC
|
2013-10-25 18:03:57 +04:00
|
|
|
#define CLOCK_MONOTONIC 0
|
2017-04-06 09:05:39 +03:00
|
|
|
#endif
|
2013-10-25 18:03:57 +04:00
|
|
|
|
2016-09-22 23:42:43 +03:00
|
|
|
/* clock_gettime is not implemented on OSX prior to 10.12 */
|
2018-10-24 14:17:14 +03:00
|
|
|
int _mach_clock_gettime(int clk_id, struct timespec* t);
|
2016-09-22 23:42:43 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
int _mach_clock_gettime(int clk_id, struct timespec* t)
|
2013-10-25 18:03:57 +04:00
|
|
|
{
|
|
|
|
UINT64 time;
|
|
|
|
double seconds;
|
|
|
|
double nseconds;
|
|
|
|
mach_timebase_info_data_t timebase;
|
|
|
|
mach_timebase_info(&timebase);
|
|
|
|
time = mach_absolute_time();
|
2019-11-06 17:24:51 +03:00
|
|
|
nseconds = ((double)time * (double)timebase.numer) / ((double)timebase.denom);
|
|
|
|
seconds = ((double)time * (double)timebase.numer) / ((double)timebase.denom * 1e9);
|
2013-10-25 18:03:57 +04:00
|
|
|
t->tv_sec = seconds;
|
|
|
|
t->tv_nsec = nseconds;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-22 23:42:43 +03:00
|
|
|
/* if clock_gettime is declared, then __CLOCK_AVAILABILITY will be defined */
|
|
|
|
#ifdef __CLOCK_AVAILABILITY
|
|
|
|
/* If we compiled with Mac OSX 10.12 or later, then clock_gettime will be declared
|
|
|
|
* * but it may be NULL at runtime. So we need to check before using it. */
|
2018-10-24 14:17:14 +03:00
|
|
|
int _mach_safe_clock_gettime(int clk_id, struct timespec* t);
|
2016-09-22 23:42:43 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
int _mach_safe_clock_gettime(int clk_id, struct timespec* t)
|
2018-10-24 14:17:14 +03:00
|
|
|
{
|
|
|
|
if (clock_gettime)
|
|
|
|
{
|
|
|
|
return clock_gettime(clk_id, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _mach_clock_gettime(clk_id, t);
|
2016-09-22 23:42:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#define clock_gettime _mach_safe_clock_gettime
|
|
|
|
#else
|
|
|
|
#define clock_gettime _mach_clock_gettime
|
|
|
|
#endif
|
|
|
|
|
2013-10-25 18:03:57 +04:00
|
|
|
#endif
|
|
|
|
|
2021-05-27 23:42:19 +03:00
|
|
|
/**
|
|
|
|
* Drop in replacement for pthread_mutex_timedlock
|
|
|
|
* http://code.google.com/p/android/issues/detail?id=7807
|
|
|
|
* http://aleksmaus.blogspot.ca/2011/12/missing-pthreadmutextimedlock-on.html
|
2021-03-24 20:32:43 +03:00
|
|
|
*/
|
|
|
|
#if !defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK)
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static long long ts_difftime(const struct timespec* o, const struct timespec* n)
|
2013-11-14 16:48:20 +04:00
|
|
|
{
|
2013-11-18 20:59:53 +04:00
|
|
|
long long oldValue = o->tv_sec * 1000000000LL + o->tv_nsec;
|
|
|
|
long long newValue = n->tv_sec * 1000000000LL + n->tv_nsec;
|
|
|
|
return newValue - oldValue;
|
2013-11-14 16:48:20 +04:00
|
|
|
}
|
|
|
|
|
2021-05-27 23:42:19 +03:00
|
|
|
#ifdef ANDROID
|
|
|
|
#if (__ANDROID_API__ >= 21)
|
|
|
|
#define CONST_NEEDED const
|
|
|
|
#else
|
|
|
|
#define CONST_NEEDED
|
|
|
|
#endif
|
|
|
|
#define STATIC_NEEDED
|
|
|
|
#else /* ANDROID */
|
|
|
|
#define CONST_NEEDED const
|
|
|
|
#define STATIC_NEEDED static
|
|
|
|
#endif
|
|
|
|
|
|
|
|
STATIC_NEEDED int pthread_mutex_timedlock(pthread_mutex_t* mutex,
|
|
|
|
CONST_NEEDED struct timespec* timeout)
|
2013-10-23 14:09:09 +04:00
|
|
|
{
|
2013-11-14 12:34:16 +04:00
|
|
|
struct timespec timenow;
|
2013-10-25 18:48:37 +04:00
|
|
|
struct timespec sleepytime;
|
2014-07-14 21:36:31 +04:00
|
|
|
unsigned long long diff;
|
2013-10-25 18:48:37 +04:00
|
|
|
int retcode;
|
|
|
|
/* This is just to avoid a completely busy wait */
|
2014-07-14 21:36:31 +04:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &timenow);
|
|
|
|
diff = ts_difftime(&timenow, timeout);
|
|
|
|
sleepytime.tv_sec = diff / 1000000000LL;
|
|
|
|
sleepytime.tv_nsec = diff % 1000000000LL;
|
2013-10-25 18:48:37 +04:00
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
while ((retcode = pthread_mutex_trylock(mutex)) == EBUSY)
|
2013-10-25 18:48:37 +04:00
|
|
|
{
|
2013-11-14 12:34:16 +04:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &timenow);
|
2013-10-25 18:48:37 +04:00
|
|
|
|
2013-11-14 16:48:20 +04:00
|
|
|
if (ts_difftime(timeout, &timenow) >= 0)
|
2013-10-25 18:48:37 +04:00
|
|
|
{
|
|
|
|
return ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
nanosleep(&sleepytime, NULL);
|
2013-10-25 18:48:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return retcode;
|
2013-10-23 14:09:09 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-10-24 14:17:14 +03:00
|
|
|
static void ts_add_ms(struct timespec* ts, DWORD dwMilliseconds)
|
2013-08-14 14:15:05 +04:00
|
|
|
{
|
|
|
|
ts->tv_sec += dwMilliseconds / 1000L;
|
|
|
|
ts->tv_nsec += (dwMilliseconds % 1000L) * 1000000L;
|
2014-02-04 20:35:16 +04:00
|
|
|
ts->tv_sec += ts->tv_nsec / 1000000000L;
|
|
|
|
ts->tv_nsec = ts->tv_nsec % 1000000000L;
|
2013-08-14 14:15:05 +04:00
|
|
|
}
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
DWORD WaitForSingleObjectEx(HANDLE hHandle, DWORD dwMilliseconds, BOOL bAlertable)
|
2012-09-18 20:57:19 +04:00
|
|
|
{
|
2012-09-19 01:33:52 +04:00
|
|
|
ULONG Type;
|
2015-07-03 10:25:41 +03:00
|
|
|
WINPR_HANDLE* Object;
|
2021-03-24 20:32:43 +03:00
|
|
|
WINPR_POLL_SET pollset;
|
2012-09-19 01:33:52 +04:00
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hHandle, &Type, &Object))
|
2013-12-18 03:20:33 +04:00
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WLog_ERR(TAG, "invalid hHandle.");
|
2015-07-30 12:52:11 +03:00
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
2012-09-19 01:33:52 +04:00
|
|
|
return WAIT_FAILED;
|
2013-12-18 03:20:33 +04:00
|
|
|
}
|
2012-09-19 01:33:52 +04:00
|
|
|
|
2015-03-11 19:57:01 +03:00
|
|
|
if (Type == HANDLE_TYPE_PROCESS)
|
2013-09-23 01:23:00 +04:00
|
|
|
{
|
2021-03-16 17:15:13 +03:00
|
|
|
WINPR_PROCESS* process = (WINPR_PROCESS*)Object;
|
2013-09-23 01:23:00 +04:00
|
|
|
|
2015-07-03 12:13:48 +03:00
|
|
|
if (process->pid != waitpid(process->pid, &(process->status), 0))
|
2013-09-23 01:23:00 +04:00
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WLog_ERR(TAG, "waitpid failure [%d] %s", errno, strerror(errno));
|
2015-07-30 12:52:11 +03:00
|
|
|
SetLastError(ERROR_INTERNAL_ERROR);
|
2013-09-23 01:23:00 +04:00
|
|
|
return WAIT_FAILED;
|
|
|
|
}
|
2013-09-23 22:44:59 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
process->dwExitCode = (DWORD)process->status;
|
2015-03-11 19:57:01 +03:00
|
|
|
return WAIT_OBJECT_0;
|
2013-09-23 01:23:00 +04:00
|
|
|
}
|
2021-03-16 17:15:13 +03:00
|
|
|
|
|
|
|
if (Type == HANDLE_TYPE_MUTEX)
|
2012-09-19 01:33:52 +04:00
|
|
|
{
|
2018-10-24 14:17:14 +03:00
|
|
|
WINPR_MUTEX* mutex;
|
2019-11-06 17:24:51 +03:00
|
|
|
mutex = (WINPR_MUTEX*)Object;
|
2013-05-17 01:32:58 +04:00
|
|
|
|
2012-09-19 03:24:03 +04:00
|
|
|
if (dwMilliseconds != INFINITE)
|
2013-08-13 16:05:38 +04:00
|
|
|
{
|
2013-10-25 18:48:37 +04:00
|
|
|
int status;
|
2013-08-13 16:05:38 +04:00
|
|
|
struct timespec timeout;
|
2013-11-14 16:48:20 +04:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &timeout);
|
2014-08-18 19:22:22 +04:00
|
|
|
ts_add_ms(&timeout, dwMilliseconds);
|
2013-10-23 14:16:23 +04:00
|
|
|
status = pthread_mutex_timedlock(&mutex->mutex, &timeout);
|
2013-10-25 18:48:37 +04:00
|
|
|
|
2013-10-23 14:16:23 +04:00
|
|
|
if (ETIMEDOUT == status)
|
|
|
|
return WAIT_TIMEOUT;
|
2013-08-13 16:05:38 +04:00
|
|
|
}
|
|
|
|
else
|
2014-01-25 07:44:23 +04:00
|
|
|
{
|
2013-08-13 16:05:38 +04:00
|
|
|
pthread_mutex_lock(&mutex->mutex);
|
2014-01-25 07:44:23 +04:00
|
|
|
}
|
2012-11-28 21:47:04 +04:00
|
|
|
|
2015-03-11 19:57:01 +03:00
|
|
|
return WAIT_OBJECT_0;
|
2013-07-29 19:57:29 +04:00
|
|
|
}
|
2015-03-11 19:57:01 +03:00
|
|
|
else
|
2013-09-12 03:00:32 +04:00
|
|
|
{
|
|
|
|
int status;
|
2021-04-28 17:57:02 +03:00
|
|
|
WINPR_THREAD* thread = NULL;
|
2021-03-24 20:32:43 +03:00
|
|
|
BOOL isSet = FALSE;
|
|
|
|
size_t extraFds = 0;
|
|
|
|
DWORD ret;
|
|
|
|
BOOL autoSignaled = FALSE;
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
if (bAlertable)
|
|
|
|
{
|
|
|
|
thread = (WINPR_THREAD*)_GetCurrentThread();
|
|
|
|
if (!thread)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "failed to retrieve currentThread");
|
|
|
|
return WAIT_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* treat reentrancy, we can't switch to alertable state when we're already
|
|
|
|
treating completions */
|
|
|
|
if (thread->apc.treatingCompletions)
|
|
|
|
bAlertable = FALSE;
|
|
|
|
else
|
|
|
|
extraFds = thread->apc.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = winpr_Handle_getFd(Object);
|
2015-03-11 19:57:01 +03:00
|
|
|
if (fd < 0)
|
2015-07-30 12:52:11 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "winpr_Handle_getFd did not return a fd!");
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
2013-09-26 03:13:39 +04:00
|
|
|
return WAIT_FAILED;
|
2015-07-30 12:52:11 +03:00
|
|
|
}
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
if (!pollset_init(&pollset, 1 + extraFds))
|
2021-03-16 17:15:13 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "unable to initialize pollset");
|
|
|
|
SetLastError(ERROR_INTERNAL_ERROR);
|
|
|
|
return WAIT_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pollset_add(&pollset, fd, Object->Mode))
|
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
WLog_ERR(TAG, "unable to add fd in pollset");
|
|
|
|
goto out;
|
2021-03-16 17:15:13 +03:00
|
|
|
}
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
if (bAlertable && !apc_collectFds(thread, &pollset, &autoSignaled))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "unable to collect APC fds");
|
|
|
|
goto out;
|
|
|
|
}
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
if (!autoSignaled)
|
2013-12-18 03:20:33 +04:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
status = pollset_poll(&pollset, dwMilliseconds);
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "waitOnFd() failure [%d] %s", errno, strerror(errno));
|
|
|
|
goto out;
|
|
|
|
}
|
2013-12-18 03:20:33 +04:00
|
|
|
}
|
2013-09-12 03:00:32 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
ret = WAIT_TIMEOUT;
|
|
|
|
if (bAlertable && apc_executeCompletions(thread, &pollset, 1))
|
|
|
|
ret = WAIT_IO_COMPLETION;
|
|
|
|
|
|
|
|
isSet = pollset_isSignaled(&pollset, 0);
|
|
|
|
pollset_uninit(&pollset);
|
|
|
|
|
|
|
|
if (!isSet)
|
|
|
|
return ret;
|
2015-03-11 19:57:01 +03:00
|
|
|
|
|
|
|
return winpr_Handle_cleanup(Object);
|
2013-02-12 05:38:19 +04:00
|
|
|
}
|
2012-09-19 01:33:52 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
out:
|
|
|
|
pollset_uninit(&pollset);
|
2015-07-30 12:52:11 +03:00
|
|
|
SetLastError(ERROR_INTERNAL_ERROR);
|
2015-03-11 19:57:01 +03:00
|
|
|
return WAIT_FAILED;
|
2012-09-19 01:33:52 +04:00
|
|
|
}
|
2012-09-18 20:57:19 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
|
2012-09-19 01:33:52 +04:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
return WaitForSingleObjectEx(hHandle, dwMilliseconds, FALSE);
|
2012-09-18 20:57:19 +04:00
|
|
|
}
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
DWORD WaitForMultipleObjectsEx(DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAll,
|
|
|
|
DWORD dwMilliseconds, BOOL bAlertable)
|
2012-09-18 20:57:19 +04:00
|
|
|
{
|
2014-07-16 13:59:26 +04:00
|
|
|
DWORD signalled;
|
|
|
|
DWORD polled;
|
2021-08-25 12:10:15 +03:00
|
|
|
DWORD poll_map[MAXIMUM_WAIT_OBJECTS] = { 0 };
|
|
|
|
BOOL signalled_handles[MAXIMUM_WAIT_OBJECTS] = { FALSE };
|
2012-12-17 08:03:00 +04:00
|
|
|
int fd = -1;
|
2018-10-24 14:17:14 +03:00
|
|
|
DWORD index;
|
2012-11-27 03:02:41 +04:00
|
|
|
int status;
|
|
|
|
ULONG Type;
|
2015-07-03 10:25:41 +03:00
|
|
|
WINPR_HANDLE* Object;
|
2021-04-28 17:57:02 +03:00
|
|
|
WINPR_THREAD* thread = NULL;
|
2021-03-16 17:15:13 +03:00
|
|
|
WINPR_POLL_SET pollset;
|
|
|
|
DWORD ret = WAIT_FAILED;
|
2021-03-24 20:32:43 +03:00
|
|
|
size_t extraFds = 0;
|
|
|
|
UINT64 now, dueTime;
|
2012-11-27 03:02:41 +04:00
|
|
|
|
2014-07-07 16:50:05 +04:00
|
|
|
if (!nCount || (nCount > MAXIMUM_WAIT_OBJECTS))
|
2013-12-18 03:20:33 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "invalid handles count(%" PRIu32 ")", nCount);
|
2013-01-23 00:58:06 +04:00
|
|
|
return WAIT_FAILED;
|
2013-12-18 03:20:33 +04:00
|
|
|
}
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
if (bAlertable)
|
|
|
|
{
|
|
|
|
thread = winpr_GetCurrentThread();
|
|
|
|
if (!thread)
|
|
|
|
return WAIT_FAILED;
|
|
|
|
|
|
|
|
/* treat reentrancy, we can't switch to alertable state when we're already
|
|
|
|
treating completions */
|
|
|
|
if (thread->apc.treatingCompletions)
|
|
|
|
bAlertable = FALSE;
|
|
|
|
else
|
|
|
|
extraFds = thread->apc.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pollset_init(&pollset, nCount + extraFds))
|
2021-03-16 17:15:13 +03:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
WLog_ERR(TAG, "unable to initialize pollset for nCount=%" PRIu32 " extraCount=%" PRIu32 "",
|
|
|
|
nCount, extraFds);
|
2021-03-16 17:15:13 +03:00
|
|
|
return WAIT_FAILED;
|
|
|
|
}
|
|
|
|
|
2014-07-16 13:59:26 +04:00
|
|
|
signalled = 0;
|
2014-07-02 17:15:46 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
now = GetTickCount64();
|
|
|
|
if (dwMilliseconds != INFINITE)
|
|
|
|
dueTime = now + dwMilliseconds;
|
|
|
|
else
|
|
|
|
dueTime = 0xFFFFFFFFFFFFFFFF;
|
2021-03-16 17:15:13 +03:00
|
|
|
|
2014-11-03 11:29:35 +03:00
|
|
|
do
|
2013-08-14 14:15:05 +04:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
BOOL autoSignaled = FALSE;
|
2014-10-27 13:39:02 +03:00
|
|
|
polled = 0;
|
2012-11-27 03:02:41 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
/* first collect file descriptors to poll */
|
2014-07-16 13:59:26 +04:00
|
|
|
for (index = 0; index < nCount; index++)
|
2012-11-28 22:38:01 +04:00
|
|
|
{
|
2014-07-16 13:59:26 +04:00
|
|
|
if (bWaitAll)
|
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
if (signalled_handles[index])
|
2014-07-16 13:59:26 +04:00
|
|
|
continue;
|
2013-12-18 03:20:33 +04:00
|
|
|
|
2014-10-27 13:39:02 +03:00
|
|
|
poll_map[polled] = index;
|
2014-07-16 13:59:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(lpHandles[index], &Type, &Object))
|
2013-12-18 03:20:33 +04:00
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WLog_ERR(TAG, "invalid event file descriptor");
|
2015-07-30 12:52:11 +03:00
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
2021-03-16 17:15:13 +03:00
|
|
|
goto out;
|
2013-12-18 03:20:33 +04:00
|
|
|
}
|
2014-07-16 13:59:26 +04:00
|
|
|
|
2015-03-11 19:57:01 +03:00
|
|
|
fd = winpr_Handle_getFd(Object);
|
2013-09-26 03:13:39 +04:00
|
|
|
if (fd == -1)
|
2013-12-18 03:20:33 +04:00
|
|
|
{
|
2014-10-27 13:39:02 +03:00
|
|
|
WLog_ERR(TAG, "invalid file descriptor");
|
2015-07-30 12:52:11 +03:00
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
2021-03-16 17:15:13 +03:00
|
|
|
goto out;
|
2013-12-18 03:20:33 +04:00
|
|
|
}
|
2013-07-04 22:42:40 +04:00
|
|
|
|
2021-03-16 17:15:13 +03:00
|
|
|
if (!pollset_add(&pollset, fd, Object->Mode))
|
2020-02-18 13:54:28 +03:00
|
|
|
{
|
2021-03-16 17:15:13 +03:00
|
|
|
WLog_ERR(TAG, "unable to register fd in pollset");
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
goto out;
|
2020-02-18 13:54:28 +03:00
|
|
|
}
|
2012-11-27 03:02:41 +04:00
|
|
|
|
2014-10-27 13:39:02 +03:00
|
|
|
polled++;
|
2014-07-16 13:59:26 +04:00
|
|
|
}
|
2012-11-27 03:02:41 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
/* treat file descriptors of the APC if needed */
|
|
|
|
if (bAlertable && !apc_collectFds(thread, &pollset, &autoSignaled))
|
2013-07-29 19:57:29 +04:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
WLog_ERR(TAG, "unable to register APC fds");
|
2015-07-30 12:52:11 +03:00
|
|
|
SetLastError(ERROR_INTERNAL_ERROR);
|
2021-03-16 17:15:13 +03:00
|
|
|
goto out;
|
2013-07-29 19:57:29 +04:00
|
|
|
}
|
2014-07-16 13:59:26 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
/* poll file descriptors */
|
|
|
|
status = 0;
|
|
|
|
if (!autoSignaled)
|
2021-03-16 17:15:13 +03:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
DWORD waitTime;
|
2014-07-16 13:59:26 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
if (dwMilliseconds == INFINITE)
|
|
|
|
waitTime = INFINITE;
|
|
|
|
else
|
|
|
|
waitTime = (DWORD)(dueTime - now);
|
2014-07-16 13:59:26 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
status = pollset_poll(&pollset, waitTime);
|
|
|
|
if (status < 0)
|
2021-03-16 17:15:13 +03:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
WLog_ERR(TAG, "poll() handle %d (%" PRIu32 ") failure [%d] %s", index, nCount,
|
|
|
|
errno, strerror(errno));
|
|
|
|
#else
|
|
|
|
WLog_ERR(TAG, "select() handle %d (%" PRIu32 ") failure [%d] %s", index, nCount,
|
|
|
|
errno, strerror(errno));
|
|
|
|
#endif
|
|
|
|
winpr_log_backtrace(TAG, WLOG_ERROR, 20);
|
|
|
|
SetLastError(ERROR_INTERNAL_ERROR);
|
2021-03-16 17:15:13 +03:00
|
|
|
goto out;
|
|
|
|
}
|
2014-07-14 21:36:31 +04:00
|
|
|
}
|
2014-07-16 13:59:26 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
/* give priority to the APC queue, to return WAIT_IO_COMPLETION */
|
|
|
|
if (bAlertable && apc_executeCompletions(thread, &pollset, polled))
|
2013-09-12 03:00:32 +04:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
ret = WAIT_IO_COMPLETION;
|
|
|
|
goto out;
|
|
|
|
}
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
/* then treat pollset */
|
|
|
|
if (status)
|
|
|
|
{
|
|
|
|
for (index = 0; index < polled; index++)
|
2014-07-16 13:59:26 +04:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
DWORD handlesIndex;
|
|
|
|
BOOL signal_set = FALSE;
|
2014-07-14 21:36:31 +04:00
|
|
|
|
2014-07-16 13:59:26 +04:00
|
|
|
if (bWaitAll)
|
2021-03-24 20:32:43 +03:00
|
|
|
handlesIndex = poll_map[index];
|
|
|
|
else
|
|
|
|
handlesIndex = index;
|
2014-07-16 13:59:26 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
signal_set = pollset_isSignaled(&pollset, index);
|
|
|
|
if (signal_set)
|
|
|
|
{
|
|
|
|
DWORD rc = winpr_Handle_cleanup(lpHandles[handlesIndex]);
|
|
|
|
if (rc != WAIT_OBJECT_0)
|
2014-07-16 13:59:26 +04:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
WLog_ERR(TAG, "error in cleanup function for handle at index=%d",
|
|
|
|
handlesIndex);
|
|
|
|
ret = rc;
|
|
|
|
goto out;
|
2014-07-16 13:59:26 +04:00
|
|
|
}
|
2014-07-14 21:36:31 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
if (bWaitAll)
|
|
|
|
{
|
|
|
|
signalled_handles[handlesIndex] = TRUE;
|
|
|
|
|
|
|
|
/* Continue checks from last position. */
|
|
|
|
for (; signalled < nCount; signalled++)
|
|
|
|
{
|
|
|
|
if (!signalled_handles[signalled])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = (WAIT_OBJECT_0 + handlesIndex);
|
|
|
|
goto out;
|
|
|
|
}
|
2012-11-28 22:38:01 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
if (signalled >= nCount)
|
|
|
|
{
|
|
|
|
ret = WAIT_OBJECT_0;
|
|
|
|
goto out;
|
|
|
|
}
|
2021-03-16 17:15:13 +03:00
|
|
|
}
|
2021-03-24 20:32:43 +03:00
|
|
|
}
|
|
|
|
}
|
2015-07-03 10:25:41 +03:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
if (bAlertable && thread->apc.length > extraFds)
|
|
|
|
{
|
|
|
|
pollset_uninit(&pollset);
|
|
|
|
extraFds = thread->apc.length;
|
|
|
|
if (!pollset_init(&pollset, nCount + extraFds))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "unable reallocate pollset");
|
|
|
|
SetLastError(ERROR_INTERNAL_ERROR);
|
|
|
|
return WAIT_FAILED;
|
2014-07-16 13:59:26 +04:00
|
|
|
}
|
2012-11-28 22:38:01 +04:00
|
|
|
}
|
2021-03-24 20:32:43 +03:00
|
|
|
else
|
|
|
|
pollset_reset(&pollset);
|
2021-03-16 17:15:13 +03:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
now = GetTickCount64();
|
|
|
|
} while (now < dueTime);
|
2012-11-27 03:02:41 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
ret = WAIT_TIMEOUT;
|
2021-03-16 17:15:13 +03:00
|
|
|
|
|
|
|
out:
|
|
|
|
pollset_uninit(&pollset);
|
|
|
|
return ret;
|
2012-09-18 20:57:19 +04:00
|
|
|
}
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
DWORD WaitForMultipleObjects(DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAll,
|
|
|
|
DWORD dwMilliseconds)
|
2012-09-18 20:57:19 +04:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
return WaitForMultipleObjectsEx(nCount, lpHandles, bWaitAll, dwMilliseconds, FALSE);
|
2012-09-18 20:57:19 +04:00
|
|
|
}
|
2012-09-18 23:51:33 +04:00
|
|
|
|
2018-10-24 14:17:14 +03:00
|
|
|
DWORD SignalObjectAndWait(HANDLE hObjectToSignal, HANDLE hObjectToWaitOn, DWORD dwMilliseconds,
|
|
|
|
BOOL bAlertable)
|
2012-09-18 23:51:33 +04:00
|
|
|
{
|
2021-03-16 17:15:13 +03:00
|
|
|
if (!SetEvent(hObjectToSignal))
|
|
|
|
return WAIT_FAILED;
|
|
|
|
|
|
|
|
return WaitForSingleObjectEx(hObjectToWaitOn, dwMilliseconds, bAlertable);
|
2012-09-18 23:51:33 +04:00
|
|
|
}
|
2012-09-19 01:33:52 +04:00
|
|
|
|
|
|
|
#endif
|