xref: /openssl/include/internal/e_os.h (revision b2ac9c71)
1 /*
2  * Copyright 1995-2024 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 #   ifdef _WIN64
110 #    define strlen(s) _strlen31(s)
111 /* cut strings to 2GB */
_strlen31(const char * str)112 static __inline unsigned int _strlen31(const char *str)
113 {
114     unsigned int len = 0;
115     while (*str && len < 0x80000000U)
116         str++, len++;
117     return len & 0x7FFFFFFF;
118 }
119 #   endif   /* def(_WIN64) */
120 #   include <malloc.h>
121 #   if defined(_MSC_VER) && !defined(_WIN32_WCE) && !defined(_DLL) && defined(stdin)
122 #    if _MSC_VER>=1300 && _MSC_VER<1600
123 #     undef stdin
124 #     undef stdout
125 #     undef stderr
126 FILE *__iob_func(void);
127 #     define stdin  (&__iob_func()[0])
128 #     define stdout (&__iob_func()[1])
129 #     define stderr (&__iob_func()[2])
130 #    endif
131 #   endif
132 #  endif
133 
134 #  include <io.h>
135 #  include <fcntl.h>
136 
137 #  ifdef OPENSSL_SYS_WINCE
138 #   define OPENSSL_NO_POSIX_IO
139 #  endif
140 
141 #  define EXIT(n) exit(n)
142 #  define LIST_SEPARATOR_CHAR ';'
143 #  ifndef W_OK
144 #   define W_OK        2
145 #  endif
146 #  ifndef R_OK
147 #   define R_OK        4
148 #  endif
149 #  ifdef OPENSSL_SYS_WINCE
150 #   define DEFAULT_HOME  ""
151 #  else
152 #   define DEFAULT_HOME  "C:"
153 #  endif
154 
155 /* Avoid Visual Studio 13 GetVersion deprecated problems */
156 #  if defined(_MSC_VER) && _MSC_VER>=1800
157 #   define check_winnt() (1)
158 #   define check_win_minplat(x) (1)
159 #  else
160 #   define check_winnt() (GetVersion() < 0x80000000)
161 #   define check_win_minplat(x) (LOBYTE(LOWORD(GetVersion())) >= (x))
162 #  endif
163 
164 # else                          /* The non-microsoft world */
165 
166 #  if defined(OPENSSL_SYS_VXWORKS)
167 #   include <time.h>
168 #  else
169 #   include <sys/time.h>
170 #  endif
171 
172 #  ifdef OPENSSL_SYS_VMS
173 #   define VMS 1
174   /*
175    * some programs don't include stdlib, so exit() and others give implicit
176    * function warnings
177    */
178 #   include <stdlib.h>
179 #   if defined(__DECC)
180 #    include <unistd.h>
181 #   else
182 #    include <unixlib.h>
183 #   endif
184 #   define LIST_SEPARATOR_CHAR ','
185   /* We don't have any well-defined random devices on VMS, yet... */
186 #   undef DEVRANDOM
187   /*-
188      We need to do this since VMS has the following coding on status codes:
189 
190      Bits 0-2: status type: 0 = warning, 1 = success, 2 = error, 3 = info ...
191                The important thing to know is that odd numbers are considered
192                good, while even ones are considered errors.
193      Bits 3-15: actual status number
194      Bits 16-27: facility number.  0 is considered "unknown"
195      Bits 28-31: control bits.  If bit 28 is set, the shell won't try to
196                  output the message (which, for random codes, just looks ugly)
197 
198      So, what we do here is to change 0 to 1 to get the default success status,
199      and everything else is shifted up to fit into the status number field, and
200      the status is tagged as an error, which is what is wanted here.
201 
202      Finally, we add the VMS C facility code 0x35a000, because there are some
203      programs, such as Perl, that will reinterpret the code back to something
204      POSIX.  'man perlvms' explains it further.
205 
206      NOTE: the perlvms manual wants to turn all codes 2 to 255 into success
207      codes (status type = 1).  I couldn't disagree more.  Fortunately, the
208      status type doesn't seem to bother Perl.
209      -- Richard Levitte
210   */
211 #   define EXIT(n)  exit((n) ? (((n) << 3) | 2 | 0x10000000 | 0x35a000) : 1)
212 
213 #   define DEFAULT_HOME "SYS$LOGIN:"
214 
215 #  else
216      /* !defined VMS */
217 #   include <unistd.h>
218 #   include <sys/types.h>
219 #   ifdef OPENSSL_SYS_WIN32_CYGWIN
220 #    include <io.h>
221 #    include <fcntl.h>
222 #   endif
223 
224 #   define LIST_SEPARATOR_CHAR ':'
225 #   define EXIT(n)             exit(n)
226 #  endif
227 
228 # endif
229 
230 /***********************************************/
231 
232 # if defined(OPENSSL_SYS_WINDOWS)
233 #  if defined(_MSC_VER) && (_MSC_VER >= 1310) && !defined(_WIN32_WCE)
234 #   define open _open
235 #   define fdopen _fdopen
236 #   define close _close
237 #   ifndef strdup
238 #    define strdup _strdup
239 #   endif
240 #   define unlink _unlink
241 #   define fileno _fileno
242 #   define isatty _isatty
243 #  endif
244 # else
245 #  include <strings.h>
246 # endif
247 
248 /* vxworks */
249 # if defined(OPENSSL_SYS_VXWORKS)
250 #  include <ioLib.h>
251 #  include <tickLib.h>
252 #  include <sysLib.h>
253 #  include <vxWorks.h>
254 #  include <sockLib.h>
255 #  include <taskLib.h>
256 
257 #  define TTY_STRUCT int
258 #  define sleep(a) taskDelay((a) * sysClkRateGet())
259 
260 /*
261  * NOTE: these are implemented by helpers in database app! if the database is
262  * not linked, we need to implement them elsewhere
263  */
264 struct hostent *gethostbyname(const char *name);
265 struct hostent *gethostbyaddr(const char *addr, int length, int type);
266 struct servent *getservbyname(const char *name, const char *proto);
267 
268 # endif
269 /* end vxworks */
270 
271 /* ----------------------------- HP NonStop -------------------------------- */
272 /* Required to support platform variant without getpid() and pid_t. */
273 # if defined(__TANDEM) && defined(_GUARDIAN_TARGET)
274 #  include <strings.h>
275 #  include <netdb.h>
276 #  define getservbyname(name,proto)          getservbyname((char*)name,proto)
277 #  define gethostbyname(name)                gethostbyname((char*)name)
278 #  define ioctlsocket(a,b,c)	ioctl(a,b,c)
279 #  ifdef NO_GETPID
280 inline int nssgetpid(void);
281 #   ifndef NSSGETPID_MACRO
282 #    define NSSGETPID_MACRO
283 #    include <cextdecs.h(PROCESSHANDLE_GETMINE_)>
284 #    include <cextdecs.h(PROCESSHANDLE_DECOMPOSE_)>
nssgetpid(void)285        inline int nssgetpid(void)
286        {
287          short phandle[10]={0};
288          union pseudo_pid {
289           struct {
290            short cpu;
291            short pin;
292          } cpu_pin ;
293          int ppid;
294         } ppid = { 0 };
295         PROCESSHANDLE_GETMINE_(phandle);
296         PROCESSHANDLE_DECOMPOSE_(phandle, &ppid.cpu_pin.cpu, &ppid.cpu_pin.pin);
297         return ppid.ppid;
298        }
299 #    define getpid(a) nssgetpid(a)
300 #   endif /* NSSGETPID_MACRO */
301 #  endif /* NO_GETPID */
302 /*#  define setsockopt(a,b,c,d,f) setsockopt(a,b,c,(char*)d,f)*/
303 /*#  define getsockopt(a,b,c,d,f) getsockopt(a,b,c,(char*)d,f)*/
304 /*#  define connect(a,b,c) connect(a,(struct sockaddr *)b,c)*/
305 /*#  define bind(a,b,c) bind(a,(struct sockaddr *)b,c)*/
306 /*#  define sendto(a,b,c,d,e,f) sendto(a,(char*)b,c,d,(struct sockaddr *)e,f)*/
307 #  if defined(OPENSSL_THREADS) && !defined(_PUT_MODEL_)
308   /*
309    * HPNS SPT threads
310    */
311 #   define  SPT_THREAD_SIGNAL 1
312 #   define  SPT_THREAD_AWARE 1
313 #   include <spthread.h>
314 #   undef close
315 #   define close spt_close
316 /*
317 #   define get_last_socket_error()	errno
318 #   define clear_socket_error()	errno=0
319 #   define ioctlsocket(a,b,c)	ioctl(a,b,c)
320 #   define closesocket(s)		close(s)
321 #   define readsocket(s,b,n)	read((s),(char*)(b),(n))
322 #   define writesocket(s,b,n)	write((s),(char*)(b),(n)
323 */
324 #   define accept(a,b,c)        accept(a,(struct sockaddr *)b,c)
325 #   define recvfrom(a,b,c,d,e,f) recvfrom(a,b,(socklen_t)c,d,e,f)
326 #  endif
327 # endif
328 
329 # ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
330 #  define CRYPTO_memcmp memcmp
331 # endif
332 
333 # ifndef OPENSSL_NO_SECURE_MEMORY
334    /* unistd.h defines _POSIX_VERSION */
335 #  if (defined(OPENSSL_SYS_UNIX) \
336         && ( (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L)      \
337              || defined(__sun) || defined(__hpux) || defined(__sgi)      \
338              || defined(__osf__) )) \
339       || defined(_WIN32)
340       /* secure memory is implemented */
341 #   else
342 #     define OPENSSL_NO_SECURE_MEMORY
343 #   endif
344 # endif
345 
346 /*
347  * str[n]casecmp_l is defined in POSIX 2008-01. Value is taken accordingly
348  * https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
349  * There are also equivalent functions on Windows.
350  * There is no locale_t on NONSTOP.
351  */
352 # if defined(OPENSSL_SYS_WINDOWS)
353 #  define locale_t _locale_t
354 #  define freelocale _free_locale
355 #  define strcasecmp_l _stricmp_l
356 #  define strncasecmp_l _strnicmp_l
357 #  define strcasecmp _stricmp
358 #  define strncasecmp _strnicmp
359 # elif !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L \
360      || defined(OPENSSL_SYS_TANDEM)
361 #  ifndef OPENSSL_NO_LOCALE
362 #   define OPENSSL_NO_LOCALE
363 #  endif
364 # endif
365 
366 #endif
367