/src/openssl/crypto/sleep.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <openssl/crypto.h> |
11 | | #include "internal/e_os.h" |
12 | | |
13 | | /* system-specific variants defining OSSL_sleep() */ |
14 | | #if defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__) |
15 | | #include <unistd.h> |
16 | | |
17 | | void OSSL_sleep(uint64_t millis) |
18 | 0 | { |
19 | | # ifdef OPENSSL_SYS_VXWORKS |
20 | | struct timespec ts; |
21 | | |
22 | | ts.tv_sec = (long int) (millis / 1000); |
23 | | ts.tv_nsec = (long int) (millis % 1000) * 1000000ul; |
24 | | nanosleep(&ts, NULL); |
25 | | # elif defined(__TANDEM) |
26 | | # if !defined(_REENTRANT) |
27 | | # include <cextdecs.h(PROCESS_DELAY_)> |
28 | | |
29 | | /* HPNS does not support usleep for non threaded apps */ |
30 | | PROCESS_DELAY_(millis * 1000); |
31 | | # elif defined(_SPT_MODEL_) |
32 | | # include <spthread.h> |
33 | | # include <spt_extensions.h> |
34 | | |
35 | | usleep(millis * 1000); |
36 | | # else |
37 | | usleep(millis * 1000); |
38 | | # endif |
39 | | # else |
40 | 0 | unsigned int s = (unsigned int)(millis / 1000); |
41 | 0 | unsigned int us = (unsigned int)((millis % 1000) * 1000); |
42 | |
|
43 | 0 | sleep(s); |
44 | 0 | usleep(us); |
45 | 0 | # endif |
46 | 0 | } |
47 | | #elif defined(_WIN32) && !defined(OPENSSL_SYS_UEFI) |
48 | | # include <windows.h> |
49 | | |
50 | | void OSSL_sleep(uint64_t millis) |
51 | | { |
52 | | /* |
53 | | * Windows' Sleep() takes a DWORD argument, which is smaller than |
54 | | * a uint64_t, so we need to limit it to 49 days, which should be enough. |
55 | | */ |
56 | | DWORD limited_millis = (DWORD)-1; |
57 | | |
58 | | if (millis < limited_millis) |
59 | | limited_millis = (DWORD)millis; |
60 | | Sleep(limited_millis); |
61 | | } |
62 | | |
63 | | #else |
64 | | /* Fallback to a busy wait */ |
65 | | # include "internal/time.h" |
66 | | |
67 | | static void ossl_sleep_secs(uint64_t secs) |
68 | | { |
69 | | /* |
70 | | * sleep() takes an unsigned int argument, which is smaller than |
71 | | * a uint64_t, so it needs to be limited to 136 years which |
72 | | * should be enough even for Sleeping Beauty. |
73 | | */ |
74 | | unsigned int limited_secs = UINT_MAX; |
75 | | |
76 | | if (secs < limited_secs) |
77 | | limited_secs = (unsigned int)secs; |
78 | | sleep(limited_secs); |
79 | | } |
80 | | |
81 | | static void ossl_sleep_millis(uint64_t millis) |
82 | | { |
83 | | const OSSL_TIME finish |
84 | | = ossl_time_add(ossl_time_now(), ossl_ms2time(millis)); |
85 | | |
86 | | while (ossl_time_compare(ossl_time_now(), finish) < 0) |
87 | | /* busy wait */ ; |
88 | | } |
89 | | |
90 | | void OSSL_sleep(uint64_t millis) |
91 | | { |
92 | | ossl_sleep_secs(millis / 1000); |
93 | | ossl_sleep_millis(millis % 1000); |
94 | | } |
95 | | #endif /* defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__) */ |