e_os.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 HEADER_E_OS_H
  10. # define HEADER_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/nelem.h"
  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. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. # ifndef DEVRANDOM
  24. /*
  25. * set this to a comma-separated list of 'random' device files to try out. By
  26. * default, we will try to read at least one of these files
  27. */
  28. # if defined(__s390__)
  29. # define DEVRANDOM "/dev/prandom","/dev/urandom","/dev/hwrng","/dev/random"
  30. # else
  31. # define DEVRANDOM "/dev/urandom","/dev/random","/dev/srandom"
  32. # endif
  33. # endif
  34. # if !defined(OPENSSL_NO_EGD) && !defined(DEVRANDOM_EGD)
  35. /*
  36. * set this to a comma-separated list of 'egd' sockets to try out. These
  37. * sockets will be tried in the order listed in case accessing the device
  38. * files listed in DEVRANDOM did not return enough randomness.
  39. */
  40. # define DEVRANDOM_EGD "/var/run/egd-pool","/dev/egd-pool","/etc/egd-pool","/etc/entropy"
  41. # endif
  42. # if defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)
  43. # define NO_CHMOD
  44. # define NO_SYSLOG
  45. # endif
  46. # define get_last_sys_error() errno
  47. # define clear_sys_error() errno=0
  48. /********************************************************************
  49. The Microsoft section
  50. ********************************************************************/
  51. # if defined(OPENSSL_SYS_WIN32) && !defined(WIN32)
  52. # define WIN32
  53. # endif
  54. # if defined(OPENSSL_SYS_WINDOWS) && !defined(WINDOWS)
  55. # define WINDOWS
  56. # endif
  57. # if defined(OPENSSL_SYS_MSDOS) && !defined(MSDOS)
  58. # define MSDOS
  59. # endif
  60. # ifdef WIN32
  61. # undef get_last_sys_error
  62. # undef clear_sys_error
  63. # define get_last_sys_error() GetLastError()
  64. # define clear_sys_error() SetLastError(0)
  65. # if !defined(WINNT)
  66. # define WIN_CONSOLE_BUG
  67. # endif
  68. # else
  69. # endif
  70. # if (defined(WINDOWS) || defined(MSDOS))
  71. # ifdef __DJGPP__
  72. # include <unistd.h>
  73. # include <sys/stat.h>
  74. # define _setmode setmode
  75. # define _O_TEXT O_TEXT
  76. # define _O_BINARY O_BINARY
  77. # define HAS_LFN_SUPPORT(name) (pathconf((name), _PC_NAME_MAX) > 12)
  78. # undef DEVRANDOM_EGD /* Neither MS-DOS nor FreeDOS provide 'egd' sockets. */
  79. # undef DEVRANDOM
  80. # define DEVRANDOM "/dev/urandom\x24"
  81. # endif /* __DJGPP__ */
  82. # ifndef S_IFDIR
  83. # define S_IFDIR _S_IFDIR
  84. # endif
  85. # ifndef S_IFMT
  86. # define S_IFMT _S_IFMT
  87. # endif
  88. # if !defined(WINNT) && !defined(__DJGPP__)
  89. # define NO_SYSLOG
  90. # endif
  91. # ifdef WINDOWS
  92. # if !defined(_WIN32_WCE) && !defined(_WIN32_WINNT)
  93. /*
  94. * Defining _WIN32_WINNT here in e_os.h implies certain "discipline."
  95. * Most notably we ought to check for availability of each specific
  96. * routine that was introduced after denoted _WIN32_WINNT with
  97. * GetProcAddress(). Normally newer functions are masked with higher
  98. * _WIN32_WINNT in SDK headers. So that if you wish to use them in
  99. * some module, you'd need to override _WIN32_WINNT definition in
  100. * the target module in order to "reach for" prototypes, but replace
  101. * calls to new functions with indirect calls. Alternatively it
  102. * might be possible to achieve the goal by /DELAYLOAD-ing .DLLs
  103. * and check for current OS version instead.
  104. */
  105. # define _WIN32_WINNT 0x0501
  106. # endif
  107. # if defined(_WIN32_WINNT) || defined(_WIN32_WCE)
  108. /*
  109. * Just like defining _WIN32_WINNT including winsock2.h implies
  110. * certain "discipline" for maintaining [broad] binary compatibility.
  111. * As long as structures are invariant among Winsock versions,
  112. * it's sufficient to check for specific Winsock2 API availability
  113. * at run-time [DSO_global_lookup is recommended]...
  114. */
  115. # include <winsock2.h>
  116. # include <ws2tcpip.h>
  117. /* yes, they have to be #included prior to <windows.h> */
  118. # endif
  119. # include <windows.h>
  120. # include <stdio.h>
  121. # include <stddef.h>
  122. # include <errno.h>
  123. # if defined(_WIN32_WCE) && !defined(EACCES)
  124. # define EACCES 13
  125. # endif
  126. # include <string.h>
  127. # ifdef _WIN64
  128. # define strlen(s) _strlen31(s)
  129. /* cut strings to 2GB */
  130. static __inline unsigned int _strlen31(const char *str)
  131. {
  132. unsigned int len = 0;
  133. while (*str && len < 0x80000000U)
  134. str++, len++;
  135. return len & 0x7FFFFFFF;
  136. }
  137. # endif
  138. # include <malloc.h>
  139. # if defined(_MSC_VER) && !defined(_WIN32_WCE) && !defined(_DLL) && defined(stdin)
  140. # if _MSC_VER>=1300 && _MSC_VER<1600
  141. # undef stdin
  142. # undef stdout
  143. # undef stderr
  144. FILE *__iob_func();
  145. # define stdin (&__iob_func()[0])
  146. # define stdout (&__iob_func()[1])
  147. # define stderr (&__iob_func()[2])
  148. # elif _MSC_VER<1300 && defined(I_CAN_LIVE_WITH_LNK4049)
  149. # undef stdin
  150. # undef stdout
  151. # undef stderr
  152. /*
  153. * pre-1300 has __p__iob(), but it's available only in msvcrt.lib,
  154. * or in other words with /MD. Declaring implicit import, i.e. with
  155. * _imp_ prefix, works correctly with all compiler options, but
  156. * without /MD results in LINK warning LNK4049: 'locally defined
  157. * symbol "__iob" imported'.
  158. */
  159. extern FILE *_imp___iob;
  160. # define stdin (&_imp___iob[0])
  161. # define stdout (&_imp___iob[1])
  162. # define stderr (&_imp___iob[2])
  163. # endif
  164. # endif
  165. # endif
  166. # include <io.h>
  167. # include <fcntl.h>
  168. # ifdef OPENSSL_SYS_WINCE
  169. # define OPENSSL_NO_POSIX_IO
  170. # endif
  171. # define EXIT(n) exit(n)
  172. # define LIST_SEPARATOR_CHAR ';'
  173. # ifndef W_OK
  174. # define W_OK 2
  175. # endif
  176. # ifndef R_OK
  177. # define R_OK 4
  178. # endif
  179. # ifdef OPENSSL_SYS_WINCE
  180. # define DEFAULT_HOME ""
  181. # else
  182. # define DEFAULT_HOME "C:"
  183. # endif
  184. /* Avoid Visual Studio 13 GetVersion deprecated problems */
  185. # if defined(_MSC_VER) && _MSC_VER>=1800
  186. # define check_winnt() (1)
  187. # define check_win_minplat(x) (1)
  188. # else
  189. # define check_winnt() (GetVersion() < 0x80000000)
  190. # define check_win_minplat(x) (LOBYTE(LOWORD(GetVersion())) >= (x))
  191. # endif
  192. # else /* The non-microsoft world */
  193. # if defined(OPENSSL_SYS_VXWORKS)
  194. # include <sys/times.h>
  195. # else
  196. # include <sys/time.h>
  197. # endif
  198. # ifdef OPENSSL_SYS_VMS
  199. # define VMS 1
  200. /*
  201. * some programs don't include stdlib, so exit() and others give implicit
  202. * function warnings
  203. */
  204. # include <stdlib.h>
  205. # if defined(__DECC)
  206. # include <unistd.h>
  207. # else
  208. # include <unixlib.h>
  209. # endif
  210. # define LIST_SEPARATOR_CHAR ','
  211. /* We don't have any well-defined random devices on VMS, yet... */
  212. # undef DEVRANDOM
  213. /*-
  214. We need to do this since VMS has the following coding on status codes:
  215. Bits 0-2: status type: 0 = warning, 1 = success, 2 = error, 3 = info ...
  216. The important thing to know is that odd numbers are considered
  217. good, while even ones are considered errors.
  218. Bits 3-15: actual status number
  219. Bits 16-27: facility number. 0 is considered "unknown"
  220. Bits 28-31: control bits. If bit 28 is set, the shell won't try to
  221. output the message (which, for random codes, just looks ugly)
  222. So, what we do here is to change 0 to 1 to get the default success status,
  223. and everything else is shifted up to fit into the status number field, and
  224. the status is tagged as an error, which is what is wanted here.
  225. Finally, we add the VMS C facility code 0x35a000, because there are some
  226. programs, such as Perl, that will reinterpret the code back to something
  227. POSIXly. 'man perlvms' explains it further.
  228. NOTE: the perlvms manual wants to turn all codes 2 to 255 into success
  229. codes (status type = 1). I couldn't disagree more. Fortunately, the
  230. status type doesn't seem to bother Perl.
  231. -- Richard Levitte
  232. */
  233. # define EXIT(n) exit((n) ? (((n) << 3) | 2 | 0x10000000 | 0x35a000) : 1)
  234. # define DEFAULT_HOME "SYS$LOGIN:"
  235. # else
  236. /* !defined VMS */
  237. # ifdef OPENSSL_UNISTD
  238. # include OPENSSL_UNISTD
  239. # else
  240. # include <unistd.h>
  241. # endif
  242. # include <sys/types.h>
  243. # ifdef OPENSSL_SYS_WIN32_CYGWIN
  244. # include <io.h>
  245. # include <fcntl.h>
  246. # endif
  247. # define LIST_SEPARATOR_CHAR ':'
  248. # define EXIT(n) exit(n)
  249. # endif
  250. # endif
  251. /***********************************************/
  252. # if defined(OPENSSL_SYS_WINDOWS)
  253. # define strcasecmp _stricmp
  254. # define strncasecmp _strnicmp
  255. # if (_MSC_VER >= 1310)
  256. # define open _open
  257. # define fdopen _fdopen
  258. # define close _close
  259. # ifndef strdup
  260. # define strdup _strdup
  261. # endif
  262. # define unlink _unlink
  263. # define fileno _fileno
  264. # endif
  265. # else
  266. # include <strings.h>
  267. # endif
  268. /* vxworks */
  269. # if defined(OPENSSL_SYS_VXWORKS)
  270. # include <ioLib.h>
  271. # include <tickLib.h>
  272. # include <sysLib.h>
  273. # include <vxWorks.h>
  274. # include <sockLib.h>
  275. # include <taskLib.h>
  276. # define TTY_STRUCT int
  277. # define sleep(a) taskDelay((a) * sysClkRateGet())
  278. /*
  279. * NOTE: these are implemented by helpers in database app! if the database is
  280. * not linked, we need to implement them elsewhere
  281. */
  282. struct hostent *gethostbyname(const char *name);
  283. struct hostent *gethostbyaddr(const char *addr, int length, int type);
  284. struct servent *getservbyname(const char *name, const char *proto);
  285. # endif
  286. /* end vxworks */
  287. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  288. # define CRYPTO_memcmp memcmp
  289. #endif
  290. #ifdef __cplusplus
  291. }
  292. #endif
  293. #endif