e_os.h 11 KB

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