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
|
|
|
|
|
2014-07-10 02:03:20 +04:00
|
|
|
#ifdef HAVE_PTHREAD_GNU_EXT
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#endif
|
|
|
|
|
2012-09-19 02:36:13 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2014-07-02 17:15:46 +04:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
#include <poll.h>
|
2014-07-04 17:29:21 +04:00
|
|
|
#else
|
2014-07-04 18:11:19 +04:00
|
|
|
#ifndef _WIN32
|
2014-07-04 17:29:21 +04:00
|
|
|
#include <sys/select.h>
|
2014-07-02 17:15:46 +04:00
|
|
|
#endif
|
2014-07-04 18:11:19 +04:00
|
|
|
#endif
|
2014-07-02 17:15:46 +04:00
|
|
|
|
2013-08-13 16:05:38 +04:00
|
|
|
#include <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>
|
2012-09-18 20:57:19 +04:00
|
|
|
|
|
|
|
#include "synch.h"
|
2013-01-23 21:54:09 +04:00
|
|
|
#include "../thread/thread.h"
|
|
|
|
#include <winpr/thread.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"
|
|
|
|
|
2013-10-25 18:03:57 +04:00
|
|
|
#ifdef __MACH__
|
|
|
|
|
|
|
|
#include <mach/mach_time.h>
|
|
|
|
|
|
|
|
#define CLOCK_REALTIME 0
|
|
|
|
#define CLOCK_MONOTONIC 0
|
|
|
|
|
2014-11-03 11:29:35 +03:00
|
|
|
int 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();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-11-14 16:48:20 +04:00
|
|
|
|
2014-07-16 13:59:26 +04: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
|
|
|
}
|
|
|
|
|
2014-07-16 13:59:26 +04:00
|
|
|
/* Drop in replacement for the linux pthread_timedjoin_np and
|
|
|
|
* pthread_mutex_timedlock functions.
|
|
|
|
*/
|
|
|
|
#if !defined(HAVE_PTHREAD_GNU_EXT)
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2015-02-02 19:48:54 +03:00
|
|
|
#if defined(__FreeBSD__) || defined(sun)
|
2014-08-18 19:22:22 +04:00
|
|
|
/*the only way to get it work is to remove the static*/
|
2014-07-16 13:59:26 +04:00
|
|
|
int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *timeout)
|
2014-05-01 17:09:35 +04:00
|
|
|
#else
|
2014-07-16 13:59:26 +04:00
|
|
|
static int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *timeout)
|
2014-05-01 17:09:35 +04:00
|
|
|
#endif
|
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
|
|
|
|
|
2014-11-03 11:29:35 +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
|
|
|
}
|
|
|
|
|
2014-07-02 17:15:46 +04:00
|
|
|
static int waitOnFd(int fd, DWORD dwMilliseconds)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
struct pollfd pollfds;
|
|
|
|
pollfds.fd = fd;
|
|
|
|
pollfds.events = POLLIN;
|
|
|
|
pollfds.revents = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
status = poll(&pollfds, 1, dwMilliseconds);
|
|
|
|
}
|
|
|
|
while ((status < 0) && (errno == EINTR));
|
|
|
|
|
|
|
|
#else
|
2014-07-05 00:14:04 +04:00
|
|
|
struct timeval timeout;
|
2014-07-02 17:15:46 +04:00
|
|
|
fd_set rfds;
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(fd, &rfds);
|
|
|
|
ZeroMemory(&timeout, sizeof(timeout));
|
|
|
|
|
|
|
|
if ((dwMilliseconds != INFINITE) && (dwMilliseconds != 0))
|
|
|
|
{
|
|
|
|
timeout.tv_sec = dwMilliseconds / 1000;
|
|
|
|
timeout.tv_usec = (dwMilliseconds % 1000) * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
status = select(fd + 1, &rfds, NULL, NULL, (dwMilliseconds == INFINITE) ? NULL : &timeout);
|
|
|
|
}
|
|
|
|
while (status < 0 && (errno == EINTR));
|
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
#endif
|
2014-07-02 17:15:46 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2012-09-18 20:57:19 +04:00
|
|
|
DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
|
|
|
|
{
|
2012-09-19 01:33:52 +04:00
|
|
|
ULONG Type;
|
|
|
|
PVOID Object;
|
|
|
|
|
|
|
|
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.");
|
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
|
|
|
{
|
2014-07-16 13:59:26 +04:00
|
|
|
WINPR_PROCESS *process;
|
|
|
|
process = (WINPR_PROCESS *) Object;
|
2013-09-23 01:23:00 +04:00
|
|
|
|
|
|
|
if (waitpid(process->pid, &(process->status), 0) != -1)
|
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WLog_ERR(TAG, "waitpid failure [%d] %s", errno, strerror(errno));
|
2013-09-23 01:23:00 +04:00
|
|
|
return WAIT_FAILED;
|
|
|
|
}
|
2013-09-23 22:44:59 +04: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
|
|
|
}
|
2013-02-12 05:38:19 +04:00
|
|
|
else if (Type == HANDLE_TYPE_MUTEX)
|
2012-09-19 01:33:52 +04:00
|
|
|
{
|
2014-07-16 13:59:26 +04:00
|
|
|
WINPR_MUTEX *mutex;
|
|
|
|
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;
|
2015-03-11 19:57:01 +03:00
|
|
|
int fd = winpr_Handle_getFd(Object);
|
|
|
|
if (fd < 0)
|
2013-09-26 03:13:39 +04:00
|
|
|
return WAIT_FAILED;
|
2013-12-18 03:20:33 +04:00
|
|
|
|
2014-07-02 17:15:46 +04:00
|
|
|
status = waitOnFd(fd, dwMilliseconds);
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2013-09-12 03:00:32 +04:00
|
|
|
if (status < 0)
|
2013-12-18 03:20:33 +04:00
|
|
|
{
|
2015-03-11 19:57:01 +03:00
|
|
|
WLog_ERR(TAG, "waitOnFd() failure [%d] %s", errno, strerror(errno));
|
2013-09-12 03:00:32 +04:00
|
|
|
return WAIT_FAILED;
|
2013-12-18 03:20:33 +04:00
|
|
|
}
|
2013-09-12 03:00:32 +04:00
|
|
|
|
|
|
|
if (status != 1)
|
|
|
|
return WAIT_TIMEOUT;
|
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
|
|
|
|
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
|
|
|
|
2012-09-19 01:33:52 +04:00
|
|
|
DWORD WaitForSingleObjectEx(HANDLE hHandle, DWORD dwMilliseconds, BOOL bAlertable)
|
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WLog_ERR(TAG, "Function not implemented.");
|
2013-08-13 16:05:38 +04:00
|
|
|
assert(0);
|
2012-09-18 20:57:19 +04:00
|
|
|
return WAIT_OBJECT_0;
|
|
|
|
}
|
|
|
|
|
2014-07-16 13:59:26 +04:00
|
|
|
DWORD WaitForMultipleObjects(DWORD nCount, const HANDLE *lpHandles, BOOL bWaitAll, DWORD dwMilliseconds)
|
2012-09-18 20:57:19 +04:00
|
|
|
{
|
2014-07-16 13:59:26 +04:00
|
|
|
struct timespec starttime;
|
|
|
|
struct timespec timenow;
|
|
|
|
unsigned long long diff;
|
|
|
|
DWORD signalled;
|
|
|
|
DWORD polled;
|
|
|
|
DWORD *poll_map;
|
|
|
|
BOOL *signalled_idx;
|
2012-12-17 08:03:00 +04:00
|
|
|
int fd = -1;
|
2012-11-27 03:02:41 +04:00
|
|
|
int index;
|
|
|
|
int status;
|
|
|
|
ULONG Type;
|
|
|
|
PVOID Object;
|
2014-07-02 17:15:46 +04:00
|
|
|
#ifdef HAVE_POLL_H
|
2014-11-03 11:29:35 +03:00
|
|
|
struct pollfd *pollfds;
|
2014-07-02 17:15:46 +04:00
|
|
|
#else
|
|
|
|
int maxfd;
|
|
|
|
fd_set fds;
|
2012-11-27 03:02:41 +04:00
|
|
|
struct timeval timeout;
|
2014-07-02 17:15:46 +04:00
|
|
|
#endif
|
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
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WLog_ERR(TAG, "invalid handles count(%d)", nCount);
|
2013-01-23 00:58:06 +04:00
|
|
|
return WAIT_FAILED;
|
2013-12-18 03:20:33 +04:00
|
|
|
}
|
|
|
|
|
2014-07-16 13:59:26 +04: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 17:15:46 +04:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
pollfds = alloca(nCount * sizeof(struct pollfd));
|
|
|
|
#endif
|
2014-07-16 13:59:26 +04:00
|
|
|
signalled = 0;
|
2014-07-02 17:15:46 +04:00
|
|
|
|
2014-11-03 11:29:35 +03:00
|
|
|
do
|
2013-08-14 14:15:05 +04:00
|
|
|
{
|
2014-07-16 13:59:26 +04:00
|
|
|
if (bWaitAll && (dwMilliseconds != INFINITE))
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &starttime);
|
2012-11-27 03:02:41 +04:00
|
|
|
|
2014-07-16 13:59:26 +04:00
|
|
|
#ifndef HAVE_POLL_H
|
|
|
|
maxfd = 0;
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
ZeroMemory(&timeout, sizeof(timeout));
|
|
|
|
#endif
|
2014-10-27 13:39:02 +03:00
|
|
|
polled = 0;
|
2012-11-27 03:02:41 +04:00
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (signalled_idx[index])
|
|
|
|
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");
|
2013-12-18 03:20:33 +04:00
|
|
|
return WAIT_FAILED;
|
|
|
|
}
|
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");
|
2013-09-26 03:13:39 +04:00
|
|
|
return WAIT_FAILED;
|
2013-12-18 03:20:33 +04:00
|
|
|
}
|
2013-07-04 22:42:40 +04:00
|
|
|
|
2014-07-02 17:15:46 +04:00
|
|
|
#ifdef HAVE_POLL_H
|
2014-10-27 13:39:02 +03:00
|
|
|
pollfds[polled].fd = fd;
|
|
|
|
pollfds[polled].events = POLLIN;
|
|
|
|
pollfds[polled].revents = 0;
|
2014-07-02 17:15:46 +04:00
|
|
|
#else
|
2014-07-16 13:59:26 +04:00
|
|
|
FD_SET(fd, &fds);
|
2012-11-27 03:02:41 +04:00
|
|
|
|
2014-07-16 13:59:26 +04:00
|
|
|
if (fd > maxfd)
|
|
|
|
maxfd = fd;
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-07-02 17:15:46 +04:00
|
|
|
#endif
|
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
|
|
|
|
2014-07-02 17:15:46 +04:00
|
|
|
#ifdef HAVE_POLL_H
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-07-16 13:59:26 +04:00
|
|
|
do
|
|
|
|
{
|
2014-10-27 13:39:02 +03:00
|
|
|
status = poll(pollfds, polled, dwMilliseconds);
|
2014-07-16 13:59:26 +04:00
|
|
|
}
|
|
|
|
while (status < 0 && errno == EINTR);
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-07-02 17:15:46 +04:00
|
|
|
#else
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-07-16 13:59:26 +04:00
|
|
|
if ((dwMilliseconds != INFINITE) && (dwMilliseconds != 0))
|
2013-07-29 19:57:29 +04:00
|
|
|
{
|
2014-07-16 13:59:26 +04:00
|
|
|
timeout.tv_sec = dwMilliseconds / 1000;
|
|
|
|
timeout.tv_usec = (dwMilliseconds % 1000) * 1000;
|
2013-07-29 19:57:29 +04:00
|
|
|
}
|
2014-07-16 13:59:26 +04:00
|
|
|
|
|
|
|
do
|
2013-07-29 19:57:29 +04:00
|
|
|
{
|
2014-07-16 13:59:26 +04:00
|
|
|
status = select(maxfd + 1, &fds, 0, 0,
|
|
|
|
(dwMilliseconds == INFINITE) ? NULL : &timeout);
|
2013-07-29 19:57:29 +04:00
|
|
|
}
|
2014-07-16 13:59:26 +04:00
|
|
|
while (status < 0 && errno == EINTR);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (status < 0)
|
2013-07-29 19:57:29 +04:00
|
|
|
{
|
2014-07-16 13:59:26 +04:00
|
|
|
#ifdef HAVE_POLL_H
|
2014-10-27 13:39:02 +03:00
|
|
|
WLog_ERR(TAG, "poll() failure [%d] %s", errno,
|
2014-11-03 11:29:35 +03:00
|
|
|
strerror(errno));
|
2014-07-16 13:59:26 +04:00
|
|
|
#else
|
2014-10-27 13:39:02 +03:00
|
|
|
WLog_ERR(TAG, "select() failure [%d] %s", errno,
|
2014-11-03 11:29:35 +03:00
|
|
|
strerror(errno));
|
2014-07-16 13:59:26 +04:00
|
|
|
#endif
|
|
|
|
return WAIT_FAILED;
|
2013-07-29 19:57:29 +04:00
|
|
|
}
|
2014-07-16 13:59:26 +04:00
|
|
|
|
|
|
|
if (status == 0)
|
|
|
|
return WAIT_TIMEOUT;
|
|
|
|
|
|
|
|
if (bWaitAll && (dwMilliseconds != INFINITE))
|
2014-07-14 21:36:31 +04:00
|
|
|
{
|
2014-07-16 13:59:26 +04: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 21:36:31 +04:00
|
|
|
}
|
2014-07-16 13:59:26 +04:00
|
|
|
|
|
|
|
for (index = 0; index < polled; index++)
|
2013-09-12 03:00:32 +04:00
|
|
|
{
|
2014-07-16 13:59:26 +04:00
|
|
|
DWORD idx;
|
2012-11-27 03:02:41 +04:00
|
|
|
|
2014-07-16 13:59:26 +04:00
|
|
|
if (bWaitAll)
|
|
|
|
idx = poll_map[index];
|
|
|
|
else
|
|
|
|
idx = index;
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2015-03-11 20:14:50 +03:00
|
|
|
if (!winpr_Handle_GetInfo(lpHandles[idx], &Type, &Object))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "invalid hHandle.");
|
|
|
|
return WAIT_FAILED;
|
|
|
|
}
|
2012-11-28 22:38:01 +04:00
|
|
|
|
2015-03-11 19:57:01 +03:00
|
|
|
fd = winpr_Handle_getFd(lpHandles[idx]);
|
|
|
|
|
|
|
|
if (fd == -1)
|
2014-07-16 13:59:26 +04:00
|
|
|
{
|
2015-03-11 19:57:01 +03:00
|
|
|
WLog_ERR(TAG, "invalid file descriptor");
|
|
|
|
return WAIT_FAILED;
|
2014-07-16 13:59:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_POLL_H
|
2013-07-29 21:07:39 +04:00
|
|
|
|
2014-07-16 13:59:26 +04:00
|
|
|
if (pollfds[index].revents & POLLIN)
|
|
|
|
#else
|
|
|
|
if (FD_ISSET(fd, &fds))
|
|
|
|
#endif
|
|
|
|
{
|
2015-03-11 19:57:01 +03:00
|
|
|
DWORD rc = winpr_Handle_cleanup(lpHandles[idx]);
|
|
|
|
if (rc != WAIT_OBJECT_0)
|
|
|
|
return rc;
|
2014-07-14 21:36:31 +04:00
|
|
|
|
2014-07-16 13:59:26 +04:00
|
|
|
if (bWaitAll)
|
2014-07-14 21:36:31 +04:00
|
|
|
{
|
2014-07-16 13:59:26 +04:00
|
|
|
signalled_idx[idx] = TRUE;
|
|
|
|
|
|
|
|
/* Continue checks from last position. */
|
|
|
|
for (; signalled < nCount; signalled++)
|
|
|
|
{
|
|
|
|
if (!signalled_idx[signalled])
|
|
|
|
break;
|
|
|
|
}
|
2014-07-14 21:36:31 +04:00
|
|
|
}
|
|
|
|
|
2014-07-16 13:59:26 +04:00
|
|
|
if (!bWaitAll)
|
|
|
|
return (WAIT_OBJECT_0 + index);
|
2012-11-28 22:38:01 +04:00
|
|
|
|
2014-07-16 13:59:26 +04:00
|
|
|
if (bWaitAll && (signalled >= nCount))
|
|
|
|
return (WAIT_OBJECT_0);
|
|
|
|
}
|
2012-11-28 22:38:01 +04:00
|
|
|
}
|
2012-11-27 03:02:41 +04:00
|
|
|
}
|
2014-07-16 13:59:26 +04:00
|
|
|
while (bWaitAll);
|
2012-11-27 03:02:41 +04:00
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
WLog_ERR(TAG, "failed (unknown error)");
|
2012-11-27 03:02:41 +04:00
|
|
|
return WAIT_FAILED;
|
2012-09-18 20:57:19 +04:00
|
|
|
}
|
|
|
|
|
2014-07-16 13:59:26 +04:00
|
|
|
DWORD WaitForMultipleObjectsEx(DWORD nCount, const HANDLE *lpHandles, BOOL bWaitAll, DWORD dwMilliseconds, BOOL bAlertable)
|
2012-09-18 20:57:19 +04:00
|
|
|
{
|
2015-01-28 21:46:17 +03:00
|
|
|
return WaitForMultipleObjects(nCount, lpHandles, bWaitAll, dwMilliseconds);
|
2012-09-18 20:57:19 +04:00
|
|
|
}
|
2012-09-18 23:51:33 +04:00
|
|
|
|
|
|
|
DWORD SignalObjectAndWait(HANDLE hObjectToSignal, HANDLE hObjectToWaitOn, DWORD dwMilliseconds, BOOL bAlertable)
|
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WLog_ERR(TAG, "Function not implemented.");
|
2013-08-13 16:05:38 +04:00
|
|
|
assert(0);
|
2012-09-18 23:51:33 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2012-09-19 01:33:52 +04:00
|
|
|
|
|
|
|
#endif
|
2014-07-02 17:15:46 +04:00
|
|
|
|