FreeRDP
wait.c
1 
21 #include <winpr/config.h>
22 
23 #ifdef WINPR_HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 
27 #include <winpr/assert.h>
28 #include <errno.h>
29 
30 #include <winpr/crt.h>
31 #include <winpr/synch.h>
32 #include <winpr/platform.h>
33 #include <winpr/sysinfo.h>
34 
35 #include "synch.h"
36 #include "pollset.h"
37 #include "../thread/thread.h"
38 #include <winpr/thread.h>
39 #include <winpr/debug.h>
40 
41 #include "../log.h"
42 #define TAG WINPR_TAG("sync.wait")
43 
51 #ifndef _WIN32
52 
53 #include <stdlib.h>
54 #include <time.h>
55 #include <sys/time.h>
56 #include <sys/wait.h>
57 
58 #include "../handle/handle.h"
59 
60 #include "../pipe/pipe.h"
61 
62 static struct timespec ts_from_ns(void)
63 {
64  const UINT64 ns = winpr_GetUnixTimeNS();
65  struct timespec timeout = { 0 };
66  timeout.tv_sec = WINPR_TIME_NS_TO_S(ns);
67  timeout.tv_nsec = WINPR_TIME_NS_REM_NS(ns);
68  return timeout;
69 }
70 
76 #if !defined(WINPR_HAVE_PTHREAD_MUTEX_TIMEDLOCK)
77 #include <pthread.h>
78 
79 static long long ts_difftime(const struct timespec* o, const struct timespec* n)
80 {
81  long long oldValue = o->tv_sec * 1000000000LL + o->tv_nsec;
82  long long newValue = n->tv_sec * 1000000000LL + n->tv_nsec;
83  return newValue - oldValue;
84 }
85 
86 #ifdef ANDROID
87 #if (__ANDROID_API__ >= 21)
88 #define CONST_NEEDED const
89 #else
90 #define CONST_NEEDED
91 #endif
92 #define STATIC_NEEDED
93 #else /* ANDROID */
94 #define CONST_NEEDED const
95 #define STATIC_NEEDED static
96 #endif
97 
98 STATIC_NEEDED int pthread_mutex_timedlock(pthread_mutex_t* mutex,
99  CONST_NEEDED struct timespec* timeout)
100 {
101  struct timespec timenow = { 0 };
102  struct timespec sleepytime = { 0 };
103  unsigned long long diff = 0;
104  int retcode = -1;
105  /* This is just to avoid a completely busy wait */
106  timenow = ts_from_ns();
107  diff = ts_difftime(&timenow, timeout);
108  sleepytime.tv_sec = diff / 1000000000LL;
109  sleepytime.tv_nsec = diff % 1000000000LL;
110 
111  while ((retcode = pthread_mutex_trylock(mutex)) == EBUSY)
112  {
113  timenow = ts_from_ns();
114 
115  if (ts_difftime(timeout, &timenow) >= 0)
116  {
117  return ETIMEDOUT;
118  }
119 
120  nanosleep(&sleepytime, NULL);
121  }
122 
123  return retcode;
124 }
125 #endif
126 
127 static void ts_add_ms(struct timespec* ts, DWORD dwMilliseconds)
128 {
129  ts->tv_sec += dwMilliseconds / 1000L;
130  ts->tv_nsec += (dwMilliseconds % 1000L) * 1000000L;
131  ts->tv_sec += ts->tv_nsec / 1000000000L;
132  ts->tv_nsec = ts->tv_nsec % 1000000000L;
133 }
134 
135 DWORD WaitForSingleObjectEx(HANDLE hHandle, DWORD dwMilliseconds, BOOL bAlertable)
136 {
137  ULONG Type = 0;
138  WINPR_HANDLE* Object = NULL;
139  WINPR_POLL_SET pollset = { 0 };
140 
141  if (!winpr_Handle_GetInfo(hHandle, &Type, &Object))
142  {
143  WLog_ERR(TAG, "invalid hHandle.");
144  SetLastError(ERROR_INVALID_HANDLE);
145  return WAIT_FAILED;
146  }
147 
148  if (Type == HANDLE_TYPE_PROCESS && winpr_Handle_getFd(hHandle) == -1)
149  {
150  /* note: if we have pidfd support (under linux and we have managed to associate a
151  * pidfd with our process), we use the regular method with pollset below.
152  * If not (on other platforms) we do a waitpid */
153  WINPR_PROCESS* process = (WINPR_PROCESS*)Object;
154 
155  do
156  {
157  DWORD status = 0;
158  DWORD waitDelay = 0;
159  int ret = waitpid(process->pid, &(process->status), WNOHANG);
160  if (ret == process->pid)
161  {
162  process->dwExitCode = (DWORD)process->status;
163  return WAIT_OBJECT_0;
164  }
165  else if (ret < 0)
166  {
167  char ebuffer[256] = { 0 };
168  WLog_ERR(TAG, "waitpid failure [%d] %s", errno,
169  winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
170  SetLastError(ERROR_INTERNAL_ERROR);
171  return WAIT_FAILED;
172  }
173 
174  /* sleep by slices of 50ms */
175  waitDelay = (dwMilliseconds < 50) ? dwMilliseconds : 50;
176 
177  status = SleepEx(waitDelay, bAlertable);
178  if (status != 0)
179  return status;
180 
181  dwMilliseconds -= waitDelay;
182 
183  } while (dwMilliseconds > 50);
184 
185  return WAIT_TIMEOUT;
186  }
187 
188  if (Type == HANDLE_TYPE_MUTEX)
189  {
190  WINPR_MUTEX* mutex = (WINPR_MUTEX*)Object;
191 
192  if (dwMilliseconds != INFINITE)
193  {
194  int status = 0;
195  struct timespec timeout = ts_from_ns();
196 
197  ts_add_ms(&timeout, dwMilliseconds);
198  status = pthread_mutex_timedlock(&mutex->mutex, &timeout);
199 
200  if (ETIMEDOUT == status)
201  return WAIT_TIMEOUT;
202  }
203  else
204  {
205  pthread_mutex_lock(&mutex->mutex);
206  }
207 
208  return WAIT_OBJECT_0;
209  }
210  else
211  {
212  int status = -1;
213  WINPR_THREAD* thread = NULL;
214  BOOL isSet = FALSE;
215  size_t extraFds = 0;
216  DWORD ret = 0;
217  BOOL autoSignaled = FALSE;
218 
219  if (bAlertable)
220  {
221  thread = (WINPR_THREAD*)_GetCurrentThread();
222  if (thread)
223  {
224  /* treat reentrancy, we can't switch to alertable state when we're already
225  treating completions */
226  if (thread->apc.treatingCompletions)
227  bAlertable = FALSE;
228  else
229  extraFds = thread->apc.length;
230  }
231  else
232  {
233  /* called from a non WinPR thread */
234  bAlertable = FALSE;
235  }
236  }
237 
238  int fd = winpr_Handle_getFd(Object);
239  if (fd < 0)
240  {
241  WLog_ERR(TAG, "winpr_Handle_getFd did not return a fd!");
242  SetLastError(ERROR_INVALID_HANDLE);
243  return WAIT_FAILED;
244  }
245 
246  if (!pollset_init(&pollset, 1 + extraFds))
247  {
248  WLog_ERR(TAG, "unable to initialize pollset");
249  SetLastError(ERROR_INTERNAL_ERROR);
250  return WAIT_FAILED;
251  }
252 
253  if (!pollset_add(&pollset, fd, Object->Mode))
254  {
255  WLog_ERR(TAG, "unable to add fd in pollset");
256  goto out;
257  }
258 
259  if (bAlertable && !apc_collectFds(thread, &pollset, &autoSignaled))
260  {
261  WLog_ERR(TAG, "unable to collect APC fds");
262  goto out;
263  }
264 
265  if (!autoSignaled)
266  {
267  status = pollset_poll(&pollset, dwMilliseconds);
268  if (status < 0)
269  {
270  char ebuffer[256] = { 0 };
271  WLog_ERR(TAG, "pollset_poll() failure [%d] %s", errno,
272  winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
273  goto out;
274  }
275  }
276 
277  ret = WAIT_TIMEOUT;
278  if (bAlertable && apc_executeCompletions(thread, &pollset, 1))
279  ret = WAIT_IO_COMPLETION;
280 
281  isSet = pollset_isSignaled(&pollset, 0);
282  pollset_uninit(&pollset);
283 
284  if (!isSet)
285  return ret;
286 
287  return winpr_Handle_cleanup(Object);
288  }
289 
290 out:
291  pollset_uninit(&pollset);
292  SetLastError(ERROR_INTERNAL_ERROR);
293  return WAIT_FAILED;
294 }
295 
296 DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
297 {
298  return WaitForSingleObjectEx(hHandle, dwMilliseconds, FALSE);
299 }
300 
301 DWORD WaitForMultipleObjectsEx(DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAll,
302  DWORD dwMilliseconds, BOOL bAlertable)
303 {
304  DWORD signalled = 0;
305  DWORD polled = 0;
306  DWORD poll_map[MAXIMUM_WAIT_OBJECTS] = { 0 };
307  BOOL signalled_handles[MAXIMUM_WAIT_OBJECTS] = { FALSE };
308  int fd = -1;
309  int status = -1;
310  ULONG Type = 0;
311  WINPR_HANDLE* Object = NULL;
312  WINPR_THREAD* thread = NULL;
313  WINPR_POLL_SET pollset = { 0 };
314  DWORD ret = WAIT_FAILED;
315  size_t extraFds = 0;
316  UINT64 now = 0;
317  UINT64 dueTime = 0;
318 
319  if (!nCount || (nCount > MAXIMUM_WAIT_OBJECTS))
320  {
321  WLog_ERR(TAG, "invalid handles count(%" PRIu32 ")", nCount);
322  return WAIT_FAILED;
323  }
324 
325  if (bAlertable)
326  {
327  thread = winpr_GetCurrentThread();
328  if (thread)
329  {
330  /* treat reentrancy, we can't switch to alertable state when we're already
331  treating completions */
332  if (thread->apc.treatingCompletions)
333  bAlertable = FALSE;
334  else
335  extraFds = thread->apc.length;
336  }
337  else
338  {
339  /* most probably we're not called from WinPR thread, so we can't have any APC */
340  bAlertable = FALSE;
341  }
342  }
343 
344  if (!pollset_init(&pollset, nCount + extraFds))
345  {
346  WLog_ERR(TAG, "unable to initialize pollset for nCount=%" PRIu32 " extraCount=%" PRIu32 "",
347  nCount, extraFds);
348  return WAIT_FAILED;
349  }
350 
351  signalled = 0;
352 
353  now = GetTickCount64();
354  if (dwMilliseconds != INFINITE)
355  dueTime = now + dwMilliseconds;
356  else
357  dueTime = 0xFFFFFFFFFFFFFFFF;
358 
359  do
360  {
361  BOOL autoSignaled = FALSE;
362  polled = 0;
363 
364  /* first collect file descriptors to poll */
365  DWORD idx = 0;
366  for (; idx < nCount; idx++)
367  {
368  if (bWaitAll)
369  {
370  if (signalled_handles[idx])
371  continue;
372 
373  poll_map[polled] = idx;
374  }
375 
376  if (!winpr_Handle_GetInfo(lpHandles[idx], &Type, &Object))
377  {
378  WLog_ERR(TAG, "invalid event file descriptor at %" PRIu32, idx);
379  winpr_log_backtrace(TAG, WLOG_ERROR, 20);
380  SetLastError(ERROR_INVALID_HANDLE);
381  goto out;
382  }
383 
384  fd = winpr_Handle_getFd(Object);
385  if (fd == -1)
386  {
387  WLog_ERR(TAG, "invalid file descriptor at %" PRIu32, idx);
388  winpr_log_backtrace(TAG, WLOG_ERROR, 20);
389  SetLastError(ERROR_INVALID_HANDLE);
390  goto out;
391  }
392 
393  if (!pollset_add(&pollset, fd, Object->Mode))
394  {
395  WLog_ERR(TAG, "unable to register fd in pollset at %" PRIu32, idx);
396  winpr_log_backtrace(TAG, WLOG_ERROR, 20);
397  SetLastError(ERROR_INVALID_HANDLE);
398  goto out;
399  }
400 
401  polled++;
402  }
403 
404  /* treat file descriptors of the APC if needed */
405  if (bAlertable && !apc_collectFds(thread, &pollset, &autoSignaled))
406  {
407  WLog_ERR(TAG, "unable to register APC fds");
408  winpr_log_backtrace(TAG, WLOG_ERROR, 20);
409  SetLastError(ERROR_INTERNAL_ERROR);
410  goto out;
411  }
412 
413  /* poll file descriptors */
414  status = 0;
415  if (!autoSignaled)
416  {
417  DWORD waitTime = 0;
418 
419  if (dwMilliseconds == INFINITE)
420  waitTime = INFINITE;
421  else
422  waitTime = (DWORD)(dueTime - now);
423 
424  status = pollset_poll(&pollset, waitTime);
425  if (status < 0)
426  {
427  char ebuffer[256] = { 0 };
428 #ifdef WINPR_HAVE_POLL_H
429  WLog_ERR(TAG, "poll() handle %" PRIu32 " (%" PRIu32 ") failure [%d] %s", idx,
430  nCount, errno, winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
431 #else
432  WLog_ERR(TAG, "select() handle %" PRIu32 " (%" PRIu32 ") failure [%d] %s", idx,
433  nCount, errno, winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
434 #endif
435  winpr_log_backtrace(TAG, WLOG_ERROR, 20);
436  SetLastError(ERROR_INTERNAL_ERROR);
437  goto out;
438  }
439  }
440 
441  /* give priority to the APC queue, to return WAIT_IO_COMPLETION */
442  if (bAlertable && apc_executeCompletions(thread, &pollset, polled))
443  {
444  ret = WAIT_IO_COMPLETION;
445  goto out;
446  }
447 
448  /* then treat pollset */
449  if (status)
450  {
451  for (DWORD index = 0; index < polled; index++)
452  {
453  DWORD handlesIndex = 0;
454  BOOL signal_set = FALSE;
455 
456  if (bWaitAll)
457  handlesIndex = poll_map[index];
458  else
459  handlesIndex = index;
460 
461  signal_set = pollset_isSignaled(&pollset, index);
462  if (signal_set)
463  {
464  DWORD rc = winpr_Handle_cleanup(lpHandles[handlesIndex]);
465  if (rc != WAIT_OBJECT_0)
466  {
467  WLog_ERR(TAG, "error in cleanup function for handle at index=%" PRIu32,
468  handlesIndex);
469  ret = rc;
470  goto out;
471  }
472 
473  if (bWaitAll)
474  {
475  signalled_handles[handlesIndex] = TRUE;
476 
477  /* Continue checks from last position. */
478  for (; signalled < nCount; signalled++)
479  {
480  if (!signalled_handles[signalled])
481  break;
482  }
483  }
484  else
485  {
486  ret = (WAIT_OBJECT_0 + handlesIndex);
487  goto out;
488  }
489 
490  if (signalled >= nCount)
491  {
492  ret = WAIT_OBJECT_0;
493  goto out;
494  }
495  }
496  }
497  }
498 
499  if (bAlertable && thread->apc.length > extraFds)
500  {
501  pollset_uninit(&pollset);
502  extraFds = thread->apc.length;
503  if (!pollset_init(&pollset, nCount + extraFds))
504  {
505  WLog_ERR(TAG, "unable reallocate pollset");
506  SetLastError(ERROR_INTERNAL_ERROR);
507  return WAIT_FAILED;
508  }
509  }
510  else
511  pollset_reset(&pollset);
512 
513  now = GetTickCount64();
514  } while (now < dueTime);
515 
516  ret = WAIT_TIMEOUT;
517 
518 out:
519  pollset_uninit(&pollset);
520  return ret;
521 }
522 
523 DWORD WaitForMultipleObjects(DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAll,
524  DWORD dwMilliseconds)
525 {
526  return WaitForMultipleObjectsEx(nCount, lpHandles, bWaitAll, dwMilliseconds, FALSE);
527 }
528 
529 DWORD SignalObjectAndWait(HANDLE hObjectToSignal, HANDLE hObjectToWaitOn, DWORD dwMilliseconds,
530  BOOL bAlertable)
531 {
532  if (!SetEvent(hObjectToSignal))
533  return WAIT_FAILED;
534 
535  return WaitForSingleObjectEx(hObjectToWaitOn, dwMilliseconds, bAlertable);
536 }
537 
538 #endif