]> git.ipfire.org Git - thirdparty/openssl.git/blame - include/internal/e_os.h
Copyright year updates
[thirdparty/openssl.git] / include / internal / e_os.h
CommitLineData
b6cff313 1/*
da1c088f 2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
0f113f3e 3 *
402dd558 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b6cff313
RS
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
d02b48c6
RE
8 */
9
ae4186b0
DMSP
10#ifndef OSSL_E_OS_H
11# define OSSL_E_OS_H
d02b48c6 12
07016a8a 13# include <limits.h>
0f113f3e 14# include <openssl/opensslconf.h>
06018c46 15
0f113f3e 16# include <openssl/e_os2.h>
98d132cf 17# include <openssl/crypto.h>
1a298b00 18# include "internal/numbers.h" /* Ensure the definition of SIZE_MAX */
b99fe5f4 19
0f113f3e
MC
20/*
21 * <openssl/e_os2.h> contains what we can justify to make visible to the
22 * outside; this file e_os.h is not part of the exported interface.
23 */
fa470a84 24
0e200d2a
ČK
25/* ossl_static_assert_type_eq: gcc-only variable type static assertion */
26# if defined(__GNUC__) && !defined(__clang__)
27# define ossl_static_assert_type_eq(type, x) \
28 _Static_assert((__builtin_types_compatible_p(type, __typeof__(x))), \
29 #x " type check failed, expected: " #type)
30# else
31# define ossl_static_assert_type_eq(type, x)
32# endif
33
cff55b90 34# if defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)
3e83e686
RL
35# define NO_CHMOD
36# define NO_SYSLOG
0f113f3e
MC
37# endif
38
0e97f1e1
RS
39# define get_last_sys_error() errno
40# define clear_sys_error() errno=0
71b1ceff 41# define set_sys_error(e) errno=(e)
0e97f1e1 42
d02b48c6
RE
43/********************************************************************
44 The Microsoft section
45 ********************************************************************/
0f113f3e 46# if defined(OPENSSL_SYS_WIN32) && !defined(WIN32)
b8384651 47# define WIN32
0f113f3e
MC
48# endif
49# if defined(OPENSSL_SYS_WINDOWS) && !defined(WINDOWS)
cf1b7d96 50# define WINDOWS
0f113f3e
MC
51# endif
52# if defined(OPENSSL_SYS_MSDOS) && !defined(MSDOS)
cf1b7d96 53# define MSDOS
0f113f3e 54# endif
d02b48c6 55
0f113f3e 56# ifdef WIN32
0e97f1e1
RS
57# undef get_last_sys_error
58# undef clear_sys_error
71b1ceff 59# undef set_sys_error
0f113f3e
MC
60# define get_last_sys_error() GetLastError()
61# define clear_sys_error() SetLastError(0)
71b1ceff 62# define set_sys_error(e) SetLastError(e)
0f113f3e
MC
63# if !defined(WINNT)
64# define WIN_CONSOLE_BUG
65# endif
66# else
0f113f3e 67# endif
58964a49 68
0f113f3e 69# if (defined(WINDOWS) || defined(MSDOS))
d02b48c6 70
f642ebc1 71# ifdef __DJGPP__
0f113f3e
MC
72# include <unistd.h>
73# include <sys/stat.h>
0f113f3e
MC
74# define _setmode setmode
75# define _O_TEXT O_TEXT
76# define _O_BINARY O_BINARY
2c25ebd1 77# undef DEVRANDOM_EGD /* Neither MS-DOS nor FreeDOS provide 'egd' sockets. */
0f113f3e
MC
78# undef DEVRANDOM
79# define DEVRANDOM "/dev/urandom\x24"
80# endif /* __DJGPP__ */
f642ebc1 81
0056ef5d 82# ifndef S_IFDIR
0f113f3e 83# define S_IFDIR _S_IFDIR
0056ef5d 84# endif
6e064240 85
0056ef5d 86# ifndef S_IFMT
0f113f3e 87# define S_IFMT _S_IFMT
0056ef5d 88# endif
6e064240 89
f642ebc1 90# if !defined(WINNT) && !defined(__DJGPP__)
0f113f3e 91# define NO_SYSLOG
0056ef5d 92# endif
d02b48c6 93
d02b48c6 94# ifdef WINDOWS
0f113f3e 95# if !defined(_WIN32_WCE) && !defined(_WIN32_WINNT)
e476f942 96 /*
0f113f3e
MC
97 * Defining _WIN32_WINNT here in e_os.h implies certain "discipline."
98 * Most notably we ought to check for availability of each specific
0ee05b73
AP
99 * routine that was introduced after denoted _WIN32_WINNT with
100 * GetProcAddress(). Normally newer functions are masked with higher
101 * _WIN32_WINNT in SDK headers. So that if you wish to use them in
102 * some module, you'd need to override _WIN32_WINNT definition in
103 * the target module in order to "reach for" prototypes, but replace
104 * calls to new functions with indirect calls. Alternatively it
105 * might be possible to achieve the goal by /DELAYLOAD-ing .DLLs
106 * and check for current OS version instead.
0f113f3e 107 */
0ee05b73 108# define _WIN32_WINNT 0x0501
0f113f3e 109# endif
2469e76b 110# if defined(_WIN32_WINNT) || defined(_WIN32_WCE)
61aa2134
AP
111 /*
112 * Just like defining _WIN32_WINNT including winsock2.h implies
8711efb4 113 * certain "discipline" for maintaining [broad] binary compatibility.
61aa2134
AP
114 * As long as structures are invariant among Winsock versions,
115 * it's sufficient to check for specific Winsock2 API availability
116 * at run-time [DSO_global_lookup is recommended]...
117 */
0f113f3e
MC
118# include <winsock2.h>
119# include <ws2tcpip.h>
3e4981dd
TI
120 /*
121 * Clang-based C++Builder 10.3.3 toolchains cannot find C inline
122 * definitions at link-time. This header defines WspiapiLoad() as an
123 * __inline function. https://quality.embarcadero.com/browse/RSP-33806
124 */
125# if !defined(__BORLANDC__) || !defined(__clang__)
126# include <wspiapi.h>
127# endif
8711efb4 128 /* yes, they have to be #included prior to <windows.h> */
0f113f3e
MC
129# endif
130# include <windows.h>
131# include <stdio.h>
132# include <stddef.h>
133# include <errno.h>
134# if defined(_WIN32_WCE) && !defined(EACCES)
135# define EACCES 13
136# endif
137# include <string.h>
138# ifdef _WIN64
139# define strlen(s) _strlen31(s)
1875e6db 140/* cut strings to 2GB */
55c7a4cf 141static __inline unsigned int _strlen31(const char *str)
0f113f3e
MC
142{
143 unsigned int len = 0;
144 while (*str && len < 0x80000000U)
145 str++, len++;
146 return len & 0x7FFFFFFF;
147}
148# endif
149# include <malloc.h>
0f113f3e
MC
150# if defined(_MSC_VER) && !defined(_WIN32_WCE) && !defined(_DLL) && defined(stdin)
151# if _MSC_VER>=1300 && _MSC_VER<1600
152# undef stdin
153# undef stdout
154# undef stderr
155FILE *__iob_func();
156# define stdin (&__iob_func()[0])
157# define stdout (&__iob_func()[1])
158# define stderr (&__iob_func()[2])
e527201f 159# endif
0f113f3e 160# endif
d02b48c6 161# endif
0f113f3e
MC
162# include <io.h>
163# include <fcntl.h>
d02b48c6 164
0bf23d9b 165# ifdef OPENSSL_SYS_WINCE
0f113f3e 166# define OPENSSL_NO_POSIX_IO
0bf23d9b
RL
167# endif
168
be7b4458 169# define EXIT(n) exit(n)
d02b48c6 170# define LIST_SEPARATOR_CHAR ';'
0056ef5d 171# ifndef W_OK
0f113f3e 172# define W_OK 2
0056ef5d
RL
173# endif
174# ifndef R_OK
0f113f3e 175# define R_OK 4
0056ef5d 176# endif
0bf23d9b 177# ifdef OPENSSL_SYS_WINCE
0f113f3e 178# define DEFAULT_HOME ""
0bf23d9b 179# else
0f113f3e 180# define DEFAULT_HOME "C:"
0bf23d9b 181# endif
d02b48c6 182
86d21d0b 183/* Avoid Visual Studio 13 GetVersion deprecated problems */
0f113f3e
MC
184# if defined(_MSC_VER) && _MSC_VER>=1800
185# define check_winnt() (1)
186# define check_win_minplat(x) (1)
187# else
188# define check_winnt() (GetVersion() < 0x80000000)
189# define check_win_minplat(x) (LOBYTE(LOWORD(GetVersion())) >= (x))
190# endif
f511b25a 191
0f113f3e 192# else /* The non-microsoft world */
d02b48c6 193
4cff10dc 194# if defined(OPENSSL_SYS_VXWORKS)
61783db5 195# include <time.h>
4cff10dc
P
196# else
197# include <sys/time.h>
198# endif
199
cf1b7d96 200# ifdef OPENSSL_SYS_VMS
0f113f3e
MC
201# define VMS 1
202 /*
203 * some programs don't include stdlib, so exit() and others give implicit
204 * function warnings
205 */
206# include <stdlib.h>
207# if defined(__DECC)
208# include <unistd.h>
209# else
210# include <unixlib.h>
211# endif
0f113f3e 212# define LIST_SEPARATOR_CHAR ','
0c61e299 213 /* We don't have any well-defined random devices on VMS, yet... */
0f113f3e 214# undef DEVRANDOM
1d97c843
TH
215 /*-
216 We need to do this since VMS has the following coding on status codes:
511e596b
RL
217
218 Bits 0-2: status type: 0 = warning, 1 = success, 2 = error, 3 = info ...
219 The important thing to know is that odd numbers are considered
0f113f3e 220 good, while even ones are considered errors.
511e596b
RL
221 Bits 3-15: actual status number
222 Bits 16-27: facility number. 0 is considered "unknown"
223 Bits 28-31: control bits. If bit 28 is set, the shell won't try to
224 output the message (which, for random codes, just looks ugly)
225
226 So, what we do here is to change 0 to 1 to get the default success status,
227 and everything else is shifted up to fit into the status number field, and
f625d383 228 the status is tagged as an error, which is what is wanted here.
c3944a99
RL
229
230 Finally, we add the VMS C facility code 0x35a000, because there are some
231 programs, such as Perl, that will reinterpret the code back to something
46d08509 232 POSIX. 'man perlvms' explains it further.
f625d383 233
c3944a99
RL
234 NOTE: the perlvms manual wants to turn all codes 2 to 255 into success
235 codes (status type = 1). I couldn't disagree more. Fortunately, the
236 status type doesn't seem to bother Perl.
511e596b
RL
237 -- Richard Levitte
238 */
f625d383
RL
239# define EXIT(n) exit((n) ? (((n) << 3) | 2 | 0x10000000 | 0x35a000) : 1)
240
b8f304f7
RL
241# define DEFAULT_HOME "SYS$LOGIN:"
242
d02b48c6 243# else
06018c46 244 /* !defined VMS */
6b10d29c 245# include <unistd.h>
b379fe6c 246# include <sys/types.h>
0f113f3e
MC
247# ifdef OPENSSL_SYS_WIN32_CYGWIN
248# include <io.h>
249# include <fcntl.h>
250# endif
251
0f113f3e 252# define LIST_SEPARATOR_CHAR ':'
0f113f3e 253# define EXIT(n) exit(n)
d02b48c6 254# endif
d02b48c6 255
0f113f3e 256# endif
7d7d2cbc 257
d02b48c6
RE
258/***********************************************/
259
0f113f3e 260# if defined(OPENSSL_SYS_WINDOWS)
606e0426 261# if defined(_MSC_VER) && (_MSC_VER >= 1310) && !defined(_WIN32_WCE)
dc6b5c7b
RL
262# define open _open
263# define fdopen _fdopen
264# define close _close
8d95ca36
M
265# ifndef strdup
266# define strdup _strdup
267# endif
dc6b5c7b 268# define unlink _unlink
2e38091c 269# define fileno _fileno
dc6b5c7b 270# endif
7233bea2
RL
271# else
272# include <strings.h>
0f113f3e 273# endif
3bfd99bf 274
6a89a25c 275/* vxworks */
0f113f3e
MC
276# if defined(OPENSSL_SYS_VXWORKS)
277# include <ioLib.h>
278# include <tickLib.h>
279# include <sysLib.h>
0f113f3e
MC
280# include <vxWorks.h>
281# include <sockLib.h>
282# include <taskLib.h>
6a89a25c 283
0e97f1e1
RS
284# define TTY_STRUCT int
285# define sleep(a) taskDelay((a) * sysClkRateGet())
6a89a25c 286
0f113f3e
MC
287/*
288 * NOTE: these are implemented by helpers in database app! if the database is
a303e9a6 289 * not linked, we need to implement them elsewhere
0f113f3e 290 */
6a89a25c
RL
291struct hostent *gethostbyname(const char *name);
292struct hostent *gethostbyaddr(const char *addr, int length, int type);
293struct servent *getservbyname(const char *name, const char *proto);
294
0f113f3e 295# endif
6a89a25c
RL
296/* end vxworks */
297
08073700
RB
298/* ----------------------------- HP NonStop -------------------------------- */
299/* Required to support platform variant without getpid() and pid_t. */
650c6687 300# if defined(__TANDEM) && defined(_GUARDIAN_TARGET)
08073700
RB
301# include <strings.h>
302# include <netdb.h>
303# define getservbyname(name,proto) getservbyname((char*)name,proto)
304# define gethostbyname(name) gethostbyname((char*)name)
305# define ioctlsocket(a,b,c) ioctl(a,b,c)
306# ifdef NO_GETPID
307inline int nssgetpid();
308# ifndef NSSGETPID_MACRO
309# define NSSGETPID_MACRO
310# include <cextdecs.h(PROCESSHANDLE_GETMINE_)>
311# include <cextdecs.h(PROCESSHANDLE_DECOMPOSE_)>
312 inline int nssgetpid()
313 {
314 short phandle[10]={0};
315 union pseudo_pid {
316 struct {
317 short cpu;
318 short pin;
319 } cpu_pin ;
320 int ppid;
321 } ppid = { 0 };
322 PROCESSHANDLE_GETMINE_(phandle);
323 PROCESSHANDLE_DECOMPOSE_(phandle, &ppid.cpu_pin.cpu, &ppid.cpu_pin.pin);
324 return ppid.ppid;
325 }
326# define getpid(a) nssgetpid(a)
327# endif /* NSSGETPID_MACRO */
328# endif /* NO_GETPID */
329/*# define setsockopt(a,b,c,d,f) setsockopt(a,b,c,(char*)d,f)*/
330/*# define getsockopt(a,b,c,d,f) getsockopt(a,b,c,(char*)d,f)*/
331/*# define connect(a,b,c) connect(a,(struct sockaddr *)b,c)*/
332/*# define bind(a,b,c) bind(a,(struct sockaddr *)b,c)*/
333/*# define sendto(a,b,c,d,e,f) sendto(a,(char*)b,c,d,(struct sockaddr *)e,f)*/
334# if defined(OPENSSL_THREADS) && !defined(_PUT_MODEL_)
335 /*
336 * HPNS SPT threads
337 */
338# define SPT_THREAD_SIGNAL 1
339# define SPT_THREAD_AWARE 1
340# include <spthread.h>
341# undef close
342# define close spt_close
343/*
344# define get_last_socket_error() errno
345# define clear_socket_error() errno=0
346# define ioctlsocket(a,b,c) ioctl(a,b,c)
347# define closesocket(s) close(s)
348# define readsocket(s,b,n) read((s),(char*)(b),(n))
349# define writesocket(s,b,n) write((s),(char*)(b),(n)
350*/
351# define accept(a,b,c) accept(a,(struct sockaddr *)b,c)
352# define recvfrom(a,b,c,d,e,f) recvfrom(a,b,(socklen_t)c,d,e,f)
353# endif
354# endif
355
8529b156
DMSP
356# ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
357# define CRYPTO_memcmp memcmp
358# endif
43c56417 359
6943335e
RS
360# ifndef OPENSSL_NO_SECURE_MEMORY
361 /* unistd.h defines _POSIX_VERSION */
f31ac320
JG
362# if (defined(OPENSSL_SYS_UNIX) \
363 && ( (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) \
364 || defined(__sun) || defined(__hpux) || defined(__sgi) \
365 || defined(__osf__) )) \
366 || defined(_WIN32)
6943335e
RS
367 /* secure memory is implemented */
368# else
369# define OPENSSL_NO_SECURE_MEMORY
370# endif
8529b156 371# endif
6943335e 372
b98f989e
TM
373/*
374 * str[n]casecmp_l is defined in POSIX 2008-01. Value is taken accordingly
375 * https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
376 * There are also equivalent functions on Windows.
377 * There is no locale_t on NONSTOP.
378 */
379# if defined(OPENSSL_SYS_WINDOWS)
380# define locale_t _locale_t
381# define freelocale _free_locale
382# define strcasecmp_l _stricmp_l
383# define strncasecmp_l _strnicmp_l
384# define strcasecmp _stricmp
92d05016 385# define strncasecmp _strnicmp
b98f989e
TM
386# elif !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L \
387 || defined(OPENSSL_SYS_TANDEM)
388# ifndef OPENSSL_NO_LOCALE
389# define OPENSSL_NO_LOCALE
390# endif
391# endif
392
d02b48c6 393#endif