Coverage Report

Created: 2026-05-11 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/winpr/libwinpr/synch/wait.c
Line
Count
Source
1
/**
2
 * WinPR: Windows Portable Runtime
3
 * Synchronization Functions
4
 *
5
 * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2014 Hardening <contact@hardening-consulting.com>
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
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
0
#define TAG WINPR_TAG("sync.wait")
43
44
/**
45
 * WaitForSingleObject
46
 * WaitForSingleObjectEx
47
 * WaitForMultipleObjectsEx
48
 * SignalObjectAndWait
49
 */
50
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
0
{
64
0
  const UINT64 ns = winpr_GetUnixTimeNS();
65
0
  struct timespec timeout = WINPR_C_ARRAY_INIT;
66
0
  timeout.tv_sec = WINPR_TIME_NS_TO_S(ns);
67
0
  timeout.tv_nsec = WINPR_TIME_NS_REM_NS(ns);
68
0
  return timeout;
69
0
}
70
71
/**
72
 * Drop in replacement for pthread_mutex_timedlock
73
 * http://code.google.com/p/android/issues/detail?id=7807
74
 * http://aleksmaus.blogspot.ca/2011/12/missing-pthreadmutextimedlock-on.html
75
 */
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 = WINPR_C_ARRAY_INIT;
102
  struct timespec sleepytime = WINPR_C_ARRAY_INIT;
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, nullptr);
121
  }
122
123
  return retcode;
