Coverage Report

Created: 2023-11-19 06:16

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