e_os.h 12 KB

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