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