2012-09-18 12:57:19 -04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Synchronization Functions
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2014-07-02 15:15:46 +02:00
|
|
|
* Copyright 2014 Hardening <contact@hardening-consulting.com>
|
2012-09-18 12: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-18 18:36:13 -04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2014-07-02 15:15:46 +02:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
#include <poll.h>
|
2014-07-04 15:29:21 +02:00
|
|
|
#else
|
2014-07-04 16:11:19 +02:00
|
|
|
#ifndef _WIN32
|
2014-07-04 15:29:21 +02:00
|
|
|
#include <sys/select.h>
|
2014-07-02 15:15:46 +02:00
|
|
|
#endif
|
2014-07-04 16:11:19 +02:00
|
|
|
#endif
|
2014-07-02 15:15:46 +02:00
|
|
|
|
2013-08-13 14:05:38 +02:00
|
|
|
#include <assert.h>
|
2013-10-23 12:13:53 +02:00
|
|
|
#include <errno.h>
|
2013-08-13 14:05:38 +02:00
|
|
|
|
2012-09-18 18:36:13 -04:00
|
|
|
#include <winpr/crt.h>
|
2012-09-18 12:57:19 -04:00
|
|
|
#include <winpr/synch.h>
|
2013-10-25 10:48:37 -04:00
|
|
|
#include <winpr/platform.h>
|
2012-09-18 12:57:19 -04:00
|
|
|
|
|
|
|
#include "synch.h"
|
2013-01-23 12:54:09 -05:00
|
|
|
#include "../thread/thread.h"
|
|
|
|
#include <winpr/thread.h>
|
2015-07-03 09:25:41 +02:00
|
|
|
#include <winpr/debug.h>
|
2012-09-18 12:57:19 -04:00
|
|
|
|
2014-08-18 17:22:22 +02:00
|
|
|
#include "../log.h"
|
2014-08-18 18:57:08 +02:00
|
|
|
#define TAG WINPR_TAG("sync.wait")
|
2014-08-18 17:22:22 +02:00
|
|
|
|
2012-09-18 12:57:19 -04:00
|
|
|
/**
|
|
|
|
* WaitForSingleObject
|
|
|
|
* WaitForSingleObjectEx
|
|
|
|
* WaitForMultipleObjectsEx
|
2012-09-18 15:51:33 -04:00
|
|
|
* SignalObjectAndWait
|
2012-09-18 12:57:19 -04:00
|
|
|
*/
|
|
|
|
|
2012-09-18 17:33:52 -04:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
2015-03-10 01:21:28 +03:00
|
|
|
#include <stdlib.h>
|
2013-10-25 10:03:57 -04:00
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
2013-09-22 17:23:00 -04:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2013-05-16 17:32:58 -04:00
|
|
|
#include "../handle/handle.h"
|
|
|
|
|
2013-09-11 19:00:32 -04:00
|
|
|
#include "../pipe/pipe.h"
|
|
|
|
|
2016-09-22 15:42:43 -05:00
|
|
|
/* clock_gettime is not implemented on OSX prior to 10.12 */
|
2013-10-25 10:03:57 -04:00
|
|
|
#ifdef __MACH__
|
|
|
|
|
|
|
|
#include <mach/mach_time.h>
|
|
|
|
|
2017-04-06 08:05:39 +02:00
|
|
|
#ifndef CLOCK_REALTIME
|
2013-10-25 10:03:57 -04:00
|
|
|
#define CLOCK_REALTIME 0
|
2017-04-06 08:05:39 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CLOCK_MONOTONIC
|
2013-10-25 10:03:57 -04:00
|
|
|
#define CLOCK_MONOTONIC 0
|
2017-04-06 08:05:39 +02:00
|
|
|
#endif
|
2013-10-25 10:03:57 -04:00
|
|
|
|
2016-09-22 15:42:43 -05:00
|
|
|
/* clock_gettime is not implemented on OSX prior to 10.12 */
|
2018-10-24 13:17:14 +02:00
|
|
|
int _mach_clock_gettime(int clk_id, struct timespec* t);
|
2016-09-22 15:42:43 -05:00
|
|
|
|
2018-10-24 13:17:14 +02:00
|
|
|
int
|
|
|
|
_mach_clock_gettime(int clk_id, struct timespec* t)
|
2013-10-25 10:03:57 -04:00
|
|
|
{
|
|
|
|
UINT64 time;
|
|
|
|
double seconds;
|
|
|
|
double nseconds;
|
|
|
|
mach_timebase_info_data_t timebase;
|
|
|
|
mach_timebase_info(&timebase);
|
|
|
|
time = mach_absolute_time();
|
|
|
|
nseconds = ((double) time * (double) timebase.numer) / ((double) timebase.denom);
|
|
|
|
seconds = ((double) time * (double) timebase.numer) / ((double) timebase.denom * 1e9);
|
|
|
|
t->tv_sec = seconds;
|
|
|
|
t->tv_nsec = nseconds;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-22 15:42:43 -05: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 13:17:14 +02:00
|
|
|
int _mach_safe_clock_gettime(int clk_id, struct timespec* t);
|
2016-09-22 15:42:43 -05:00
|
|
|
|
|
|
|
int
|
2018-10-24 13:17:14 +02:00
|
|
|
_mach_safe_clock_gettime(int clk_id, struct timespec* t)
|
|
|
|
{
|
|
|
|
if (clock_gettime)
|
|
|
|
{
|
|
|
|
return clock_gettime(clk_id, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _mach_clock_gettime(clk_id, t);
|
2016-09-22 15:42:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#define clock_gettime _mach_safe_clock_gettime
|
|
|
|
#else
|
|
|
|
#define clock_gettime _mach_clock_gettime
|
|
|
|
#endif
|
|
|
|
|
2013-10-25 10:03:57 -04:00
|
|
|
#endif
|
|
|
|
|
2013-11-14 13:48:20 +01:00
|
|
|
|
2018-10-24 13:17:14 +02:00
|
|
|
static long long ts_difftime(const struct timespec* o,
|
|
|
|
const struct timespec* n)
|
2013-11-14 13:48:20 +01:00
|
|
|
{
|
2013-11-18 11:59:53 -05: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 13:48:20 +01:00
|
|
|
}
|
|
|
|
|
2015-04-22 09:30:50 +02:00
|
|
|
/* Drop in replacement for pthread_mutex_timedlock
|
2014-07-16 11:59:26 +02:00
|
|
|
*/
|
2015-04-22 09:30:50 +02:00
|
|
|
#if !defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK)
|
2014-07-16 11:59:26 +02:00
|
|
|
#include <pthread.h>
|
|
|
|
|
2018-10-24 13:17:14 +02:00
|
|
|
static int pthread_mutex_timedlock(pthread_mutex_t* mutex, const struct timespec* timeout)
|
2013-10-23 12:09:09 +02:00
|
|
|
{
|
2013-11-14 09:34:16 +01:00
|
|
|
struct timespec timenow;
|
2013-10-25 10:48:37 -04:00
|
|
|
struct timespec sleepytime;
|
2014-07-14 19:36:31 +02:00
|
|
|
unsigned long long diff;
|
2013-10-25 10:48:37 -04:00
|
|
|
int retcode;
|
|
|
|
/* This is just to avoid a completely busy wait */
|
2014-07-14 19:36:31 +02:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &timenow);
|
|
|
|
diff = ts_difftime(&timenow, timeout);
|
|
|
|
sleepytime.tv_sec = diff / 1000000000LL;
|
|
|
|
sleepytime.tv_nsec = diff % 1000000000LL;
|
2013-10-25 10:48:37 -04:00
|
|
|
|
2014-08-18 17:22:22 +02:00
|
|
|
while ((retcode = pthread_mutex_trylock(mutex)) == EBUSY)
|
2013-10-25 10:48:37 -04:00
|
|
|
{
|
2013-11-14 09:34:16 +01:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &timenow);
|
2013-10-25 10:48:37 -04:00
|
|
|
|
2013-11-14 13:48:20 +01:00
|
|
|
if (ts_difftime(timeout, &timenow) >= 0)
|
2013-10-25 10:48:37 -04:00
|
|
|
{
|
|
|
|
return ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
2014-08-18 17:22:22 +02:00
|
|
|
nanosleep(&sleepytime, NULL);
|
2013-10-25 10:48:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return retcode;
|
2013-10-23 12:09:09 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-07-03 09:25:41 +02:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
static DWORD handle_mode_to_pollevent(ULONG mode)
|
|
|
|
{
|
|
|
|
DWORD event = 0;
|
2018-10-24 13:17:14 +02:00
|
|
|
|
2015-07-14 11:39:41 +02:00
|
|
|
if (mode & WINPR_FD_READ)
|
2015-07-03 09:25:41 +02:00
|
|
|
event |= POLLIN;
|
2018-10-24 13:17:14 +02:00
|
|
|
|
2015-07-14 11:39:41 +02:00
|
|
|
if (mode & WINPR_FD_WRITE)
|
2015-07-03 09:25:41 +02:00
|
|
|
event |= POLLOUT;
|
|
|
|
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-10-24 13:17:14 +02:00
|
|
|
static void ts_add_ms(struct timespec* ts, DWORD dwMilliseconds)
|
2013-08-14 12:15:05 +02:00
|
|
|
{
|
|
|
|
ts->tv_sec += dwMilliseconds / 1000L;
|
|
|
|
ts->tv_nsec += (dwMilliseconds % 1000L) * 1000000L;
|
2014-02-04 11:35:16 -05:00
|
|
|
ts->tv_sec += ts->tv_nsec / 1000000000L;
|
|
|
|
ts->tv_nsec = ts->tv_nsec % 1000000000L;
|
2013-08-14 12:15:05 +02:00
|
|
|
}
|
|
|
|
|
2015-07-03 09:25:41 +02:00
|
|
|
static int waitOnFd(int fd, ULONG mode, DWORD dwMilliseconds)
|
2014-07-02 15:15:46 +02:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
struct pollfd pollfds;
|
|
|
|
pollfds.fd = fd;
|
2015-07-03 09:25:41 +02:00
|
|
|
pollfds.events = handle_mode_to_pollevent(mode);
|
2014-07-02 15:15:46 +02:00
|
|
|
pollfds.revents = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
status = poll(&pollfds, 1, dwMilliseconds);
|
|
|
|
}
|
|
|
|
while ((status < 0) && (errno == EINTR));
|
|
|
|
|
|
|
|
#else
|
2014-07-04 22:14:04 +02:00
|
|
|
struct timeval timeout;
|
2015-07-03 09:25:41 +02:00
|
|
|
fd_set rfds, wfds;
|
|
|
|
fd_set* prfds = NULL;
|
|
|
|
fd_set* pwfds = NULL;
|
|
|
|
fd_set* pefds = NULL;
|
2014-07-02 15:15:46 +02:00
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(fd, &rfds);
|
2015-07-03 09:25:41 +02:00
|
|
|
FD_ZERO(&wfds);
|
|
|
|
FD_SET(fd, &wfds);
|
2014-07-02 15:15:46 +02:00
|
|
|
ZeroMemory(&timeout, sizeof(timeout));
|
|
|
|
|
2015-07-14 11:39:41 +02:00
|
|
|
if (mode & WINPR_FD_READ)
|
2015-07-03 09:25:41 +02:00
|
|
|
prfds = &rfds;
|
2018-10-24 13:17:14 +02:00
|
|
|
|
2015-07-14 11:39:41 +02:00
|
|
|
if (mode & WINPR_FD_WRITE)
|
2015-07-03 09:25:41 +02:00
|
|
|
pwfds = &wfds;
|
|
|
|
|
2014-07-02 15:15:46 +02:00
|
|
|
if ((dwMilliseconds != INFINITE) && (dwMilliseconds != 0))
|
|
|
|
{
|
|
|
|
timeout.tv_sec = dwMilliseconds / 1000;
|
|
|
|
timeout.tv_usec = (dwMilliseconds % 1000) * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2015-07-03 09:25:41 +02:00
|
|
|
status = select(fd + 1, prfds, pwfds, pefds, (dwMilliseconds == INFINITE) ? NULL : &timeout);
|
2014-07-02 15:15:46 +02:00
|
|
|
}
|
|
|
|
while (status < 0 && (errno == EINTR));
|
|
|
|
|
2014-08-18 17:22:22 +02:00
|
|
|
#endif
|
2014-07-02 15:15:46 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2012-09-18 12:57:19 -04:00
|
|
|
DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
|
|
|
|
{
|
2012-09-18 17:33:52 -04:00
|
|
|
ULONG Type;
|
2015-07-03 09:25:41 +02:00
|
|
|
WINPR_HANDLE* Object;
|
2012-09-18 17:33:52 -04:00
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hHandle, &Type, &Object))
|
2013-12-17 18:20:33 -05:00
|
|
|
{
|
2014-08-18 17:22:22 +02:00
|
|
|
WLog_ERR(TAG, "invalid hHandle.");
|
2015-07-30 02:52:11 -07:00
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
2012-09-18 17:33:52 -04:00
|
|
|
return WAIT_FAILED;
|
2013-12-17 18:20:33 -05:00
|
|
|
}
|
2012-09-18 17:33:52 -04:00
|
|
|
|
2015-03-11 17:57:01 +01:00
|
|
|
if (Type == HANDLE_TYPE_PROCESS)
|
2013-09-22 17:23:00 -04:00
|
|
|
{
|
2018-10-24 13:17:14 +02:00
|
|
|
WINPR_PROCESS* process;
|
|
|
|
process = (WINPR_PROCESS*) Object;
|
2013-09-22 17:23:00 -04:00
|
|
|
|
2015-07-03 11:13:48 +02:00
|
|
|
if (process->pid != waitpid(process->pid, &(process->status), 0))
|
2013-09-22 17:23:00 -04:00
|
|
|
{
|
2014-08-18 17:22:22 +02:00
|
|
|
WLog_ERR(TAG, "waitpid failure [%d] %s", errno, strerror(errno));
|
2015-07-30 02:52:11 -07:00
|
|
|
SetLastError(ERROR_INTERNAL_ERROR);
|
2013-09-22 17:23:00 -04:00
|
|
|
return WAIT_FAILED;
|
|
|
|
}
|
2013-09-23 14:44:59 -04:00
|
|
|
|
|
|
|
process->dwExitCode = (DWORD) process->status;
|
2015-03-11 17:57:01 +01:00
|
|
|
return WAIT_OBJECT_0;
|
2013-09-22 17:23:00 -04:00
|
|
|
}
|
2013-02-11 20:38:19 -05:00
|
|
|
else if (Type == HANDLE_TYPE_MUTEX)
|
2012-09-18 17:33:52 -04:00
|
|
|
{
|
2018-10-24 13:17:14 +02:00
|
|
|
WINPR_MUTEX* mutex;
|
|
|
|
mutex = (WINPR_MUTEX*) Object;
|
2013-05-16 17:32:58 -04:00
|
|
|
|
2012-09-18 19:24:03 -04:00
|
|
|
if (dwMilliseconds != INFINITE)
|
2013-08-13 14:05:38 +02:00
|
|
|
{
|
2013-10-25 10:48:37 -04:00
|
|
|
int status;
|
2013-08-13 14:05:38 +02:00
|
|
|
struct timespec timeout;
|
2013-11-14 13:48:20 +01:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &timeout);
|
2014-08-18 17:22:22 +02:00
|
|
|
ts_add_ms(&timeout, dwMilliseconds);
|
2013-10-23 12:16:23 +02:00
|
|
|
status = pthread_mutex_timedlock(&mutex->mutex, &timeout);
|
2013-10-25 10:48:37 -04:00
|
|
|
|
2013-10-23 12:16:23 +02:00
|
|
|
if (ETIMEDOUT == status)
|
|
|
|
return WAIT_TIMEOUT;
|
2013-08-13 14:05:38 +02:00
|
|
|
}
|
|
|
|
else
|
2014-01-24 22:44:23 -05:00
|
|
|
{
|
2013-08-13 14:05:38 +02:00
|
|
|
pthread_mutex_lock(&mutex->mutex);
|
2014-01-24 22:44:23 -05:00
|
|
|
}
|
2012-11-28 12:47:04 -05:00
|
|
|
|
2015-03-11 17:57:01 +01:00
|
|
|
return WAIT_OBJECT_0;
|
2013-07-29 11:57:29 -04:00
|
|
|
}
|
2015-03-11 17:57:01 +01:00
|
|
|
else
|
2013-09-11 19:00:32 -04:00
|
|
|
{
|
|
|
|
int status;
|
2015-03-11 17:57:01 +01:00
|
|
|
int fd = winpr_Handle_getFd(Object);
|
2018-10-24 13:17:14 +02:00
|
|
|
|
2015-03-11 17:57:01 +01:00
|
|
|
if (fd < 0)
|
2015-07-30 02:52:11 -07:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "winpr_Handle_getFd did not return a fd!");
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
2013-09-25 19:13:39 -04:00
|
|
|
return WAIT_FAILED;
|
2015-07-30 02:52:11 -07:00
|
|
|
}
|
|
|
|
|
2015-07-03 09:25:41 +02:00
|
|
|
status = waitOnFd(fd, Object->Mode, dwMilliseconds);
|
2014-08-18 17:22:22 +02:00
|
|
|
|
2013-09-11 19:00:32 -04:00
|
|
|
if (status < 0)
|
2013-12-17 18:20:33 -05:00
|
|
|
{
|
2015-03-11 17:57:01 +01:00
|
|
|
WLog_ERR(TAG, "waitOnFd() failure [%d] %s", errno, strerror(errno));
|
2015-07-30 02:52:11 -07:00
|
|
|
SetLastError(ERROR_INTERNAL_ERROR);
|
2013-09-11 19:00:32 -04:00
|
|
|
return WAIT_FAILED;
|
2013-12-17 18:20:33 -05:00
|
|
|
}
|
2013-09-11 19:00:32 -04:00
|
|
|
|
|
|
|
if (status != 1)
|
|
|
|
return WAIT_TIMEOUT;
|
2015-03-11 17:57:01 +01:00
|
|
|
|
|
|
|
return winpr_Handle_cleanup(Object);
|
2013-02-11 20:38:19 -05:00
|
|
|
}
|
2012-09-18 17:33:52 -04:00
|
|
|
|
2015-07-30 02:52:11 -07:00
|
|
|
SetLastError(ERROR_INTERNAL_ERROR);
|
2015-03-11 17:57:01 +01:00
|
|
|
return WAIT_FAILED;
|
2012-09-18 17:33:52 -04:00
|
|
|
}
|
2012-09-18 12:57:19 -04:00
|
|
|
|
2012-09-18 17:33:52 -04:00
|
|
|
DWORD WaitForSingleObjectEx(HANDLE hHandle, DWORD dwMilliseconds, BOOL bAlertable)
|
|
|
|
{
|
2016-10-06 15:40:11 +02:00
|
|
|
WLog_ERR(TAG, "%s: Not implemented.", __FUNCTION__);
|
2016-06-01 16:26:26 +02:00
|
|
|
SetLastError(ERROR_NOT_SUPPORTED);
|
|
|
|
return WAIT_FAILED;
|
2012-09-18 12:57:19 -04:00
|
|
|
}
|
|
|
|
|
2018-10-24 13:17:14 +02:00
|
|
|
DWORD WaitForMultipleObjects(DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAll,
|
|
|
|
DWORD dwMilliseconds)
|
2012-09-18 12:57:19 -04:00
|
|
|
{
|
2014-07-16 11:59:26 +02:00
|
|
|
struct timespec starttime;
|
|
|
|
struct timespec timenow;
|
|
|
|
unsigned long long diff;
|
|
|
|
DWORD signalled;
|
|
|
|
DWORD polled;
|
2018-10-24 13:17:14 +02:00
|
|
|
DWORD* poll_map = NULL;
|
|
|
|
BOOL* signalled_idx = NULL;
|
2012-12-16 23:03:00 -05:00
|
|
|
int fd = -1;
|
2018-10-24 13:17:14 +02:00
|
|
|
DWORD index;
|
2012-11-26 18:02:41 -05:00
|
|
|
int status;
|
|
|
|
ULONG Type;
|
2015-07-03 09:25:41 +02:00
|
|
|
BOOL signal_handled = FALSE;
|
|
|
|
WINPR_HANDLE* Object;
|
2014-07-02 15:15:46 +02:00
|
|
|
#ifdef HAVE_POLL_H
|
2018-10-24 13:17:14 +02:00
|
|
|
struct pollfd* pollfds;
|
2014-07-02 15:15:46 +02:00
|
|
|
#else
|
|
|
|
int maxfd;
|
2015-07-03 09:25:41 +02:00
|
|
|
fd_set rfds;
|
|
|
|
fd_set wfds;
|
2012-11-26 18:02:41 -05:00
|
|
|
struct timeval timeout;
|
2014-07-02 15:15:46 +02:00
|
|
|
#endif
|
2012-11-26 18:02:41 -05:00
|
|
|
|
2014-07-07 14:50:05 +02:00
|
|
|
if (!nCount || (nCount > MAXIMUM_WAIT_OBJECTS))
|
2013-12-17 18:20:33 -05:00
|
|
|
{
|
2016-12-13 22:47:08 +01:00
|
|
|
WLog_ERR(TAG, "invalid handles count(%"PRIu32")", nCount);
|
2013-01-22 21:58:06 +01:00
|
|
|
return WAIT_FAILED;
|
2013-12-17 18:20:33 -05:00
|
|
|
}
|
|
|
|
|
2014-07-16 11:59:26 +02:00
|
|
|
if (bWaitAll)
|
|
|
|
{
|
|
|
|
signalled_idx = alloca(nCount * sizeof(BOOL));
|
|
|
|
memset(signalled_idx, FALSE, nCount * sizeof(BOOL));
|
|
|
|
poll_map = alloca(nCount * sizeof(DWORD));
|
|
|
|
memset(poll_map, 0, nCount * sizeof(DWORD));
|
|
|
|
}
|
|
|
|
|
2014-07-02 15:15:46 +02:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
pollfds = alloca(nCount * sizeof(struct pollfd));
|
|
|
|
#endif
|
2014-07-16 11:59:26 +02:00
|
|
|
signalled = 0;
|
2014-07-02 15:15:46 +02:00
|
|
|
|
2014-11-03 09:29:35 +01:00
|
|
|
do
|
2013-08-14 12:15:05 +02:00
|
|
|
{
|
2014-07-16 11:59:26 +02:00
|
|
|
#ifndef HAVE_POLL_H
|
2015-07-03 09:25:41 +02:00
|
|
|
fd_set* prfds = NULL;
|
|
|
|
fd_set* pwfds = NULL;
|
2014-07-16 11:59:26 +02:00
|
|
|
maxfd = 0;
|
2015-07-03 09:25:41 +02:00
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_ZERO(&wfds);
|
2014-07-16 11:59:26 +02:00
|
|
|
ZeroMemory(&timeout, sizeof(timeout));
|
|
|
|
#endif
|
2018-10-24 13:17:14 +02:00
|
|
|
|
2015-07-03 09:25:41 +02:00
|
|
|
if (bWaitAll && (dwMilliseconds != INFINITE))
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &starttime);
|
|
|
|
|
2014-10-27 11:39:02 +01:00
|
|
|
polled = 0;
|
2012-11-26 18:02:41 -05:00
|
|
|
|
2014-07-16 11:59:26 +02:00
|
|
|
for (index = 0; index < nCount; index++)
|
2012-11-28 13:38:01 -05:00
|
|
|
{
|
2014-07-16 11:59:26 +02:00
|
|
|
if (bWaitAll)
|
|
|
|
{
|
|
|
|
if (signalled_idx[index])
|
|
|
|
continue;
|
2013-12-17 18:20:33 -05:00
|
|
|
|
2014-10-27 11:39:02 +01:00
|
|
|
poll_map[polled] = index;
|
2014-07-16 11:59:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(lpHandles[index], &Type, &Object))
|
2013-12-17 18:20:33 -05:00
|
|
|
{
|
2014-08-18 17:22:22 +02:00
|
|
|
WLog_ERR(TAG, "invalid event file descriptor");
|
2015-07-30 02:52:11 -07:00
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
2013-12-17 18:20:33 -05:00
|
|
|
return WAIT_FAILED;
|
|
|
|
}
|
2014-07-16 11:59:26 +02:00
|
|
|
|
2015-03-11 17:57:01 +01:00
|
|
|
fd = winpr_Handle_getFd(Object);
|
2013-09-25 19:13:39 -04:00
|
|
|
|
|
|
|
if (fd == -1)
|
2013-12-17 18:20:33 -05:00
|
|
|
{
|
2014-10-27 11:39:02 +01:00
|
|
|
WLog_ERR(TAG, "invalid file descriptor");
|
2015-07-30 02:52:11 -07:00
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
2013-09-25 19:13:39 -04:00
|
|
|
return WAIT_FAILED;
|
2013-12-17 18:20:33 -05:00
|
|
|
}
|
2013-07-04 14:42:40 -04:00
|
|
|
|
2014-07-02 15:15:46 +02:00
|
|
|
#ifdef HAVE_POLL_H
|
2014-10-27 11:39:02 +01:00
|
|
|
pollfds[polled].fd = fd;
|
2015-07-03 09:25:41 +02:00
|
|
|
pollfds[polled].events = handle_mode_to_pollevent(Object->Mode);
|
2014-10-27 11:39:02 +01:00
|
|
|
pollfds[polled].revents = 0;
|
2014-07-02 15:15:46 +02:00
|
|
|
#else
|
2015-07-03 09:25:41 +02:00
|
|
|
FD_SET(fd, &rfds);
|
|
|
|
FD_SET(fd, &wfds);
|
|
|
|
|
2015-07-14 11:39:41 +02:00
|
|
|
if (Object->Mode & WINPR_FD_READ)
|
2015-07-03 09:25:41 +02:00
|
|
|
prfds = &rfds;
|
2018-10-24 13:17:14 +02:00
|
|
|
|
2015-07-14 11:39:41 +02:00
|
|
|
if (Object->Mode & WINPR_FD_WRITE)
|
2015-07-03 09:25:41 +02:00
|
|
|
pwfds = &wfds;
|
2012-11-26 18:02:41 -05:00
|
|
|
|
2014-07-16 11:59:26 +02:00
|
|
|
if (fd > maxfd)
|
|
|
|
maxfd = fd;
|
2014-08-18 17:22:22 +02:00
|
|
|
|
2014-07-02 15:15:46 +02:00
|
|
|
#endif
|
2014-10-27 11:39:02 +01:00
|
|
|
polled++;
|
2014-07-16 11:59:26 +02:00
|
|
|
}
|
2012-11-26 18:02:41 -05:00
|
|
|
|
2014-07-02 15:15:46 +02:00
|
|
|
#ifdef HAVE_POLL_H
|
2014-08-18 17:22:22 +02:00
|
|
|
|
2014-07-16 11:59:26 +02:00
|
|
|
do
|
|
|
|
{
|
2014-10-27 11:39:02 +01:00
|
|
|
status = poll(pollfds, polled, dwMilliseconds);
|
2014-07-16 11:59:26 +02:00
|
|
|
}
|
|
|
|
while (status < 0 && errno == EINTR);
|
2014-08-18 17:22:22 +02:00
|
|
|
|
2014-07-02 15:15:46 +02:00
|
|
|
#else
|
2014-08-18 17:22:22 +02:00
|
|
|
|
2014-07-16 11:59:26 +02:00
|
|
|
if ((dwMilliseconds != INFINITE) && (dwMilliseconds != 0))
|
2013-07-29 11:57:29 -04:00
|
|
|
{
|
2014-07-16 11:59:26 +02:00
|
|
|
timeout.tv_sec = dwMilliseconds / 1000;
|
|
|
|
timeout.tv_usec = (dwMilliseconds % 1000) * 1000;
|
2013-07-29 11:57:29 -04:00
|
|
|
}
|
2014-07-16 11:59:26 +02:00
|
|
|
|
|
|
|
do
|
2013-07-29 11:57:29 -04:00
|
|
|
{
|
2015-07-03 09:25:41 +02:00
|
|
|
status = select(maxfd + 1, prfds, pwfds, 0,
|
2018-10-24 13:17:14 +02:00
|
|
|
(dwMilliseconds == INFINITE) ? NULL : &timeout);
|
2013-07-29 11:57:29 -04:00
|
|
|
}
|
2014-07-16 11:59:26 +02:00
|
|
|
while (status < 0 && errno == EINTR);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (status < 0)
|
2013-07-29 11:57:29 -04:00
|
|
|
{
|
2014-07-16 11:59:26 +02:00
|
|
|
#ifdef HAVE_POLL_H
|
2016-12-13 22:47:08 +01:00
|
|
|
WLog_ERR(TAG, "poll() handle %d (%"PRIu32") failure [%d] %s", index, nCount, errno,
|
2018-10-24 13:17:14 +02:00
|
|
|
strerror(errno));
|
2014-07-16 11:59:26 +02:00
|
|
|
#else
|
2016-12-13 22:47:08 +01:00
|
|
|
WLog_ERR(TAG, "select() handle %d (%"PRIu32") failure [%d] %s", index, nCount, errno,
|
2018-10-24 13:17:14 +02:00
|
|
|
strerror(errno));
|
2014-07-16 11:59:26 +02:00
|
|
|
#endif
|
2015-07-03 09:25:41 +02:00
|
|
|
winpr_log_backtrace(TAG, WLOG_ERROR, 20);
|
2015-07-30 02:52:11 -07:00
|
|
|
SetLastError(ERROR_INTERNAL_ERROR);
|
2014-07-16 11:59:26 +02:00
|
|
|
return WAIT_FAILED;
|
2013-07-29 11:57:29 -04:00
|
|
|
}
|
2014-07-16 11:59:26 +02:00
|
|
|
|
|
|
|
if (status == 0)
|
|
|
|
return WAIT_TIMEOUT;
|
|
|
|
|
|
|
|
if (bWaitAll && (dwMilliseconds != INFINITE))
|
2014-07-14 19:36:31 +02:00
|
|
|
{
|
2014-07-16 11:59:26 +02:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &timenow);
|
|
|
|
diff = ts_difftime(&timenow, &starttime);
|
|
|
|
|
|
|
|
if (diff / 1000 > dwMilliseconds)
|
|
|
|
return WAIT_TIMEOUT;
|
|
|
|
else
|
|
|
|
dwMilliseconds -= (diff / 1000);
|
2014-07-14 19:36:31 +02:00
|
|
|
}
|
2014-07-16 11:59:26 +02:00
|
|
|
|
2015-07-03 09:25:41 +02:00
|
|
|
signal_handled = FALSE;
|
2018-10-24 13:17:14 +02:00
|
|
|
|
2014-07-16 11:59:26 +02:00
|
|
|
for (index = 0; index < polled; index++)
|
2013-09-11 19:00:32 -04:00
|
|
|
{
|
2014-07-16 11:59:26 +02:00
|
|
|
DWORD idx;
|
2015-07-03 09:25:41 +02:00
|
|
|
BOOL signal_set = FALSE;
|
2012-11-26 18:02:41 -05:00
|
|
|
|
2014-07-16 11:59:26 +02:00
|
|
|
if (bWaitAll)
|
|
|
|
idx = poll_map[index];
|
|
|
|
else
|
|
|
|
idx = index;
|
2014-08-18 17:22:22 +02:00
|
|
|
|
2015-03-11 18:14:50 +01:00
|
|
|
if (!winpr_Handle_GetInfo(lpHandles[idx], &Type, &Object))
|
|
|
|
{
|
2018-10-24 13:17:14 +02:00
|
|
|
WLog_ERR(TAG, "invalid hHandle.");
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
return WAIT_FAILED;
|
2015-03-11 18:14:50 +01:00
|
|
|
}
|
2012-11-28 13:38:01 -05:00
|
|
|
|
2015-03-11 17:57:01 +01:00
|
|
|
fd = winpr_Handle_getFd(lpHandles[idx]);
|
|
|
|
|
|
|
|
if (fd == -1)
|
2014-07-16 11:59:26 +02:00
|
|
|
{
|
2015-03-11 17:57:01 +01:00
|
|
|
WLog_ERR(TAG, "invalid file descriptor");
|
2015-07-30 02:52:11 -07:00
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
2015-03-11 17:57:01 +01:00
|
|
|
return WAIT_FAILED;
|
2014-07-16 11:59:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_POLL_H
|
2015-07-03 09:25:41 +02:00
|
|
|
signal_set = pollfds[index].revents & pollfds[index].events;
|
2014-07-16 11:59:26 +02:00
|
|
|
#else
|
2018-10-24 13:17:14 +02:00
|
|
|
|
2015-07-14 11:39:41 +02:00
|
|
|
if (Object->Mode & WINPR_FD_READ)
|
2015-09-18 11:38:27 -04:00
|
|
|
signal_set = FD_ISSET(fd, &rfds) ? 1 : 0;
|
2018-10-24 13:17:14 +02:00
|
|
|
|
2015-07-14 11:39:41 +02:00
|
|
|
if (Object->Mode & WINPR_FD_WRITE)
|
2015-09-18 11:38:27 -04:00
|
|
|
signal_set |= FD_ISSET(fd, &wfds) ? 1 : 0;
|
2018-10-24 13:17:14 +02:00
|
|
|
|
2014-07-16 11:59:26 +02:00
|
|
|
#endif
|
2018-10-24 13:17:14 +02:00
|
|
|
|
2015-07-03 09:25:41 +02:00
|
|
|
if (signal_set)
|
2014-07-16 11:59:26 +02:00
|
|
|
{
|
2015-03-11 17:57:01 +01:00
|
|
|
DWORD rc = winpr_Handle_cleanup(lpHandles[idx]);
|
2018-10-24 13:17:14 +02:00
|
|
|
|
2015-03-11 17:57:01 +01:00
|
|
|
if (rc != WAIT_OBJECT_0)
|
|
|
|
return rc;
|
2014-07-14 19:36:31 +02:00
|
|
|
|
2014-07-16 11:59:26 +02:00
|
|
|
if (bWaitAll)
|
2014-07-14 19:36:31 +02:00
|
|
|
{
|
2014-07-16 11:59:26 +02:00
|
|
|
signalled_idx[idx] = TRUE;
|
|
|
|
|
|
|
|
/* Continue checks from last position. */
|
|
|
|
for (; signalled < nCount; signalled++)
|
|
|
|
{
|
|
|
|
if (!signalled_idx[signalled])
|
|
|
|
break;
|
|
|
|
}
|
2014-07-14 19:36:31 +02:00
|
|
|
}
|
|
|
|
|
2014-07-16 11:59:26 +02:00
|
|
|
if (!bWaitAll)
|
|
|
|
return (WAIT_OBJECT_0 + index);
|
2012-11-28 13:38:01 -05:00
|
|
|
|
2018-11-15 01:51:44 +05:00
|
|
|
if (signalled >= nCount)
|
2014-07-16 11:59:26 +02:00
|
|
|
return (WAIT_OBJECT_0);
|
2015-07-03 09:25:41 +02:00
|
|
|
|
|
|
|
signal_handled = TRUE;
|
2014-07-16 11:59:26 +02:00
|
|
|
}
|
2012-11-28 13:38:01 -05:00
|
|
|
}
|
2012-11-26 18:02:41 -05:00
|
|
|
}
|
2015-07-03 09:25:41 +02:00
|
|
|
while (bWaitAll || !signal_handled);
|
2012-11-26 18:02:41 -05:00
|
|
|
|
2014-08-18 17:22:22 +02:00
|
|
|
WLog_ERR(TAG, "failed (unknown error)");
|
2015-07-30 02:52:11 -07:00
|
|
|
SetLastError(ERROR_INTERNAL_ERROR);
|
2012-11-26 18:02:41 -05:00
|
|
|
return WAIT_FAILED;
|
2012-09-18 12:57:19 -04:00
|
|
|
}
|
|
|
|
|
2018-10-24 13:17:14 +02:00
|
|
|
DWORD WaitForMultipleObjectsEx(DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAll,
|
|
|
|
DWORD dwMilliseconds, BOOL bAlertable)
|
2012-09-18 12:57:19 -04:00
|
|
|
{
|
2015-01-28 13:46:17 -05:00
|
|
|
return WaitForMultipleObjects(nCount, lpHandles, bWaitAll, dwMilliseconds);
|
2012-09-18 12:57:19 -04:00
|
|
|
}
|
2012-09-18 15:51:33 -04:00
|
|
|
|
2018-10-24 13:17:14 +02:00
|
|
|
DWORD SignalObjectAndWait(HANDLE hObjectToSignal, HANDLE hObjectToWaitOn, DWORD dwMilliseconds,
|
|
|
|
BOOL bAlertable)
|
2012-09-18 15:51:33 -04:00
|
|
|
{
|
2016-10-06 15:40:11 +02:00
|
|
|
WLog_ERR(TAG, "%s: Not implemented.", __FUNCTION__);
|
2016-06-01 16:26:26 +02:00
|
|
|
SetLastError(ERROR_NOT_SUPPORTED);
|
|
|
|
return WAIT_FAILED;
|
2012-09-18 15:51:33 -04:00
|
|
|
}
|
2012-09-18 17:33:52 -04:00
|
|
|
|
|
|
|
#endif
|
2014-07-02 15:15:46 +02:00
|
|
|
|