e_os.h 9.9 KB

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