xref: /freebsd/crypto/openssl/include/internal/e_os.h (revision e7be843b4a162e68651d3911f0357ed464915629)
1 /*
2  * Copyright 1995-2025 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 #ifndef OSSL_E_OS_H
11 # define OSSL_E_OS_H
12 
13 # include <limits.h>
14 # include <openssl/opensslconf.h>
15 
16 # include <openssl/e_os2.h>
17 # include <openssl/crypto.h>
18 # include "internal/numbers.h"   /* Ensure the definition of SIZE_MAX */
19 
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  */
24 
25 # if defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)
26 #  define NO_CHMOD
27 #  define NO_SYSLOG
28 # endif
29 
30 # define get_last_sys_error()    errno
31 # define clear_sys_error()       errno=0
32 # define set_sys_error(e)        errno=(e)
33 
34 /********************************************************************
35  The Microsoft section
36  ********************************************************************/
37 # if defined(OPENSSL_SYS_WIN32) && !defined(WIN32)
38 #  define WIN32
39 # endif
40 # if defined(OPENSSL_SYS_WINDOWS) && !defined(WINDOWS)
41 #  define WINDOWS
42 # endif
43 # if defined(OPENSSL_SYS_MSDOS) && !defined(MSDOS)
44 #  define MSDOS
45 # endif
46 
47 # ifdef WIN32
48 #  undef get_last_sys_error
49 #  undef clear_sys_error
50 #  undef set_sys_error
51 #  define get_last_sys_error()    GetLastError()
52 #  define clear_sys_error()       SetLastError(0)
53 #  define set_sys_error(e)        SetLastError(e)
54 #  if !defined(WINNT)
55 #   define WIN_CONSOLE_BUG
56 #  endif
57 # else
58 # endif
59 
60 # if (defined(WINDOWS) || defined(MSDOS))
61 
62 #  ifdef __DJGPP__
63 #   include <unistd.h>
64 #   include <sys/stat.h>
65 #   define _setmode setmode
66 #   define _O_TEXT O_TEXT
67 #   define _O_BINARY O_BINARY
68 #   undef DEVRANDOM_EGD  /*  Neither MS-DOS nor FreeDOS provide 'egd' sockets.  */
69 #   undef DEVRANDOM
70 #   define DEVRANDOM "/dev/urandom\x24"
71 #  endif                        /* __DJGPP__ */
72 
73 #  ifndef S_IFDIR
74 #   define S_IFDIR     _S_IFDIR
75 #  endif
76 
77 #  ifndef S_IFMT
78 #   define S_IFMT      _S_IFMT
79 #  endif
80 
81 #  if !defined(WINNT) && !defined(__DJGPP__)
82 #   define NO_SYSLOG
83 #  endif
84 
85 #  ifdef WINDOWS
86 #   if !defined(_WIN32_WCE) && !defined(_WIN32_WINNT)
87        /*
88         * Defining _WIN32_WINNT here in e_os.h implies certain "discipline."
89         * Most notably we ought to check for availability of each specific
90         * routine that was introduced after denoted _WIN32_WINNT with
91         * GetProcAddress(). Normally newer functions are masked with higher
92         * _WIN32_WINNT in SDK headers. So that if you wish to use them in
93         * some module, you'd need to override _WIN32_WINNT definition in
94         * the target module in order to "reach for" prototypes, but replace
95         * calls to new functions with indirect calls. Alternatively it
96         * might be possible to achieve the goal by /DELAYLOAD-ing .DLLs
97         * and check for current OS version instead.
98         */
99 #    define _WIN32_WINNT 0x0501
100 #   endif
101 #   include <windows.h>
102 #   include <stdio.h>
103 #   include <stddef.h>
104 #   include <errno.h>
105 #   if defined(_WIN32_WCE) && !defined(EACCES)
106 #    define EACCES   13
107 #   endif
108 #   include <string.h>
109 #   include <malloc.h>
110 #   if defined(_MSC_VER) && !defined(_WIN32_WCE) && !defined(_DLL) && defined(stdin)
111 #    if _MSC_VER>=1300 && _MSC_VER<1600
112 #     undef stdin
113 #     undef stdout
114 #     undef stderr
115 FILE *__iob_func(void);
116 #     define stdin  (&__iob_func()[0])
117 #     define stdout (&__iob_func()[1])
118 #     define stderr (&__iob_func()[2])
119 #    endif
120 #   endif
121 #  endif
122 
123 #  include <io.h>
124 #  include <fcntl.h>
125 
126 #  ifdef OPENSSL_SYS_WINCE
127 #   define OPENSSL_NO_POSIX_IO
128 #  endif
129 
130 #  define EXIT(n) exit(n)
131 #  define LIST_SEPARATOR_CHAR ';'
132 #  ifndef W_OK
133 #   define W_OK        2
134 #  endif
135 #  ifndef R_OK
136 #   define R_OK        4
137 #  endif
138 #  ifdef OPENSSL_SYS_WINCE
139 #   define DEFAULT_HOME  ""
140 #  else
141 #   define DEFAULT_HOME  "C:"
142 #  endif
143 
144 /* Avoid Visual Studio 13 GetVersion deprecated problems */
145 #  if defined(_MSC_VER) && _MSC_VER>=1800
146 #   define check_winnt() (1)
147 #   define check_win_minplat(x) (1)
148 #  else
149 #   define check_winnt() (GetVersion() < 0x80000000)
150 #   define check_win_minplat(x) (LOBYTE(LOWORD(GetVersion())) >= (x))
151 #  endif
152 
153 # else                          /* The non-microsoft world */
154 
155 #  if defined(OPENSSL_SYS_VXWORKS)
156 #   include <time.h>
157 #  else
158 #   include <sys/time.h>
159 #  endif
160 
161 #  ifdef OPENSSL_SYS_VMS
162 #   define VMS 1
163   /*
164    * some programs don't include stdlib, so exit() and others give implicit
165    * function warnings
166    */
167 #   include <stdlib.h>
168 #   if defined(__DECC)
169 #    include <unistd.h>
170 #   else
171 #    include <unixlib.h>
172 #   endif
173 #   define LIST_SEPARATOR_CHAR ','
174   /* We don't have any well-defined random devices on VMS, yet... */
175 #   undef DEVRANDOM
176   /*-
177      We need to do this since VMS has the following coding on status codes:
178 
179      Bits 0-2: status type: 0 = warning, 1 = success, 2 = error, 3 = info ...
180                The important thing to know is that odd numbers are considered
181                good, while even ones are considered errors.
182      Bits 3-15: actual status number
183      Bits 16-27: facility number.  0 is considered "unknown"
184      Bits 28-31: control bits.  If bit 28 is set, the shell won't try to
185                  output the message (which, for random codes, just looks ugly)
186 
187      So, what we do here is to change 0 to 1 to get the default success status,
188      and everything else is shifted up to fit into the status number field, and
189      the status is tagged as an error, which is what is wanted here.
190 
191      Finally, we add the VMS C facility code 0x35a000, because there are some
192      programs, such as Perl, that will reinterpret the code back to something
193      POSIX.  'man perlvms' explains it further.
194 
195      NOTE: the perlvms manual wants to turn all codes 2 to 255 into success
196      codes (status type = 1).  I couldn't disagree more.  Fortunately, the
197      status type doesn't seem to bother Perl.
198      -- Richard Levitte
199   */
200 #   define EXIT(n)  exit((n) ? (((n) << 3) | 2 | 0x10000000 | 0x35a000) : 1)
201 
202 #   define DEFAULT_HOME "SYS$LOGIN:"
203 
204 #  else
205      /* !defined VMS */
206 #   include <unistd.h>
207 #   include <sys/types.h>
208 #   ifdef OPENSSL_SYS_WIN32_CYGWIN
209 #    include <io.h>
210 #    include <fcntl.h>
211 #   endif
212 
213 #   define LIST_SEPARATOR_CHAR ':'
214 #   define EXIT(n)             exit(n)
215 #  endif
216 
217 # endif
218 
219 /***********************************************/
220 
221 # if defined(OPENSSL_SYS_WINDOWS)
222 #  if defined(_MSC_VER) && (_MSC_VER >= 1310) && !defined(_WIN32_WCE)
223 #   define open _open
224 #   define fdopen _fdopen
225 #   define close _close
226 #   ifndef strdup
227 #    define strdup _strdup
228 #   endif
229 #   define unlink _unlink
230 #   define fileno _fileno
231 #   define isatty _isatty
232 #  endif
233 # else
234 #  include <strings.h>
235 # endif
236 
237 /* vxworks */
238 # if defined(OPENSSL_SYS_VXWORKS)
239 #  include <ioLib.h>
240 #  include <tickLib.h>
241 #  include <sysLib.h>
242 #  include <vxWorks.h>
243 #  include <sockLib.h>
244 #  include <taskLib.h>
245 
246 #  define TTY_STRUCT int
247 #  define sleep(a) taskDelay((a) * sysClkRateGet())
248 
249 /*
250  * NOTE: these are implemented by helpers in database app! if the database is
251  * not linked, we need to implement them elsewhere
252  */
253 struct hostent *gethostbyname(const char *name);
254 struct hostent *gethostbyaddr(const char *addr, int length, int type);
255 struct servent *getservbyname(const char *name, const char *proto);
256 
257 # endif
258 /* end vxworks */
259 
260 /* ----------------------------- HP NonStop -------------------------------- */
261 /* Required to support platform variant without getpid() and pid_t. */
262 # if defined(__TANDEM) && defined(_GUARDIAN_TARGET)
263 #  include <strings.h>
264 #  include <netdb.h>
265 #  define getservbyname(name,proto)          getservbyname((char*)name,proto)
266 #  define gethostbyname(name)                gethostbyname((char*)name)
267 #  define ioctlsocket(a,b,c)	ioctl(a,b,c)
268 #  ifdef NO_GETPID
269 inline int nssgetpid(void);
270 #   ifndef NSSGETPID_MACRO
271 #    define NSSGETPID_MACRO
272 #    include <cextdecs.h(PROCESSHANDLE_GETMINE_)>
273 #    include <cextdecs.h(PROCESSHANDLE_DECOMPOSE_)>
nssgetpid(void)274        inline int nssgetpid(void)
275        {
276          short phandle[10]={0};
277          union pseudo_pid {
278           struct {
279            short cpu;
280            short pin;
281          } cpu_pin ;
282          int ppid;
283         } ppid = { 0 };
284         PROCESSHANDLE_GETMINE_(phandle);
285         PROCESSHANDLE_DECOMPOSE_(phandle, &ppid.cpu_pin.cpu, &ppid.cpu_pin.pin);
286         return ppid.ppid;
287        }
288 #    define getpid(a) nssgetpid(a)
289 #   endif /* NSSGETPID_MACRO */
290 #  endif /* NO_GETPID */
291 /*#  define setsockopt(a,b,c,d,f) setsockopt(a,b,c,(char*)d,f)*/
292 /*#  define getsockopt(a,b,c,d,f) getsockopt(a,b,c,(char*)d,f)*/
293 /*#  define connect(a,b,c) connect(a,(struct sockaddr *)b,c)*/
294 /*#  define bind(a,b,c) bind(a,(struct sockaddr *)b,c)*/
295 /*#  define sendto(a,b,c,d,e,f) sendto(a,(char*)b,c,d,(struct sockaddr *)e,f)*/
296 #  if defined(OPENSSL_THREADS) && !defined(_PUT_MODEL_)
297   /*
298    * HPNS SPT threads
299    */
300 #   define  SPT_THREAD_SIGNAL 1
301 #   define  SPT_THREAD_AWARE 1
302 #   include <spthread.h>
303 #   undef close
304 #   define close spt_close
305 /*
306 #   define get_last_socket_error()	errno
307 #   define clear_socket_error()	errno=0
308 #   define ioctlsocket(a,b,c)	ioctl(a,b,c)
309 #   define closesocket(s)		close(s)
310 #   define readsocket(s,b,n)	read((s),(char*)(b),(n))
311 #   define writesocket(s,b,n)	write((s),(char*)(b),(n)
312 */
313 #   define accept(a,b,c)        accept(a,(struct sockaddr *)b,c)
314 #   define recvfrom(a,b,c,d,e,f) recvfrom(a,b,(socklen_t)c,d,e,f)
315 #  endif
316 # endif
317 
318 # ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
319 #  define CRYPTO_memcmp memcmp
320 # endif
321 
322 # ifndef OPENSSL_NO_SECURE_MEMORY
323    /* unistd.h defines _POSIX_VERSION */
324 #  if (defined(OPENSSL_SYS_UNIX) \
325         && ( (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L)      \
326              || defined(__sun) || defined(__hpux) || defined(__sgi)      \
327              || defined(__osf__) )) \
328       || defined(_WIN32)
329       /* secure memory is implemented */
330 #   else
331 #     define OPENSSL_NO_SECURE_MEMORY
332 #   endif
333 # endif
334 
335 /*
336  * str[n]casecmp_l is defined in POSIX 2008-01. Value is taken accordingly
337  * https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
338  * There are also equivalent functions on Windows.
339  * There is no locale_t on NONSTOP.
340  */
341 # if defined(OPENSSL_SYS_WINDOWS)
342 #  define locale_t _locale_t
343 #  define freelocale _free_locale
344 #  define strcasecmp_l _stricmp_l
345 #  define strncasecmp_l _strnicmp_l
346 #  define strcasecmp _stricmp
347 #  define strncasecmp _strnicmp
348 # elif !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L \
349      || defined(OPENSSL_SYS_TANDEM)
350 #  ifndef OPENSSL_NO_LOCALE
351 #   define OPENSSL_NO_LOCALE
352 #  endif
353 # endif
354 
355 #endif
356