124
}
125
#endif
126
127
static void ts_add_ms(struct timespec* ts, DWORD dwMilliseconds)
128
0
{
129
0
  ts->tv_sec += dwMilliseconds / 1000L;
130
0
  ts->tv_nsec += (dwMilliseconds % 1000L) * 1000000L;
131
0
  ts->tv_sec += ts->tv_nsec / 1000000000L;
132
0
  ts->tv_nsec = ts->tv_nsec % 1000000000L;
133
0
}
134
135
DWORD WaitForSingleObjectEx(HANDLE hHandle, DWORD dwMilliseconds, BOOL bAlertable)
136
0
{
137
0
  ULONG Type = 0;
138
0
  WINPR_HANDLE* Object = nullptr;
139
0
  WINPR_POLL_SET pollset = WINPR_C_ARRAY_INIT;
140
141
0
  if (!winpr_Handle_GetInfo(hHandle, &Type, &Object))
142
0
  {
143
0
    WLog_ERR(TAG, "invalid hHandle.");
144
0
    SetLastError(ERROR_INVALID_HANDLE);
145
0
    return WAIT_FAILED;
146
0
  }
147
148
0
  if (Type == HANDLE_TYPE_PROCESS && winpr_Handle_getFd(hHandle) == -1)
149
0
  {
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
0
    WINPR_PROCESS* process = (WINPR_PROCESS*)Object;
154
155
0
    while (TRUE)
156
0
    {
157
0
      int ret = waitpid(process->pid, &(process->status), WNOHANG);
158
0
      if (ret == process->pid)
159
0
      {
160
0
        if (WIFEXITED(process->status))
161
0
          process->dwExitCode = (DWORD)WEXITSTATUS(process->status);
162
0
        else if (WIFSIGNALED(process->status))
163
0
          process->dwExitCode = (DWORD)(128 + WTERMSIG(process->status));
164
0
        else
165
0
          process->dwExitCode = (DWORD)process->status;
166
0
        return WAIT_OBJECT_0;
167
0
      }
168
0
      else if (ret < 0)
169
0
      {
170
0
        char ebuffer[256] = WINPR_C_ARRAY_INIT;
171
0
        WLog_ERR(TAG, "waitpid failure [%d] %s", errno,
172
0
                 winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
173
0
        SetLastError(ERROR_INTERNAL_ERROR);
174
0
        return WAIT_FAILED;
175
0
      }
176
177
0
      if (dwMilliseconds == 0)
178
0
        return WAIT_TIMEOUT;
179
180
      /* sleep by slices of 50ms */
181
0
      DWORD waitDelay = 50;
182
0
      if (dwMilliseconds != INFINITE)
183
0
      {
184
0
        if (dwMilliseconds < 50)
185
0
          waitDelay = dwMilliseconds;
186
0
        dwMilliseconds -= waitDelay;
187
0
      }
188
189
0
      DWORD status = SleepEx(waitDelay, bAlertable);
190
0
      if (status != 0)
191
0
        return status;
192
0
    }
193
0
  }
194
195
0
  if (Type == HANDLE_TYPE_MUTEX)
196
0
  {
197
0
    WINPR_MUTEX* mutex = (WINPR_MUTEX*)Object;
198
199
0
    if (dwMilliseconds != INFINITE)
200
0
    {
201
0
      int status = 0;
202
0
      struct timespec timeout = ts_from_ns();
203
204
0
      ts_add_ms(&timeout, dwMilliseconds);
205
0
      status = pthread_mutex_timedlock(&mutex->mutex, &timeout);
206
207
0
      if (ETIMEDOUT == status)
208
0
        return WAIT_TIMEOUT;
209
0
    }
210
0
    else
211
0
    {
212
0
      pthread_mutex_lock(&mutex->mutex);
213
0
    }
214
215
0
    return WAIT_OBJECT_0;
216
0
  }
217
0
  else
218
0
  {
219
0
    int status = -1;
220
0
    WINPR_THREAD* thread = nullptr;
221
0
    BOOL isSet = FALSE;
222
0
    size_t extraFds = 0;
223
0
    DWORD ret = 0;
224
0
    BOOL autoSignaled = FALSE;
225
226
0
    if (bAlertable)
227
0
    {
228
0
      thread = (WINPR_THREAD*)_GetCurrentThread();
229
0
      if (thread)
230
0
      {
231
        /* treat reentrancy, we can't switch to alertable state when we're already
232
           treating completions */
233
0
        if (thread->apc.treatingCompletions)
234
0
          bAlertable = FALSE;
235
0
        else
236
0
          extraFds = thread->apc.length;
237
0
      }
238
0
      else
239
0
      {
240
        /* called from a non WinPR thread */
241
0
        bAlertable = FALSE;
242
0
      }
243
0
    }
244
245
0
    int fd = winpr_Handle_getFd(Object);
246
0
    if (fd < 0)
247
0
    {
248
0
      WLog_ERR(TAG, "winpr_Handle_getFd did not return a fd!");
249
0
      SetLastError(ERROR_INVALID_HANDLE);
250
0
      return WAIT_FAILED;
251
0
    }
252
253
0
    if (!pollset_init(&pollset, 1 + extraFds))
254
0
    {
255
0
      WLog_ERR(TAG, "unable to initialize pollset");
256
0
      SetLastError(ERROR_INTERNAL_ERROR);
257
0
      return WAIT_FAILED;
258
0
    }
259
260
0
    if (!pollset_add(&pollset, fd, Object->Mode))
261
0
    {
262
0
      WLog_ERR(TAG, "unable to add fd in pollset");
263
0
      goto out;
264
0
    }
265
266
0
    if (bAlertable && !apc_collectFds(thread, &pollset, &autoSignaled))
267
0
    {
268
0
      WLog_ERR(TAG, "unable to collect APC fds");
269
0
      goto out;
270
0
    }
271
272
0
    if (!autoSignaled)
273
0
    {
274
0
      status = pollset_poll(&pollset, dwMilliseconds);
275
0
      if (status < 0)
276
0
      {
277
0
        char ebuffer[256] = WINPR_C_ARRAY_INIT;
278
0
        WLog_ERR(TAG, "pollset_poll() failure [%d] %s", errno,
279
0
                 winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
280
0
        goto out;
281
0
      }
282
0
    }
283
284
0
    ret = WAIT_TIMEOUT;
285
0
    if (bAlertable && apc_executeCompletions(thread, &pollset, 1))
286
0
      ret = WAIT_IO_COMPLETION;
287
288
0
    isSet = pollset_isSignaled(&pollset, 0);
289
0
    pollset_uninit(&pollset);
290
291
0
    if (!isSet)
292
0
      return ret;
293
294
0
    return winpr_Handle_cleanup(Object);
295
0
  }
296
297
0
out:
298
0
  pollset_uninit(&pollset);
299
0
  SetLastError(ERROR_INTERNAL_ERROR);
300
0
  return WAIT_FAILED;
301
0
}
302
303
DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
304
0
{
305
0
  return WaitForSingleObjectEx(hHandle, dwMilliseconds, FALSE);
306
0
}
307
308
DWORD WaitForMultipleObjectsEx(DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAll,
309
                               DWORD dwMilliseconds, BOOL bAlertable)
310
0
{
311
0
  DWORD signalled = 0;
312
0
  DWORD polled = 0;
313
0
  DWORD poll_map[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
314
0
  BOOL signalled_handles[MAXIMUM_WAIT_OBJECTS] = { FALSE };
315
0
  int fd = -1;
316
0
  int status = -1;
317
0
  ULONG Type = 0;
318
0
  WINPR_HANDLE* Object = nullptr;
319
0
  WINPR_THREAD* thread = nullptr;
320
0
  WINPR_POLL_SET pollset = WINPR_C_ARRAY_INIT;
321
0
  DWORD ret = WAIT_FAILED;
322
0
  size_t extraFds = 0;
323
0
  UINT64 now = 0;
324
0
  UINT64 dueTime = 0;
325
326
0
  if (!nCount || (nCount > MAXIMUM_WAIT_OBJECTS))
327
0
  {
328
0
    WLog_ERR(TAG, "invalid handles count(%" PRIu32 ")", nCount);
329
0
    return WAIT_FAILED;
330
0
  }
331
332
0
  if (bAlertable)
333
0
  {
334
0
    thread = winpr_GetCurrentThread();
335
0
    if (thread)
336
0
    {
337
      /* treat reentrancy, we can't switch to alertable state when we're already
338
         treating completions */
339
0
      if (thread->apc.treatingCompletions)
340
0
        bAlertable = FALSE;
341
0
      else
342
0
        extraFds = thread->apc.length;
343
0
    }
344
0
    else
345
0
    {
346
      /* most probably we're not called from WinPR thread, so we can't have any APC */
347
0
      bAlertable = FALSE;
348
0
    }
349
0
  }
350
351
0
  if (!pollset_init(&pollset, nCount + extraFds))
352
0
  {
353
0
    WLog_ERR(TAG, "unable to initialize pollset for nCount=%" PRIu32 " extraCount=%" PRIuz "",
354
0
             nCount, extraFds);
355
0
    return WAIT_FAILED;
356
0
  }
357
358
0
  signalled = 0;
359
360
0
  now = GetTickCount64();
361
0
  if (dwMilliseconds != INFINITE)
362
0
    dueTime = now + dwMilliseconds;
363
0
  else
364
0
    dueTime = 0xFFFFFFFFFFFFFFFF;
365
366
0
  do
367
0
  {
368
0
    BOOL autoSignaled = FALSE;
369
0
    polled = 0;
370
371
    /* first collect file descriptors to poll */
372
0
    DWORD idx = 0;
373
0
    for (; idx < nCount; idx++)
374
0
    {
375
0
      if (bWaitAll)
376
0
      {
377
0
        if (signalled_handles[idx])
378
0
          continue;
379
380
0
        poll_map[polled] = idx;
381
0
      }
382
383
0
      if (!winpr_Handle_GetInfo(lpHandles[idx], &Type, &Object))
384
0
      {
385
0
        WLog_ERR(TAG, "invalid event file descriptor at %" PRIu32, idx);
386
0
        winpr_log_backtrace(TAG, WLOG_ERROR, 20);
387
0
        SetLastError(ERROR_INVALID_HANDLE);
388
0
        goto out;
389
0
      }
390
391
0
      fd = winpr_Handle_getFd(Object);
392
0
      if (fd == -1)
393
0
      {
394
0
        WLog_ERR(TAG, "invalid file descriptor at %" PRIu32, idx);
395
0
        winpr_log_backtrace(TAG, WLOG_ERROR, 20);
396
0
        SetLastError(ERROR_INVALID_HANDLE);
397
0
        goto out;
398
0
      }
399
400
0
      if (!pollset_add(&pollset, fd, Object->Mode))
401
0
      {
402
0
        WLog_ERR(TAG, "unable to register fd in pollset at %" PRIu32, idx);
403
0
        winpr_log_backtrace(TAG, WLOG_ERROR, 20);
404
0
        SetLastError(ERROR_INVALID_HANDLE);
405
0
        goto out;
406
0
      }
407
408
0
      polled++;
409
0
    }
410
411
    /* treat file descriptors of the APC if needed */
412
0
    if (bAlertable && !apc_collectFds(thread, &pollset, &autoSignaled))
413
0
    {
414
0
      WLog_ERR(TAG, "unable to register APC fds");
415
0
      winpr_log_backtrace(TAG, WLOG_ERROR, 20);
416
0
      SetLastError(ERROR_INTERNAL_ERROR);
417
0
      goto out;
418
0
    }
419
420
    /* poll file descriptors */
421
0
    status = 0;
422
0
    if (!autoSignaled)
423
0
    {
424
0
      DWORD waitTime = 0;
425
426
0
      if (dwMilliseconds == INFINITE)
427
0
        waitTime = INFINITE;
428
0
      else
429
0
        waitTime = (DWORD)(dueTime - now);
430
431
0
      status = pollset_poll(&pollset, waitTime);
432
0
      if (status < 0)
433
0
      {
434
0
        char ebuffer[256] = WINPR_C_ARRAY_INIT;
435
0
#ifdef WINPR_HAVE_POLL_H
436
0
        WLog_ERR(TAG, "poll() handle %" PRIu32 " (%" PRIu32 ") failure [%d] %s", idx,
437
0
                 nCount, errno, winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
438
#else
439
        WLog_ERR(TAG, "select() handle %" PRIu32 " (%" PRIu32 ") failure [%d] %s", idx,
440
                 nCount, errno, winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
441
#endif
442
0
        winpr_log_backtrace(TAG, WLOG_ERROR, 20);
443
0
        SetLastError(ERROR_INTERNAL_ERROR);
444
0
        goto out;
445
0
      }
446
0
    }
447
448
    /* give priority to the APC queue, to return WAIT_IO_COMPLETION */
449
0
    if (bAlertable && apc_executeCompletions(thread, &pollset, polled))
450
0
    {
451
0
      ret = WAIT_IO_COMPLETION;
452
0
      goto out;
453
0
    }
454
455
    /* then treat pollset */
456
0
    if (status)
457
0
    {
458
0
      for (DWORD index = 0; index < polled; index++)
459
0
      {
460
0
        DWORD handlesIndex = 0;
461
0
        BOOL signal_set = FALSE;
462
463
0
        if (bWaitAll)
464
0
          handlesIndex = poll_map[index];
465
0
        else
466
0
          handlesIndex = index;
467
468
0
        signal_set = pollset_isSignaled(&pollset, index);
469
0
        if (signal_set)
470
0
        {
471
0
          DWORD rc = winpr_Handle_cleanup(lpHandles[handlesIndex]);
472
0
          if (rc != WAIT_OBJECT_0)
473
0
          {
474
0
            WLog_ERR(TAG, "error in cleanup function for handle at index=%" PRIu32,
475
0
                     handlesIndex);
476
0
            ret = rc;
477
0
            goto out;
478
0
          }
479
480
0
          if (bWaitAll)
481
0
          {
482
0
            signalled_handles[handlesIndex] = TRUE;
483
484
            /* Continue checks from last position. */
485
0
            for (; signalled < nCount; signalled++)
486
0
            {
487
0
              if (!signalled_handles[signalled])
488
0
                break;
489
0
            }
490
0
          }
491
0
          else
492
0
          {
493
0
            ret = (WAIT_OBJECT_0 + handlesIndex);
494
0
            goto out;
495
0
          }
496
497
0
          if (signalled >= nCount)
498
0
          {
499
0
            ret = WAIT_OBJECT_0;
500
0
            goto out;
501
0
          }
502
0
        }
503
0
      }
504
0
    }
505
506
0
    if (bAlertable && thread->apc.length > extraFds)
507
0
    {
508
0
      pollset_uninit(&pollset);
509
0
      extraFds = thread->apc.length;
510
0
      if (!pollset_init(&pollset, nCount + extraFds))
511
0
      {
512
0
        WLog_ERR(TAG, "unable reallocate pollset");
513
0
        SetLastError(ERROR_INTERNAL_ERROR);
514
0
        return WAIT_FAILED;
515
0
      }
516
0
    }
517
0
    else
518
0
      pollset_reset(&pollset);
519
520
0
    now = GetTickCount64();
521
0
  } while (now < dueTime);
522
523
0
  ret = WAIT_TIMEOUT;
524
525
0
out:
526
0
  pollset_uninit(&pollset);
527
0
  return ret;
528
0
}
529
530
DWORD WaitForMultipleObjects(DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAll,
531
                             DWORD dwMilliseconds)
532
0
{
533
0
  return WaitForMultipleObjectsEx(nCount, lpHandles, bWaitAll, dwMilliseconds, FALSE);
534
0
}
535
536
DWORD SignalObjectAndWait(HANDLE hObjectToSignal, HANDLE hObjectToWaitOn, DWORD dwMilliseconds,
537
                          BOOL bAlertable)
538
0
{
539
0
  if (!SetEvent(hObjectToSignal))
540
0
    return WAIT_FAILED;
541
542
0
  return WaitForSingleObjectEx(hObjectToWaitOn, dwMilliseconds, bAlertable);
543
0
}
544
545
#endif